-
-
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.
- Loading branch information
Showing
6 changed files
with
132 additions
and
11 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,49 @@ | ||
--- | ||
"astro": minor | ||
--- | ||
|
||
Add experimental rerouting in Astro, via `reroute()` function and `next()` function. | ||
|
||
The feature is available via experimental flag: | ||
|
||
```js | ||
export default defineConfig({ | ||
experimental: { | ||
rerouting: true | ||
} | ||
}) | ||
``` | ||
|
||
When enabled, you can use `reroute()` to **render** another page without changing the URL of the browser in Astro pages and endpoints. | ||
|
||
```astro | ||
--- | ||
// src/pages/dashboard.astro | ||
if (!Astro.props.allowed) { | ||
return Astro.reroute("/") | ||
} | ||
--- | ||
``` | ||
|
||
```js | ||
// src/pages/api.js | ||
export function GET(ctx) { | ||
if (!ctx.locals.allowed) { | ||
return ctx.reroute("/") | ||
} | ||
} | ||
``` | ||
|
||
The middleware `next()` function now accepts the same payload of the `reroute()` function. For example, with `next("/")`, you can call the next middleware function with a new `Request`. | ||
|
||
```js | ||
// src/middleware.js | ||
export function onRequest(ctx, next) { | ||
if (!ctx.cookies.get("allowed")) { | ||
return next("/") // new signature | ||
} | ||
return next(); | ||
} | ||
``` | ||
|
||
> **NOTE**: please [read the RFC](https://github.com/withastro/roadmap/blob/feat/reroute/proposals/0047-rerouting.md) to understand the current expectations of the new APIs. |
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