Skip to content

Commit 64f684f

Browse files
committedSep 1, 2023
fix: throw redirect(/some-url/, 301) (fix #1104)
1 parent fe47e55 commit 64f684f

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed
 

‎docs/pages/redirect.page.server.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function onSomeHook() {
2020
}
2121
```
2222

23-
> `throw redirect()` makes temporary redirections (HTTP status code `302`). For permanent redirections (HTTP status code `301`) use <Link href="/redirects" noBreadcrumb={true} /> instead.
23+
> `throw redirect()` makes temporary redirections (HTTP status code `302`). For permanent redirections (HTTP status code `301`), you can set <Link href="/redirects" noBreadcrumb={true} /> or pass a second argument `throw redirect('/some-url', 301)`.
2424
2525
While it's most commonly used with [`guard()`](/guard) or [`onBeforeRender()`](/onBeforeRender) you can use it with any hook.
2626

‎vite-plugin-ssr/shared/route/abort.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ import {
2828
} from './utils.js'
2929
import pc from '@brillout/picocolors'
3030

31-
type RedirectStatusCode = 301 | 302
31+
type RedirectStatusCode = number &
32+
// For improved IntelliSense, we define the list of status code directly on redirect()'s argument type
33+
Parameters<typeof redirect>[1]
3234
type AbortStatusCode = number &
3335
// For improved IntelliSense, we define the list of status code directly on render()'s argument type
3436
Parameters<typeof render>[0]
@@ -44,22 +46,15 @@ type AbortRedirect = Error
4446
*
4547
* https://vite-plugin-ssr.com/redirect
4648
*
47-
* @param statusCode `301` (permanent) or `302` (temporary) redirection.
4849
* @param url The URL to redirect to.
50+
* @param statusCode By default a `302` (temporary) redirection is performed, but you can trigger a `301` (permanent) redirection instead. Alternatively, you can define permanent redirections by setting `config.redirects`, see https://vite-plugin-ssr.com/redirects.
4951
*/
50-
function redirect(url: `/${string}` | `https://${string}` | `http://${string}`): AbortRedirect
5152
function redirect(
5253
url: `/${string}` | `https://${string}` | `http://${string}`,
53-
statusCode?: RedirectStatusCode
54+
statusCode: 301 | 302 = 302
5455
): AbortRedirect {
55-
const abortCaller = 'throw redirect()' as const
56-
statusCode ??= 302
5756
assertStatusCode(statusCode, [301, 302], 'redirect')
58-
assertWarning(
59-
statusCode !== 301,
60-
"Status code 301 for `throw redirect()' is experimental and may be removed at any point",
61-
{ onlyOnce: true }
62-
)
57+
const abortCaller = 'throw redirect()' as const
6358
const pageContextAbort = {}
6459
objectAssign(pageContextAbort, {
6560
_abortCaller: abortCaller,

0 commit comments

Comments
 (0)
Please sign in to comment.