-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: stabilise the rewrite APIs (#11542)
* feat: stabilise the rewrite APIs * chore: rewrite changeset * oops * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <[email protected]> * chore: fix linting * fix: update exemple * code formatting * edit changeset code examples --------- Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: Erika <[email protected]>
- Loading branch information
1 parent
a62345f
commit 45ad326
Showing
20 changed files
with
85 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
The `experimental.rewriting` feature introduced behind a flag in [v4.8.0](https://github.com/withastro/astro/blob/main/packages/astro/CHANGELOG.md#480) is no longer experimental and is available for general use. | ||
|
||
`Astro.rewrite()` and `context.rewrite()` allow you to render a different page without changing the URL in the browser. Unlike using a redirect, your visitor is kept on the original page they visited. | ||
|
||
Rewrites can be useful for showing the same content at multiple paths (e.g. /products/shoes/men/ and /products/men/shoes/) without needing to maintain two identical source files. | ||
|
||
Rewrites are supported in Astro pages, endpoints, and middleware. | ||
|
||
Return `Astro.rewrite()` in the frontmatter of a `.astro` page component to display a different page's content, such as fallback localized content: | ||
|
||
```astro | ||
--- | ||
--- | ||
// src/pages/es-cu/articles/introduction.astro | ||
return Astro.rewrite("/es/articles/introduction") | ||
--- | ||
} | ||
--- | ||
``` | ||
|
||
Use `context.rewrite()` in endpoints, for example to reroute to a different page: | ||
|
||
```js | ||
// src/pages/api.js | ||
export function GET(context) { | ||
if (!context.locals.allowed) { | ||
return context.rewrite('/'); | ||
} | ||
} | ||
``` | ||
|
||
The middleware `next()` function now accepts a parameter with the same type as the `rewrite()` function. For example, with `next("/")`, you can call the next middleware function with a new `Request`. | ||
|
||
```js | ||
// src/middleware.js | ||
export function onRequest(context, next) { | ||
if (!context.cookies.get('allowed')) { | ||
return next('/'); // new signature | ||
} | ||
return next(); | ||
} | ||
``` | ||
|
||
If you were previously using this feature, please remove the experimental flag from your Astro config: | ||
|
||
```diff | ||
// astro.config.mjs | ||
export default defineConfig({ | ||
- experimental: { | ||
- rewriting: true | ||
- } | ||
}) | ||
``` | ||
|
||
If you have been waiting for stabilization before using rewrites in Astro, you can now do so. | ||
|
||
Please see [the routing guide in docs](https://docs.astro.build/en/guides/routing/#rewrites) for more about using this feature. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import {defineConfig} from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
experimental: { | ||
rewriting: true | ||
}, | ||
site: "https://example.com" | ||
}); |
5 changes: 1 addition & 4 deletions
5
packages/astro/test/fixtures/rewrite-custom-404/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import {defineConfig} from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
experimental: { | ||
rewriting: true | ||
}, | ||
site: "https://example.com" | ||
}); |
5 changes: 1 addition & 4 deletions
5
packages/astro/test/fixtures/rewrite-i18n-manual-routing/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 1 addition & 4 deletions
5
packages/astro/test/fixtures/rewrite-runtime-error-custom500/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import {defineConfig} from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
experimental: { | ||
rewriting: true | ||
}, | ||
site: "https://example.com" | ||
}); |
5 changes: 1 addition & 4 deletions
5
packages/astro/test/fixtures/rewrite-runtime-error/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import {defineConfig} from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
experimental: { | ||
rewriting: true | ||
}, | ||
site: "https://example.com" | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import {defineConfig} from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
output: "server", | ||
experimental: { | ||
rewriting: true | ||
}, | ||
site: "https://example.com" | ||
}); |
Oops, something went wrong.