-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathconfig.ts
60 lines (50 loc) · 2.29 KB
/
config.ts
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
import { existsSync } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { join, resolve } from 'node:path'
import type { NextConfigComplete } from 'next/dist/server/config-shared.js'
import { PLUGIN_DIR, RUN_CONFIG } from './constants.js'
import { setInMemoryCacheMaxSizeFromNextConfig } from './regional-blob-store.cjs'
/**
* Get Next.js config from the build output
*/
export const getRunConfig = async () => {
return JSON.parse(await readFile(resolve(PLUGIN_DIR, RUN_CONFIG), 'utf-8'))
}
type NextConfigForMultipleVersions = NextConfigComplete & {
experimental: NextConfigComplete['experimental'] & {
// those are pre 14.1.0 options that were moved out of experimental in // https://github.com/vercel/next.js/pull/57953/files#diff-c49c4767e6ed8627e6e1b8f96b141ee13246153f5e9142e1da03450c8e81e96fL311
// https://github.com/vercel/next.js/blob/v14.0.4/packages/next/src/server/config-shared.ts#L182-L183
// custom path to a cache handler to use
incrementalCacheHandlerPath?: string
// https://github.com/vercel/next.js/blob/v14.0.4/packages/next/src/server/config-shared.ts#L207-L212
/**
* In-memory cache size in bytes.
*
* If `isrMemoryCacheSize: 0` disables in-memory caching.
*/
isrMemoryCacheSize?: number
}
}
/**
* Configure the custom cache handler at request time
*/
export const setRunConfig = (config: NextConfigForMultipleVersions) => {
const cacheHandler = join(PLUGIN_DIR, '.netlify/dist/run/handlers/cache.cjs')
if (!existsSync(cacheHandler)) {
throw new Error(`Cache handler not found at ${cacheHandler}`)
}
// set the path to the cache handler
config.experimental = {
...config.experimental,
// Before Next.js 14.1.0 path to the cache handler was in experimental section, see NextConfigForMultipleVersions type
incrementalCacheHandlerPath: cacheHandler,
}
// Next.js 14.1.0 moved the cache handler from experimental to stable, see NextConfigForMultipleVersions type
config.cacheHandler = cacheHandler
// honor the in-memory cache size from next.config (either one set by user or Next.js default)
setInMemoryCacheMaxSizeFromNextConfig(
config.cacheMaxMemorySize ?? config.experimental?.isrMemoryCacheSize,
)
// set config
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(config)
}