|
1 | | -import type { IncomingMessage, ServerResponse } from 'http' |
2 | | - |
3 | 1 | import type { NextConfigComplete } from 'next/dist/server/config-shared.js' |
4 | 2 |
|
5 | | -type HeaderValue = number | string | string[] |
6 | | - |
7 | | -interface NetlifyVaryDirectives { |
| 3 | +interface NetlifyVaryValues { |
8 | 4 | headers: string[] |
9 | 5 | languages: string[] |
10 | 6 | cookies: string[] |
11 | 7 | } |
12 | 8 |
|
13 | | -const generateNetlifyVaryDirectives = ({ |
14 | | - headers, |
15 | | - languages, |
16 | | - cookies, |
17 | | -}: NetlifyVaryDirectives): string[] => { |
18 | | - const directives = [] |
| 9 | +const generateNetlifyVaryValues = ({ headers, languages, cookies }: NetlifyVaryValues): string => { |
| 10 | + const values = [] |
19 | 11 | if (headers.length !== 0) { |
20 | | - directives.push(`header=${headers.join(`|`)}`) |
| 12 | + values.push(`header=${headers.join(`|`)}`) |
21 | 13 | } |
22 | 14 | if (languages.length !== 0) { |
23 | | - directives.push(`language=${languages.join(`|`)}`) |
| 15 | + values.push(`language=${languages.join(`|`)}`) |
24 | 16 | } |
25 | 17 | if (cookies.length !== 0) { |
26 | | - directives.push(`cookie=${cookies.join(`|`)}`) |
| 18 | + values.push(`cookie=${cookies.join(`|`)}`) |
27 | 19 | } |
28 | | - return directives |
| 20 | + return values.join(',') |
29 | 21 | } |
30 | 22 |
|
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()) |
| 23 | +const getHeaderValueArray = (header: string): string[] => { |
| 24 | + return header.split(',').map((value) => value.trim()) |
| 25 | +} |
| 26 | + |
| 27 | +const removeHeaderValues = (header: string, values: string[]): string => { |
| 28 | + const headerValues = getHeaderValueArray(header) |
| 29 | + const filteredValues = headerValues.filter( |
| 30 | + (value) => !values.some((val) => value.startsWith(val)), |
| 31 | + ) |
| 32 | + return filteredValues.join(', ') |
37 | 33 | } |
| 34 | + |
38 | 35 | /** |
39 | 36 | * Ensure the Netlify CDN varies on things that Next.js varies on, |
40 | 37 | * e.g. i18n, preview mode, etc. |
41 | 38 | */ |
42 | 39 | export const setVaryHeaders = ( |
43 | | - res: ServerResponse, |
44 | | - req: IncomingMessage, |
| 40 | + headers: Headers, |
| 41 | + request: Request, |
45 | 42 | { basePath, i18n }: NextConfigComplete, |
46 | 43 | ) => { |
47 | | - const netlifyVaryDirectives: NetlifyVaryDirectives = { |
| 44 | + const netlifyVaryValues: NetlifyVaryValues = { |
48 | 45 | headers: [], |
49 | 46 | languages: [], |
50 | 47 | cookies: ['__prerender_bypass', '__next_preview_data'], |
51 | 48 | } |
52 | 49 |
|
53 | | - const vary = res.getHeader('vary') |
54 | | - if (vary !== undefined) { |
55 | | - netlifyVaryDirectives.headers.push(...getDirectives(vary)) |
| 50 | + const vary = headers.get('vary') |
| 51 | + if (vary !== null) { |
| 52 | + netlifyVaryValues.headers.push(...getHeaderValueArray(vary)) |
56 | 53 | } |
57 | 54 |
|
58 | | - const path = new URL(req.url ?? '/', `http://${req.headers.host}`).pathname |
| 55 | + const path = new URL(request.url).pathname |
59 | 56 | const locales = i18n && i18n.localeDetection !== false ? i18n.locales : [] |
60 | 57 |
|
61 | 58 | if (locales.length > 1) { |
62 | 59 | const logicalPath = basePath && path.startsWith(basePath) ? path.slice(basePath.length) : path |
63 | 60 | if (logicalPath === `/`) { |
64 | | - netlifyVaryDirectives.languages.push(...locales) |
65 | | - netlifyVaryDirectives.cookies.push(`NEXT_LOCALE`) |
| 61 | + netlifyVaryValues.languages.push(...locales) |
| 62 | + netlifyVaryValues.cookies.push(`NEXT_LOCALE`) |
66 | 63 | } |
67 | 64 | } |
68 | 65 |
|
69 | | - res.setHeader(`netlify-vary`, generateNetlifyVaryDirectives(netlifyVaryDirectives)) |
| 66 | + headers.set(`netlify-vary`, generateNetlifyVaryValues(netlifyVaryValues)) |
70 | 67 | } |
71 | 68 |
|
72 | 69 | /** |
73 | 70 | * Ensure stale-while-revalidate and s-maxage don't leak to the client, but |
74 | 71 | * assume the user knows what they are doing if CDN cache controls are set |
75 | 72 | */ |
76 | | -export const setCacheControlHeaders = (res: ServerResponse) => { |
77 | | - const cacheControl = res.getHeader('cache-control') |
| 73 | +export const setCacheControlHeaders = (headers: Headers) => { |
| 74 | + const cacheControl = headers.get('cache-control') |
78 | 75 | if ( |
79 | | - cacheControl !== undefined && |
80 | | - !res.hasHeader('cdn-cache-control') && |
81 | | - !res.hasHeader('netlify-cdn-cache-control') |
| 76 | + cacheControl !== null && |
| 77 | + !headers.has('cdn-cache-control') && |
| 78 | + !headers.has('netlify-cdn-cache-control') |
82 | 79 | ) { |
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 | | - ) |
| 80 | + const clientCacheControl = removeHeaderValues(cacheControl, [ |
| 81 | + 's-maxage', |
| 82 | + 'stale-while-revalidate', |
| 83 | + ]) |
| 84 | + headers.set('cache-control', clientCacheControl || 'public, max-age=0, must-revalidate') |
| 85 | + headers.set('netlify-cdn-cache-control', cacheControl) |
93 | 86 | } |
94 | 87 | } |
| 88 | + |
| 89 | +export const setCacheTagsHeaders = (headers: Headers) => { |
| 90 | + // TODO: implement |
| 91 | +} |
0 commit comments