opennextjs-aws: Rewrites in next.config.js doesnt work
After updating Next.js to 13.5.3 to handle a bad error in production (500, attemp to render client in the server). The following stopped working:
redirects: () => {
const SIGNIN_ROUTE = /login;
const DEFAULT_ROUTE = /dashboard;
return [
{
source: /,
destination: DEFAULT_ROUTE,
permanent: true,
has: [{type: cookie, key: next-auth.session-token}],
},
{
source: /,
destination: DEFAULT_ROUTE,
permanent: true,
has: [{type: cookie, key: __Secure-next-auth.session-token}],
},
{
source: /,
destination: SIGNIN_ROUTE,
permanent: true,
missing: [{type: cookie, key: __Secure-next-auth.session-token}],
},
{
source: /,
destination: SIGNIN_ROUTE,
permanent: true,
missing: [{type: cookie, key: next-auth.session-token}],
},
];
I am in a damn loop hole, for few weeks already. My original issue was Next trying to generated protected pages during ISG, and then caching a 500 result. Then I updated next.js and sst to the latest stable versions (13.5.4, sst 2.26.10). My folder setup is as following: src ├── app │ ├── @authenticated │ ├── @login │ ├── _components │ ├── _hooks │ ├── _lib │ ├── api │ ├── layout.tsx │ ├── loading.tsx │ ├── page.tsx │ └── providers.tsx └── middleware.ts
Then my root layout conditionally renders login/auth. Everything works as expected except the root dir “/” which is an empty route. NextAuth should redirect to ‘/login’ and next itelsf to ‘dashboard’. It works locally without setting next.config.ts. I tried adding the same login in the middleware but it doesnt seem like the middleware is even running (not seeing any logs form there). It gets a successful response from ‘/api/auth/session’ but the redirect doesnt happen 😕 Any ideas?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 31 (15 by maintainers)
Commits related to this issue
- should fix most of #263 — committed to conico974/open-next by conico974 3 years ago
- should fix most of #263 — committed to conico974/open-next by conico974 3 years ago
- Fix next rewrites (#411) * should fix most of #263 * added e2e test * directly return * Create fluffy-lamps-confess.md — committed to opennextjs/opennextjs-aws by conico974 3 years ago
One things that is really weird is that the same code work locally, but not on lambda. During my test i’ve tried running a fetch in lambda and a fetch locally, in lambda it returns a bunch of symbols and locally it works fine. Not sure what’s going on here
You are the best! thank you so much I also joined the discord.
Are you using SST? You most likely need to configure CloudFront behaviors to get this working. You coud look at this gist for inspiration.
@ghardin1314 Could you create a new issue ? There is like 5 different issues here, and all of them are solved or does not have a reproduction. To help with debugging you could deploy using
OPEN_NEXT_DEBUG=true sst deploy, you’ll get a ton of logs that might help pinpoint the issueRetried with
next@14.0.4,sst@2.29.2andopen-next@2.3.3, none of our rewrites innext.config.jsseem to work.Haven’t had the time yet to create a minimal reproduction, this currently blocks us in several ways, so I hope I can spend some time to debug further and provide a reproduction.
I had the same issue on
open-next@2.3.2andnext@14.0.3. But the rewrites seem to work fine after bumping toopen-next@2.3.3. Perhaps this patch fixed the problem for me.Update:
The simple rewrites such as below works
But not for the more complex one which has query string involved. Take Sentry as an example (source):
The types might be incorrect. .getHeaders doesn’t exist, it’s in req.headers. When a site has encoding, the response renders a bunch of symbols. I sort of have half a solution but it doesn’t work in all scenarios… It’s a bit tricky, especially when the site uses prefetch or dynamic relative resources
Should be fixed in https://github.com/sst/open-next/pull/261, will release 2.2.2 soon. If not please reopen