Skip to content

chore: add docs on rewrites and redirects #767

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

Merged
merged 5 commits into from
Nov 8, 2021
Merged

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Nov 5, 2021

Adds a page to the plugin docs that explains some of the subtleties of how redirects and rewrites now work in the plugin.

Fixes #756

Rendered version

@ascorbic ascorbic added the documentation Improvements or additions to documentation label Nov 5, 2021
@github-actions github-actions bot added the type: chore work needed to keep the product and development running smoothly label Nov 5, 2021
@ascorbic ascorbic requested review from a team and rstavchansky November 5, 2021 10:41
@ascorbic ascorbic self-assigned this Nov 5, 2021
Version 4 of the Essential Next.js build plugin adds support for native Next.js [rewrites](https://nextjs.org/docs/api-reference/next.config.js/rewrites) and [redirects](https://nextjs.org/docs/api-reference/next.config.js/redirects). These are defined in your `next.config.js` file and include support for some features that are not included in Netlify redirects and rewrites.

## Using Netlify redirects and rewrites on a Next.js site
Every site on Netlify supports [redirects and rewrites](https://docs.netlify.com/routing/redirects/), which are defined in a `_redirects` file or `netlify.toml`, and sites that use this plugin are no exceptions. However there are some caveats to bear in mind when using them. The plugin generates several rewrites of its own, which are used to map paths from the site to different Netlify functions which handle SSR, preview mode and images, as well as assets in `/_next/static`. Any Netlify redirects or rewrites that you create take precedence over these rewrites, so you should avoid adding a root level rewrite, because that would override the rewrites required by the plugin.
Copy link

@tiffafoo tiffafoo Nov 5, 2021

Choose a reason for hiding this comment

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

[dust] Having an example here of what they should avoid vs what they should do might be helpful, but super clear changes 👌

Netlify and Next.js redirects support different features and are evaluated at different points in the request lifecycle, so there are some considerations in deciding which one to use with your site.

### When to use Netlify redirects or rewrites:
- Generally if your redirect can be handled with Netlify redirects, these are faster to evaluate and should be preferred.
Copy link

@tiffafoo tiffafoo Nov 5, 2021

Choose a reason for hiding this comment

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

[sand, not for this pr] I'm guessing we should have some metrics/blog posts showing why/how much Netlify is faster at some point.

tiffafoo
tiffafoo previously approved these changes Nov 5, 2021
Copy link

@tiffafoo tiffafoo left a comment

Choose a reason for hiding this comment

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

Looks great! I'm sure Rachel will have more input :)

Release-as: 4.0.0-beta.6

Co-authored-by: Tiffany Le-Nguyen <tiffany@netlify.com>
@kodiakhq kodiakhq bot merged commit b32a08c into main Nov 8, 2021
@kodiakhq kodiakhq bot deleted the mk/redirect-docs branch November 8, 2021 15:24
Copy link

@rstavchansky rstavchansky left a comment

Choose a reason for hiding this comment

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

Since I was out and missed the review window, I'll open a tiny PR to update the broken link and raise the option of adding an example of a root level rewrite.

Version 4 of the Essential Next.js build plugin adds support for native Next.js [rewrites](https://nextjs.org/docs/api-reference/next.config.js/rewrites) and [redirects](https://nextjs.org/docs/api-reference/next.config.js/redirects). These are defined in your `next.config.js` file and include support for some features that are not included in Netlify redirects and rewrites.

## Using Netlify redirects and rewrites on a Next.js site
Every site on Netlify supports [redirects and rewrites](https://docs.netlify.com/routing/redirects/), which are defined in a `_redirects` file or `netlify.toml`, and sites that use this plugin are no exceptions. However there are some caveats to bear in mind when using them. The plugin generates several rewrites of its own, which are used to map paths from the site to different Netlify functions which handle SSR, preview mode and images, as well as assets in `/_next/static`. Any Netlify redirects or rewrites that you create [take precedence over these rewrites](#Redirect-and-rewrite-precedence), so you should avoid adding a root level rewrite, because that would override the rewrites required by the plugin.

Choose a reason for hiding this comment

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

I'm late to the party on this one after being out for a few days, but an example of what you mean by a root level rewrite could be useful. I may be off base, but something like this?

/*   /index.html   200

If you make this addition, this sentence could benefit from one more Netlify mention to be crystal clear:

Any Netlify redirects or rewrites that you create take precedence over these rewrites, so you should avoid adding a root level Netlify rewrite, because that would override the rewrites required by the plugin.


If you are using a monorepo you will need to change `publish` to point to the full path to the built `.next` directory, which may be in a subdirectory. If you have changed your `distDir` then it will need to match that.

If you are using Nx, then you will need to point `publish` to the folder inside `dist`, e.g. `dist/apps/myapp/.next`.

If you currently use redirects or rewrites on your site, see [the Rewrites and Redirects guide](https://github.com/netlify/netlify-plugin-nextjs/blob/main/docs/rewrites-and-redirects.md) for information on changes to how they are handled in this version.

Choose a reason for hiding this comment

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

This link is causing a 404 error. It should point here: https://github.com/netlify/netlify-plugin-nextjs/blob/main/docs/redirects-rewrites.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge documentation Improvements or additions to documentation type: chore work needed to keep the product and development running smoothly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document change to Netlify redirect handling
3 participants