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

fix: disregard presence/absence of trailing slash in prerendered redirect #12966

Merged
merged 3 commits into from
Nov 7, 2024

Conversation

Rich-Harris
Copy link
Member

closes #12869. With this change, if the prerenderer encounters a redirect when rendering /foo, the resulting entry in vercel.json will have a src of /foo/? rather than /foo or /foo/.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

Copy link

changeset-bot bot commented Nov 6, 2024

🦋 Changeset detected

Latest commit: ad68def

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/adapter-vercel Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -446,7 +446,7 @@ function static_vercel_config(builder, config, dir) {

for (const [src, redirect] of builder.prerendered.redirects) {
prerendered_redirects.push({
src,
src: src.replace(/\/?$/, '/?'),
headers: {
Location: redirect.location
Copy link
Member

Choose a reason for hiding this comment

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

Is there any chance that the redirect location is the same except the trailing slash, in which case this would create an infinite loop? Maybe we should double-check that before applying the /? replacement

Copy link
Member Author

Choose a reason for hiding this comment

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

Super edge case, since you can't create a /foo route alongside a /foo/ route (best you could do is /foo alongside /[blah]/, but one would match over the other). But I guess you could do something wacky with a handle hook or a custom server, and it does indeed create an infinite loop. Pushed some code to handle that case

@benmccann benmccann added the pkg:adapter-vercel Pertaining to the Vercel adapter label Nov 6, 2024
for (const [src, redirect] of builder.prerendered.redirects) {
for (let [src, redirect] of builder.prerendered.redirects) {
if (src.replace(/\/$/, '') !== redirect.location.replace(/\/$/, '')) {
// handle the extreme edge case of a `/foo` -> `/foo/` redirect,
Copy link
Member

Choose a reason for hiding this comment

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

I'm on mobile, so maybe I'm just misreading, but I feel like this comment should apply to the if statement rather than this line

This line could probably use a different comment about disregarding the presence/absence of trailing slash in prerendered redirect

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah the position of the comment is tricky because even if it's outside the if it looks like it's describing what happens inside rather than when you don't get there

fixed slightly unconventionally: inverted the condition and made the antecedent a no-op, just the comment

Copy link
Member

Choose a reason for hiding this comment

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

lol. that works

I probably would have done it by changing the wording to something like "only apply if we're not hitting the extreme edge case of..."

but I guess I'm happy with this too. maybe the bundler will make them the same anyway 😆

@Rich-Harris Rich-Harris merged commit 030a099 into main Nov 7, 2024
12 checks passed
@Rich-Harris Rich-Harris deleted the redirect-trailing-slashes branch November 7, 2024 01:27
@github-actions github-actions bot mentioned this pull request Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:adapter-vercel Pertaining to the Vercel adapter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SvelteKit doesn't fix trailing slashes for prerendered routes
3 participants