File size: 3,801 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 |
import { z } from 'next/dist/compiled/zod'
import { formatZodError } from '../../../shared/lib/zod'
/**
* The schema for configuration for a page.
*/
const AppSegmentConfigSchema = z.object({
/**
* The number of seconds to revalidate the page or false to disable revalidation.
*/
revalidate: z
.union([z.number().int().nonnegative(), z.literal(false)])
.optional(),
/**
* Whether the page supports dynamic parameters.
*/
dynamicParams: z.boolean().optional(),
/**
* The dynamic behavior of the page.
*/
dynamic: z
.enum(['auto', 'error', 'force-static', 'force-dynamic'])
.optional(),
/**
* The caching behavior of the page.
*/
fetchCache: z
.enum([
'auto',
'default-cache',
'only-cache',
'force-cache',
'force-no-store',
'default-no-store',
'only-no-store',
])
.optional(),
/**
* The preferred region for the page.
*/
preferredRegion: z.union([z.string(), z.array(z.string())]).optional(),
/**
* Whether the page supports partial prerendering. When true, the page will be
* served using partial prerendering. This setting will only take affect if
* it's enabled via the `experimental.ppr = "incremental"` option.
*/
experimental_ppr: z.boolean().optional(),
/**
* The runtime to use for the page.
*/
runtime: z.enum(['edge', 'nodejs']).optional(),
/**
* The maximum duration for the page in seconds.
*/
maxDuration: z.number().int().nonnegative().optional(),
})
/**
* Parse the app segment config.
* @param data - The data to parse.
* @param route - The route of the app.
* @returns The parsed app segment config.
*/
export function parseAppSegmentConfig(
data: unknown,
route: string
): AppSegmentConfig {
const parsed = AppSegmentConfigSchema.safeParse(data, {
errorMap: (issue, ctx) => {
if (issue.path.length === 1 && issue.path[0] === 'revalidate') {
return {
message: `Invalid revalidate value ${JSON.stringify(
ctx.data
)} on "${route}", must be a non-negative number or false`,
}
}
return { message: ctx.defaultError }
},
})
if (!parsed.success) {
throw formatZodError(
`Invalid segment configuration options detected for "${route}". Read more at https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config`,
parsed.error
)
}
return parsed.data
}
/**
* The configuration for a page.
*/
export type AppSegmentConfig = {
/**
* The revalidation period for the page in seconds, or false to disable ISR.
*/
revalidate?: number | false
/**
* Whether the page supports dynamic parameters.
*/
dynamicParams?: boolean
/**
* The dynamic behavior of the page.
*/
dynamic?: 'auto' | 'error' | 'force-static' | 'force-dynamic'
/**
* The caching behavior of the page.
*/
fetchCache?:
| 'auto'
| 'default-cache'
| 'default-no-store'
| 'force-cache'
| 'force-no-store'
| 'only-cache'
| 'only-no-store'
/**
* The preferred region for the page.
*/
preferredRegion?: string | string[]
/**
* Whether the page supports partial prerendering. When true, the page will be
* served using partial prerendering. This setting will only take affect if
* it's enabled via the `experimental.ppr = "incremental"` option.
*/
experimental_ppr?: boolean
/**
* The runtime to use for the page.
*/
runtime?: 'edge' | 'nodejs'
/**
* The maximum duration for the page in seconds.
*/
maxDuration?: number
}
/**
* The keys of the configuration for a page.
*
* @internal - required to exclude zod types from the build
*/
export const AppSegmentConfigSchemaKeys = AppSegmentConfigSchema.keyof().options
|