-
-
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(routing): emit error for forbidden rewrite #12339
Changes from all commits
e4cf625
2544f5e
588cc95
c35212f
b474c12
20f1118
db29dd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Adds an error when `Astro.rewrite()` is used to rewrite an on-demand route with a static route when using the `"server"` output. | ||
|
||
This is a forbidden rewrite because Astro can't retrieve the emitted static route at runtime. This route is served by the hosting platform, and not Astro itself. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import type { MiddlewareHandler, RewritePayload } from '../../types/public/common.js'; | ||
import type { APIContext } from '../../types/public/context.js'; | ||
import { AstroCookies } from '../cookies/cookies.js'; | ||
import { ForbiddenRewrite } from '../errors/errors-data.js'; | ||
import { AstroError } from '../errors/index.js'; | ||
import { apiContextRoutesSymbol } from '../render-context.js'; | ||
import { type Pipeline, getParams } from '../render/index.js'; | ||
import { defineMiddleware } from './index.js'; | ||
|
@@ -49,6 +51,22 @@ export function sequence(...handlers: MiddlewareHandler[]): MiddlewareHandler { | |
payload, | ||
handleContext.request, | ||
); | ||
|
||
// This is a case where the user tries to rewrite from a SSR route to a prerendered route (SSG). | ||
// This case isn't valid because when building for SSR, the prerendered route disappears from the server output because it becomes an HTML file, | ||
// so Astro can't retrieve it from the emitted manifest. | ||
if ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way we could avoid the duplication across all the pipelines? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It really depends on how you want to avoid the duplication. We can put the check in one single function, but I think the error should be thrown where we consume the check. What do you have in mind? |
||
pipeline.serverLike === true && | ||
handleContext.isPrerendered === false && | ||
routeData.prerender === true | ||
) { | ||
throw new AstroError({ | ||
...ForbiddenRewrite, | ||
message: ForbiddenRewrite.message(pathname, pathname, routeData.component), | ||
hint: ForbiddenRewrite.hint(routeData.component), | ||
}); | ||
} | ||
|
||
carriedPayload = payload; | ||
handleContext.request = newRequest; | ||
handleContext.url = new URL(newRequest.url); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
return Astro.rewrite("/forbidden/static") | ||
export const prerender = false | ||
--- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
export const prerender = true | ||
--- |
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.
Just noting that there may eventually be other kinds of "forbidden" rewrites, so not sure whether you want to add more context like
ForbiddenRewriteToStatic
or something like that?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'd prefer not. The kind of error would be the same, but we can customise the message based on the situation.