-
Notifications
You must be signed in to change notification settings - Fork 27.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract redirect utils into a separate file (#39433)
extract redirect utils
- Loading branch information
Showing
11 changed files
with
43 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { PERMANENT_REDIRECT_STATUS } from '../shared/lib/constants' | ||
import { TEMPORARY_REDIRECT_STATUS } from '../shared/lib/constants' | ||
|
||
export const allowedStatusCodes = new Set([301, 302, 303, 307, 308]) | ||
|
||
export function getRedirectStatus(route: { | ||
statusCode?: number | ||
permanent?: boolean | ||
}): number { | ||
return ( | ||
route.statusCode || | ||
(route.permanent ? PERMANENT_REDIRECT_STATUS : TEMPORARY_REDIRECT_STATUS) | ||
) | ||
} | ||
|
||
// for redirects we restrict matching /_next and for all routes | ||
// we add an optional trailing slash at the end for easier | ||
// configuring between trailingSlash: true/false | ||
export function modifyRouteRegex(regex: string, restrictedPaths?: string[]) { | ||
if (restrictedPaths) { | ||
regex = regex.replace( | ||
/\^/, | ||
`^(?!${restrictedPaths | ||
.map((path) => path.replace(/\//g, '\\/')) | ||
.join('|')})` | ||
) | ||
} | ||
regex = regex.replace(/\$$/, '(?:\\/)?$') | ||
return regex | ||
} |
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