-
Notifications
You must be signed in to change notification settings - Fork 46
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
feat: basePath and allow multiple next.js sites on same distribution (passing custom distribution) #148
Conversation
Fixing prefix to s3Origin... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great stuff, thanks!
related #147 |
I have already deployed a an environment using this new version and it works fine here. |
Nice work! Interesting... you didn't have to set |
@khuezy yes, they work together, you set that on the next.config.js so the next lambda server knows how to handle it, than you can set it on the Nextjs deployment to |
I didn't see a |
because the examples use open-next git submodule I cannot change it |
BTW, you also need to set |
I see. None of the open-next examples router apps have |
to validate it you would need to manually edit the referenced modules |
@onhate, thanks so much for contributing this feature! This feature brings a lot more flexibility to this construct. Let me test this out and get back to you. |
… READMEs; rename example stack
@onhate, great job on this feature. I've made some simplifications (IMO), moved |
nice! I've just added you @bestickley |
feat: simplify getCloudFrontDistribution; move props location; update…
@bestickley the part of get/create distribution got simpler indeed using just the addBehavior. My only concern is that now we have two options of passing 'distribution' (Distribution and OverwriteConfig) and Distribution has higher precedence than config, maybe a warning? |
@onhate, great point. With that said, I still don't think it makes sense to keep them as the same prop. Do you think I should log a warning or throw an error? Logged warning could be missed, error will make sure user sees it. There is no reason to customize distribution when providing distribution. It will have no affect. |
well, passing an overwrite when passing a distribution could lead to think the passed distribution can be 'customized', so an error would prevent this thought, I'll add this check here. |
@onhate, agreed. I've added it my PR. |
we both did it 🤣 |
Haha, yours looks good to me. Ok, so I've deployed but I'm getting a 404. Could you check it out? |
@bestickley checking, I already have a deployment working with 2 next.js sites and a home page, it can be something on the application, I'll return with the findings |
@onhate, another test case I tried is specifying 7:22:55 AM | CREATE_FAILED | AWS::CloudFront::Distribution | pagesrouterDistribution428530EB
Resource handler returned message: "Invalid request provided: AWS::CloudFront::Distribution: The parameter path pattern must be unique. (Service: CloudFront, Status Code: 400, Request ID: 6edac
785-8957-4521-9b08-b3335daf7775)" (RequestToken: 6c0846e4-77fa-496d-85f9-f56fd532ba2e, HandlerErrorCode: InvalidRequest) It could just be me though. Do you mind trying as well? |
sure, checking this too, I may know what it is |
@onhate, my bad! Thank you :) |
try to run an Invalidation on your cloudfront to validate if the 404 goes away |
@bestickley that's because the application needs to be ready to load assets from a prefix path, as you can see the code examples they all load from / directly instead of /prefix, if you run the apps in dev mode with assetsPrefix and basePath they will also fail. In summary, your app needs to developed to run under a basePath. Regarding the 404 that's weird as my deployment just works, could you share a printscreen of the CloudFront behaviors configuration page? |
hmmm, wait, the css issue was supposed to not happen as I also "wrap" the assets folder under the prefix basePath, let me check. |
ah, the only difference from your deployments and mine is that you are not passing a custom distribution right? In this case it will create a default route (catch all) pointing to the lambda, but the lambda was built to run under a basePath so the default route will always return 404, let me try to debug deploying without passing a custom distribution and using basePath. |
@onhate, sorry not CSS, but images of your app router: |
@bestickley Like I thought, the app needs to be developed with /subpath in mind, if you see the code that adds the image on the screen, https://github.com/sst/open-next/blob/main/examples/shared/components/Nav/index.tsx#L12 it is just assuming the image is on /static, if you run app-routes locally in dev mode with basePath and assetPrefix it will fail the same way to load the images. |
@onhate, that makes sense. issue resolved. |
https://djaxa5d84nqeo.cloudfront.net/high-security high security is working here, same issues with the images but ok, regarding your 404, could you make sure the basePath and assetPrefix you had built the example app are matching with the configured basePath on the CDK construct? I had a similar issue when tried to deploy on /high-security I forgot to change the basePath that was /app-router on the next.config.js and it was returning 404. |
double check because high-security example has skipBuild: false, so even if you change on next.config.js it will not rebuild |
@onhate, |
that's good news :) well, I have a production environment that I migrated from subdomains do /subpath already running, if you want to see it in action we can talk in private and I can show you, but unfortunately I cannot share the url (yet) |
Update: I was wrong about assetPrefix working for
Glad to hear that you have a production environment with it working. There are still likely features your environment is not testing that e2e tests will test. However, the e2e tests we use that are in open-next git submodule do not accomodate basePath customization and I don't have enough time now to fix all of the e2e tests. I'm going to create an issue for a future PR that pulls the Next.js example apps from open-next/examples into our examples folder and pull the e2e tests in. This way we won't have to manually update open-next/examples app to test out multi site example. One last thing, could you update the TS Doc for /**
* Optional value to prefix the Next.js site under a /prefix path on CloudFront.
* Usually used when you deploy multiple Next.js sites on same domain using /sub-path
*
* Note, you'll need to set [basePath](https://nextjs.org/docs/app/api-reference/next-config-js/basePath)
* in your `next.config.ts` to this value and ensure any files in `public`
* folder have correct prefix.
* @example "/my-base-path"
*/
readonly basePath?: string; |
@bestickley sorry, I'm not following, how should I run e2e? by your comment are we going to do it on another PR? As this change is new and optional I think we could move on with this PR and work incrementally on e2e on new PRs. Docs updated. |
See #151. We'll create them in another PR. If you wanted to support that effort, it would be greatly appreciated. Agreed. Thank you! |
@bestickley sure thing, I'll help on it |
Merged. Thank you @onhate for your hard work on this feature! |
next.js
basePath
on Distribution