Skip to content

Commit

Permalink
fix: custom incremental cache handlers should work when transpiled (v…
Browse files Browse the repository at this point in the history
…ercel#54472)

This fixes a `CurCacheHandler is not a constructor` error when the custom cache handler path is transpiled from ESM -> CJS (resulting in the handler being attached to the `default` property on the module's exports)

Closes NEXT-1558
Fixes vercel#54453
  • Loading branch information
ztanner authored Aug 24, 2023
1 parent be5f9ba commit 9ea6bc4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
@@ -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')
})
}
)
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
process.env.CUSTOM_CACHE_HANDLER = '1'
process.env.CUSTOM_CACHE_HANDLER = require.resolve('./cache-handler.js')
require('./app-static.test')
26 changes: 26 additions & 0 deletions test/e2e/app-dir/app-static/cache-handler-default-export.js
Original file line number Diff line number Diff line change
@@ -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
4 changes: 1 addition & 3 deletions test/e2e/app-dir/app-static/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 9ea6bc4

Please sign in to comment.