|
1 |
| -import type { HandlerEvent } from '@netlify/functions' |
| 1 | +import type { IncomingMessage, ServerResponse } from 'http' |
| 2 | + |
2 | 3 | import type { NextConfigComplete } from 'next/dist/server/config-shared.js'
|
3 | 4 |
|
4 |
| -export interface NetlifyVaryHeaderBuilder { |
| 5 | +type HeaderValue = number | string | string[] |
| 6 | + |
| 7 | +interface NetlifyVaryDirectives { |
5 | 8 | headers: string[]
|
6 | 9 | languages: string[]
|
7 | 10 | cookies: string[]
|
8 | 11 | }
|
9 | 12 |
|
10 |
| -const generateNetlifyVaryHeaderValue = ({ headers, languages, cookies }: NetlifyVaryHeaderBuilder): string => { |
11 |
| - let NetlifyVaryHeader = `` |
12 |
| - if (headers && headers.length !== 0) { |
13 |
| - NetlifyVaryHeader += `header=${headers.join(`|`)}` |
| 13 | +const generateNetlifyVaryDirectives = ({ |
| 14 | + headers, |
| 15 | + languages, |
| 16 | + cookies, |
| 17 | +}: NetlifyVaryDirectives): string[] => { |
| 18 | + const directives = [] |
| 19 | + if (headers.length !== 0) { |
| 20 | + directives.push(`header=${headers.join(`|`)}`) |
14 | 21 | }
|
15 |
| - if (languages && languages.length !== 0) { |
16 |
| - if (NetlifyVaryHeader.length !== 0) { |
17 |
| - NetlifyVaryHeader += `,` |
18 |
| - } |
19 |
| - NetlifyVaryHeader += `language=${languages.join(`|`)}` |
| 22 | + if (languages.length !== 0) { |
| 23 | + directives.push(`language=${languages.join(`|`)}`) |
20 | 24 | }
|
21 |
| - if (cookies && cookies.length !== 0) { |
22 |
| - if (NetlifyVaryHeader.length !== 0) { |
23 |
| - NetlifyVaryHeader += `,` |
24 |
| - } |
25 |
| - NetlifyVaryHeader += `cookie=${cookies.join(`|`)}` |
| 25 | + if (cookies.length !== 0) { |
| 26 | + directives.push(`cookie=${cookies.join(`|`)}`) |
26 | 27 | }
|
27 |
| - |
28 |
| - return NetlifyVaryHeader |
| 28 | + return directives |
29 | 29 | }
|
30 | 30 |
|
31 |
| -const getDirectives = (headerValue: string): string[] => headerValue.split(',').map((directive) => directive.trim()) |
32 |
| - |
33 |
| -const removeSMaxAgeAndStaleWhileRevalidate = (headerValue: string): string => |
34 |
| - getDirectives(headerValue) |
35 |
| - .filter((directive) => { |
36 |
| - if (directive.startsWith('s-maxage')) { |
37 |
| - return false |
38 |
| - } |
39 |
| - if (directive.startsWith('stale-while-revalidate')) { |
40 |
| - return false |
41 |
| - } |
42 |
| - return true |
43 |
| - }) |
44 |
| - .join(`,`) |
45 |
| - |
46 |
| -export const handleVary = (headers: Record<string, string>, event: HandlerEvent, nextConfig: NextConfigComplete) => { |
47 |
| - const netlifyVaryBuilder: NetlifyVaryHeaderBuilder = { |
| 31 | +/** |
| 32 | + * Parse a header value into an array of directives |
| 33 | + */ |
| 34 | +const getDirectives = (headerValue: HeaderValue): string[] => { |
| 35 | + const directives = Array.isArray(headerValue) ? headerValue : String(headerValue).split(',') |
| 36 | + return directives.map((directive) => directive.trim()) |
| 37 | +} |
| 38 | +/** |
| 39 | + * Ensure the Netlify CDN varies on things that Next.js varies on, |
| 40 | + * e.g. i18n, preview mode, etc. |
| 41 | + */ |
| 42 | +export const setVaryHeaders = ( |
| 43 | + res: ServerResponse, |
| 44 | + req: IncomingMessage, |
| 45 | + { basePath, i18n }: NextConfigComplete, |
| 46 | +) => { |
| 47 | + const netlifyVaryDirectives: NetlifyVaryDirectives = { |
48 | 48 | headers: [],
|
49 | 49 | languages: [],
|
50 | 50 | cookies: ['__prerender_bypass', '__next_preview_data'],
|
51 | 51 | }
|
52 | 52 |
|
53 |
| - if (headers.vary.length !== 0) { |
54 |
| - netlifyVaryBuilder.headers.push(...getDirectives(headers.vary)) |
| 53 | + const vary = res.getHeader('vary') |
| 54 | + if (vary !== undefined) { |
| 55 | + netlifyVaryDirectives.headers.push(...getDirectives(vary)) |
55 | 56 | }
|
56 | 57 |
|
57 |
| - const autoDetectedLocales = getAutoDetectedLocales(nextConfig) |
58 |
| - |
59 |
| - if (autoDetectedLocales.length > 1) { |
60 |
| - const logicalPath = |
61 |
| - nextConfig.basePath && event.path.startsWith(nextConfig.basePath) |
62 |
| - ? event.path.slice(nextConfig.basePath.length) |
63 |
| - : event.path |
| 58 | + const path = new URL(req.url ?? '/', `http://${req.headers.host}`).pathname |
| 59 | + const locales = i18n && i18n.localeDetection !== false ? i18n.locales : [] |
64 | 60 |
|
| 61 | + if (locales.length > 1) { |
| 62 | + const logicalPath = basePath && path.startsWith(basePath) ? path.slice(basePath.length) : path |
65 | 63 | if (logicalPath === `/`) {
|
66 |
| - netlifyVaryBuilder.languages.push(...autoDetectedLocales) |
67 |
| - netlifyVaryBuilder.cookies.push(`NEXT_LOCALE`) |
| 64 | + netlifyVaryDirectives.languages.push(...locales) |
| 65 | + netlifyVaryDirectives.cookies.push(`NEXT_LOCALE`) |
68 | 66 | }
|
69 | 67 | }
|
70 | 68 |
|
71 |
| - const NetlifyVaryHeader = generateNetlifyVaryHeaderValue(netlifyVaryBuilder) |
72 |
| - if (NetlifyVaryHeader.length !== 0) { |
73 |
| - headers[`netlify-vary`] = NetlifyVaryHeader |
74 |
| - } |
75 |
| -} |
76 |
| - |
77 |
| -export const handleCacheControl = (headers: Record<string, string>) => { |
78 |
| - if (headers['cache-control'] && !headers['cdn-cache-control'] && !headers['netlify-cdn-cache-control']) { |
79 |
| - headers['netlify-cdn-cache-control'] = headers['cache-control'] |
80 |
| - |
81 |
| - const filteredCacheControlDirectives = removeSMaxAgeAndStaleWhileRevalidate(headers['cache-control']) |
82 |
| - |
83 |
| - // use default cache-control if no directives are left |
84 |
| - headers['cache-control'] = |
85 |
| - filteredCacheControlDirectives.length === 0 |
86 |
| - ? 'public, max-age=0, must-revalidate' |
87 |
| - : filteredCacheControlDirectives |
88 |
| - } |
| 69 | + res.setHeader(`netlify-vary`, generateNetlifyVaryDirectives(netlifyVaryDirectives)) |
89 | 70 | }
|
90 | 71 |
|
91 |
| -export const getAutoDetectedLocales = (config: NextConfigComplete): Array<string> => { |
92 |
| - if (config.i18n && config.i18n.localeDetection !== false && config.i18n.locales.length > 1) { |
93 |
| - return config.i18n.locales |
| 72 | +/** |
| 73 | + * Ensure stale-while-revalidate and s-maxage don't leak to the client, but |
| 74 | + * assume the user knows what they are doing if CDN cache controls are set |
| 75 | + */ |
| 76 | +export const setCacheControlHeaders = (res: ServerResponse) => { |
| 77 | + const cacheControl = res.getHeader('cache-control') |
| 78 | + if ( |
| 79 | + cacheControl !== undefined && |
| 80 | + !res.hasHeader('cdn-cache-control') && |
| 81 | + !res.hasHeader('netlify-cdn-cache-control') |
| 82 | + ) { |
| 83 | + const directives = getDirectives(cacheControl).filter( |
| 84 | + (directive) => |
| 85 | + !directive.startsWith('s-maxage') && !directive.startsWith('stale-while-revalidate'), |
| 86 | + ) |
| 87 | + |
| 88 | + res.setHeader('netlify-cdn-cache-control', cacheControl) |
| 89 | + res.setHeader( |
| 90 | + 'cache-control', |
| 91 | + directives.length === 0 ? 'public, max-age=0, must-revalidate' : directives, |
| 92 | + ) |
94 | 93 | }
|
95 |
| - |
96 |
| - return [] |
97 | 94 | }
|
0 commit comments