opennextjs / opennextjs-aws

Open-source Next.js adapter for AWS
https://opennext.js.org
MIT License
4.11k stars 125 forks source link

Problem using deployed server-function from open-next when using basePath #476

Closed yiytan92 closed 2 months ago

yiytan92 commented 3 months ago

Does anyone use the basePath configuration in their next.config.js?

I am migrating to use open-next for deployment from using serverless-nextjs and facing an issue after deploying the server function onto Lambda.

For example, in my next.config.js i have a basePath: "/basePath", and a page named index When i deploy the open-next server function onto a lambda url function and try to access the page with the basePath https://example.lambda-url.ap-southeast-1.on.aws/basePath/index, it returns me 404. however accessing https://example.lambda-url.ap-southeast-1.on.aws/index routes properly.

Looking at how the serverless-nextjs library did it, they have a function to slice away the basePath in the lambda function handler: https://github.com/serverless-nextjs/serverless-next.js/blob/e6367b585fb98608cd2e9327e2c8d4058ba73b00/packages/libs/core/src/route/basepath.ts

Have i misconfigured something on my open-next setup?

conico974 commented 3 months ago

This is not done on the server function in OpenNext. OpenNext creates an open-next.output.json file in the output folder (i.e. .open-next) with a behaviors field that should already contains the basePath that you can use directly as a cloudfront behavior. You can take a look at the cdk implementation used for e2e test https://github.com/sst/open-next/blob/main/examples/sst/stacks/OpenNextReferenceImplementation.ts

jarodsim commented 3 months ago

Does anyone use the basePath configuration in their next.config.js?

I am migrating to use open-next for deployment from using serverless-nextjs and facing an issue after deploying the server function onto Lambda.

For example, in my next.config.js i have a basePath: "/basePath", and a page named index When i deploy the open-next server function onto a lambda url function and try to access the page with the basePath https://example.lambda-url.ap-southeast-1.on.aws/basePath/index, it returns me 404. however accessing https://example.lambda-url.ap-southeast-1.on.aws/index routes properly.

Looking at how the serverless-nextjs library did it, they have a function to slice away the basePath in the lambda function handler: https://github.com/serverless-nextjs/serverless-next.js/blob/e6367b585fb98608cd2e9327e2c8d4058ba73b00/packages/libs/core/src/route/basepath.ts

Have i misconfigured something on my open-next setup?

Im with the same issue :/

conico974 commented 3 months ago

@jarodsim What issue ?? This is not meant to be done in OpenNext but by the IAC. This is working in Ion since OpenNext 3.0.0-rc.8

jarodsim commented 3 months ago

@jarodsimQual problema?? Isso não deve ser feito no OpenNext, mas pelo IAC. Isso está funcionando no Ion desde o OpenNext 3.0.0-rc.8

Oh, sorry

I'll take a look at SST, thanks

conico974 commented 3 months ago

No problem, just use the behavior defined in the open-next.output.json in cloudfront and it should work

yiytan92 commented 3 months ago

Hi @conico974, thanks for replying. I took a look at the open-next.output.json output, and verified that my current cloudfront behaviors matches it. Regardless of the cloudfront, when i invoke the lambda directly with my lambda function url it faces this basePath issue.

https://example.lambda-url.ap-southeast-1.on.aws/basePath/index returns 404. https://example.lambda-url.ap-southeast-1.on.aws/index routes properly.

Would you happen to know if there's any examples of deploying the open-next server-functions to lambda with basePath working?

conico974 commented 3 months ago

@yiytan92 Does it work if you invoke it through cloudfront ? And if you want to test it you can use sst ion it works there last time i tested.

What version of OpenNext are you using BTW ? This PR might cause some issue with basePath https://github.com/sst/open-next/pull/426. Try with 3.0.2 if you're on latest

yiytan92 commented 3 months ago

Doesn't work when invoked via cloudfront either.

https://cloudfront.net/basePath/index returns 404. https://cloudfront.net/index routes properly.

I have deployed open-next webs without baseUrl and it works fine, that's why i suspect its a basePath handling issue. I am using OpenNext v3.0.6.

And if you want to test it you can use sst ion it works there last time i tested.

Ok, will try that and report back, thanks for the suggestions!

eschaefer commented 3 months ago

Just chiming in since I'm facing the exact same issue. Using OpenNext 3.0.8 via SST ion.

Not related to using server functions, but rather: cloudfront.net/basePath/index returns 404. cloudfront.net/index routes properly.

Can confirm that pinning openNextVersion: "3.0.2", on SST ion fixes the issue.

yiytan92 commented 3 months ago

Hey, thanks @eschaefer, after reading your comment, i also fixed my open-next version to 3.0.2 and the issue is fixed. @conico974 FYI

https://github.com/sst/open-next/pull/426