File size: 22,357 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 |
import type { NextConfig } from '../server/config'
import type { Token } from 'next/dist/compiled/path-to-regexp'
import { bold, yellow } from './picocolors'
import { escapeStringRegexp } from '../shared/lib/escape-regexp'
import { tryToParsePath } from './try-to-parse-path'
import { allowedStatusCodes } from './redirect-status'
import { isFullStringUrl } from './url'
export type RouteHas =
| {
type: string
key: string
value?: string
}
| {
type: 'host'
key?: undefined
value: string
}
export type Rewrite = {
source: string
destination: string
basePath?: false
locale?: false
has?: RouteHas[]
missing?: RouteHas[]
/**
* @internal - used internally for routing
*/
internal?: boolean
}
export type Header = {
source: string
basePath?: false
locale?: false
headers: Array<{ key: string; value: string }>
has?: RouteHas[]
missing?: RouteHas[]
/**
* @internal - used internally for routing
*/
internal?: boolean
}
// internal type used for validation (not user facing)
export type Redirect = {
source: string
destination: string
basePath?: false
locale?: false
has?: RouteHas[]
missing?: RouteHas[]
/**
* @internal - used internally for routing
*/
internal?: boolean
} & (
| {
statusCode?: never
permanent: boolean
}
| {
statusCode: number
permanent?: never
}
)
export type Middleware = {
source: string
locale?: false
has?: RouteHas[]
missing?: RouteHas[]
}
const allowedHasTypes = new Set(['header', 'cookie', 'query', 'host'])
const namedGroupsRegex = /\(\?<([a-zA-Z][a-zA-Z0-9]*)>/g
export function normalizeRouteRegex(regex: string) {
// clean up un-necessary escaping from regex.source which turns / into \\/
return regex.replace(/\\\//g, '/')
}
function checkRedirect(route: Redirect): {
invalidParts: string[]
hadInvalidStatus: boolean
} {
const invalidParts: string[] = []
let hadInvalidStatus: boolean = false
if (route.statusCode && !allowedStatusCodes.has(route['statusCode'])) {
hadInvalidStatus = true
invalidParts.push(`\`statusCode\` is not undefined or valid statusCode`)
}
if (typeof route.permanent !== 'boolean' && !route['statusCode']) {
invalidParts.push(`\`permanent\` is not set to \`true\` or \`false\``)
}
return {
invalidParts,
hadInvalidStatus,
}
}
function checkHeader(route: Header): string[] {
const invalidParts: string[] = []
if (!Array.isArray(route.headers)) {
invalidParts.push('`headers` field must be an array')
} else if (route.headers.length === 0) {
invalidParts.push('`headers` field cannot be empty')
} else {
for (const header of route.headers) {
if (!header || typeof header !== 'object') {
invalidParts.push(
"`headers` items must be object with { key: '', value: '' }"
)
break
}
if (typeof header.key !== 'string') {
invalidParts.push('`key` in header item must be string')
break
}
if (typeof header.value !== 'string') {
invalidParts.push('`value` in header item must be string')
break
}
}
}
return invalidParts
}
export type RouteType = 'rewrite' | 'redirect' | 'header'
export function checkCustomRoutes(
routes: Redirect[] | Header[] | Rewrite[] | Middleware[],
type: RouteType | 'middleware'
): void {
if (!Array.isArray(routes)) {
console.error(
`Error: ${type}s must return an array, received ${typeof routes}.\n` +
`See here for more info: https://nextjs.org/docs/messages/routes-must-be-array`
)
process.exit(1)
}
let numInvalidRoutes = 0
let hadInvalidStatus = false
let hadInvalidHas = false
let hadInvalidMissing = false
const allowedKeys = new Set<string>(['source', 'locale', 'has', 'missing'])
if (type === 'rewrite') {
allowedKeys.add('basePath')
allowedKeys.add('destination')
}
if (type === 'redirect') {
allowedKeys.add('basePath')
allowedKeys.add('statusCode')
allowedKeys.add('permanent')
allowedKeys.add('destination')
}
if (type === 'header') {
allowedKeys.add('basePath')
allowedKeys.add('headers')
}
for (const route of routes) {
if (!route || typeof route !== 'object') {
console.error(
`The route ${JSON.stringify(
route
)} is not a valid object with \`source\`${
type !== 'middleware'
? ` and \`${type === 'header' ? 'headers' : 'destination'}\``
: ''
}`
)
numInvalidRoutes++
continue
}
if (
type === 'rewrite' &&
(route as Rewrite).basePath === false &&
!(
(route as Rewrite).destination.startsWith('http://') ||
(route as Rewrite).destination.startsWith('https://')
)
) {
console.error(
`The route ${
(route as Rewrite).source
} rewrites urls outside of the basePath. Please use a destination that starts with \`http://\` or \`https://\` https://nextjs.org/docs/messages/invalid-external-rewrite`
)
numInvalidRoutes++
continue
}
const keys = Object.keys(route)
const invalidKeys = keys.filter((key) => !allowedKeys.has(key))
const invalidParts: string[] = []
if (
'basePath' in route &&
typeof route.basePath !== 'undefined' &&
route.basePath !== false
) {
invalidParts.push('`basePath` must be undefined or false')
}
if (typeof route.locale !== 'undefined' && route.locale !== false) {
invalidParts.push('`locale` must be undefined or false')
}
const checkInvalidHasMissing = (
items: any,
fieldName: 'has' | 'missing'
) => {
let hadInvalidItem = false
if (typeof items !== 'undefined' && !Array.isArray(items)) {
invalidParts.push(
`\`${fieldName}\` must be undefined or valid has object`
)
hadInvalidItem = true
} else if (items) {
const invalidHasItems = []
for (const hasItem of items) {
let invalidHasParts = []
if (!allowedHasTypes.has(hasItem.type)) {
invalidHasParts.push(`invalid type "${hasItem.type}"`)
}
if (typeof hasItem.key !== 'string' && hasItem.type !== 'host') {
invalidHasParts.push(`invalid key "${hasItem.key}"`)
}
if (
typeof hasItem.value !== 'undefined' &&
typeof hasItem.value !== 'string'
) {
invalidHasParts.push(`invalid value "${hasItem.value}"`)
}
if (typeof hasItem.value === 'undefined' && hasItem.type === 'host') {
invalidHasParts.push(`value is required for "host" type`)
}
if (invalidHasParts.length > 0) {
invalidHasItems.push(
`${invalidHasParts.join(', ')} for ${JSON.stringify(hasItem)}`
)
}
}
if (invalidHasItems.length > 0) {
hadInvalidItem = true
const itemStr = `item${invalidHasItems.length === 1 ? '' : 's'}`
console.error(
`Invalid \`${fieldName}\` ${itemStr}:\n` +
invalidHasItems.join('\n')
)
console.error()
invalidParts.push(`invalid \`${fieldName}\` ${itemStr} found`)
}
}
return hadInvalidItem
}
if (checkInvalidHasMissing(route.has, 'has')) {
hadInvalidHas = true
}
if (checkInvalidHasMissing(route.missing, 'missing')) {
hadInvalidMissing = true
}
if (!route.source) {
invalidParts.push('`source` is missing')
} else if (typeof route.source !== 'string') {
invalidParts.push('`source` is not a string')
} else if (!route.source.startsWith('/')) {
invalidParts.push('`source` does not start with /')
}
if (type === 'header') {
invalidParts.push(...checkHeader(route as Header))
} else if (type !== 'middleware') {
let _route = route as Rewrite | Redirect
if (!_route.destination) {
invalidParts.push('`destination` is missing')
} else if (typeof _route.destination !== 'string') {
invalidParts.push('`destination` is not a string')
} else if (
type === 'rewrite' &&
!_route.destination.match(/^(\/|https:\/\/|http:\/\/)/)
) {
invalidParts.push(
'`destination` does not start with `/`, `http://`, or `https://`'
)
}
}
if (type === 'redirect') {
const result = checkRedirect(route as Redirect)
hadInvalidStatus = hadInvalidStatus || result.hadInvalidStatus
invalidParts.push(...result.invalidParts)
}
let sourceTokens: Token[] | undefined
if (typeof route.source === 'string' && route.source.startsWith('/')) {
// only show parse error if we didn't already show error
// for not being a string
const { tokens, error, regexStr } = tryToParsePath(route.source)
if (error) {
invalidParts.push('`source` parse failed')
}
if (regexStr && regexStr.length > 4096) {
invalidParts.push('`source` exceeds max built length of 4096')
}
sourceTokens = tokens
}
const hasSegments = new Set<string>()
if (route.has) {
for (const hasItem of route.has) {
if (!hasItem.value && hasItem.key) {
hasSegments.add(hasItem.key)
}
if (hasItem.value) {
for (const match of hasItem.value.matchAll(namedGroupsRegex)) {
if (match[1]) {
hasSegments.add(match[1])
}
}
if (hasItem.type === 'host') {
hasSegments.add('host')
}
}
}
}
// make sure no unnamed patterns are attempted to be used in the
// destination as this can cause confusion and is not allowed
if (typeof (route as Rewrite).destination === 'string') {
if (
(route as Rewrite).destination.startsWith('/') &&
Array.isArray(sourceTokens)
) {
const unnamedInDest = new Set()
for (const token of sourceTokens) {
if (typeof token === 'object' && typeof token.name === 'number') {
const unnamedIndex = new RegExp(`:${token.name}(?!\\d)`)
if ((route as Rewrite).destination.match(unnamedIndex)) {
unnamedInDest.add(`:${token.name}`)
}
}
}
if (unnamedInDest.size > 0) {
invalidParts.push(
`\`destination\` has unnamed params ${[...unnamedInDest].join(
', '
)}`
)
} else {
const {
tokens: destTokens,
regexStr: destRegexStr,
error: destinationParseFailed,
} = tryToParsePath((route as Rewrite).destination, {
handleUrl: true,
})
if (destRegexStr && destRegexStr.length > 4096) {
invalidParts.push('`destination` exceeds max built length of 4096')
}
if (destinationParseFailed) {
invalidParts.push('`destination` parse failed')
} else {
const sourceSegments = new Set(
sourceTokens
.map((item) => typeof item === 'object' && item.name)
.filter(Boolean)
)
const invalidDestSegments = new Set()
for (const token of destTokens!) {
if (
typeof token === 'object' &&
!sourceSegments.has(token.name) &&
!hasSegments.has(token.name as string)
) {
invalidDestSegments.add(token.name)
}
}
if (invalidDestSegments.size) {
invalidParts.push(
`\`destination\` has segments not in \`source\` or \`has\` (${[
...invalidDestSegments,
].join(', ')})`
)
}
}
}
}
}
const hasInvalidKeys = invalidKeys.length > 0
const hasInvalidParts = invalidParts.length > 0
if (hasInvalidKeys || hasInvalidParts) {
console.error(
`${invalidParts.join(', ')}${
invalidKeys.length
? (hasInvalidParts ? ',' : '') +
` invalid field${invalidKeys.length === 1 ? '' : 's'}: ` +
invalidKeys.join(',')
: ''
} for route ${JSON.stringify(route)}`
)
console.error()
numInvalidRoutes++
}
}
if (numInvalidRoutes > 0) {
if (hadInvalidStatus) {
console.error(
`\nValid redirect statusCode values are ${[...allowedStatusCodes].join(
', '
)}`
)
}
if (hadInvalidHas) {
console.error(
`\nValid \`has\` object shape is ${JSON.stringify(
{
type: [...allowedHasTypes].join(', '),
key: 'the key to check for',
value: 'undefined or a value string to match against',
},
null,
2
)}`
)
}
if (hadInvalidMissing) {
console.error(
`\nValid \`missing\` object shape is ${JSON.stringify(
{
type: [...allowedHasTypes].join(', '),
key: 'the key to check for',
value: 'undefined or a value string to match against',
},
null,
2
)}`
)
}
console.error()
console.error(
`Error: Invalid ${type}${numInvalidRoutes === 1 ? '' : 's'} found`
)
process.exit(1)
}
}
export interface CustomRoutes {
headers: Header[]
rewrites: {
fallback: Rewrite[]
afterFiles: Rewrite[]
beforeFiles: Rewrite[]
}
redirects: Redirect[]
}
function processRoutes<T>(
routes: T,
config: NextConfig,
type: 'redirect' | 'rewrite' | 'header'
): T {
const _routes = routes as any as Array<{
source: string
locale?: false
basePath?: false
destination?: string
}>
const newRoutes: typeof _routes = []
const defaultLocales: Array<{
locale: string
base: string
}> = []
if (config.i18n && type === 'redirect') {
for (const item of config.i18n?.domains || []) {
defaultLocales.push({
locale: item.defaultLocale,
base: `http${item.http ? '' : 's'}://${item.domain}`,
})
}
defaultLocales.push({
locale: config.i18n.defaultLocale,
base: '',
})
}
for (const r of _routes) {
const srcBasePath =
config.basePath && r.basePath !== false ? config.basePath : ''
const isExternal = !r.destination?.startsWith('/')
const destBasePath = srcBasePath && !isExternal ? srcBasePath : ''
if (config.i18n && r.locale !== false) {
if (!isExternal) {
defaultLocales.forEach((item) => {
let destination
if (r.destination) {
destination = item.base
? `${item.base}${destBasePath}${r.destination}`
: `${destBasePath}${r.destination}`
}
newRoutes.push({
...r,
destination,
source: `${srcBasePath}/${item.locale}${
r.source === '/' && !config.trailingSlash ? '' : r.source
}`,
})
})
}
r.source = `/:nextInternalLocale(${config.i18n.locales
.map((locale: string) => escapeStringRegexp(locale))
.join('|')})${
r.source === '/' && !config.trailingSlash ? '' : r.source
}`
if (r.destination && r.destination?.startsWith('/')) {
r.destination = `/:nextInternalLocale${
r.destination === '/' && !config.trailingSlash ? '' : r.destination
}`
}
}
r.source = `${srcBasePath}${
r.source === '/' && srcBasePath ? '' : r.source
}`
if (r.destination) {
r.destination = `${destBasePath}${
r.destination === '/' && destBasePath ? '' : r.destination
}`
}
newRoutes.push(r)
}
return newRoutes as any as T
}
async function loadRedirects(config: NextConfig) {
if (typeof config.redirects !== 'function') {
return []
}
let redirects = await config.redirects()
// check before we process the routes and after to ensure
// they are still valid
checkCustomRoutes(redirects, 'redirect')
// save original redirects before transforms
if (Array.isArray(redirects)) {
config._originalRedirects = redirects.map((r) => ({ ...r }))
}
redirects = processRoutes(redirects, config, 'redirect')
checkCustomRoutes(redirects, 'redirect')
return redirects
}
async function loadRewrites(config: NextConfig) {
// If assetPrefix is set, add a rewrite for `/${assetPrefix}/_next/*`
// requests so that they are handled in any of dev, start, or deploy
// automatically without the user having to configure this.
// If the assetPrefix is an absolute URL, we still consider the path for automatic rewrite.
// but hostname routing must be handled by the user
let maybeAssetPrefixRewrite: Rewrite[] = []
if (config.assetPrefix) {
let prefix = config.assetPrefix
if (
isFullStringUrl(config.assetPrefix) &&
URL.canParse(config.assetPrefix)
) {
prefix = new URL(config.assetPrefix).pathname
}
if (prefix && prefix !== '/') {
const assetPrefix = prefix.startsWith('/') ? prefix : `/${prefix}`
const basePath = config.basePath || ''
// If these are the same, then this would result in an infinite rewrite.
if (assetPrefix !== basePath) {
maybeAssetPrefixRewrite.push({
source: `${assetPrefix}/_next/:path+`,
destination: `${basePath}/_next/:path+`,
})
}
}
}
if (typeof config.rewrites !== 'function') {
return {
beforeFiles: [...maybeAssetPrefixRewrite],
afterFiles: [],
fallback: [],
}
}
const _rewrites = await config.rewrites()
let beforeFiles: Rewrite[] = []
let afterFiles: Rewrite[] = []
let fallback: Rewrite[] = []
if (
!Array.isArray(_rewrites) &&
typeof _rewrites === 'object' &&
Object.keys(_rewrites).every(
(key) =>
key === 'beforeFiles' || key === 'afterFiles' || key === 'fallback'
)
) {
beforeFiles = _rewrites.beforeFiles || []
afterFiles = _rewrites.afterFiles || []
fallback = _rewrites.fallback || []
} else {
afterFiles = _rewrites as any
}
// check before we process the routes and after to ensure
// they are still valid
checkCustomRoutes(beforeFiles, 'rewrite')
checkCustomRoutes(afterFiles, 'rewrite')
checkCustomRoutes(fallback, 'rewrite')
// save original rewrites before transforms
config._originalRewrites = {
beforeFiles: beforeFiles.map((r) => ({ ...r })),
afterFiles: afterFiles.map((r) => ({ ...r })),
fallback: fallback.map((r) => ({ ...r })),
}
beforeFiles = [
...maybeAssetPrefixRewrite,
...processRoutes(beforeFiles, config, 'rewrite'),
]
afterFiles = processRoutes(afterFiles, config, 'rewrite')
fallback = processRoutes(fallback, config, 'rewrite')
checkCustomRoutes(beforeFiles, 'rewrite')
checkCustomRoutes(afterFiles, 'rewrite')
checkCustomRoutes(fallback, 'rewrite')
return {
beforeFiles,
afterFiles,
fallback,
}
}
async function loadHeaders(config: NextConfig) {
if (typeof config.headers !== 'function') {
return []
}
let headers = await config.headers()
// check before we process the routes and after to ensure
// they are still valid
checkCustomRoutes(headers, 'header')
headers = processRoutes(headers, config, 'header')
checkCustomRoutes(headers, 'header')
return headers
}
export default async function loadCustomRoutes(
config: NextConfig
): Promise<CustomRoutes> {
const [headers, rewrites, redirects] = await Promise.all([
loadHeaders(config),
loadRewrites(config),
loadRedirects(config),
])
const totalRewrites =
rewrites.beforeFiles.length +
rewrites.afterFiles.length +
rewrites.fallback.length
const totalRoutes = headers.length + redirects.length + totalRewrites
if (totalRoutes > 1000) {
console.warn(
bold(yellow(`Warning: `)) +
`total number of custom routes exceeds 1000, this can reduce performance. Route counts:\n` +
`headers: ${headers.length}\n` +
`rewrites: ${totalRewrites}\n` +
`redirects: ${redirects.length}\n` +
`See more info: https://nextjs.org/docs/messages/max-custom-routes-reached`
)
}
if (config.experimental?.useSkewCookie && config.deploymentId) {
headers.unshift({
source: '/:path*',
headers: [
{
key: 'Set-Cookie',
value: `__vdpl=${config.deploymentId}; Path=/; HttpOnly`,
},
],
})
}
if (!config.skipTrailingSlashRedirect) {
if (config.trailingSlash) {
redirects.unshift(
{
source: '/:file((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/]+\\.\\w+)/',
destination: '/:file',
permanent: true,
locale: config.i18n ? false : undefined,
internal: true,
// don't run this redirect for _next/data requests
missing: [
{
type: 'header',
key: 'x-nextjs-data',
},
],
},
{
source: '/:notfile((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/\\.]+)',
destination: '/:notfile/',
permanent: true,
locale: config.i18n ? false : undefined,
internal: true,
}
)
if (config.basePath) {
redirects.unshift({
source: config.basePath,
destination: config.basePath + '/',
permanent: true,
basePath: false,
locale: config.i18n ? false : undefined,
internal: true,
})
}
} else {
redirects.unshift({
source: '/:path+/',
destination: '/:path+',
permanent: true,
locale: config.i18n ? false : undefined,
internal: true,
})
if (config.basePath) {
redirects.unshift({
source: config.basePath + '/',
destination: config.basePath,
permanent: true,
basePath: false,
locale: config.i18n ? false : undefined,
internal: true,
})
}
}
}
return {
headers,
rewrites,
redirects,
}
}
|