75c721c583
This adds support for a `has` field to `rewrites`, `redirects`, and `headers` to allow matching against `header`, `cookie`, and `query` values. Documentation and additional tests for the feature is also added in this PR. Closes: https://github.com/vercel/next.js/issues/22345
77 lines
2 KiB
TypeScript
77 lines
2 KiB
TypeScript
const REGEXP_DIRECTORY_DUNDER = /[\\/]__[^\\/]+(?<![\\/]__(?:tests|mocks))__[\\/]/i
|
|
const REGEXP_DIRECTORY_TESTS = /[\\/]__(tests|mocks)__[\\/]/i
|
|
const REGEXP_FILE_TEST = /\.(?:spec|test)\.[^.]+$/i
|
|
|
|
const EVENT_BUILD_DURATION = 'NEXT_BUILD_COMPLETED'
|
|
type EventBuildCompleted = {
|
|
durationInSeconds: number
|
|
totalPageCount: number
|
|
hasDunderPages: boolean
|
|
hasTestPages: boolean
|
|
}
|
|
|
|
export function eventBuildCompleted(
|
|
pagePaths: string[],
|
|
event: Omit<
|
|
EventBuildCompleted,
|
|
'totalPageCount' | 'hasDunderPages' | 'hasTestPages'
|
|
>
|
|
): { eventName: string; payload: EventBuildCompleted } {
|
|
return {
|
|
eventName: EVENT_BUILD_DURATION,
|
|
payload: {
|
|
...event,
|
|
totalPageCount: pagePaths.length,
|
|
hasDunderPages: pagePaths.some((path) =>
|
|
REGEXP_DIRECTORY_DUNDER.test(path)
|
|
),
|
|
hasTestPages: pagePaths.some(
|
|
(path) =>
|
|
REGEXP_DIRECTORY_TESTS.test(path) || REGEXP_FILE_TEST.test(path)
|
|
),
|
|
},
|
|
}
|
|
}
|
|
|
|
const EVENT_BUILD_OPTIMIZE = 'NEXT_BUILD_OPTIMIZED'
|
|
type EventBuildOptimized = {
|
|
durationInSeconds: number
|
|
totalPageCount: number
|
|
staticPageCount: number
|
|
staticPropsPageCount: number
|
|
serverPropsPageCount: number
|
|
ssrPageCount: number
|
|
hasDunderPages: boolean
|
|
hasTestPages: boolean
|
|
hasStatic404: boolean
|
|
hasReportWebVitals: boolean
|
|
headersCount: number
|
|
rewritesCount: number
|
|
redirectsCount: number
|
|
headersWithHasCount: number
|
|
rewritesWithHasCount: number
|
|
redirectsWithHasCount: number
|
|
}
|
|
|
|
export function eventBuildOptimize(
|
|
pagePaths: string[],
|
|
event: Omit<
|
|
EventBuildOptimized,
|
|
'totalPageCount' | 'hasDunderPages' | 'hasTestPages'
|
|
>
|
|
): { eventName: string; payload: EventBuildOptimized } {
|
|
return {
|
|
eventName: EVENT_BUILD_OPTIMIZE,
|
|
payload: {
|
|
...event,
|
|
totalPageCount: pagePaths.length,
|
|
hasDunderPages: pagePaths.some((path) =>
|
|
REGEXP_DIRECTORY_DUNDER.test(path)
|
|
),
|
|
hasTestPages: pagePaths.some(
|
|
(path) =>
|
|
REGEXP_DIRECTORY_TESTS.test(path) || REGEXP_FILE_TEST.test(path)
|
|
),
|
|
},
|
|
}
|
|
}
|