-
-
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: 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 <bjornlu.dev@gmail.com> --------- Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
- Loading branch information
Showing
11 changed files
with
138 additions
and
10 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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { sequence } from 'astro:middleware'; | ||
|
||
let contextReroute = false; | ||
|
||
export const first = async (context, next) => { | ||
if (context.url.pathname.includes('/auth')) { | ||
} | ||
|
||
return next(); | ||
}; | ||
|
||
export const second = async (context, next) => { | ||
if (context.url.pathname.includes('/auth')) { | ||
if (context.url.pathname.includes('/auth/dashboard')) { | ||
contextReroute = true; | ||
return await context.reroute('/'); | ||
} | ||
if (context.url.pathname.includes('/auth/base')) { | ||
return await next('/'); | ||
} | ||
} | ||
return next(); | ||
}; | ||
|
||
export const third = async (context, next) => { | ||
// just making sure that we are testing the change in context coming from `next()` | ||
if (context.url.pathname.startsWith('/') && contextReroute === false) { | ||
context.locals.auth = 'Third function called'; | ||
} | ||
return next(); | ||
}; | ||
|
||
export const onRequest = sequence(first, second, third); |
10 changes: 10 additions & 0 deletions
10
packages/astro/test/fixtures/reroute/src/pages/auth/base.astro
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,10 @@ | ||
--- | ||
--- | ||
<html> | ||
<head> | ||
<title>Base</title> | ||
</head> | ||
<body> | ||
<h1>Base</h1> | ||
</body> | ||
</html> |
10 changes: 10 additions & 0 deletions
10
packages/astro/test/fixtures/reroute/src/pages/auth/dashboard.astro
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,10 @@ | ||
--- | ||
--- | ||
<html> | ||
<head> | ||
<title>Dashboard</title> | ||
</head> | ||
<body> | ||
<h1>Dashboard</h1> | ||
</body> | ||
</html> |
10 changes: 10 additions & 0 deletions
10
packages/astro/test/fixtures/reroute/src/pages/auth/settings.astro
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,10 @@ | ||
--- | ||
--- | ||
<html> | ||
<head> | ||
<title>Settings</title> | ||
</head> | ||
<body> | ||
<h1>Settings</h1> | ||
</body> | ||
</html> |
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,12 @@ | ||
--- | ||
const auth = Astro.locals.auth; | ||
--- | ||
<html> | ||
<head> | ||
<title>Index</title> | ||
</head> | ||
<body> | ||
<h1>Index</h1> | ||
{auth ? <p>Called auth</p>: ""} | ||
</body> | ||
</html> |
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