Skip to content

Commit 268c79c

Browse files
committed
fix: ntl serve compat
1 parent 4d4cad6 commit 268c79c

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/adapter/run/pages-and-app-handler.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from 'node:http'
2+
import { join } from 'node:path/posix'
3+
import { fileURLToPath } from 'node:url'
24

35
import { ComputeJsOutgoingMessage, toComputeResponse, toReqRes } from '@fastly/http-compute-js'
46
import type { Context } from '@netlify/functions'
@@ -53,6 +55,15 @@ const NEXT_CACHE_TO_CACHE_STATUS: Record<string, string> = {
5355
STALE: `hit; fwd=stale`,
5456
}
5557

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+
5667
type NextHandler = (
5768
req: IncomingMessage,
5869
res: ServerResponse,
@@ -133,18 +144,19 @@ export async function runNextHandler(
133144
'cache-control',
134145
browserCacheControl || 'public, max-age=0, must-revalidate',
135146
)
136-
response.headers.set('netlify-cdn-cache-control', cdnCacheControl)
147+
// response.headers.set('netlify-cdn-cache-control', cdnCacheControl)
137148
}
138149
}
139150

140151
{
141152
// set Cache-Status header based on Next.js cache status
142153
const nextCache = response.headers.get('x-nextjs-cache')
143154
if (nextCache) {
155+
// eslint-disable-next-line unicorn/no-lonely-if
144156
if (nextCache in NEXT_CACHE_TO_CACHE_STATUS) {
145157
response.headers.set('cache-status', NEXT_CACHE_TO_CACHE_STATUS[nextCache])
146158
}
147-
response.headers.delete('x-nextjs-cache')
159+
// response.headers.delete('x-nextjs-cache')
148160
}
149161
}
150162

0 commit comments

Comments
 (0)