|
1 | 1 | import type { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from 'node:http' |
| 2 | +import { join } from 'node:path/posix' |
| 3 | +import { fileURLToPath } from 'node:url' |
2 | 4 |
|
3 | 5 | import { ComputeJsOutgoingMessage, toComputeResponse, toReqRes } from '@fastly/http-compute-js' |
4 | 6 | import type { Context } from '@netlify/functions' |
@@ -53,6 +55,15 @@ const NEXT_CACHE_TO_CACHE_STATUS: Record<string, string> = { |
53 | 55 | STALE: `hit; fwd=stale`, |
54 | 56 | } |
55 | 57 |
|
| 58 | +const FUNCTION_ROOT = fileURLToPath(new URL('.', import.meta.url)) |
| 59 | +export const FUNCTION_ROOT_DIR = join(FUNCTION_ROOT, '..', '..', '..', '..') |
| 60 | +if (process.cwd() !== FUNCTION_ROOT_DIR) { |
| 61 | + // setting CWD only needed for `ntl serve` as otherwise CWD is set to root of the project |
| 62 | + // when deployed CWD is correct |
| 63 | + // TODO(pieh): test with monorepo if this will work there as well, or cwd will need to have packagePath appended |
| 64 | + process.cwd = () => FUNCTION_ROOT_DIR |
| 65 | +} |
| 66 | + |
56 | 67 | type NextHandler = ( |
57 | 68 | req: IncomingMessage, |
58 | 69 | res: ServerResponse, |
@@ -133,18 +144,19 @@ export async function runNextHandler( |
133 | 144 | 'cache-control', |
134 | 145 | browserCacheControl || 'public, max-age=0, must-revalidate', |
135 | 146 | ) |
136 | | - response.headers.set('netlify-cdn-cache-control', cdnCacheControl) |
| 147 | + // response.headers.set('netlify-cdn-cache-control', cdnCacheControl) |
137 | 148 | } |
138 | 149 | } |
139 | 150 |
|
140 | 151 | { |
141 | 152 | // set Cache-Status header based on Next.js cache status |
142 | 153 | const nextCache = response.headers.get('x-nextjs-cache') |
143 | 154 | if (nextCache) { |
| 155 | + // eslint-disable-next-line unicorn/no-lonely-if |
144 | 156 | if (nextCache in NEXT_CACHE_TO_CACHE_STATUS) { |
145 | 157 | response.headers.set('cache-status', NEXT_CACHE_TO_CACHE_STATUS[nextCache]) |
146 | 158 | } |
147 | | - response.headers.delete('x-nextjs-cache') |
| 159 | + // response.headers.delete('x-nextjs-cache') |
148 | 160 | } |
149 | 161 | } |
150 | 162 |
|
|
0 commit comments