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

Most upvoted comments

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

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

Hey friend, were you able to fix this? Currently running into the same issue when installing PostHog. thanks!

Are you using SST? You most likely need to configure CloudFront behaviors to get this working. You coud look at this gist for inspiration.

You are the best! thank you so much I also joined the discord.

Hey friend, were you able to fix this? Currently running into the same issue when installing PostHog. thanks!

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 issue

Retried with next@14.0.4, sst@2.29.2 and open-next@2.3.3, none of our rewrites in next.config.js seem 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.2 and next@14.0.3. But the rewrites seem to work fine after bumping to open-next@2.3.3. Perhaps this patch fixed the problem for me.


Update:

The simple rewrites such as below works

const nextConfig = {
  rewrites: async () => [
    { source: "/rewrite", destination: "https://github.com" },
  ],
}
module.exports = nextConfig

But not for the more complex one which has query string involved. Take Sentry as an example (source):

const injectedRewrite = {
  // Matched rewrite routes will look like the following: `[tunnelPath]?o=[orgid]&p=[projectid]`
  // Nextjs will automatically convert `source` into a regex for us
  source: `${tunnelPath}(/?)`,
  has: [
    {
      type: 'query',
      key: 'o', // short for orgId - we keep it short so matching is harder for ad-blockers
      value: '(?<orgid>\\d*)',
    },
    {
      type: 'query',
      key: 'p', // short for projectId - we keep it short so matching is harder for ad-blockers
      value: '(?<projectid>\\d*)',
    },
  ],
  destination: 'https://o:orgid.ingest.sentry.io/api/:projectid/envelope/?hsts=0',
};

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