serverless-next.js icon indicating copy to clipboard operation
serverless-next.js copied to clipboard

Rewrites don't work with special symbols in URL

Open savvaleukhin opened this issue 2 years ago • 3 comments

Issue Summary

Rewrites don't work with special symbols in URL

  1. no-op rewrites don't work with paths that include symbols like @ and =
  2. external rewrites will build incorrect destination routes for paths that include symbols like @ and =

No-op rewrites example:

rewrite config:

{
  source: '/:path*',
  destination: '/:path*',
},
{
  source: "/:path*",
  destination: `https://external.app/:path*`
}

PageManifest:

pageManifest = {
  pages: {
    dynamic: [],
    html: {
      dynamic: {},
      nonDynamic: {}
    },
    ssg: {
      dynamic: {},
      nonDynamic: {},
      notFound: {}
    },
    ssr: {
      dynamic: {},
      nonDynamic: {}
    }
  }
};

Actual behavior

input URL: /@iamatest rewrite result: /%40iamatest

input URL: /iamatest= rewrite result: /iamatest%3D

Expected behavior

according to how Next.js server rewrites work

input URL: /@iamatest rewrite result: https://external.app/@iamatest

input URL: /iamatest= rewrite result: https://external.app/iamatest=

External rewrites example:

rewrite config:

{
  source: "/:path*",
  destination: `https://external.app/:path*`
}

Actual behavior

input URL: /@iamatest rewrite result: https://external.app/%40iamatest

input URL: /iamatest= rewrite result: https://external.app/iamatest%3D

Expected behavior

according to how Next.js server rewrites work

input URL: /@iamatest rewrite result: https://external.app/@iamatest

input URL: /iamatest= rewrite result: https://external.app/iamatest=

Results from above show that rewrites create incorrect URLs when symbols like @ and = are used.

In my case, this causes an additional problem because the external.app redirects to the correct URL and I get an infinite loop of redirects:

  • a user requests https://next.js.app/@iamatest
  • next.js.app rewrites to https://external.app/%40iamatest
  • external.app redirects to https://next.js.app/@iamatest
  • ... repeat ...

Versions

  • @sls-next/serverless-component version: 3.7.0
  • Next.js version: 11.1.4

Checklist

  • [x] You have reviewed the README and FAQs, which answers several common questions.
  • [x] You have reviewed our DEBUGGING wiki and have tried your best to include complete information and reproduction steps (including your configuration) as is possible. As there is only one maintainer (who maintains this in his free time) and thus very limited resources, if you have time, please try to debug the issue a bit yourself if possible.
  • [x] You have first tried using the most recent latest or alpha @sls-next/serverless-component release version, which may have already fixed your issue or implemented the feature you are trying to use. Note that the old serverless-next.js component and the serverless-next.js plugin are deprecated and no longer maintained.

savvaleukhin avatar May 11 '22 12:05 savvaleukhin

In my understanding, it's related to the usage of the encoder here and here.

savvaleukhin avatar May 11 '22 12:05 savvaleukhin

Just to chime in, I experienced this issue as well working on this for a side project. We were trying to use the # symbol at first, but ended up nixing it because it just never worked and I wasn't going to fork it.

lucasbaze avatar May 11 '22 14:05 lucasbaze

Inspected the code referenced by @sleukhin and don't see a reason to encode/decode URLs at least from point of tests provided. @dphang I am about to investigate the problem with a fix but need a bit more context beforehands.

maximkoretskiy avatar May 12 '22 10:05 maximkoretskiy