Skip to content

Commit

Permalink
fix: check experimental flag when using the rewrite function (#11349)
Browse files Browse the repository at this point in the history
* fix: check experimental flag when using the rewrite function

* apply feedback
  • Loading branch information
ematipico authored Jun 26, 2024
1 parent d7ae91c commit 98d9ce4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/curvy-elephants-fry.md
Original file line number Diff line number Diff line change
@@ -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
30 changes: 29 additions & 1 deletion packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions packages/astro/test/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ describe('Astro Actions', () => {
fixture = await loadFixture({
root: './fixtures/actions/',
adapter: testAdapter(),
experimental: {
rewriting: true
}
});
});

Expand Down
3 changes: 3 additions & 0 deletions packages/astro/test/astro-cookies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ describe('Astro.cookies', () => {
root: './fixtures/astro-cookies/',
output: 'server',
adapter: testAdapter(),
experimental: {
rewriting: true
}
});
});

Expand Down

0 comments on commit 98d9ce4

Please sign in to comment.