-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rewriting (experimental) (#10867)
* feat: implement reroute in dev (#10818) * chore: implement reroute in dev * chore: revert naming change * chore: conditionally create the new request * chore: handle error * remove only * remove only * chore: add tests and remove logs * chore: fix regression * chore: fix regression route matching * chore: remove unwanted test * feat: reroute in SSG (#10843) * feat: rerouting in ssg * linting * feat: reroute for SSR (#10845) * feat: implement reroute in dev (#10818) * chore: implement reroute in dev * chore: revert naming change * chore: conditionally create the new request * chore: handle error * remove only * remove only * chore: add tests and remove logs * chore: fix regression * chore: fix regression route matching * chore: remove unwanted test * feat: reroute in SSG (#10843) * feat: rerouting in ssg * linting * feat: rerouting in ssg * linting * feat: reroute for SSR * fix rebase * fix merge issue * feat: rerouting in the middleware (#10853) * feat: implement reroute in dev (#10818) * chore: implement reroute in dev * chore: revert naming change * chore: conditionally create the new request * chore: handle error * remove only * remove only * chore: add tests and remove logs * chore: fix regression * chore: fix regression route matching * chore: remove unwanted test * feat: reroute in SSG (#10843) * feat: rerouting in ssg * linting * feat: rerouting in ssg * linting * feat: reroute for SSR * fix rebase * fix merge issue * feat: implement the `next(payload)` feature for rerouting * chore: revert code * chore: fix code * Apply suggestions from code review Co-authored-by: Bjorn Lu <[email protected]> --------- Co-authored-by: Bjorn Lu <[email protected]> * feat: rerouting * chore: rename to `rewrite` * chore: better error message * chore: update the chageset * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <[email protected]> * chore: update docs based on feedback * lock file * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: Matthew Phillips <[email protected]> Co-authored-by: Ben Holmes <[email protected]> * feedback * rename * add tests for 404 * revert change * fix regression * Update .changeset/pink-ligers-share.md Co-authored-by: Sarah Rainsberger <[email protected]> --------- Co-authored-by: Bjorn Lu <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: Matthew Phillips <[email protected]> Co-authored-by: Ben Holmes <[email protected]>
- Loading branch information
1 parent
7bbd664
commit 47877a7
Showing
41 changed files
with
1,192 additions
and
222 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 | ||
--- | ||
|
||
Adds experimental rewriting in Astro with a new `rewrite()` function and the middleware `next()` function. | ||
|
||
The feature is available via an experimental flag in `astro.config.mjs`: | ||
|
||
```js | ||
export default defineConfig({ | ||
experimental: { | ||
rewriting: true | ||
} | ||
}) | ||
``` | ||
|
||
When enabled, you can use `rewrite()` 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.rewrite("/") | ||
} | ||
--- | ||
``` | ||
|
||
```js | ||
// src/pages/api.js | ||
export function GET(ctx) { | ||
if (!ctx.locals.allowed) { | ||
return ctx.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(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
Oops, something went wrong.