Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support incrementalCacheHandlerPath for standalone output #48694

Merged
merged 12 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,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 @@ -842,12 +844,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 @@ -1908,9 +1906,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 @@ -383,9 +383,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 @@ -299,6 +303,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