diff --git a/.changeset/curvy-elephants-fry.md b/.changeset/curvy-elephants-fry.md new file mode 100644 index 000000000000..41bbd9e4c3eb --- /dev/null +++ b/.changeset/curvy-elephants-fry.md @@ -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 diff --git a/packages/astro/src/core/render-context.ts b/packages/astro/src/core/render-context.ts index e6ac423648ad..93899129d9e5 100644 --- a/packages/astro/src/core/render-context.ts +++ b/packages/astro/src/core/render-context.ts @@ -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, diff --git a/packages/astro/test/actions.test.js b/packages/astro/test/actions.test.js index 081e83bf6dd7..e36d29a105e2 100644 --- a/packages/astro/test/actions.test.js +++ b/packages/astro/test/actions.test.js @@ -10,6 +10,9 @@ describe('Astro Actions', () => { fixture = await loadFixture({ root: './fixtures/actions/', adapter: testAdapter(), + experimental: { + rewriting: true + } }); }); diff --git a/packages/astro/test/astro-cookies.test.js b/packages/astro/test/astro-cookies.test.js index 482474b54be2..c27efb243407 100644 --- a/packages/astro/test/astro-cookies.test.js +++ b/packages/astro/test/astro-cookies.test.js @@ -13,6 +13,9 @@ describe('Astro.cookies', () => { root: './fixtures/astro-cookies/', output: 'server', adapter: testAdapter(), + experimental: { + rewriting: true + } }); });