Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda-at-edge): support custom redirects #627

Merged
merged 11 commits into from
Sep 29, 2020
Merged

Conversation

dphang
Copy link
Collaborator

@dphang dphang commented Sep 26, 2020

This is the first part of #587. Summary:

  • Add path matcher code using path-to-regexp for custom redirects (same one as Next.js uses). Some of this can also be reused for custom rewrites and headers.
  • Add support for custom redirects in next.config.js. Custom redirects are matched one-by-one from the provided redirects in routes-manifest and the first matching redirect is returned. The default trailing slash redirects are ignored for now (by removing them at build time) as they are already handled in a non-regex solution. I also need to put the redirect logic near the default-handler start right after trailing slash redirect, as redirects can overwrite an existing source page (same behavior in Vercel).
  • There may be slight performance concerns here, but AFAIK path-to-regexp uses RegExp underneath which should be quite performant. I would only expect there to be performance issues if users have really long lists of redirects and/or are using complex paths. For the majority of users, this shouldn't be the case - and the default trailing slash redirects are anyway handled without regex.
  • Caveat: redirects (both custom and trailing slash) don't work with API route sources yet (will add in later PR) - Vercel supports this, although I imagine it may be fairly uncommon to redirect an API route.

Note: big PR but a lot of it is updating the tests to have good coverage.

Testing

Added lots of test coverage via unit tests, integration tests, and e2e tests.

Note: there is no e2e test with base path and trailingSlash = true, although I have locally tested it and unit/integration tests should cover it. I will add in a future PR as part of larger work for more e2e tests.

If anyone can pull this branch and test this and/or suggest other test cases I may have missed, that will be really helpful. I think I covered most of them. Thanks!

import { compileDestination, matchPath } from "./matcher";
import { RedirectData, RoutesManifest } from "../../types";
import * as http from "http";

Copy link
Collaborator Author

@dphang dphang Sep 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we have Rollup, we can make the component more maintainable by using different files - it will all get bundled/optimized into the handler JS files. Not sure what's a good name, let me know if you have thoughts on this (I put the in routing/ for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think routing/ makes sense. On a related note, would be nice if at some point we refactored all routing concerns into a router, similar to what the next-server impl does

Copy link
Contributor

@benjipott benjipott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work 👏

packages/libs/lambda-at-edge/src/routing/redirector.ts Outdated Show resolved Hide resolved
@danielcondemarin danielcondemarin merged commit d2f9679 into master Sep 29, 2020
@delete-merged-branch delete-merged-branch bot deleted the dphang/matcher branch September 29, 2020 07:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants