Skip to content

Commit 7f6e8db

Browse files
Support incrementalCacheHandlerPath for standalone output (#48694)
<!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation or adding/fixing Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change --> ### What? This is a follow-up to #46290 by @ijjk Currently, nextjs build resolves provided incrementalCacheHandlerPath over destDir (.next by default). And for standalone builds the custom cache can't be resolved and leads to runtime error. ### Why? This fix basically introduces support of incrementalCacheHandlerPath for the standalone build. ### How? incrementalCacheHandlerPath is now always resolved relatively to distDir, which should work for both default and standalone builds. Also, for convenience (and better testability) incrementalCacheHandlerPath can now be provided as a relative path to the project directory.
1 parent acd2280 commit 7f6e8db

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

packages/next/src/build/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,8 @@ export default async function build(
826826

827827
const manifestPath = path.join(distDir, SERVER_DIRECTORY, PAGES_MANIFEST)
828828

829+
const { incrementalCacheHandlerPath } = config.experimental
830+
829831
const requiredServerFiles = nextBuildSpan
830832
.traceChild('generate-required-server-files')
831833
.traceFn(() => ({
@@ -841,12 +843,8 @@ export default async function build(
841843
experimental: {
842844
...config.experimental,
843845
trustHostHeader: ciEnvironment.hasNextSupport,
844-
incrementalCacheHandlerPath: config.experimental
845-
.incrementalCacheHandlerPath
846-
? path.relative(
847-
distDir,
848-
config.experimental.incrementalCacheHandlerPath
849-
)
846+
incrementalCacheHandlerPath: incrementalCacheHandlerPath
847+
? path.relative(distDir, incrementalCacheHandlerPath)
850848
: undefined,
851849
},
852850
},
@@ -1918,9 +1916,13 @@ export default async function build(
19181916

19191917
// ensure we trace any dependencies needed for custom
19201918
// incremental cache handler
1921-
if (config.experimental.incrementalCacheHandlerPath) {
1919+
if (incrementalCacheHandlerPath) {
19221920
toTrace.push(
1923-
require.resolve(config.experimental.incrementalCacheHandlerPath)
1921+
require.resolve(
1922+
path.isAbsolute(incrementalCacheHandlerPath)
1923+
? incrementalCacheHandlerPath
1924+
: path.join(dir, incrementalCacheHandlerPath)
1925+
)
19241926
)
19251927
}
19261928

packages/next/src/server/next-server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import type { MiddlewareRouteMatch } from '../shared/lib/router/utils/middleware
3030
import type { RouteMatch } from './future/route-matches/route-match'
3131

3232
import fs from 'fs'
33-
import { join, relative, resolve, sep } from 'path'
33+
import { join, relative, resolve, sep, isAbsolute } from 'path'
3434
import { IncomingMessage, ServerResponse } from 'http'
3535
import { addRequestMeta, getRequestMeta } from './request-meta'
3636
import {
@@ -385,9 +385,9 @@ export default class NextNodeServer extends BaseServer {
385385
const { incrementalCacheHandlerPath } = this.nextConfig.experimental
386386

387387
if (incrementalCacheHandlerPath) {
388-
CacheHandler = require(this.minimalMode
389-
? join(this.distDir, incrementalCacheHandlerPath)
390-
: incrementalCacheHandlerPath)
388+
CacheHandler = require(isAbsolute(incrementalCacheHandlerPath)
389+
? incrementalCacheHandlerPath
390+
: join(this.distDir, incrementalCacheHandlerPath))
391391
CacheHandler = CacheHandler.default || CacheHandler
392392
}
393393

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const cache = new Map()
2+
3+
module.exports = class CacheHandler {
4+
constructor() {
5+
console.log('initialized custom cache-handler')
6+
}
7+
8+
async get(key) {
9+
console.log('cache-handler get', key)
10+
return cache.get(key)
11+
}
12+
13+
async set(key, data) {
14+
console.log('cache-handler set', key)
15+
cache.set(key, {
16+
value: data,
17+
lastModified: Date.now(),
18+
})
19+
}
20+
}

test/production/standalone-mode/required-server-files/required-server-files.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ describe('should set-up next', () => {
3737
pages: new FileRef(join(__dirname, 'pages')),
3838
lib: new FileRef(join(__dirname, 'lib')),
3939
'middleware.js': new FileRef(join(__dirname, 'middleware.js')),
40+
'cache-handler.js': new FileRef(join(__dirname, 'cache-handler.js')),
4041
'data.txt': new FileRef(join(__dirname, 'data.txt')),
4142
'.env': new FileRef(join(__dirname, '.env')),
4243
'.env.local': new FileRef(join(__dirname, '.env.local')),
4344
'.env.production': new FileRef(join(__dirname, '.env.production')),
4445
},
4546
nextConfig: {
47+
experimental: {
48+
incrementalCacheHandlerPath: './cache-handler.js',
49+
},
4650
eslint: {
4751
ignoreDuringBuilds: true,
4852
},
@@ -297,6 +301,16 @@ describe('should set-up next', () => {
297301
).toContain('"compress":false')
298302
})
299303

304+
it('`incrementalCacheHandlerPath` should have correct path', async () => {
305+
expect(
306+
await fs.pathExists(join(next.testDir, 'standalone/cache-handler.js'))
307+
).toBe(true)
308+
309+
expect(
310+
await fs.readFileSync(join(next.testDir, 'standalone/server.js'), 'utf8')
311+
).toContain('"incrementalCacheHandlerPath":"../cache-handler.js"')
312+
})
313+
300314
it('should output middleware correctly', async () => {
301315
expect(
302316
await fs.pathExists(

0 commit comments

Comments
 (0)