open-next icon indicating copy to clipboard operation
open-next copied to clipboard

Trailing slash redirects not working

Open leanderiversen opened this issue 3 years ago • 7 comments

Hi,

I am getting the following error in CloudWatch when accessing a URL without a trailing slash, showing only Internal Server Error on the page. The trailingSlash option is enabled in next.config.js, so I am expecting the URL to be redirected to the trailing slash version.

{
    "errorType": "TypeError",
    "errorMessage": "Expected \"notfile\" to match \"[^\\/#\\?]+?\", but got \"en-gb/test-page\"",
    "stack": [
        "TypeError: Expected \"notfile\" to match \"[^\\/#\\?]+?\", but got \"en-gb/test-page\"",
        "    at file:///var/task/index.mjs:44:200831",
        "    at zm (file:///var/task/index.mjs:54:3634)",
        "    at s9 (file:///var/task/index.mjs:54:3868)",
        "    at uJ (file:///var/task/index.mjs:54:8973)",
        "    at Runtime._J [as handler] (file:///var/task/index.mjs:54:10780)",
        "    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1147:29)"
    ]
}

The error only appears when using Next.js versions above 13.4.12. Version 2.2.1 of open-next is used, along with 2.28.0 of SST.

Thank you.

leanderiversen avatar Oct 07 '23 09:10 leanderiversen

I've not been able to reproduce the issue. Are you using page dir or app dir?

Could you create a minimum reproduction repo or at least provide your next.config.js file?

conico974 avatar Oct 07 '23 10:10 conico974

I'm using the app dir, and have tried a few different versions of Next, with anything above 13.4.2 resulting in this issue. There are no issues when running it locally, and is therefore a bit more difficult to debug.

This is the next.config.js used:

/** @type {import('next').NextConfig} */

const nextConfig = {
  async redirects() {}, // redacted
  images: {
    remotePatterns: [], // redacted
  },
  poweredByHeader: false,
  reactStrictMode: true,
  trailingSlash: true,
};

module.exports = nextConfig;

leanderiversen avatar Oct 07 '23 11:10 leanderiversen

@leanderiversen redirects with cookies (possible headers) are broken: https://github.com/sst/open-next/pull/261 Can you share your redirect config (redact the sensitive paths/keys/values but keep its structure)

khuezy avatar Oct 07 '23 17:10 khuezy

I can confirm that this is still an issue without the redirects present in next.config.js, but they are structured like the Next.js documentation:

module.exports = {
  async redirects() {
    return [
      {
        source: '/about',
        destination: '/',
        permanent: true,
      },
    ]
  },
}

leanderiversen avatar Oct 07 '23 18:10 leanderiversen

I'm not getting an error, although on a related note, our redirects don't include trailing slashes. We may want to add the trailing slash if the config.trailingSlash is true, cc @conico974

khuezy avatar Oct 07 '23 21:10 khuezy

Ok i've been able to reproduce it, it happens for nested path only for me. I think we'll need to filter those internals redirect, i don't get why they had to make this complicated regex, we just need to add or remove a trailing slash. I'll write a PR later today or tomorrow to fix this

conico974 avatar Oct 08 '23 11:10 conico974

Thank you very much, @conico974. I really appreciate it!

leanderiversen avatar Oct 08 '23 11:10 leanderiversen