Skip to content

Commit

Permalink
use consistent const for status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Nov 28, 2023
1 parent f43bd1b commit 1d2ab51
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/next/src/client/components/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function getRedirectError(
/**
* When used in a streaming context, this will insert a meta tag to
* redirect the user to the target page. When used in a custom app route, it
* will serve a 307/302 to the caller.
* will serve a 307/303 to the caller.
*
* @param url the url to redirect to
*/
Expand Down
9 changes: 4 additions & 5 deletions packages/next/src/lib/redirect-status.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
PERMANENT_REDIRECT_STATUS,
TEMPORARY_REDIRECT_STATUS,
} from '../shared/lib/constants'
import { RedirectStatusCode } from '../shared/lib/constants'

export const allowedStatusCodes = new Set([301, 302, 303, 307, 308])

Expand All @@ -11,7 +8,9 @@ export function getRedirectStatus(route: {
}): number {
return (
route.statusCode ||
(route.permanent ? PERMANENT_REDIRECT_STATUS : TEMPORARY_REDIRECT_STATUS)
(route.permanent
? RedirectStatusCode.PermanentRedirect
: RedirectStatusCode.TemporaryRedirect)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '../../client/components/redirect'
import { renderToReadableStream } from 'react-dom/server.edge'
import { streamToString } from '../stream-utils/node-web-streams-helper'
import { RedirectStatusCode } from '../../shared/lib/constants'

export function makeGetServerInsertedHTML({
polyfills,
Expand Down Expand Up @@ -38,8 +39,9 @@ export function makeGetServerInsertedHTML({
)
} else if (isRedirectError(error)) {
const redirectUrl = getURLFromRedirectError(error)
const statusCode = getRedirectStatusCodeFromError(error)
const isPermanent =
getRedirectStatusCodeFromError(error) === 308 ? true : false
statusCode === RedirectStatusCode.PermanentRedirect ? true : false
if (redirectUrl) {
errorMetaTags.push(
<meta
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/base-http/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http'
import type { I18NConfig } from '../config-shared'

import { PERMANENT_REDIRECT_STATUS } from '../../shared/lib/constants'
import { RedirectStatusCode } from '../../shared/lib/constants'
import type { NextApiRequestCookies } from '../api-utils'
import { getCookieParser } from '../api-utils/get-cookie-parser'

Expand Down Expand Up @@ -84,7 +84,7 @@ export abstract class BaseNextResponse<Destination = any> {

// Since IE11 doesn't support the 308 header add backwards
// compatibility using refresh header
if (statusCode === PERMANENT_REDIRECT_STATUS) {
if (statusCode === RedirectStatusCode.PermanentRedirect) {
this.setHeader('Refresh', `0;url=${destination}`)
}
return this
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ import {
APP_PATHS_MANIFEST,
NEXT_BUILTIN_DOCUMENT,
PAGES_MANIFEST,
RedirectStatusCode,
STATIC_STATUS_PAGES,
TEMPORARY_REDIRECT_STATUS,
} from '../shared/lib/constants'
import { isDynamicRoute } from '../shared/lib/router/utils'
import { checkIsOnDemandRevalidate } from './api-utils'
Expand Down Expand Up @@ -1213,7 +1213,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {

if (redirect) {
return res
.redirect(redirect, TEMPORARY_REDIRECT_STATUS)
.redirect(redirect, RedirectStatusCode.TemporaryRedirect)
.body(redirect)
.send()
}
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { isPostpone } from './router-utils/is-postpone'
import {
PHASE_PRODUCTION_SERVER,
PHASE_DEVELOPMENT_SERVER,
PERMANENT_REDIRECT_STATUS,
RedirectStatusCode,
} from '../../shared/lib/constants'
import { DevBundlerService } from './dev-bundler-service'
import { type Span, trace } from '../../trace'
Expand Down Expand Up @@ -353,7 +353,7 @@ export async function initialize(opts: {
res.statusCode = statusCode
res.setHeader('location', destination)

if (statusCode === PERMANENT_REDIRECT_STATUS) {
if (statusCode === RedirectStatusCode.PermanentRedirect) {
res.setHeader('Refresh', `0;url=${destination}`)
}
return res.end(destination)
Expand Down
4 changes: 0 additions & 4 deletions packages/next/src/shared/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ export const CLIENT_STATIC_FILES_RUNTIME_POLYFILLS_SYMBOL = Symbol(
CLIENT_STATIC_FILES_RUNTIME_POLYFILLS
)
export const EDGE_RUNTIME_WEBPACK = 'edge-runtime-webpack'
export const TEMPORARY_REDIRECT_STATUS = 307
export const PERMANENT_REDIRECT_STATUS = 308
export const STATIC_PROPS_ID = '__N_SSG'
export const SERVER_PROPS_ID = '__N_SSP'
export const PAGE_SEGMENT_KEY = '__PAGE__'
Expand Down Expand Up @@ -158,8 +156,6 @@ export const SYSTEM_ENTRYPOINTS = new Set<string>([
])

export enum RedirectStatusCode {
MovedPermanently = 301,
Found = 302,
SeeOther = 303,
TemporaryRedirect = 307,
PermanentRedirect = 308,
Expand Down

0 comments on commit 1d2ab51

Please sign in to comment.