-
-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: client side redirects #928
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
916f157
test: add redirects to static host
pmelab 28dbcb9
test: integration tests for client side redirect handling
pmelab 5892e4a
test: handle client side redirects by redirecting RSC files
pmelab 0a350e4
docs: documentation on redirects
pmelab 6b898a6
docs: move redirects documentation into the docs folder
pmelab b224891
Merge branch 'main' into client-side-redirects
dai-shi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,87 @@ | ||
--- | ||
slug: redirects | ||
title: Redirects | ||
description: How to add HTTP redirects to your Waku project. | ||
--- | ||
|
||
### Redirects | ||
|
||
Redirects are not handled by Waku directly. Instead, you can use either a custom middleware or the hosting environment to achieve that. The `<Link />` component does not deal with redirects either and will by default show the **404** page instead. To resolve this, you have to add an additional redirect for each redirected path, that points Waku to the correct `RSC` file. If there is a redirect from `/old` to `/new`, there also has to be one from `/RSC/old.txt`` to `/RSC/new.txt`to make the `<Link />` component`s smooth page transition work. | ||
|
||
> The `/RSC/` file naming convention is [subject to change](https://github.com/dai-shi/waku/discussions/929#discussioncomment-10825975) in future versions of Waku. | ||
|
||
#### Redirect via middleware | ||
|
||
Create a new middleware somewhere in your project and add it to the `waku.config.ts` file. | ||
|
||
```typescript | ||
// ./src/middleware/redirect.ts | ||
import type { Middleware } from 'waku/config'; | ||
|
||
const redirectsMiddleware: Middleware = () => async (ctx, next) => { | ||
// Define the list of redirects. | ||
const redirects = { | ||
'/old': '/new', | ||
// ... add more redirects here | ||
}; | ||
|
||
// Create a corresponding /RSC/ entry for each redirect. | ||
const withRSC = Object.fromEntries( | ||
Object.entries(redirects).flatMap(([from, to]) => [ | ||
[from, to], | ||
[`/RSC${from}.txt`, `/RSC${to}.txt`], | ||
]), | ||
); | ||
|
||
if (withRSC[ctx.req.url.pathname]) { | ||
ctx.res.status = 301; | ||
ctx.res.headers = { | ||
Location: redirects[ctx.req.url.pathname], | ||
}; | ||
} else { | ||
return await next(); | ||
} | ||
}; | ||
|
||
export default redirectsMiddleware; | ||
``` | ||
|
||
```typescript | ||
// ./waku.config.ts | ||
import type { Config } from 'waku/config'; | ||
|
||
export default { | ||
middleware: () => [ | ||
import('./src/middleware/redirects.js'), | ||
import('waku/middleware/dev-server'), | ||
import('waku/middleware/headers'), | ||
import('waku/middleware/rsc'), | ||
import('waku/middleware/ssr'), | ||
], | ||
} satisfies Config; | ||
``` | ||
|
||
#### Redirect via hosting environment | ||
|
||
This very much depends on the hosting environment you are using. For example, on [Vercel](https://vercel.com/docs/projects/project-configuration#redirects) you can use the `vercel.json` file to define redirects. | ||
|
||
```json | ||
{ | ||
"redirects": [ | ||
{ "source": "/old", "destination": "/new", "permanent": true }, | ||
{ | ||
"source": "/RSC/old.txt", | ||
"destination": "/RSC/new.txt", | ||
"permanent": true | ||
} | ||
] | ||
} | ||
``` | ||
|
||
[Netlify](https://docs.netlify.com/routing/redirects/#syntax-for-the-redirects-file) and [Cloudflare pages](https://developers.cloudflare.com/pages/configuration/redirects/) will respect a `_redirects` file that you can place in the `public` folder: | ||
bbb | ||
|
||
``` | ||
/old /new 301 | ||
/RSC/old.txt /RSC/new.txt 301 | ||
``` |
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,7 @@ | ||
{ | ||
"redirects": [ | ||
{ "source": "/redirect", "destination": "/exists" }, | ||
{ "source": "/RSC/redirect.txt", "destination": "/RSC/exists.txt" }, | ||
{ "source": "/broken-redirect", "destination": "/broken" } | ||
] | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to change it in the next patch version. 😝