question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: Redirects/rewrites/headers support

See original GitHub issue

Just starting this thread about custom redirects/rewrites/headers support. It seems to be the most requested feature in Next.js 9.5 as of now, so I think it makes sense to support this next.

Next.js docs:

Redirects: https://nextjs.org/docs/api-reference/next.config.js/redirects Rewrites: https://nextjs.org/docs/api-reference/next.config.js/rewrites Headers: https://nextjs.org/docs/api-reference/next.config.js/headers

I am grouping these three features together they are similar and were added at the same time:

  • The data for this is stored in routes-manifest.json after Next.js build, so it’s already accessible in the handler code. Note, Next.js does have the regexes stored here as well.
  • They all support hardcoded routes + path matching: simple matching, wildcard matching, and regex matching.

Note that there is existing code that I wrote to handle trailing slash redirects (which is not using the default-added redirect in routes-manifest.json right now for perf reasons)

Since we are now using Rollup.js to bundle the handlers, we can write separate files for better maintainability. I suggest the following steps:

  • Create a matcher class that will do all matching including simple path matching, wildcard path matching, and regex matching. This code is shared with header/rewrite/redirects and other functionality that may need it in the future.
  • Redirects/rewrites: in origin request handler, create new functions/classes to handle rewrites/redirects. For example, for redirects, we would (1) refactor existing trailing slash redirect code into this class, and then (2) add support for custom redirects. Similar for rewrites. These would be done close to the start (e.g replacing the redirect code).
  • Headers: create new functions/class to add headers and use it in origin response handler (public files, HTML pages). I don’t believe it’s needed in origin request handler (SSR pages) as you can just set res headers in the page code itself.
  • In each of these classes (redirect/rewrite/headers), at a high level, we would:
  1. Load routes-manifest.json for the redirects/rewrites/headers list.
  2. Check custom paths against the matcher.
  3. Do the appropriate action: e.g return redirect response (with correct status code), rewrite the URI, or add a header.

I imagine the tricky part would be the path matching.

I’m already working on the e2e tests, so if someone wants to start on this or has any thoughts on the design, feel free to add your thoughts here. If not, I will work on this sometime later this month.

@danielcondemarin FYI.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:30
  • Comments:24 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
dphangcommented, Oct 7, 2020

The latest alpha should have the fixes for redirects, and also adds rewrites support (except for external URLs are not implemented yet): https://github.com/serverless-nextjs/serverless-next.js/releases/tag/%40sls-next%2Fserverless-component%401.18.0-alpha.2.

Please try it out and open an issue in case of any bugs.

4reactions
dphangcommented, Sep 25, 2020

Update: sorry for the delay, I have been busy helping stabilize this component with end-to-end tests and other bug fixes. I will start working on the matcher next (probably this weekend or so)

Read more comments on GitHub >

github_iconTop Results From Across the Web

RFC 7231: Hypertext Transfer Protocol (HTTP/1.1)
This document defines the semantics of HTTP/1.1 messages, as expressed by request methods, request header fields, response status codes, and response header ......
Read more >
RFC 2616 HTTP/1.1 - IETF
A cache that does not support the Range and Content-Range headers MUST NOT cache 206 (Partial) responses. 10.3 Redirection 3xx This class of...
Read more >
HTTP/1.1: Status Code Definitions
This class of status code indicates a provisional response, consisting only of the Status-Line and optional headers, and is terminated by an empty...
Read more >
Rewrite HTTP headers and URL with Application Gateway
This article provides an overview of rewriting HTTP headers and URL in Azure Application Gateway.
Read more >
Are there any known cases of HTTP clients not supporting ...
Relative redirects (i.e. relative URLs in the HTTP Location: header) were originally not supported according to RFC 2616, section 14.30.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found