Unverified Commit 7f6e8dbe authored by Ivan Kryvorotenko's avatar Ivan Kryvorotenko Committed by GitHub

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 https://github.com/vercel/next.js/pull/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.
parent acd22802
...@@ -826,6 +826,8 @@ export default async function build( ...@@ -826,6 +826,8 @@ export default async function build(
const manifestPath = path.join(distDir, SERVER_DIRECTORY, PAGES_MANIFEST) const manifestPath = path.join(distDir, SERVER_DIRECTORY, PAGES_MANIFEST)
const { incrementalCacheHandlerPath } = config.experimental
const requiredServerFiles = nextBuildSpan const requiredServerFiles = nextBuildSpan
.traceChild('generate-required-server-files') .traceChild('generate-required-server-files')
.traceFn(() => ({ .traceFn(() => ({
...@@ -841,12 +843,8 @@ export default async function build( ...@@ -841,12 +843,8 @@ export default async function build(
experimental: { experimental: {
...config.experimental, ...config.experimental,
trustHostHeader: ciEnvironment.hasNextSupport, trustHostHeader: ciEnvironment.hasNextSupport,
incrementalCacheHandlerPath: config.experimental incrementalCacheHandlerPath: incrementalCacheHandlerPath
.incrementalCacheHandlerPath ? path.relative(distDir, incrementalCacheHandlerPath)
? path.relative(
distDir,
config.experimental.incrementalCacheHandlerPath
)
: undefined, : undefined,
}, },
}, },
...@@ -1918,9 +1916,13 @@ export default async function build( ...@@ -1918,9 +1916,13 @@ export default async function build(
// ensure we trace any dependencies needed for custom // ensure we trace any dependencies needed for custom
// incremental cache handler // incremental cache handler
if (config.experimental.incrementalCacheHandlerPath) { if (incrementalCacheHandlerPath) {
toTrace.push( toTrace.push(
require.resolve(config.experimental.incrementalCacheHandlerPath) require.resolve(
path.isAbsolute(incrementalCacheHandlerPath)
? incrementalCacheHandlerPath
: path.join(dir, incrementalCacheHandlerPath)
)
) )
} }
......
...@@ -30,7 +30,7 @@ import type { MiddlewareRouteMatch } from '../shared/lib/router/utils/middleware ...@@ -30,7 +30,7 @@ import type { MiddlewareRouteMatch } from '../shared/lib/router/utils/middleware
import type { RouteMatch } from './future/route-matches/route-match' import type { RouteMatch } from './future/route-matches/route-match'
import fs from 'fs' import fs from 'fs'
import { join, relative, resolve, sep } from 'path' import { join, relative, resolve, sep, isAbsolute } from 'path'
import { IncomingMessage, ServerResponse } from 'http' import { IncomingMessage, ServerResponse } from 'http'
import { addRequestMeta, getRequestMeta } from './request-meta' import { addRequestMeta, getRequestMeta } from './request-meta'
import { import {
...@@ -385,9 +385,9 @@ export default class NextNodeServer extends BaseServer { ...@@ -385,9 +385,9 @@ export default class NextNodeServer extends BaseServer {
const { incrementalCacheHandlerPath } = this.nextConfig.experimental const { incrementalCacheHandlerPath } = this.nextConfig.experimental
if (incrementalCacheHandlerPath) { if (incrementalCacheHandlerPath) {
CacheHandler = require(this.minimalMode CacheHandler = require(isAbsolute(incrementalCacheHandlerPath)
? join(this.distDir, incrementalCacheHandlerPath) ? incrementalCacheHandlerPath
: incrementalCacheHandlerPath) : join(this.distDir, incrementalCacheHandlerPath))
CacheHandler = CacheHandler.default || CacheHandler CacheHandler = CacheHandler.default || CacheHandler
} }
......
const cache = new Map()
module.exports = class CacheHandler {
constructor() {
console.log('initialized custom cache-handler')
}
async get(key) {
console.log('cache-handler get', key)
return cache.get(key)
}
async set(key, data) {
console.log('cache-handler set', key)
cache.set(key, {
value: data,
lastModified: Date.now(),
})
}
}
...@@ -37,12 +37,16 @@ describe('should set-up next', () => { ...@@ -37,12 +37,16 @@ describe('should set-up next', () => {
pages: new FileRef(join(__dirname, 'pages')), pages: new FileRef(join(__dirname, 'pages')),
lib: new FileRef(join(__dirname, 'lib')), lib: new FileRef(join(__dirname, 'lib')),
'middleware.js': new FileRef(join(__dirname, 'middleware.js')), 'middleware.js': new FileRef(join(__dirname, 'middleware.js')),
'cache-handler.js': new FileRef(join(__dirname, 'cache-handler.js')),
'data.txt': new FileRef(join(__dirname, 'data.txt')), 'data.txt': new FileRef(join(__dirname, 'data.txt')),
'.env': new FileRef(join(__dirname, '.env')), '.env': new FileRef(join(__dirname, '.env')),
'.env.local': new FileRef(join(__dirname, '.env.local')), '.env.local': new FileRef(join(__dirname, '.env.local')),
'.env.production': new FileRef(join(__dirname, '.env.production')), '.env.production': new FileRef(join(__dirname, '.env.production')),
}, },
nextConfig: { nextConfig: {
experimental: {
incrementalCacheHandlerPath: './cache-handler.js',
},
eslint: { eslint: {
ignoreDuringBuilds: true, ignoreDuringBuilds: true,
}, },
...@@ -297,6 +301,16 @@ describe('should set-up next', () => { ...@@ -297,6 +301,16 @@ describe('should set-up next', () => {
).toContain('"compress":false') ).toContain('"compress":false')
}) })
it('`incrementalCacheHandlerPath` should have correct path', async () => {
expect(
await fs.pathExists(join(next.testDir, 'standalone/cache-handler.js'))
).toBe(true)
expect(
await fs.readFileSync(join(next.testDir, 'standalone/server.js'), 'utf8')
).toContain('"incrementalCacheHandlerPath":"../cache-handler.js"')
})
it('should output middleware correctly', async () => { it('should output middleware correctly', async () => {
expect( expect(
await fs.pathExists( await fs.pathExists(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment