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: handle requests for double slash in dev #12733

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ascorbic
Copy link
Contributor

Changes

Fixes a bug caused by the fact that new URL("//", "http://localhost") is invalid: it treats the double slash as an empty protocol-relative URL. This caused a 500 error if there was a request for http://localhost//. Now it handles that type of URL correctly, and also redirects to /. This redirect is only implemented for dev, because support for it in platforms is poor, and needs special handling. For that reason we can defer rto the adapters to handle it.

Fixes #12722

Testing

Adds tests

Docs

Copy link

changeset-bot bot commented Dec 13, 2024

🦋 Changeset detected

Latest commit: ae9f1b9

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

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

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Dec 13, 2024
@ascorbic ascorbic force-pushed the double-slash-redirect branch from 529b6e5 to 8a76199 Compare December 13, 2024 10:56
@ascorbic ascorbic force-pushed the double-slash-redirect branch from 8a76199 to 3e00a3d Compare December 13, 2024 10:57
Copy link

codspeed-hq bot commented Dec 13, 2024

CodSpeed Performance Report

Merging #12733 will not alter performance

Comparing double-slash-redirect (ae9f1b9) with main (25c1e59)

Summary

✅ 6 untouched benchmarks

@ascorbic ascorbic marked this pull request as draft December 13, 2024 11:58
@ascorbic ascorbic marked this pull request as ready for review December 13, 2024 12:14
Comment on lines +512 to +528
redirectRoutes.push({
type: 'redirect',
isIndex: false,
route: '//',
pattern: /^\/+$/,
segments: [],
params: [],
component: '/',
generate: () => '//',
pathname: '//',
prerender: false,
redirect: '/',
redirectRoute: routeMap.get('/'),
fallbackRoutes: [],
distURL: [],
origin: 'project',
});
Copy link
Member

Choose a reason for hiding this comment

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

I would advise against creating full routes like this because many of those data might not be mandatory, and some of them need to be built using the proper functions. E.g. getPattern or getRouteGenerator.

Plus, we need to take into consideration things like base, trailing slash and more. Follow this pattern:

components.push(item.file);
const component = item.file;
const { trailingSlash } = settings.config;
const pattern = getPattern(segments, settings.config.base, trailingSlash);
const generate = getRouteGenerator(segments, trailingSlash);
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
? `/${segments.map((segment) => segment[0].content).join('/')}`
: null;
const route = joinSegments(segments);
routes.push({
route,
isIndex: item.isIndex,
type: item.isPage ? 'page' : 'endpoint',
pattern,
segments,
params,
component,
generate,
pathname: pathname || undefined,
prerender,
fallbackRoutes: [],
distURL: [],
origin: 'project',
});
}

It requires more code, but it's more reliable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried that at first but it didn't seem to be possible for this. Unless I'm missing something, the path isn't one that can be represented with segments. It's for a specific path, regardless of trailing slash or base rules. The pattern needs to match two or more slashes, whether or not trailing slash is enabled, and doesn't apply if base is set. Of the fields I included, it looks like pathname is the only one that's optional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid URL error when domain contains multiple slashes at the end
2 participants