Commit bed0fcda authored by Jimmy Lai's avatar Jimmy Lai

fix(cache): avoid pulling server internals into browser bundles

parent bbd5b865
const cacheExports = { let cacheExports
unstable_cache: require('next/dist/server/web/spec-extension/unstable-cache')
if (process.env.NEXT_RUNTIME === '') {
const notAvailableInClient = (name) => {
return function notAvailable() {
throw new Error(`\`${name}\` is only available in a Server Component.`)
}
}
cacheExports = {
unstable_cache: function unstable_cache(cb) {
// Legacy behavior: allow importing/using unstable_cache from client bundles
// without pulling in server internals.
if (typeof cb !== 'function') return cb
return function cached() {
return cb.apply(this, arguments)
}
},
unstable_noStore: function unstable_noStore() {},
updateTag: notAvailableInClient('updateTag'),
revalidateTag: notAvailableInClient('revalidateTag'),
revalidatePath: notAvailableInClient('revalidatePath'),
refresh: notAvailableInClient('refresh'),
cacheLife: notAvailableInClient('cacheLife'),
cacheTag: notAvailableInClient('cacheTag'),
}
} else {
// Keep server requires in this branch so browser builds can DCE them.
cacheExports = {
unstable_cache:
require('next/dist/server/web/spec-extension/unstable-cache')
.unstable_cache, .unstable_cache,
updateTag: require('next/dist/server/web/spec-extension/revalidate') updateTag: require('next/dist/server/web/spec-extension/revalidate')
...@@ -17,6 +47,7 @@ const cacheExports = { ...@@ -17,6 +47,7 @@ const cacheExports = {
.unstable_noStore, .unstable_noStore,
cacheLife: require('next/dist/server/use-cache/cache-life').cacheLife, cacheLife: require('next/dist/server/use-cache/cache-life').cacheLife,
cacheTag: require('next/dist/server/use-cache/cache-tag').cacheTag, cacheTag: require('next/dist/server/use-cache/cache-tag').cacheTag,
}
} }
let didWarnCacheLife = false let didWarnCacheLife = false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment