diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index 1e1a1586088b5..c0fde0830c4c1 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -1278,6 +1278,7 @@ export default async function build( CacheHandler = require(path.isAbsolute(incrementalCacheHandlerPath) ? incrementalCacheHandlerPath : path.join(dir, incrementalCacheHandlerPath)) + CacheHandler = CacheHandler.default || CacheHandler } const { ipcPort, ipcValidationKey } = await initialize({ 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 new file mode 100644 index 0000000000000..105629f57ec03 --- /dev/null +++ b/test/e2e/app-dir/app-static/app-static-custom-cache-handler-esm.test.ts @@ -0,0 +1,27 @@ +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/e2e/app-dir/app-static/app-static-custom-handler.test.ts b/test/e2e/app-dir/app-static/app-static-custom-handler.test.ts index 7092b09389e90..214a9a38986c6 100644 --- a/test/e2e/app-dir/app-static/app-static-custom-handler.test.ts +++ b/test/e2e/app-dir/app-static/app-static-custom-handler.test.ts @@ -1,2 +1,2 @@ -process.env.CUSTOM_CACHE_HANDLER = '1' +process.env.CUSTOM_CACHE_HANDLER = require.resolve('./cache-handler.js') require('./app-static.test') diff --git a/test/e2e/app-dir/app-static/cache-handler-default-export.js b/test/e2e/app-dir/app-static/cache-handler-default-export.js new file mode 100644 index 0000000000000..1b6c91517eb8a --- /dev/null +++ b/test/e2e/app-dir/app-static/cache-handler-default-export.js @@ -0,0 +1,26 @@ +Object.defineProperty(exports, '__esModule', { value: true }) + +const cache = new Map() + +var CacheHandler = /** @class */ (function () { + function CacheHandler(options) { + this.options = options + this.cache = cache + console.log('initialized custom cache-handler') + } + 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-static/next.config.js b/test/e2e/app-dir/app-static/next.config.js index 7b4d12323800d..dd3e90f7b88c5 100644 --- a/test/e2e/app-dir/app-static/next.config.js +++ b/test/e2e/app-dir/app-static/next.config.js @@ -2,9 +2,7 @@ module.exports = { experimental: { logging: 'verbose', - incrementalCacheHandlerPath: process.env.CUSTOM_CACHE_HANDLER - ? require.resolve('./cache-handler.js') - : undefined, + incrementalCacheHandlerPath: process.env.CUSTOM_CACHE_HANDLER, }, // assetPrefix: '/assets', rewrites: async () => {