From b6d6c71d3ae0c45c6c00a5b0f9d368c7943eea5c Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 22 Dec 2023 00:21:20 +0100 Subject: [PATCH 01/10] Use import for inc cache --- packages/next/src/build/index.ts | 8 +++++--- packages/next/src/build/utils.ts | 8 +++++--- .../src/export/helpers/create-incremental-cache.ts | 10 ++++++---- packages/next/src/export/worker.ts | 2 +- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index 45da66f31ee95..436c60a81ab21 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -1332,9 +1332,11 @@ export default async function build( if (config.experimental.staticWorkerRequestDeduping) { let CacheHandler if (incrementalCacheHandlerPath) { - CacheHandler = require(path.isAbsolute(incrementalCacheHandlerPath) - ? incrementalCacheHandlerPath - : path.join(dir, incrementalCacheHandlerPath)) + CacheHandler = await import( + path.isAbsolute(incrementalCacheHandlerPath) + ? incrementalCacheHandlerPath + : path.join(dir, incrementalCacheHandlerPath) + ) CacheHandler = CacheHandler.default || CacheHandler } diff --git a/packages/next/src/build/utils.ts b/packages/next/src/build/utils.ts index 8a7d94b35cc63..9e4ea9be58989 100644 --- a/packages/next/src/build/utils.ts +++ b/packages/next/src/build/utils.ts @@ -1307,9 +1307,11 @@ export async function buildAppStaticPaths({ let CacheHandler: any if (incrementalCacheHandlerPath) { - CacheHandler = require(path.isAbsolute(incrementalCacheHandlerPath) - ? incrementalCacheHandlerPath - : path.join(dir, incrementalCacheHandlerPath)) + CacheHandler = await import( + path.isAbsolute(incrementalCacheHandlerPath) + ? incrementalCacheHandlerPath + : path.join(dir, incrementalCacheHandlerPath) + ) CacheHandler = CacheHandler.default || CacheHandler } diff --git a/packages/next/src/export/helpers/create-incremental-cache.ts b/packages/next/src/export/helpers/create-incremental-cache.ts index 8228005f4a3bc..aadac67f361bd 100644 --- a/packages/next/src/export/helpers/create-incremental-cache.ts +++ b/packages/next/src/export/helpers/create-incremental-cache.ts @@ -5,7 +5,7 @@ import { IncrementalCache } from '../../server/lib/incremental-cache' import { hasNextSupport } from '../../telemetry/ci-info' import { nodeFs } from '../../server/lib/node-fs-methods' -export function createIncrementalCache({ +export async function createIncrementalCache({ incrementalCacheHandlerPath, isrMemoryCacheSize, fetchCacheKeyPrefix, @@ -27,9 +27,11 @@ export function createIncrementalCache({ // Custom cache handler overrides. let CacheHandler: any if (incrementalCacheHandlerPath) { - CacheHandler = require(path.isAbsolute(incrementalCacheHandlerPath) - ? incrementalCacheHandlerPath - : path.join(dir, incrementalCacheHandlerPath)) + CacheHandler = await import( + path.isAbsolute(incrementalCacheHandlerPath) + ? incrementalCacheHandlerPath + : path.join(dir, incrementalCacheHandlerPath) + ) CacheHandler = CacheHandler.default || CacheHandler } diff --git a/packages/next/src/export/worker.ts b/packages/next/src/export/worker.ts index b503846f5055c..5d2fd08578c95 100644 --- a/packages/next/src/export/worker.ts +++ b/packages/next/src/export/worker.ts @@ -220,7 +220,7 @@ async function exportPageImpl( // cache instance for this page. const incrementalCache = isAppDir && fetchCache - ? createIncrementalCache({ + ? await createIncrementalCache({ incrementalCacheHandlerPath, isrMemoryCacheSize, fetchCacheKeyPrefix, From 8d3feade8ae80bb9ffb0edcf92be2886c978e075 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 22 Dec 2023 00:32:49 +0100 Subject: [PATCH 02/10] interop --- packages/next/src/build/index.ts | 11 +++++++---- packages/next/src/build/utils.ts | 11 +++++++---- .../src/export/helpers/create-incremental-cache.ts | 11 +++++++---- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index 436c60a81ab21..25fa29dd221b8 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -162,6 +162,7 @@ import { formatManifest } from './manifests/formatter/format-manifest' import { getStartServerInfo, logStartInfo } from '../server/lib/app-info-log' import type { NextEnabledDirectories } from '../server/base-server' import { hasCustomExportOutput } from '../export/utils' +import { interopDefault } from '../lib/interop-default' interface ExperimentalBypassForInfo { experimentalBypassFor?: RouteHas[] @@ -1332,10 +1333,12 @@ export default async function build( if (config.experimental.staticWorkerRequestDeduping) { let CacheHandler if (incrementalCacheHandlerPath) { - CacheHandler = await import( - path.isAbsolute(incrementalCacheHandlerPath) - ? incrementalCacheHandlerPath - : path.join(dir, incrementalCacheHandlerPath) + CacheHandler = interopDefault( + await import( + path.isAbsolute(incrementalCacheHandlerPath) + ? incrementalCacheHandlerPath + : path.join(dir, incrementalCacheHandlerPath) + ) ) CacheHandler = CacheHandler.default || CacheHandler } diff --git a/packages/next/src/build/utils.ts b/packages/next/src/build/utils.ts index 9e4ea9be58989..8be053f7957d3 100644 --- a/packages/next/src/build/utils.ts +++ b/packages/next/src/build/utils.ts @@ -72,6 +72,7 @@ import { normalizeAppPath } from '../shared/lib/router/utils/app-paths' import { denormalizeAppPagePath } from '../shared/lib/page-path/denormalize-app-path' import { RouteKind } from '../server/future/route-kind' import { isAppRouteRouteModule } from '../server/future/route-modules/checks' +import { interopDefault } from '../lib/interop-default' export type ROUTER_TYPE = 'pages' | 'app' @@ -1307,10 +1308,12 @@ export async function buildAppStaticPaths({ let CacheHandler: any if (incrementalCacheHandlerPath) { - CacheHandler = await import( - path.isAbsolute(incrementalCacheHandlerPath) - ? incrementalCacheHandlerPath - : path.join(dir, incrementalCacheHandlerPath) + CacheHandler = interopDefault( + await import( + path.isAbsolute(incrementalCacheHandlerPath) + ? incrementalCacheHandlerPath + : path.join(dir, incrementalCacheHandlerPath) + ) ) CacheHandler = CacheHandler.default || CacheHandler } diff --git a/packages/next/src/export/helpers/create-incremental-cache.ts b/packages/next/src/export/helpers/create-incremental-cache.ts index aadac67f361bd..9456a2b8b2889 100644 --- a/packages/next/src/export/helpers/create-incremental-cache.ts +++ b/packages/next/src/export/helpers/create-incremental-cache.ts @@ -4,6 +4,7 @@ import path from 'path' import { IncrementalCache } from '../../server/lib/incremental-cache' import { hasNextSupport } from '../../telemetry/ci-info' import { nodeFs } from '../../server/lib/node-fs-methods' +import { interopDefault } from '../../lib/interop-default' export async function createIncrementalCache({ incrementalCacheHandlerPath, @@ -27,10 +28,12 @@ export async function createIncrementalCache({ // Custom cache handler overrides. let CacheHandler: any if (incrementalCacheHandlerPath) { - CacheHandler = await import( - path.isAbsolute(incrementalCacheHandlerPath) - ? incrementalCacheHandlerPath - : path.join(dir, incrementalCacheHandlerPath) + CacheHandler = interopDefault( + await import( + path.isAbsolute(incrementalCacheHandlerPath) + ? incrementalCacheHandlerPath + : path.join(dir, incrementalCacheHandlerPath) + ) ) CacheHandler = CacheHandler.default || CacheHandler } From 45bdf2c0b822567692a4394c1341ae0d56bebcc4 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 22 Dec 2023 15:52:25 +0100 Subject: [PATCH 03/10] add test --- packages/next/src/server/base-server.ts | 8 +- packages/next/src/server/next-server.ts | 6 +- .../app-custom-cache-handler/app/layout.js | 12 +++ .../app-custom-cache-handler/app/page.js | 12 +++ .../cache-handler-cjs-default-export.js | 27 ++++++ .../cache-handler-esm.js | 27 ++++++ .../app-custom-cache-handler/cache-handler.js | 27 ++++++ .../app-custom-cache-handler/index.test.ts | 83 +++++++++++++++++++ .../app-custom-cache-handler/next.config.js | 6 ++ ...pp-static-custom-cache-handler-esm.test.ts | 27 ------ test/lib/next-modes/base.ts | 8 +- 11 files changed, 206 insertions(+), 37 deletions(-) create mode 100644 test/e2e/app-dir/app-custom-cache-handler/app/layout.js create mode 100644 test/e2e/app-dir/app-custom-cache-handler/app/page.js create mode 100644 test/e2e/app-dir/app-custom-cache-handler/cache-handler-cjs-default-export.js create mode 100644 test/e2e/app-dir/app-custom-cache-handler/cache-handler-esm.js create mode 100644 test/e2e/app-dir/app-custom-cache-handler/cache-handler.js create mode 100644 test/e2e/app-dir/app-custom-cache-handler/index.test.ts create mode 100644 test/e2e/app-dir/app-custom-cache-handler/next.config.js delete mode 100644 test/e2e/app-dir/app-static/app-static-custom-cache-handler-esm.test.ts diff --git a/packages/next/src/server/base-server.ts b/packages/next/src/server/base-server.ts index b1ddcca4f6425..7a2ec83cc4431 100644 --- a/packages/next/src/server/base-server.ts +++ b/packages/next/src/server/base-server.ts @@ -387,7 +387,7 @@ export default abstract class Server { protected abstract getIncrementalCache(options: { requestHeaders: Record requestProtocol: 'http' | 'https' - }): import('./lib/incremental-cache').IncrementalCache + }): Promise protected abstract getResponseCache(options: { dev: boolean @@ -1263,7 +1263,7 @@ export default abstract class Server { protocol = parsedFullUrl.protocol as 'https:' | 'http:' } catch {} - const incrementalCache = this.getIncrementalCache({ + const incrementalCache = await this.getIncrementalCache({ requestHeaders: Object.assign({}, req.headers), requestProtocol: protocol.substring(0, protocol.length - 1) as | 'http' @@ -2127,12 +2127,12 @@ export default abstract class Server { // use existing incrementalCache instance if available const incrementalCache = (globalThis as any).__incrementalCache || - this.getIncrementalCache({ + (await this.getIncrementalCache({ requestHeaders: Object.assign({}, req.headers), requestProtocol: protocol.substring(0, protocol.length - 1) as | 'http' | 'https', - }) + })) const { routeModule } = components diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index c02af8fd254eb..a19dd25ee3f65 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -107,7 +107,7 @@ declare const __non_webpack_require__: NodeRequire const dynamicRequire = process.env.NEXT_MINIMAL ? __non_webpack_require__ - : require + : (mod: string) => import(mod) function writeStdoutLine(text: string) { process.stdout.write(' ' + text + '\n') @@ -289,7 +289,7 @@ export default class NextNodeServer extends BaseServer { ) } - protected getIncrementalCache({ + protected async getIncrementalCache({ requestHeaders, requestProtocol, }: { @@ -301,7 +301,7 @@ export default class NextNodeServer extends BaseServer { const { incrementalCacheHandlerPath } = this.nextConfig.experimental if (incrementalCacheHandlerPath) { - CacheHandler = dynamicRequire( + CacheHandler = await dynamicRequire( isAbsolute(incrementalCacheHandlerPath) ? incrementalCacheHandlerPath : join(this.distDir, incrementalCacheHandlerPath) diff --git a/test/e2e/app-dir/app-custom-cache-handler/app/layout.js b/test/e2e/app-dir/app-custom-cache-handler/app/layout.js new file mode 100644 index 0000000000000..8525f5f8c0b2a --- /dev/null +++ b/test/e2e/app-dir/app-custom-cache-handler/app/layout.js @@ -0,0 +1,12 @@ +export const metadata = { + title: 'Next.js', + description: 'Generated by Next.js', +} + +export default function RootLayout({ children }) { + return ( + + {children} + + ) +} diff --git a/test/e2e/app-dir/app-custom-cache-handler/app/page.js b/test/e2e/app-dir/app-custom-cache-handler/app/page.js new file mode 100644 index 0000000000000..e3bbe6995c282 --- /dev/null +++ b/test/e2e/app-dir/app-custom-cache-handler/app/page.js @@ -0,0 +1,12 @@ +export default async function Page() { + const data = await fetch( + 'https://next-data-api-endpoint.vercel.app/api/random?page' + ).then((res) => res.text()) + + return ( + <> +

{data}

+

{Date.now()}

+ + ) +} diff --git a/test/e2e/app-dir/app-custom-cache-handler/cache-handler-cjs-default-export.js b/test/e2e/app-dir/app-custom-cache-handler/cache-handler-cjs-default-export.js new file mode 100644 index 0000000000000..ce601fad4d2f0 --- /dev/null +++ b/test/e2e/app-dir/app-custom-cache-handler/cache-handler-cjs-default-export.js @@ -0,0 +1,27 @@ +Object.defineProperty(exports, '__esModule', { value: true }) + +const cache = new Map() + +const CacheHandler = /** @class */ (function () { + function CacheHandler(options) { + this.options = options + this.cache = cache + console.log('initialized custom cache-handler') + console.log('cache handler - cjs default export') + } + CacheHandler.prototype.get = function (key) { + console.log('cache-handler get', key) + return Promise.resolve(this.cache.get(key)) + } + CacheHandler.prototype.set = function (key, data) { + console.log('cache-handler set', key) + this.cache.set(key, { + value: data, + lastModified: Date.now(), + }) + return Promise.resolve(undefined) + } + return CacheHandler +})() + +exports.default = CacheHandler diff --git a/test/e2e/app-dir/app-custom-cache-handler/cache-handler-esm.js b/test/e2e/app-dir/app-custom-cache-handler/cache-handler-esm.js new file mode 100644 index 0000000000000..216ab27fd2850 --- /dev/null +++ b/test/e2e/app-dir/app-custom-cache-handler/cache-handler-esm.js @@ -0,0 +1,27 @@ +const cache = new Map() + +class CacheHandler { + constructor(options) { + this.options = options + this.cache = {} + console.log('initialized custom cache-handler') + console.log('cache handler - esm default export') + } + + async get(key) { + console.log('key', key) + console.log('cache-handler get', key) + return cache.get(key) + } + + async set(key, data) { + console.log('set key', key) + console.log('cache-handler set', key) + cache.set(key, { + value: data, + lastModified: Date.now(), + }) + } +} + +export default CacheHandler diff --git a/test/e2e/app-dir/app-custom-cache-handler/cache-handler.js b/test/e2e/app-dir/app-custom-cache-handler/cache-handler.js new file mode 100644 index 0000000000000..5e1bf3639036c --- /dev/null +++ b/test/e2e/app-dir/app-custom-cache-handler/cache-handler.js @@ -0,0 +1,27 @@ +const cache = new Map() + +class CacheHandler { + constructor(options) { + this.options = options + this.cache = {} + console.log('initialized custom cache-handler') + console.log('cache handler - cjs module exports') + } + + async get(key) { + console.log('key', key) + console.log('cache-handler get', key) + return cache.get(key) + } + + async set(key, data) { + console.log('set key', key) + console.log('cache-handler set', key) + cache.set(key, { + value: data, + lastModified: Date.now(), + }) + } +} + +module.exports = CacheHandler diff --git a/test/e2e/app-dir/app-custom-cache-handler/index.test.ts b/test/e2e/app-dir/app-custom-cache-handler/index.test.ts new file mode 100644 index 0000000000000..fed8457303b61 --- /dev/null +++ b/test/e2e/app-dir/app-custom-cache-handler/index.test.ts @@ -0,0 +1,83 @@ +import fs from 'fs' +import { type NextInstance, createNextDescribe, FileRef } from 'e2e-utils' +import { check } from 'next-test-utils' + +const originalNextConfig = fs.readFileSync( + __dirname + '/next.config.js', + 'utf8' +) + +function runTests( + exportType: string, + { next, isNextDev }: { next: NextInstance; isNextDev: boolean } +) { + describe(exportType, () => { + it('should have logs from cache-handler', async () => { + if (isNextDev) { + await next.fetch('/') + } + await check(() => { + expect(next.cliOutput).toContain('cache handler - ' + exportType) + expect(next.cliOutput).toContain('initialized custom cache-handler') + expect(next.cliOutput).toContain('cache-handler get') + expect(next.cliOutput).toContain('cache-handler set') + return 'success' + }, 'success') + }) + }) +} + +createNextDescribe( + 'app-dir - custom-cache-handler - cjs', + { + files: __dirname, + skipDeployment: true, + env: { + CUSTOM_CACHE_HANDLER: 'cache-handler.js', + }, + }, + ({ next, isNextDev }) => { + runTests('cjs module exports', { next, isNextDev }) + } +) + +createNextDescribe( + 'app-dir - custom-cache-handler - cjs-default-export', + { + files: __dirname, + skipDeployment: true, + packageJson: { + type: 'module', + }, + env: { + CUSTOM_CACHE_HANDLER: 'cache-handler-cjs-default-export.js', + }, + }, + ({ next, isNextDev }) => { + runTests('cjs default export', { next, isNextDev }) + } +) + +createNextDescribe( + 'app-dir - custom-cache-handler - esm', + { + files: { + app: new FileRef(__dirname + '/app'), + 'cache-handler-esm.js': new FileRef(__dirname + '/cache-handler-esm.js'), + 'next.config.js': originalNextConfig.replace( + 'module.exports = ', + 'export default ' + ), + }, + skipDeployment: true, + packageJson: { + type: 'module', + }, + env: { + CUSTOM_CACHE_HANDLER: 'cache-handler-esm.js', + }, + }, + ({ next, isNextDev }) => { + runTests('esm default export', { next, isNextDev }) + } +) diff --git a/test/e2e/app-dir/app-custom-cache-handler/next.config.js b/test/e2e/app-dir/app-custom-cache-handler/next.config.js new file mode 100644 index 0000000000000..16c82f0775544 --- /dev/null +++ b/test/e2e/app-dir/app-custom-cache-handler/next.config.js @@ -0,0 +1,6 @@ +module.exports = { + experimental: { + incrementalCacheHandlerPath: + process.cwd() + '/' + process.env.CUSTOM_CACHE_HANDLER, + }, +} diff --git a/test/e2e/app-dir/app-static/app-static-custom-cache-handler-esm.test.ts b/test/e2e/app-dir/app-static/app-static-custom-cache-handler-esm.test.ts deleted file mode 100644 index 105629f57ec03..0000000000000 --- a/test/e2e/app-dir/app-static/app-static-custom-cache-handler-esm.test.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { createNextDescribe } from 'e2e-utils' -import { join } from 'path' - -createNextDescribe( - 'app-static-custom-cache-handler-esm', - { - files: __dirname, - env: { - CUSTOM_CACHE_HANDLER: join( - __dirname, - './cache-handler-default-export.js' - ), - }, - }, - ({ next, isNextStart }) => { - if (!isNextStart) { - it('should skip', () => {}) - return - } - - it('should have logs from cache-handler', async () => { - expect(next.cliOutput).toContain('initialized custom cache-handler') - expect(next.cliOutput).toContain('cache-handler get') - expect(next.cliOutput).toContain('cache-handler set') - }) - } -) diff --git a/test/lib/next-modes/base.ts b/test/lib/next-modes/base.ts index 348624e818cf3..ed106ddb07035 100644 --- a/test/lib/next-modes/base.ts +++ b/test/lib/next-modes/base.ts @@ -233,11 +233,13 @@ export class NextInstance { ((global as any).isNextDeploy && !nextConfigFile) ) { const functions = [] - + const exportDeclare = + this.packageJson?.type === 'module' + ? 'export default' + : 'module.exports = ' await fs.writeFile( path.join(this.testDir, 'next.config.js'), - ` - module.exports = ` + + exportDeclare + JSON.stringify( { ...this.nextConfig, From 3ba746a1f28de65372c9d553e59555935486322a Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 22 Dec 2023 15:53:54 +0100 Subject: [PATCH 04/10] rm duplicate interop --- packages/next/src/build/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index 25fa29dd221b8..120388b943171 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -1340,7 +1340,6 @@ export default async function build( : path.join(dir, incrementalCacheHandlerPath) ) ) - CacheHandler = CacheHandler.default || CacheHandler } const cacheInitialization = await initializeIncrementalCache({ From c959f8a9a4867a7f894095b2ee8b3d97636fec20 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 22 Dec 2023 16:40:48 +0100 Subject: [PATCH 05/10] fix ts --- packages/next/src/server/web-server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/src/server/web-server.ts b/packages/next/src/server/web-server.ts index 6125efdb9ecbd..36072c0b890b2 100644 --- a/packages/next/src/server/web-server.ts +++ b/packages/next/src/server/web-server.ts @@ -54,7 +54,7 @@ export default class NextWebServer extends BaseServer { Object.assign(this.renderOpts, options.webServerConfig.extendRenderOpts) } - protected getIncrementalCache({ + protected async getIncrementalCache({ requestHeaders, }: { requestHeaders: IncrementalCache['requestHeaders'] From ea12a64356bce862fe4dad0847de471bd82ffeb4 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 22 Dec 2023 17:06:28 +0100 Subject: [PATCH 06/10] interop --- packages/next/src/build/utils.ts | 1 - .../helpers/create-incremental-cache.ts | 1 - packages/next/src/export/worker.ts | 29 ++++++++++--------- packages/next/src/server/next-server.ts | 4 +-- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/packages/next/src/build/utils.ts b/packages/next/src/build/utils.ts index 8be053f7957d3..ea15603e855af 100644 --- a/packages/next/src/build/utils.ts +++ b/packages/next/src/build/utils.ts @@ -1315,7 +1315,6 @@ export async function buildAppStaticPaths({ : path.join(dir, incrementalCacheHandlerPath) ) ) - CacheHandler = CacheHandler.default || CacheHandler } const incrementalCache = new IncrementalCache({ diff --git a/packages/next/src/export/helpers/create-incremental-cache.ts b/packages/next/src/export/helpers/create-incremental-cache.ts index 9456a2b8b2889..5640bed653065 100644 --- a/packages/next/src/export/helpers/create-incremental-cache.ts +++ b/packages/next/src/export/helpers/create-incremental-cache.ts @@ -35,7 +35,6 @@ export async function createIncrementalCache({ : path.join(dir, incrementalCacheHandlerPath) ) ) - CacheHandler = CacheHandler.default || CacheHandler } const incrementalCache = new IncrementalCache({ diff --git a/packages/next/src/export/worker.ts b/packages/next/src/export/worker.ts index 5d2fd08578c95..6cde4adf03b8a 100644 --- a/packages/next/src/export/worker.ts +++ b/packages/next/src/export/worker.ts @@ -35,6 +35,7 @@ import { createIncrementalCache } from './helpers/create-incremental-cache' import { isPostpone } from '../server/lib/router-utils/is-postpone' import { isMissingPostponeDataError } from '../server/app-render/is-missing-postpone-error' import { isDynamicUsageError } from './helpers/is-dynamic-usage-error' +import { interopDefault } from '../lib/interop-default' const envConfig = require('../shared/lib/runtime-config.external') @@ -220,19 +221,21 @@ async function exportPageImpl( // cache instance for this page. const incrementalCache = isAppDir && fetchCache - ? await createIncrementalCache({ - incrementalCacheHandlerPath, - isrMemoryCacheSize, - fetchCacheKeyPrefix, - distDir, - dir, - enabledDirectories, - // PPR is not available for Pages. - experimental: { ppr: false }, - // skip writing to disk in minimal mode for now, pending some - // changes to better support it - flushToDisk: !hasNextSupport, - }) + ? interopDefault( + await createIncrementalCache({ + incrementalCacheHandlerPath, + isrMemoryCacheSize, + fetchCacheKeyPrefix, + distDir, + dir, + enabledDirectories, + // PPR is not available for Pages. + experimental: { ppr: false }, + // skip writing to disk in minimal mode for now, pending some + // changes to better support it + flushToDisk: !hasNextSupport, + }) + ) : undefined // Handle App Routes. diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index a19dd25ee3f65..e8055f1752d24 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -100,6 +100,7 @@ import { RouteModuleLoader } from './future/helpers/module-loader/route-module-l import { loadManifest } from './load-manifest' import { lazyRenderAppPage } from './future/route-modules/app-page/module.render' import { lazyRenderPagesPage } from './future/route-modules/pages/module.render' +import { interopDefault } from '../lib/interop-default' export * from './base-server' @@ -107,7 +108,7 @@ declare const __non_webpack_require__: NodeRequire const dynamicRequire = process.env.NEXT_MINIMAL ? __non_webpack_require__ - : (mod: string) => import(mod) + : async (mod: string) => interopDefault(await import(mod)) function writeStdoutLine(text: string) { process.stdout.write(' ' + text + '\n') @@ -306,7 +307,6 @@ export default class NextNodeServer extends BaseServer { ? incrementalCacheHandlerPath : join(this.distDir, incrementalCacheHandlerPath) ) - CacheHandler = CacheHandler.default || CacheHandler } // incremental-cache is request specific From c94e5561ef2aea4bd4a2c8a037ac7a7500125dde Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 22 Dec 2023 17:35:56 +0100 Subject: [PATCH 07/10] separate --- packages/next/src/server/next-server.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index e8055f1752d24..b3d605727a886 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -106,10 +106,14 @@ export * from './base-server' declare const __non_webpack_require__: NodeRequire -const dynamicRequire = process.env.NEXT_MINIMAL +const dynamicRequireEsm = process.env.NEXT_MINIMAL ? __non_webpack_require__ : async (mod: string) => interopDefault(await import(mod)) +const dynamicRequire = process.env.NEXT_MINIMAL + ? __non_webpack_require__ + : require + function writeStdoutLine(text: string) { process.stdout.write(' ' + text + '\n') } @@ -302,7 +306,7 @@ export default class NextNodeServer extends BaseServer { const { incrementalCacheHandlerPath } = this.nextConfig.experimental if (incrementalCacheHandlerPath) { - CacheHandler = await dynamicRequire( + CacheHandler = await dynamicRequireEsm( isAbsolute(incrementalCacheHandlerPath) ? incrementalCacheHandlerPath : join(this.distDir, incrementalCacheHandlerPath) From 9dc532ee1f81772b5c8b4001b3f5674a506fc680 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 22 Dec 2023 17:41:58 +0100 Subject: [PATCH 08/10] interop outside --- packages/next/src/server/next-server.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index b3d605727a886..0c9279ef8ea6f 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -108,7 +108,7 @@ declare const __non_webpack_require__: NodeRequire const dynamicRequireEsm = process.env.NEXT_MINIMAL ? __non_webpack_require__ - : async (mod: string) => interopDefault(await import(mod)) + : async (mod: string) => await import(mod) const dynamicRequire = process.env.NEXT_MINIMAL ? __non_webpack_require__ @@ -306,10 +306,12 @@ export default class NextNodeServer extends BaseServer { const { incrementalCacheHandlerPath } = this.nextConfig.experimental if (incrementalCacheHandlerPath) { - CacheHandler = await dynamicRequireEsm( - isAbsolute(incrementalCacheHandlerPath) - ? incrementalCacheHandlerPath - : join(this.distDir, incrementalCacheHandlerPath) + CacheHandler = interopDefault( + await dynamicRequireEsm( + isAbsolute(incrementalCacheHandlerPath) + ? incrementalCacheHandlerPath + : join(this.distDir, incrementalCacheHandlerPath) + ) ) } From ac25ccd4baa94a1d46519c70e7edf4a71a61a05a Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 22 Dec 2023 18:33:56 +0100 Subject: [PATCH 09/10] fix test --- .../helpers/create-incremental-cache.ts | 12 ++++---- packages/next/src/export/worker.ts | 28 +++++++++---------- packages/next/src/server/next-server.ts | 8 ++++-- .../app-custom-cache-handler/index.test.ts | 5 +--- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/packages/next/src/export/helpers/create-incremental-cache.ts b/packages/next/src/export/helpers/create-incremental-cache.ts index 5640bed653065..075c805a89256 100644 --- a/packages/next/src/export/helpers/create-incremental-cache.ts +++ b/packages/next/src/export/helpers/create-incremental-cache.ts @@ -29,11 +29,13 @@ export async function createIncrementalCache({ let CacheHandler: any if (incrementalCacheHandlerPath) { CacheHandler = interopDefault( - await import( - path.isAbsolute(incrementalCacheHandlerPath) - ? incrementalCacheHandlerPath - : path.join(dir, incrementalCacheHandlerPath) - ) + ( + await import( + path.isAbsolute(incrementalCacheHandlerPath) + ? incrementalCacheHandlerPath + : path.join(dir, incrementalCacheHandlerPath) + ) + ).default ) } diff --git a/packages/next/src/export/worker.ts b/packages/next/src/export/worker.ts index 6cde4adf03b8a..7010d1c47a814 100644 --- a/packages/next/src/export/worker.ts +++ b/packages/next/src/export/worker.ts @@ -221,21 +221,19 @@ async function exportPageImpl( // cache instance for this page. const incrementalCache = isAppDir && fetchCache - ? interopDefault( - await createIncrementalCache({ - incrementalCacheHandlerPath, - isrMemoryCacheSize, - fetchCacheKeyPrefix, - distDir, - dir, - enabledDirectories, - // PPR is not available for Pages. - experimental: { ppr: false }, - // skip writing to disk in minimal mode for now, pending some - // changes to better support it - flushToDisk: !hasNextSupport, - }) - ) + ? await createIncrementalCache({ + incrementalCacheHandlerPath, + isrMemoryCacheSize, + fetchCacheKeyPrefix, + distDir, + dir, + enabledDirectories, + // PPR is not available for Pages. + experimental: { ppr: false }, + // skip writing to disk in minimal mode for now, pending some + // changes to better support it + flushToDisk: !hasNextSupport, + }) : undefined // Handle App Routes. diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index 0c9279ef8ea6f..f8c6e2b9c9b9b 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -106,10 +106,12 @@ export * from './base-server' declare const __non_webpack_require__: NodeRequire -const dynamicRequireEsm = process.env.NEXT_MINIMAL +// For module that can be both CJS or ESM +const dynamicImportEsmDefault = process.env.NEXT_MINIMAL ? __non_webpack_require__ - : async (mod: string) => await import(mod) + : async (mod: string) => (await import(mod)).default +// For module that will be compiled to CJS, e.g. instrument const dynamicRequire = process.env.NEXT_MINIMAL ? __non_webpack_require__ : require @@ -307,7 +309,7 @@ export default class NextNodeServer extends BaseServer { if (incrementalCacheHandlerPath) { CacheHandler = interopDefault( - await dynamicRequireEsm( + await dynamicImportEsmDefault( isAbsolute(incrementalCacheHandlerPath) ? incrementalCacheHandlerPath : join(this.distDir, incrementalCacheHandlerPath) diff --git a/test/e2e/app-dir/app-custom-cache-handler/index.test.ts b/test/e2e/app-dir/app-custom-cache-handler/index.test.ts index fed8457303b61..80cfa8c15b80d 100644 --- a/test/e2e/app-dir/app-custom-cache-handler/index.test.ts +++ b/test/e2e/app-dir/app-custom-cache-handler/index.test.ts @@ -1,6 +1,6 @@ -import fs from 'fs' import { type NextInstance, createNextDescribe, FileRef } from 'e2e-utils' import { check } from 'next-test-utils' +import fs from 'fs' const originalNextConfig = fs.readFileSync( __dirname + '/next.config.js', @@ -46,9 +46,6 @@ createNextDescribe( { files: __dirname, skipDeployment: true, - packageJson: { - type: 'module', - }, env: { CUSTOM_CACHE_HANDLER: 'cache-handler-cjs-default-export.js', }, From 676b71dc92428577e79119443d2211dc181f59b2 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 22 Dec 2023 18:43:57 +0100 Subject: [PATCH 10/10] fix lint --- packages/next/src/export/worker.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/next/src/export/worker.ts b/packages/next/src/export/worker.ts index 7010d1c47a814..5d2fd08578c95 100644 --- a/packages/next/src/export/worker.ts +++ b/packages/next/src/export/worker.ts @@ -35,7 +35,6 @@ import { createIncrementalCache } from './helpers/create-incremental-cache' import { isPostpone } from '../server/lib/router-utils/is-postpone' import { isMissingPostponeDataError } from '../server/app-render/is-missing-postpone-error' import { isDynamicUsageError } from './helpers/is-dynamic-usage-error' -import { interopDefault } from '../lib/interop-default' const envConfig = require('../shared/lib/runtime-config.external')