Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prerender.io usage with Amplify Console #91

Closed
smakinson opened this issue Aug 29, 2019 · 37 comments
Closed

prerender.io usage with Amplify Console #91

smakinson opened this issue Aug 29, 2019 · 37 comments
Labels
archived This issue has been locked. feature-request New feature or request redirects

Comments

@smakinson
Copy link

smakinson commented Aug 29, 2019

Hey everyone,

I'm trying to determine if Amplify can use prerender.io. They provided me a link to Cloudfront middleware: https://github.com/jinty/prerender-cloudfront

Is it possible to use that through Amplify (Console)? If yes, how might I do so?

If not is it possible to get Amplify like features around the various branch urls, etc outside of Amplify?

I also read somewhere that using rewrites to send over to prerender.io is an option. I believe I would need to send Basic Auth info to do this. This is the url I found an example of that from someone using IIS: https://mono.software/2016/02/22/Installing-Prerender-io/ Its down under the "Client Side" heading. If I am unable to use middleware with Amplify, is it possible to provide the Basic Auth info like that via the rewrites? I did not see the docs make any mention of this. Need to be able to check User Agent also.

Thank you for your help!

@swaminator swaminator added the question Further information is requested label Aug 29, 2019
@BabyDino
Copy link

BabyDino commented Sep 4, 2019

We have exactly the same question.

I do have a prerender server up and running. Redirect based on user agent could be a solution?

@smakinson
Copy link
Author

I have tried the middleware via their prerender-cloudfront.yaml from the middleware and that setup seems to work fine, however it involves a S3 bucket. I'm thinking the rewrite may be the best option here. @swaminator What are your thoughts on this?

@swaminator swaminator added the 5 label Sep 24, 2019
@aegonx
Copy link

aegonx commented Nov 13, 2019

Any progress on this? Is AWS Amplify console a valid solution at all if it comes to this?

@smakinson
Copy link
Author

Thank you for investigating!

@swaminator
Copy link
Contributor

@smakinson apologies we didn't get to this sooner. We had a number of other priorities that came above this. At this point, it is close to the top of our list of issues and I'm hoping to have a response here in the next 2-3 weeks.

@smakinson
Copy link
Author

@swaminator That sounds great! Thank you!

@srinivas-gangji
Copy link

@swaminator Do you have any further updates on this ?

@srinivas-gangji
Copy link

@swaminator Appreciate if you could provide an update on this request, for now, I have moved my site deployment to 'netlify' ( as they have built in pre-rendering service). Hoping that Amplify team implements the pre-rendering service at the earliest.

@simpson
Copy link

simpson commented Jul 7, 2020

Any updates?

@ianmartorell
Copy link

I'm trying to find out about the recommended practices for SSR with Amplify. Is there anything official on the matter?

@abhi7cr abhi7cr added the feature-request New feature or request label Sep 11, 2020
@swaminator swaminator removed the question Further information is requested label Sep 11, 2020
@abhi7cr abhi7cr added feature-request New feature or request and removed feature-request New feature or request labels Sep 11, 2020
@pmanion0
Copy link

I am trying to figure out an answer on this same question. I'd appreciate any help or suggestions!

@arturocanalda
Copy link

arturocanalda commented Sep 24, 2020

I eventually solved it with Cloudfront + Lambda (got the link to a repo from the first post in this thread)

https://github.com/jinty/prerender-cloudfront

So far it's been working perfectly for me.
If you go for this solution, pay special attention to increase the versions of your lambda whenever you update it.

@pmanion0
Copy link

@arturocanalda Thanks for the suggestion, and that's awesome you got this working!

Did you stand up a new Cloudfront distribution that has those two Lambda@Edge functions to handle the routing? And the origin for that distribution was the Amplify-deployed URL?

If I understand correctly, the example has the direct S3 link to the SPA build that we don't have access to with Amplify. I wasn't sure if standing up a second Cloudfront distribution to deploy the Lambda would (a) not work or (b) incur double Cloudfront/access costs. However, I'd probably still go this path if it ends up working.

@arturocanalda
Copy link

arturocanalda commented Sep 25, 2020

I already had a CloudFront distribution.

I don't see any reference to S3 in the template :)

In that example there are 2 Lambda (edge) functions:

  • SetPrerenderHeader
    The first one just adds some attributes to the HTTP request header in case the request comes from any of these services: /googlebot|adsbot\-google|Feedfetcher\-Google|bingbot|yandex|baiduspider|Facebot|facebookexternalhit|twitterbot|Whatsapp|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator
  • RedirectToPrerenderArn
    It will redirect to prerender.io if the http request header contains some extra/custom headers (added by SetPrerenderHeader). Otherwise it won't modify the request.

Once those functions are created, in you Clooudformation template (amplify/backend/hosting/S3AndCloudfront/template.json -> Add I added the following to DefaultCacheBehavior:

                       "LambdaFunctionAssociations": [
                            {
                                "EventType" : "viewer-request",
                                "LambdaFunctionARN" : {
                                    "Ref": "setPrerenderHeaderArn"
                                }
                            },
                            {
                                "EventType" : "origin-request",
                                "LambdaFunctionARN" : {
                                    "Ref": "redirectToPrerenderArn"
                                }
                            }
                        ]

Where setPrerenderHeaderArn and redirectToPrerenderArn are passed as a template param from amplify/backend/hosting/S3AndCloudfront/parameters.json

(*) Notice I created this functions once and they are used by my 3 environments (dev, stg, prd). Not a best practice, but didn't have more time to invest on it.

@pmanion0
Copy link

@arturocanalda Perfect, thanks so much for your help! The WebBucket at the start of the YAML threw me off a bit, but your reply helped me realize that you just setup the Cloudfront distribution with the Amplify URL as the origin, setup the two edge lambdas, and everything works out.

I ended up changing the origin-request lambda to redirect to an S3 bucket with my own pre-rendered pages, so it took me the weekend to get it all working right. I should have just saved my time and used prerender.io... but it was certainly a good learning experience.

I do think Amplify is standing up its own Cloudfront distribution as part of the original deployment. However, it's hidden and can't be modified with edge lambdas, so we need to stand-up our own distribution to make this work. I think this means we're double-paying in some sense (with some CDN cost built into the Amplify pricing), but I'll tackle this problem when it actually becomes a practical versus theoretical issue. Hopefully, the Amplify team will have found a solution before that time.

Thanks again for the help @arturocanalda !

@arturocanalda
Copy link

@theforager you're welcome. Somehow we are all in the same boat :)

@fnavarrodev
Copy link

Hi @arturocanalda and @theforager How did you exactly make it work? I'm using the Amplify console, so I can't access to the S3, Cloudfront and execute LamdaEdge functions. Should I start a new project just using the cli and use the S3Cloudfront hosting? What are the steps to make it work? The documentation is quiet poor. Thanks

@pmanion0
Copy link

Hey @fnavarrodev,

As you noted, you don't have access to any of the "behind the scenes" tools for Amplify (S3, Cloudfront, etc.), so you actually need to stand-up a new Cloudfront distribution that points to the remote origin of your Amplify URL. You can then create the Lambda@Edge functions, attach them to your new Cloudfront distribution, and direct an incoming origin-request to either Prerender.io or your Amplify-hosted origin. I believe this means requests are routed through two Cloudfront distributions (first your newly created one and then a second controlled by Amplify), but it appeared to work fine.

Due to some of these limitations, I ended up moving off Amplify by creating my own CICD pipeline using GitHub Actions to build my SPA, deploy it on S3, and then I changed the Cloudfront origin from the Amplify URL to my newly deployed S3 URL. It took several days basically rebuilding some of the things Amplify handles really nicely out of the box, but it's given me a lot more flexibility in setting things up and a deeper understanding of some of the core tools.

Hope this helps, and let me know if you have other questions.

@fnavarrodev
Copy link

@theforager Thanks so much for your quick response. Amplify is a great tool/framework but to me still looks very uncompleted and all those "behind the scenes" things happening just make it worst. We are evaluating if we should continue using amplify or, as you did, have more control with a more customized CI/CD pipeline.

First I'll try having 2 cloudfront distributions like you suggested and see how much they charge us ;)

PD: We are all in the same boat.

@arturocanalda
Copy link

@fnavarrodev I only have one Cloudfront distribution. It's the one created by Amplify as "Hosting" resource (for production environments - S3AndCloudFront). I didn't have to setup a new one on my own.

What I had to setup on my own is the Lambda function (Lambda@edge when executed/deployed by CloudFront) and modify the CloudFormation template in order to trigger the function as part of the DefaultCacheBehavior. In my case this template is in amplify/backend/hosting/S3AndCloudfront/template.json, but that will depend on which name you gave to your hosting resource.

@pmanion0
Copy link

@arturocanalda Were you building your entire app with the Amplify CLI? I was actually searching around, and it appears with the Amplify CLI you can actually make some modifications to the S3 and Cloudfront distributions by setting up a backend deployment. I had connecting my frontend via the Amplify Console only to deploy the SPA, and I was separately managing all the backend pieces.

@arturocanalda
Copy link

@theforager yes, I built it entirely through the CLI. Just the Lambda@edge configuration was done manually by modifying the CloudFront CloudFormation template (many clouds... :).

@fnavarrodev
Copy link

@arturocanalda Yes is true, using the CLI you have more control, but I don't really see how to connect my github repository to that backend hosting(S3AndCloudFront) and get all the magic with new subdomains for branches and so on.

@arturocanalda
Copy link

@fnavarrodev I had no issues connecting to my github repository. At the moment I have 3 branches = 3 backend environments (dev-stg-prd). But CI/CD with all the magic happening... that will be another battle (which I try to ignore/postpone so I can still sleep in the night :)

@fnavarrodev
Copy link

fnavarrodev commented Oct 21, 2020

I added the hosting like they explain here https://docs.amplify.aws/cli/hosting so now I have a backend environment(S3AndCloudFront), but still don't see how is connected to my repo. I need to go to the console now? I do commit/push and no magic yet

@arturocanalda
Copy link

arturocanalda commented Oct 21, 2020

@fnavarrodev indeed, that part has to be done from the console. App settings -> General. You should see a button to connect your repository. I don't know the name of the button because my repo is already connected. But I attached the screenshot of how it looks after.

Screenshot 2020-10-21 at 15 27 16

@fnavarrodev
Copy link

Thanks, I'm just giving up this is what I get:
Captura de pantalla 2020-10-21 a las 15 37 37

@fnavarrodev
Copy link

So after an infinite loading... I tried from frontend environments, and works but now I have 2 hosting. Is it any guide to follow somewhere?

@vincent38wargnier
Copy link

Is there any update on a tool through amplify console to save our SEO?

@Shomari
Copy link

Shomari commented Dec 28, 2021

Also curious about any updates

@parthNJ
Copy link

parthNJ commented Aug 1, 2022

Any update?

@ghost ghost added the redirects label Jan 26, 2023
@jrgrez
Copy link

jrgrez commented Feb 15, 2023

Any updates about this topic? I'm facing the same issue

@vincent38wargnier
Copy link

I'm using nextjs now it's perfect

@gaelsuv
Copy link

gaelsuv commented Jan 26, 2024

If anyone is still looking for help, the solution pointed out above in this thread is still working fine. You just have to play around a little bit with the cloudformation template of the repo suggested in the comments. Hope this helps, I was desperate too 🥲

@mauerbac
Copy link
Member

Hey folks -- I think with Next.js SSR this use case should be solved. Going to close this for now!

Copy link

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

Copy link

This issue has been automatically locked.

@github-actions github-actions bot added the archived This issue has been locked. label Jun 27, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived This issue has been locked. feature-request New feature or request redirects
Projects
None yet
Development

No branches or pull requests