Skip to content

Commit b28b201

Browse files
committed
feat: moving incremental cache to functions
1 parent 18f2c36 commit b28b201

File tree

5 files changed

+33
-29
lines changed

5 files changed

+33
-29
lines changed

packages/runtime/src/helpers/config.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ const defaultFailBuild = (message: string, { error }): never => {
3737
export const getNextConfig = async function getNextConfig({
3838
publish,
3939
failBuild = defaultFailBuild,
40+
INTERNAL_FUNCTIONS_SRC,
4041
}): Promise<NextConfig> {
4142
try {
4243
const requiredServerFiles: RequiredServerFiles = await readJSON(join(publish, 'required-server-files.json'))
43-
const { config, appDir, ignore } = requiredServerFiles;
44+
const { config, appDir, ignore } = requiredServerFiles
4445

4546
if (!config) {
4647
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -56,15 +57,17 @@ export const getNextConfig = async function getNextConfig({
5657

5758
// For more info, see https://nextjs.org/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath
5859
// ./cache-handler.js will be copied to the root or the .next build folder
59-
60-
await writeJSON(join(publish, 'required-server-files.json'), { ...requiredServerFiles, config: {
61-
...config,
62-
experimental: {
63-
...config.experimental,
64-
incrementalCacheHandlerPath: join(publish, 'incremental-cache.js')
65-
}
66-
}
67-
})
60+
61+
await writeJSON(join(publish, 'required-server-files.json'), {
62+
...requiredServerFiles,
63+
config: {
64+
...config,
65+
experimental: {
66+
...config.experimental,
67+
incrementalCacheHandlerPath: join(INTERNAL_FUNCTIONS_SRC, '__incremental-cache', 'incremental-cache.js'),
68+
},
69+
},
70+
})
6871
const routesManifest: RoutesManifest = await readJSON(join(publish, ROUTES_MANIFEST_FILE))
6972

7073
// If you need access to other manifest files, you can add them here as well

packages/runtime/src/helpers/functions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
LAMBDA_WARNING_SIZE,
2323
} from '../constants'
2424
import { getApiHandler } from '../templates/getApiHandler'
25+
import { generateCacheHandler } from '../templates/getCacheHandler'
2526
import { getHandler } from '../templates/getHandler'
2627
import { getResolverForPages, getResolverForSourceFiles } from '../templates/getPageResolver'
2728

@@ -168,6 +169,7 @@ export const generateFunctions = async (
168169

169170
await writeHandler(HANDLER_FUNCTION_NAME, HANDLER_FUNCTION_TITLE, false)
170171
await writeHandler(ODB_FUNCTION_NAME, ODB_FUNCTION_TITLE, true)
172+
await generateCacheHandler(functionsDir)
171173
}
172174

173175
/**

packages/runtime/src/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import {
4141
warnForProblematicUserRewrites,
4242
warnForRootRedirects,
4343
} from './helpers/verification'
44-
import { generateCacheHandler } from './templates/getCacheHandler'
4544

4645
const plugin: NetlifyPlugin = {
4746
async onPreBuild({
@@ -102,6 +101,7 @@ const plugin: NetlifyPlugin = {
102101
} = await getNextConfig({
103102
publish,
104103
failBuild,
104+
INTERNAL_FUNCTIONS_SRC: constants.INTERNAL_FUNCTIONS_SRC,
105105
})
106106
await cleanupEdgeFunctions(constants)
107107

@@ -165,8 +165,6 @@ const plugin: NetlifyPlugin = {
165165
}
166166
}
167167

168-
await generateCacheHandler(publish)
169-
170168
const buildId = readFileSync(join(publish, 'BUILD_ID'), 'utf8').trim()
171169

172170
const apiLambdas: APILambda[] = splitApiRoutes(featureFlags, publish)
@@ -230,7 +228,7 @@ const plugin: NetlifyPlugin = {
230228
functions,
231229
build: { failBuild },
232230
},
233-
constants: { FUNCTIONS_DIST },
231+
constants: { FUNCTIONS_DIST, INTERNAL_FUNCTIONS_SRC },
234232
}) {
235233
await saveCache({ cache, publish })
236234

@@ -248,7 +246,7 @@ const plugin: NetlifyPlugin = {
248246

249247
await checkForOldFunctions({ functions })
250248
await checkZipSize(join(FUNCTIONS_DIST, `${ODB_FUNCTION_NAME}.zip`))
251-
const nextConfig = await getNextConfig({ publish, failBuild })
249+
const nextConfig = await getNextConfig({ publish, failBuild, INTERNAL_FUNCTIONS_SRC })
252250

253251
const { basePath, appDir, experimental } = nextConfig
254252

packages/runtime/src/templates/getCacheHandler.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { copyFile, writeFile } from 'fs-extra'
2-
import { outdent as javascript } from 'outdent'
31
import { join } from 'path'
42

5-
export const generateCacheHandler = async (publish: string): Promise<void> => {
3+
import { copyFile, ensureDir, writeFile } from 'fs-extra'
4+
import { outdent as javascript } from 'outdent'
65

7-
const blobCache = getCacheHandler()
6+
export const generateCacheHandler = async (functionsDir: string): Promise<void> => {
7+
const blobCache = getCacheHandler()
8+
await ensureDir(join(functionsDir, '__incremental-cache'))
89

9-
await writeFile(join(publish, 'incremental-cache.js'), blobCache)
10-
await copyFile(join(__dirname, '..', '..', 'lib', 'helpers', 'blobStorage.js'), join('.netlify', 'blobStorage.js'))
10+
await writeFile(join(functionsDir, '__incremental-cache', 'incremental-cache.js'), blobCache)
11+
await copyFile(join(__dirname, '..', '..', 'lib', 'helpers', 'blobStorage.js'), join('.netlify', 'blobStorage.js'))
1112
}
1213

13-
1414
const getCacheHandler = (): string =>
15-
// This is a string, but if you have the right editor plugin it should format as js (e.g. bierner.comment-tagged-templates in VS Code)
16-
javascript/* javascript */ `
15+
// This is a string, but if you have the right editor plugin it should format as js (e.g. bierner.comment-tagged-templates in VS Code)
16+
javascript/* javascript */ `
1717
1818
const { getBlobStorage } = require('./blobStorage')
1919
const { auth } = require('./handlerUtils')

packages/runtime/src/templates/getHandler.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,17 @@ const makeHandler = ({ conf, app, pageRoot, NextServer, staticManifest = [], mod
8686
} = context
8787

8888
globalThis.getBlobContext = {}
89-
const rawData = Buffer.from(customContext.blobs, "base64");
90-
const data = JSON.parse(rawData.toString("ascii"));
91-
89+
// eslint-disable-next-line n/prefer-global/buffer
90+
const rawData = Buffer.from(customContext.blobs, 'base64')
91+
const data = JSON.parse(rawData.toString('ascii'))
92+
9293
globalThis.getBlobContext = {
9394
authentication: {
9495
contextURL: data.url,
9596
token: data.token,
9697
},
97-
context: `deploy:${event.headers["x-nf-deploy-id"]}`,
98-
siteID: event.headers["x-nf-site-id"],
98+
context: `deploy:${event.headers['x-nf-deploy-id']}`,
99+
siteID: event.headers['x-nf-site-id'],
99100
}
100101

101102
if (bridge) {

0 commit comments

Comments
 (0)