0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

fix: check experimental flag when using the rewrite function (#11349)

* fix: check experimental flag when using the rewrite function

* apply feedback
This commit is contained in:
Emanuele Stoppa 2024-06-26 15:08:36 +01:00 committed by GitHub
parent d7ae91c83c
commit 98d9ce41f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 40 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes an issue where Astro didn't throw an error when `Astro.rewrite` was used without providing the experimental flag

View file

@ -147,7 +147,7 @@ export class RenderContext {
componentInstance = component;
this.isRewriting = true;
} else {
this.pipeline.logger.warn(
this.pipeline.logger.error(
'router',
'The rewrite API is experimental. To use this feature, add the `rewriting` flag to the `experimental` object in your Astro config.'
);
@ -239,6 +239,20 @@ export class RenderContext {
const rewrite = async (reroutePayload: RewritePayload) => {
pipeline.logger.debug('router', 'Called rewriting to:', reroutePayload);
if (!this.pipeline.manifest.rewritingEnabled) {
this.pipeline.logger.error(
'router',
'The rewrite API is experimental. To use this feature, add the `rewriting` flag to the `experimental` object in your Astro config.'
);
return new Response(
'The rewrite API is experimental. To use this feature, add the `rewriting` flag to the `experimental` object in your Astro config.',
{
status: 500,
statusText:
'The rewrite API is experimental. To use this feature, add the `rewriting` flag to the `experimental` object in your Astro config.',
}
);
}
const [routeData, component, newURL] = await pipeline.tryRewrite(
reroutePayload,
this.request,
@ -433,6 +447,20 @@ export class RenderContext {
};
const rewrite = async (reroutePayload: RewritePayload) => {
if (!this.pipeline.manifest.rewritingEnabled) {
this.pipeline.logger.error(
'router',
'The rewrite API is experimental. To use this feature, add the `rewriting` flag to the `experimental` object in your Astro config.'
);
return new Response(
'The rewrite API is experimental. To use this feature, add the `rewriting` flag to the `experimental` object in your Astro config.',
{
status: 500,
statusText:
'The rewrite API is experimental. To use this feature, add the `rewriting` flag to the `experimental` object in your Astro config.',
}
);
}
pipeline.logger.debug('router', 'Calling rewrite: ', reroutePayload);
const [routeData, component, newURL] = await pipeline.tryRewrite(
reroutePayload,

View file

@ -10,6 +10,9 @@ describe('Astro Actions', () => {
fixture = await loadFixture({
root: './fixtures/actions/',
adapter: testAdapter(),
experimental: {
rewriting: true
}
});
});

View file

@ -13,6 +13,9 @@ describe('Astro.cookies', () => {
root: './fixtures/astro-cookies/',
output: 'server',
adapter: testAdapter(),
experimental: {
rewriting: true
}
});
});