-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest 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 |
529b6e5
to
8a76199
Compare
8a76199
to
3e00a3d
Compare
CodSpeed Performance ReportMerging #12733 will not alter performanceComparing Summary
|
redirectRoutes.push({ | ||
type: 'redirect', | ||
isIndex: false, | ||
route: '//', | ||
pattern: /^\/+$/, | ||
segments: [], | ||
params: [], | ||
component: '/', | ||
generate: () => '//', | ||
pathname: '//', | ||
prerender: false, | ||
redirect: '/', | ||
redirectRoute: routeMap.get('/'), | ||
fallbackRoutes: [], | ||
distURL: [], | ||
origin: 'project', | ||
}); |
There was a problem hiding this comment.
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:
astro/packages/astro/src/core/routing/manifest/create.ts
Lines 237 to 261 in 901c21f
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
There was a problem hiding this comment.
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.
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 forhttp://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