Skip to content

Commit

Permalink
Support incrementalCacheHandlerPath for standalone output (#48694)
Browse files Browse the repository at this point in the history
<!-- 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.
  • Loading branch information
ikryvorotenko committed May 4, 2023
1 parent acd2280 commit 7f6e8db
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
18 changes: 10 additions & 8 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,8 @@ export default async function build(

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

const { incrementalCacheHandlerPath } = config.experimental

const requiredServerFiles = nextBuildSpan
.traceChild('generate-required-server-files')
.traceFn(() => ({
Expand All @@ -841,12 +843,8 @@ export default async function build(
experimental: {
...config.experimental,
trustHostHeader: ciEnvironment.hasNextSupport,
incrementalCacheHandlerPath: config.experimental
.incrementalCacheHandlerPath
? path.relative(
distDir,
config.experimental.incrementalCacheHandlerPath
)
incrementalCacheHandlerPath: incrementalCacheHandlerPath
? path.relative(distDir, incrementalCacheHandlerPath)
: undefined,
},
},
Expand Down Expand Up @@ -1918,9 +1916,13 @@ export default async function build(

// ensure we trace any dependencies needed for custom
// incremental cache handler
if (config.experimental.incrementalCacheHandlerPath) {
if (incrementalCacheHandlerPath) {
toTrace.push(
require.resolve(config.experimental.incrementalCacheHandlerPath)
require.resolve(
path.isAbsolute(incrementalCacheHandlerPath)
? incrementalCacheHandlerPath
: path.join(dir, incrementalCacheHandlerPath)
)
)
}

Expand Down
8 changes: 4 additions & 4 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import type { MiddlewareRouteMatch } from '../shared/lib/router/utils/middleware
import type { RouteMatch } from './future/route-matches/route-match'

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 { addRequestMeta, getRequestMeta } from './request-meta'
import {
Expand Down Expand Up @@ -385,9 +385,9 @@ export default class NextNodeServer extends BaseServer {
const { incrementalCacheHandlerPath } = this.nextConfig.experimental

if (incrementalCacheHandlerPath) {
CacheHandler = require(this.minimalMode
? join(this.distDir, incrementalCacheHandlerPath)
: incrementalCacheHandlerPath)
CacheHandler = require(isAbsolute(incrementalCacheHandlerPath)
? incrementalCacheHandlerPath
: join(this.distDir, incrementalCacheHandlerPath))
CacheHandler = CacheHandler.default || CacheHandler
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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(),
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ describe('should set-up next', () => {
pages: new FileRef(join(__dirname, 'pages')),
lib: new FileRef(join(__dirname, 'lib')),
'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')),
'.env': new FileRef(join(__dirname, '.env')),
'.env.local': new FileRef(join(__dirname, '.env.local')),
'.env.production': new FileRef(join(__dirname, '.env.production')),
},
nextConfig: {
experimental: {
incrementalCacheHandlerPath: './cache-handler.js',
},
eslint: {
ignoreDuringBuilds: true,
},
Expand Down Expand Up @@ -297,6 +301,16 @@ describe('should set-up next', () => {
).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 () => {
expect(
await fs.pathExists(
Expand Down

0 comments on commit 7f6e8db

Please sign in to comment.