Unverified Commit 9fd5b96e authored by Vũ Văn Dũng's avatar Vũ Văn Dũng Committed by GitHub

Make `permanentRedirect` return 308 in route handlers (#56065)



### Why?

`permanentRedirect` currently still returns a 307 response if called inside a route handler

<img width="465" alt="image" src="https://github.com/vercel/next.js/assets/44609036/e0cddd37-0292-4865-a423-7bf11ad6beae">

This PR tries to fix that.

### How?

Make `handleTemporaryRedirectResponse` (now renamed `handleRedirectResponse`) accept a status value, then `getRedirectStatusCodeFromError` is used to retrieve that status (307 or 308).
parent 80635d8a
import { getRedirectStatusCodeFromError } from '../../../../../client/components/get-redirect-status-code-from-error'
import { isNotFoundError } from '../../../../../client/components/not-found'
import {
getURLFromRedirectError,
......@@ -5,7 +6,7 @@ import {
} from '../../../../../client/components/redirect'
import {
handleNotFoundResponse,
handleTemporaryRedirectResponse,
handleRedirectResponse,
} from '../../helpers/response-handlers'
export function resolveHandlerError(err: any): Response | false {
......@@ -15,8 +16,10 @@ export function resolveHandlerError(err: any): Response | false {
throw new Error('Invariant: Unexpected redirect url format')
}
const status = getRedirectStatusCodeFromError(err)
// This is a redirect error! Send the redirect response.
return handleTemporaryRedirectResponse(redirect, err.mutableCookies)
return handleRedirectResponse(redirect, err.mutableCookies, status)
}
if (isNotFoundError(err)) {
......
import { appendMutableCookies } from '../../../web/spec-extension/adapters/request-cookies'
import { ResponseCookies } from '../../../web/spec-extension/cookies'
export function handleTemporaryRedirectResponse(
export function handleRedirectResponse(
url: string,
mutableCookies: ResponseCookies
mutableCookies: ResponseCookies,
status: number
): Response {
const headers = new Headers({ location: url })
appendMutableCookies(headers, mutableCookies)
return new Response(null, { status: 307, headers })
return new Response(null, { status, headers })
}
export function handleBadRequestResponse(): Response {
......
......@@ -447,6 +447,20 @@ createNextDescribe(
})
})
describe('permanentRedirect', () => {
it('can respond correctly', async () => {
const res = await next.fetch(basePath + '/hooks/permanent-redirect', {
// "Manually" perform the redirect, we want to inspect the
// redirection response, so don't actually follow it.
redirect: 'manual',
})
expect(res.status).toEqual(308)
expect(res.headers.get('location')).toEqual('https://nextjs.org/')
expect(await res.text()).toBeEmpty()
})
})
describe('notFound', () => {
it('can respond correctly', async () => {
const res = await next.fetch(basePath + '/hooks/not-found')
......
import { permanentRedirect } from 'next/navigation'
export async function GET() {
permanentRedirect('https://nextjs.org/')
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment