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

Improve rewrites documentation #34725

Merged
merged 7 commits into from Feb 24, 2022
Merged

Improve rewrites documentation #34725

merged 7 commits into from Feb 24, 2022

Conversation

Nutlope
Copy link
Contributor

@Nutlope Nutlope commented Feb 23, 2022

This is a PR to update the rewrites documentation. This is after struggling to get rewrites to work with trailingSlash for a customer as this wasn't documented. The main culprit was the :path* wildcard not catching trailing slashes. The changes made to for this commit were:

  • Added example for redirecting to blog and docs
  • Expanded on original code example for external URLs
  • Added code example for using rewrites with trailingSlash

Context

For a reproduction of the :path* wildcard not catching trailing slashes as expected, see below. Click on the "not working" demo link below, navigate to a specific blog post, then reload the page. It will redirect infinitely.

If you want to take a look at the code, here are the full repos. They both have trailingSlash: true:

@Nutlope Nutlope marked this pull request as ready for review February 23, 2022 21:10
@Nutlope Nutlope requested a review from leerob February 23, 2022 21:10
destination: 'https://example.com/blog/',
},
{
source: '/blog/:path(.+)',
Copy link
Member

Choose a reason for hiding this comment

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

This seems like a bug, wdyt @ijjk 🤔

Suggested change
source: '/blog/:path(.+)',
source: '/blog/:path(.+)',

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For context, I discovered that :path* doesn't catch trailing slashes so Davis came up with a wildcard expression that does.

Copy link
Member

Choose a reason for hiding this comment

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

We should be handling the trailing slash here

// we add an optional trailing slash at the end for easier
// configuring between trailingSlash: true/false
can you provide a reproduction where this isn't working?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ijjk yep! Here's a reproduction. Click on the "not working" demo link below, navigate to a specific blog post, then reload the page. It will redirect infinitely.

If you want to take a look at the code, here are the full repos. They both have trailingSlash: true:

Copy link
Member

Choose a reason for hiding this comment

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

Oh it looks like the destination for this rewrite needs to end with a trailingSlash to match the other deployments expectation e.g. does changing it to the below resolve this?

destination: `${BLOG_URL}/blog/:path*/`

Copy link
Member

Choose a reason for hiding this comment

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

I think documenting that the destination for a rewrite needs to honor the external destination's trailingSlash config makes sense

Copy link
Contributor Author

@Nutlope Nutlope Feb 24, 2022

Choose a reason for hiding this comment

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

Good catch, it works when you include a trailing slash for both the source and destination like this.

{
  source: "/blog/:path*/",
  destination: `${BLOG_URL}/blog/:path*/`,
},

I'm assuming this is better than the :path(.+) wildcard so I just updated the PR to reflect that.

Co-authored-by: JJ Kasper <jj@jjsweb.site>
@kodiakhq kodiakhq bot merged commit ae2d901 into canary Feb 24, 2022
@kodiakhq kodiakhq bot deleted the improve-rewrite-docs branch February 24, 2022 22:25
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants