Unverified Commit 0c756bee authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update cache handling for app (#43659)

This updates the app directory caching. 

x-ref: [slack thread
](https://vercel.slack.com/archives/C042LHPJ1NX/p1669231119199339)

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 3a7c35a9
export const RSC = 'RSC' as const export const RSC = 'RSC' as const
export const NEXT_ROUTER_STATE_TREE = 'Next-Router-State-Tree' as const export const NEXT_ROUTER_STATE_TREE = 'Next-Router-State-Tree' as const
export const NEXT_ROUTER_PREFETCH = 'Next-Router-Prefetch' as const export const NEXT_ROUTER_PREFETCH = 'Next-Router-Prefetch' as const
export const FETCH_CACHE_HEADER = 'x-vercel-sc-headers' as const
export const RSC_VARY_HEADER = export const RSC_VARY_HEADER =
`${RSC}, ${NEXT_ROUTER_STATE_TREE}, ${NEXT_ROUTER_PREFETCH}` as const `${RSC}, ${NEXT_ROUTER_STATE_TREE}, ${NEXT_ROUTER_PREFETCH}` as const
......
...@@ -7,6 +7,9 @@ export interface StaticGenerationStore { ...@@ -7,6 +7,9 @@ export interface StaticGenerationStore {
fetchRevalidate?: number fetchRevalidate?: number
isStaticGeneration?: boolean isStaticGeneration?: boolean
forceStatic?: boolean forceStatic?: boolean
incrementalCache?: import('../../server/lib/incremental-cache').IncrementalCache
pendingRevalidates?: Promise<any>[]
isRevalidate?: boolean
} }
export let staticGenerationAsyncStorage: export let staticGenerationAsyncStorage:
......
...@@ -12,7 +12,7 @@ import '../server/node-polyfill-fetch' ...@@ -12,7 +12,7 @@ import '../server/node-polyfill-fetch'
import { loadRequireHook } from '../build/webpack/require-hook' import { loadRequireHook } from '../build/webpack/require-hook'
import { extname, join, dirname, sep } from 'path' import { extname, join, dirname, sep } from 'path'
import { promises } from 'fs' import fs, { promises } from 'fs'
import AmpHtmlValidator from 'next/dist/compiled/amphtml-validator' import AmpHtmlValidator from 'next/dist/compiled/amphtml-validator'
import { loadComponents } from '../server/load-components' import { loadComponents } from '../server/load-components'
import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic' import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic'
...@@ -32,6 +32,7 @@ import { normalizeAppPath } from '../shared/lib/router/utils/app-paths' ...@@ -32,6 +32,7 @@ import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
import { REDIRECT_ERROR_CODE } from '../client/components/redirect' import { REDIRECT_ERROR_CODE } from '../client/components/redirect'
import { DYNAMIC_ERROR_CODE } from '../client/components/hooks-server-context' import { DYNAMIC_ERROR_CODE } from '../client/components/hooks-server-context'
import { NOT_FOUND_ERROR_CODE } from '../client/components/not-found' import { NOT_FOUND_ERROR_CODE } from '../client/components/not-found'
import { IncrementalCache } from '../server/lib/incremental-cache'
loadRequireHook() loadRequireHook()
...@@ -98,6 +99,7 @@ interface RenderOpts { ...@@ -98,6 +99,7 @@ interface RenderOpts {
domainLocales?: DomainLocale[] domainLocales?: DomainLocale[]
trailingSlash?: boolean trailingSlash?: boolean
supportsDynamicHTML?: boolean supportsDynamicHTML?: boolean
incrementalCache?: import('../server/lib/incremental-cache').IncrementalCache
} }
// expose AsyncLocalStorage on globalThis for react usage // expose AsyncLocalStorage on globalThis for react usage
...@@ -310,6 +312,31 @@ export default async function exportPage({ ...@@ -310,6 +312,31 @@ export default async function exportPage({
// and bail when dynamic dependencies are detected // and bail when dynamic dependencies are detected
// only fully static paths are fully generated here // only fully static paths are fully generated here
if (isAppDir) { if (isAppDir) {
curRenderOpts.incrementalCache = new IncrementalCache({
dev: false,
requestHeaders: {},
flushToDisk: true,
maxMemoryCacheSize: 50 * 1024 * 1024,
getPrerenderManifest: () => ({
version: 3,
routes: {},
dynamicRoutes: {},
preview: {
previewModeEncryptionKey: '',
previewModeId: '',
previewModeSigningKey: '',
},
notFoundRoutes: [],
}),
fs: {
readFile: (f) => fs.promises.readFile(f, 'utf8'),
readFileSync: (f) => fs.readFileSync(f, 'utf8'),
writeFile: (f, d) => fs.promises.writeFile(f, d, 'utf8'),
mkdir: (dir) => fs.promises.mkdir(dir, { recursive: true }),
stat: (f) => fs.promises.stat(f),
},
serverDistDir: join(distDir, 'server'),
})
const { renderToHTMLOrFlight } = const { renderToHTMLOrFlight } =
require('../server/app-render') as typeof import('../server/app-render') require('../server/app-render') as typeof import('../server/app-render')
......
...@@ -80,6 +80,7 @@ function preloadComponent(Component: any, props: any) { ...@@ -80,6 +80,7 @@ function preloadComponent(Component: any, props: any) {
return Component return Component
} }
const CACHE_ONE_YEAR = 31536000
const INTERNAL_HEADERS_INSTANCE = Symbol('internal for headers readonly') const INTERNAL_HEADERS_INSTANCE = Symbol('internal for headers readonly')
function readonlyHeadersError() { function readonlyHeadersError() {
...@@ -176,6 +177,8 @@ export type RenderOptsPartial = { ...@@ -176,6 +177,8 @@ export type RenderOptsPartial = {
assetPrefix?: string assetPrefix?: string
fontLoaderManifest?: FontLoaderManifest fontLoaderManifest?: FontLoaderManifest
isBot?: boolean isBot?: boolean
incrementalCache?: import('./lib/incremental-cache').IncrementalCache
isRevalidate?: boolean
} }
export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial
...@@ -245,12 +248,115 @@ function patchFetch(ComponentMod: any) { ...@@ -245,12 +248,115 @@ function patchFetch(ComponentMod: any) {
const originFetch = globalThis.fetch const originFetch = globalThis.fetch
globalThis.fetch = async (input, init) => { globalThis.fetch = async (input, init) => {
const staticGenerationStore = const staticGenerationStore =
'getStore' in staticGenerationAsyncStorage ('getStore' in staticGenerationAsyncStorage
? staticGenerationAsyncStorage.getStore() ? staticGenerationAsyncStorage.getStore()
: staticGenerationAsyncStorage : staticGenerationAsyncStorage) || {}
const {
isStaticGeneration,
fetchRevalidate,
pathname,
incrementalCache,
isRevalidate,
} = (staticGenerationStore || {}) as StaticGenerationStore
let revalidate: number | undefined | boolean
if (typeof init?.next?.revalidate === 'number') {
revalidate = init.next.revalidate
}
if (init?.next?.revalidate === false) {
revalidate = CACHE_ONE_YEAR
}
if (
!staticGenerationStore.fetchRevalidate ||
(typeof revalidate === 'number' &&
revalidate < staticGenerationStore.fetchRevalidate)
) {
staticGenerationStore.fetchRevalidate = revalidate
}
let cacheKey: string | undefined
const doOriginalFetch = async () => {
return originFetch(input, init).then(async (res) => {
if (
incrementalCache &&
cacheKey &&
typeof revalidate === 'number' &&
revalidate > 0
) {
const clonedRes = res.clone()
let base64Body = ''
if (process.env.NEXT_RUNTIME === 'edge') {
let string = ''
new Uint8Array(await clonedRes.arrayBuffer()).forEach((byte) => {
string += String.fromCharCode(byte)
})
base64Body = btoa(string)
} else {
base64Body = Buffer.from(await clonedRes.arrayBuffer()).toString(
'base64'
)
}
await incrementalCache.set(
cacheKey,
{
kind: 'FETCH',
isStale: false,
age: 0,
data: {
headers: Object.fromEntries(clonedRes.headers.entries()),
body: base64Body,
},
revalidate,
},
revalidate,
true
)
}
return res
})
}
const { isStaticGeneration, fetchRevalidate, pathname } = if (incrementalCache && typeof revalidate === 'number' && revalidate > 0) {
staticGenerationStore || {} cacheKey = await incrementalCache?.fetchCacheKey(input.toString(), init)
const entry = await incrementalCache.get(cacheKey, true)
if (entry?.value && entry.value.kind === 'FETCH') {
// when stale and is revalidating we wait for fresh data
// so the revalidated entry has the updated data
if (!isRevalidate || !entry.isStale) {
if (entry.isStale) {
if (!staticGenerationStore.pendingRevalidates) {
staticGenerationStore.pendingRevalidates = []
}
staticGenerationStore.pendingRevalidates.push(
doOriginalFetch().catch(console.error)
)
}
const resData = entry.value.data
let decodedBody = ''
// TODO: handle non-text response bodies
if (process.env.NEXT_RUNTIME === 'edge') {
decodedBody = atob(resData.body)
} else {
decodedBody = Buffer.from(resData.body, 'base64').toString()
}
return new Response(decodedBody, {
headers: resData.headers,
status: resData.status,
})
}
}
}
if (staticGenerationStore && isStaticGeneration) { if (staticGenerationStore && isStaticGeneration) {
if (init && typeof init === 'object') { if (init && typeof init === 'object') {
...@@ -292,7 +398,7 @@ function patchFetch(ComponentMod: any) { ...@@ -292,7 +398,7 @@ function patchFetch(ComponentMod: any) {
if (hasNextConfig) delete init.next if (hasNextConfig) delete init.next
} }
} }
return originFetch(input, init) return doOriginalFetch()
} }
} }
...@@ -771,9 +877,7 @@ export async function renderToHTMLOrFlight( ...@@ -771,9 +877,7 @@ export async function renderToHTMLOrFlight(
supportsDynamicHTML, supportsDynamicHTML,
} = renderOpts } = renderOpts
if (process.env.NODE_ENV === 'production') { patchFetch(ComponentMod)
patchFetch(ComponentMod)
}
const generateStaticHTML = supportsDynamicHTML !== true const generateStaticHTML = supportsDynamicHTML !== true
const staticGenerationAsyncStorage = ComponentMod.staticGenerationAsyncStorage const staticGenerationAsyncStorage = ComponentMod.staticGenerationAsyncStorage
...@@ -1111,7 +1215,7 @@ export async function renderToHTMLOrFlight( ...@@ -1111,7 +1215,7 @@ export async function renderToHTMLOrFlight(
// otherwise // otherwise
if (layoutOrPageMod.dynamic === 'force-static') { if (layoutOrPageMod.dynamic === 'force-static') {
staticGenerationStore.forceStatic = true staticGenerationStore.forceStatic = true
} else { } else if (layoutOrPageMod.dynamic !== 'error') {
staticGenerationStore.forceStatic = false staticGenerationStore.forceStatic = false
} }
} }
...@@ -1726,6 +1830,10 @@ export async function renderToHTMLOrFlight( ...@@ -1726,6 +1830,10 @@ export async function renderToHTMLOrFlight(
} }
const renderResult = new RenderResult(await bodyResult()) const renderResult = new RenderResult(await bodyResult())
if (staticGenerationStore.pendingRevalidates) {
await Promise.all(staticGenerationStore.pendingRevalidates)
}
if (isStaticGeneration) { if (isStaticGeneration) {
const htmlResult = await streamToBufferedResult(renderResult) const htmlResult = await streamToBufferedResult(renderResult)
...@@ -1744,6 +1852,9 @@ export async function renderToHTMLOrFlight( ...@@ -1744,6 +1852,9 @@ export async function renderToHTMLOrFlight(
await generateFlight() await generateFlight()
) )
if (staticGenerationStore?.forceStatic === false) {
staticGenerationStore.fetchRevalidate = 0
}
;(renderOpts as any).pageData = filteredFlightData ;(renderOpts as any).pageData = filteredFlightData
;(renderOpts as any).revalidate = ;(renderOpts as any).revalidate =
typeof staticGenerationStore?.fetchRevalidate === 'undefined' typeof staticGenerationStore?.fetchRevalidate === 'undefined'
...@@ -1760,6 +1871,8 @@ export async function renderToHTMLOrFlight( ...@@ -1760,6 +1871,8 @@ export async function renderToHTMLOrFlight(
isStaticGeneration, isStaticGeneration,
inUse: true, inUse: true,
pathname, pathname,
incrementalCache: renderOpts.incrementalCache,
isRevalidate: renderOpts.isRevalidate,
} }
const tryGetPreviewData = const tryGetPreviewData =
......
...@@ -78,6 +78,7 @@ import { ...@@ -78,6 +78,7 @@ import {
RSC, RSC,
RSC_VARY_HEADER, RSC_VARY_HEADER,
FLIGHT_PARAMETERS, FLIGHT_PARAMETERS,
FETCH_CACHE_HEADER,
} from '../client/components/app-router-headers' } from '../client/components/app-router-headers'
export type FindComponentsResult = { export type FindComponentsResult = {
...@@ -310,6 +311,10 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -310,6 +311,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
res: BaseNextResponse res: BaseNextResponse
): void ): void
protected abstract getIncrementalCache(options: {
requestHeaders: Record<string, undefined | string | string[]>
}): import('./lib/incremental-cache').IncrementalCache
protected abstract getResponseCache(options: { protected abstract getResponseCache(options: {
dev: boolean dev: boolean
}): ResponseCacheBase }): ResponseCacheBase
...@@ -1283,11 +1288,22 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1283,11 +1288,22 @@ export default abstract class Server<ServerOptions extends Options = Options> {
ssgCacheKey = ssgCacheKey =
ssgCacheKey === '/index' && pathname === '/' ? '/' : ssgCacheKey ssgCacheKey === '/index' && pathname === '/' ? '/' : ssgCacheKey
} }
const incrementalCache = this.getIncrementalCache({
requestHeaders: Object.assign({}, req.headers),
})
if (
this.nextConfig.experimental.fetchCache &&
(opts.runtime !== 'experimental-edge' ||
(this.serverOptions as any).webServerConfig)
) {
delete req.headers[FETCH_CACHE_HEADER]
}
let isRevalidate = false
const doRender: () => Promise<ResponseCacheEntry | null> = async () => { const doRender: () => Promise<ResponseCacheEntry | null> = async () => {
let pageData: any let pageData: any
let body: RenderResult | null let body: RenderResult | null
let sprRevalidate: number | false let isrRevalidate: number | false
let isNotFound: boolean | undefined let isNotFound: boolean | undefined
let isRedirect: boolean | undefined let isRedirect: boolean | undefined
...@@ -1312,6 +1328,12 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1312,6 +1328,12 @@ export default abstract class Server<ServerOptions extends Options = Options> {
const renderOpts: RenderOpts = { const renderOpts: RenderOpts = {
...components, ...components,
...opts, ...opts,
...(isAppPath && this.nextConfig.experimental.fetchCache
? {
incrementalCache,
isRevalidate: this.minimalMode || isRevalidate,
}
: {}),
isDataReq, isDataReq,
resolvedUrl, resolvedUrl,
locale, locale,
...@@ -1346,7 +1368,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1346,7 +1368,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
body = renderResult body = renderResult
// TODO: change this to a different passing mechanism // TODO: change this to a different passing mechanism
pageData = (renderOpts as any).pageData pageData = (renderOpts as any).pageData
sprRevalidate = (renderOpts as any).revalidate isrRevalidate = (renderOpts as any).revalidate
isNotFound = (renderOpts as any).isNotFound isNotFound = (renderOpts as any).isNotFound
isRedirect = (renderOpts as any).isRedirect isRedirect = (renderOpts as any).isRedirect
...@@ -1361,7 +1383,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1361,7 +1383,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
} }
value = { kind: 'PAGE', html: body, pageData } value = { kind: 'PAGE', html: body, pageData }
} }
return { revalidate: sprRevalidate, value } return { revalidate: isrRevalidate, value }
} }
const cacheEntry = await this.responseCache.get( const cacheEntry = await this.responseCache.get(
...@@ -1371,6 +1393,10 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1371,6 +1393,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
const isDynamicPathname = isDynamicRoute(pathname) const isDynamicPathname = isDynamicRoute(pathname)
const didRespond = hasResolved || res.sent const didRespond = hasResolved || res.sent
if (hadCache) {
isRevalidate = true
}
if (!staticPaths) { if (!staticPaths) {
;({ staticPaths, fallbackMode } = hasStaticPaths ;({ staticPaths, fallbackMode } = hasStaticPaths
? await this.getStaticPaths({ pathname }) ? await this.getStaticPaths({ pathname })
...@@ -1486,6 +1512,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1486,6 +1512,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
} }
}, },
{ {
incrementalCache,
isManualRevalidate, isManualRevalidate,
isPrefetch: req.headers.purpose === 'prefetch', isPrefetch: req.headers.purpose === 'prefetch',
} }
......
...@@ -277,6 +277,9 @@ const configSchema = { ...@@ -277,6 +277,9 @@ const configSchema = {
fallbackNodePolyfills: { fallbackNodePolyfills: {
type: 'boolean', type: 'boolean',
}, },
fetchCache: {
type: 'boolean',
},
forceSwcTransforms: { forceSwcTransforms: {
type: 'boolean', type: 'boolean',
}, },
......
...@@ -79,6 +79,7 @@ export interface NextJsWebpackConfig { ...@@ -79,6 +79,7 @@ export interface NextJsWebpackConfig {
} }
export interface ExperimentalConfig { export interface ExperimentalConfig {
fetchCache?: boolean
allowMiddlewareResponseBody?: boolean allowMiddlewareResponseBody?: boolean
skipMiddlewareUrlNormalize?: boolean skipMiddlewareUrlNormalize?: boolean
skipTrailingSlashRedirect?: boolean skipTrailingSlashRedirect?: boolean
...@@ -565,6 +566,7 @@ export const defaultConfig: NextConfig = { ...@@ -565,6 +566,7 @@ export const defaultConfig: NextConfig = {
swcMinify: true, swcMinify: true,
output: !!process.env.NEXT_PRIVATE_STANDALONE ? 'standalone' : undefined, output: !!process.env.NEXT_PRIVATE_STANDALONE ? 'standalone' : undefined,
experimental: { experimental: {
fetchCache: false,
middlewarePrefetch: 'flexible', middlewarePrefetch: 'flexible',
optimisticClientCache: true, optimisticClientCache: true,
runtime: undefined, runtime: undefined,
......
import LRUCache from 'next/dist/compiled/lru-cache'
import { FETCH_CACHE_HEADER } from '../../../client/components/app-router-headers'
import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './'
let memoryCache: LRUCache<string, CacheHandlerValue> | undefined
export default class FetchCache implements CacheHandler {
private headers: Record<string, string>
private cacheEndpoint: string
private debug: boolean
constructor(ctx: CacheHandlerContext) {
if (ctx.maxMemoryCacheSize && !memoryCache) {
memoryCache = new LRUCache({
max: ctx.maxMemoryCacheSize,
length({ value }) {
if (!value) {
return 25
} else if (value.kind === 'REDIRECT') {
return JSON.stringify(value.props).length
} else if (value.kind === 'IMAGE') {
throw new Error('invariant image should not be incremental-cache')
} else if (value.kind === 'FETCH') {
return JSON.stringify(value.data || '').length
}
// rough estimate of size of cache value
return (
value.html.length + (JSON.stringify(value.pageData)?.length || 0)
)
},
})
}
this.debug = !!process.env.NEXT_PRIVATE_DEBUG_CACHE
this.headers = {}
this.headers['Content-Type'] = 'application/json'
if (FETCH_CACHE_HEADER in ctx._requestHeaders) {
const newHeaders = JSON.parse(
ctx._requestHeaders[FETCH_CACHE_HEADER] as string
)
for (const k in newHeaders) {
this.headers[k] = newHeaders[k]
}
}
this.cacheEndpoint = `https://${ctx._requestHeaders['x-vercel-sc-host']}${
ctx._requestHeaders['x-vercel-sc-basepath'] || ''
}`
if (this.debug) {
console.log('using cache endpoint', this.cacheEndpoint)
}
}
public async get(key: string, fetchCache?: boolean) {
if (!fetchCache) return null
let data = memoryCache?.get(key)
// get data from fetch cache
if (!data) {
try {
const start = Date.now()
const res = await fetch(
`${this.cacheEndpoint}/v1/suspense-cache/getItems`,
{
method: 'POST',
body: JSON.stringify([key]),
headers: this.headers,
}
)
if (!res.ok) {
console.error(await res.text())
throw new Error(`invalid response from cache ${res.status}`)
}
const items = await res.json()
const item = items[key]
if (!item || !item.value) {
console.log({ item })
throw new Error(`invalid item returned`)
}
const cached = JSON.parse(item.value)
if (!cached || cached.kind !== 'FETCH') {
this.debug && console.log({ cached })
throw new Error(`invalid cache value`)
}
data = {
lastModified: Date.now() - item.age * 1000,
value: cached,
}
if (this.debug) {
console.log(
'got fetch cache entry duration:',
Date.now() - start,
data
)
}
if (data) {
memoryCache?.set(key, data)
}
} catch (err) {
// unable to get data from fetch-cache
console.error(`Failed to get from fetch-cache`, err)
}
}
return data || null
}
public async set(
key: string,
data: CacheHandlerValue['value'],
fetchCache?: boolean
) {
if (!fetchCache) return
memoryCache?.set(key, {
value: data,
lastModified: Date.now(),
})
try {
const start = Date.now()
const body = JSON.stringify([
{
id: key,
value: JSON.stringify(data),
},
])
const res = await fetch(
`${this.cacheEndpoint}/v1/suspense-cache/setItems`,
{
method: 'POST',
headers: this.headers,
body: body,
}
)
if (!res.ok) {
this.debug && console.log(await res.text())
throw new Error(`invalid response ${res.status}`)
}
if (this.debug) {
console.log(
'successfully set to fetch-cache duration:',
Date.now() - start,
body
)
}
} catch (err) {
// unable to set to fetch-cache
console.error(`Failed to update fetch cache`, err)
}
return
}
}
...@@ -2,11 +2,12 @@ import LRUCache from 'next/dist/compiled/lru-cache' ...@@ -2,11 +2,12 @@ import LRUCache from 'next/dist/compiled/lru-cache'
import path from '../../../shared/lib/isomorphic/path' import path from '../../../shared/lib/isomorphic/path'
import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './' import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './'
let memoryCache: LRUCache<string, CacheHandlerValue> | undefined
export default class FileSystemCache implements CacheHandler { export default class FileSystemCache implements CacheHandler {
private fs: CacheHandlerContext['fs'] private fs: CacheHandlerContext['fs']
private flushToDisk?: CacheHandlerContext['flushToDisk'] private flushToDisk?: CacheHandlerContext['flushToDisk']
private serverDistDir: CacheHandlerContext['serverDistDir'] private serverDistDir: CacheHandlerContext['serverDistDir']
private memoryCache?: LRUCache<string, CacheHandlerValue>
private appDir: boolean private appDir: boolean
constructor(ctx: CacheHandlerContext) { constructor(ctx: CacheHandlerContext) {
...@@ -15,8 +16,8 @@ export default class FileSystemCache implements CacheHandler { ...@@ -15,8 +16,8 @@ export default class FileSystemCache implements CacheHandler {
this.serverDistDir = ctx.serverDistDir this.serverDistDir = ctx.serverDistDir
this.appDir = !!ctx._appDir this.appDir = !!ctx._appDir
if (ctx.maxMemoryCacheSize) { if (ctx.maxMemoryCacheSize && !memoryCache) {
this.memoryCache = new LRUCache({ memoryCache = new LRUCache({
max: ctx.maxMemoryCacheSize, max: ctx.maxMemoryCacheSize,
length({ value }) { length({ value }) {
if (!value) { if (!value) {
...@@ -25,6 +26,8 @@ export default class FileSystemCache implements CacheHandler { ...@@ -25,6 +26,8 @@ export default class FileSystemCache implements CacheHandler {
return JSON.stringify(value.props).length return JSON.stringify(value.props).length
} else if (value.kind === 'IMAGE') { } else if (value.kind === 'IMAGE') {
throw new Error('invariant image should not be incremental-cache') throw new Error('invariant image should not be incremental-cache')
} else if (value.kind === 'FETCH') {
return JSON.stringify(value.data || '').length
} }
// rough estimate of size of cache value // rough estimate of size of cache value
return ( return (
...@@ -35,40 +38,55 @@ export default class FileSystemCache implements CacheHandler { ...@@ -35,40 +38,55 @@ export default class FileSystemCache implements CacheHandler {
} }
} }
public async get(key: string) { public async get(key: string, fetchCache?: boolean) {
let data = this.memoryCache?.get(key) let data = memoryCache?.get(key)
// let's check the disk for seed data // let's check the disk for seed data
if (!data) { if (!data) {
try { try {
const { filePath: htmlPath, isAppPath } = await this.getFsPath( const { filePath, isAppPath } = await this.getFsPath({
`${key}.html` pathname: fetchCache ? key : `${key}.html`,
) fetchCache,
const html = await this.fs.readFile(htmlPath) })
const pageData = isAppPath const fileData = await this.fs.readFile(filePath)
? await this.fs.readFile( const { mtime } = await this.fs.stat(filePath)
(
await this.getFsPath(`${key}.rsc`, true) if (fetchCache) {
).filePath const lastModified = mtime.getTime()
) data = {
: JSON.parse( lastModified,
await this.fs.readFile( value: JSON.parse(fileData),
await ( }
await this.getFsPath(`${key}.json`, false) } else {
const pageData = isAppPath
? await this.fs.readFile(
(
await this.getFsPath({ pathname: `${key}.rsc`, appDir: true })
).filePath ).filePath
) )
) : JSON.parse(
const { mtime } = await this.fs.stat(htmlPath) await this.fs.readFile(
await (
await this.getFsPath({
pathname: `${key}.json`,
appDir: false,
})
).filePath
)
)
data = {
lastModified: mtime.getTime(),
value: {
kind: 'PAGE',
html: fileData,
pageData,
},
}
}
data = { if (data) {
lastModified: mtime.getTime(), memoryCache?.set(key, data)
value: {
kind: 'PAGE',
html,
pageData,
},
} }
this.memoryCache?.set(key, data)
} catch (_) { } catch (_) {
// unable to get data from disk // unable to get data from disk
} }
...@@ -77,41 +95,66 @@ export default class FileSystemCache implements CacheHandler { ...@@ -77,41 +95,66 @@ export default class FileSystemCache implements CacheHandler {
} }
public async set(key: string, data: CacheHandlerValue['value']) { public async set(key: string, data: CacheHandlerValue['value']) {
if (!this.flushToDisk) return memoryCache?.set(key, {
this.memoryCache?.set(key, {
value: data, value: data,
lastModified: Date.now(), lastModified: Date.now(),
}) })
if (!this.flushToDisk) return
if (data?.kind === 'PAGE') { if (data?.kind === 'PAGE') {
const isAppPath = typeof data.pageData === 'string' const isAppPath = typeof data.pageData === 'string'
const { filePath: htmlPath } = await this.getFsPath( const { filePath: htmlPath } = await this.getFsPath({
`${key}.html`, pathname: `${key}.html`,
isAppPath appDir: isAppPath,
) })
await this.fs.mkdir(path.dirname(htmlPath)) await this.fs.mkdir(path.dirname(htmlPath))
await this.fs.writeFile(htmlPath, data.html) await this.fs.writeFile(htmlPath, data.html)
await this.fs.writeFile( await this.fs.writeFile(
( (
await this.getFsPath( await this.getFsPath({
`${key}.${isAppPath ? 'rsc' : 'json'}`, pathname: `${key}.${isAppPath ? 'rsc' : 'json'}`,
isAppPath appDir: isAppPath,
) })
).filePath, ).filePath,
isAppPath ? data.pageData : JSON.stringify(data.pageData) isAppPath ? data.pageData : JSON.stringify(data.pageData)
) )
} else if (data?.kind === 'FETCH') {
const { filePath } = await this.getFsPath({
pathname: key,
fetchCache: true,
})
await this.fs.mkdir(path.dirname(filePath))
await this.fs.writeFile(filePath, JSON.stringify(data))
} }
} }
private async getFsPath( private async getFsPath({
pathname: string, pathname,
appDir,
fetchCache,
}: {
pathname: string
appDir?: boolean appDir?: boolean
): Promise<{ fetchCache?: boolean
}): Promise<{
filePath: string filePath: string
isAppPath: boolean isAppPath: boolean
}> { }> {
if (fetchCache) {
// we store in .next/cache/fetch-cache so it can be persisted
// across deploys
return {
filePath: path.join(
this.serverDistDir,
'..',
'cache',
'fetch-cache',
pathname
),
isAppPath: false,
}
}
let isAppPath = false let isAppPath = false
let filePath = path.join(this.serverDistDir, 'pages', pathname) let filePath = path.join(this.serverDistDir, 'pages', pathname)
......
...@@ -3,6 +3,7 @@ import FileSystemCache from './file-system-cache' ...@@ -3,6 +3,7 @@ import FileSystemCache from './file-system-cache'
import { PrerenderManifest } from '../../../build' import { PrerenderManifest } from '../../../build'
import path from '../../../shared/lib/isomorphic/path' import path from '../../../shared/lib/isomorphic/path'
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path' import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path'
import FetchCache from './fetch-cache'
import { import {
IncrementalCacheValue, IncrementalCacheValue,
IncrementalCacheEntry, IncrementalCacheEntry,
...@@ -18,7 +19,8 @@ export interface CacheHandlerContext { ...@@ -18,7 +19,8 @@ export interface CacheHandlerContext {
flushToDisk?: boolean flushToDisk?: boolean
serverDistDir: string serverDistDir: string
maxMemoryCacheSize?: number maxMemoryCacheSize?: number
_appDir?: boolean _appDir: boolean
_requestHeaders: IncrementalCache['requestHeaders']
} }
export interface CacheHandlerValue { export interface CacheHandlerValue {
...@@ -30,13 +32,17 @@ export class CacheHandler { ...@@ -30,13 +32,17 @@ export class CacheHandler {
// eslint-disable-next-line // eslint-disable-next-line
constructor(_ctx: CacheHandlerContext) {} constructor(_ctx: CacheHandlerContext) {}
public async get(_key: string): Promise<CacheHandlerValue | null> { public async get(
_key: string,
_fetchCache?: boolean
): Promise<CacheHandlerValue | null> {
return {} as any return {} as any
} }
public async set( public async set(
_key: string, _key: string,
_data: IncrementalCacheValue | null _data: IncrementalCacheValue | null,
_fetchCache?: boolean
): Promise<void> {} ): Promise<void> {}
} }
...@@ -44,13 +50,18 @@ export class IncrementalCache { ...@@ -44,13 +50,18 @@ export class IncrementalCache {
dev?: boolean dev?: boolean
cacheHandler: CacheHandler cacheHandler: CacheHandler
prerenderManifest: PrerenderManifest prerenderManifest: PrerenderManifest
requestHeaders: Record<string, undefined | string | string[]>
minimalMode?: boolean
constructor({ constructor({
fs, fs,
dev, dev,
appDir, appDir,
flushToDisk, flushToDisk,
fetchCache,
minimalMode,
serverDistDir, serverDistDir,
requestHeaders,
maxMemoryCacheSize, maxMemoryCacheSize,
getPrerenderManifest, getPrerenderManifest,
incrementalCacheHandlerPath, incrementalCacheHandlerPath,
...@@ -58,8 +69,11 @@ export class IncrementalCache { ...@@ -58,8 +69,11 @@ export class IncrementalCache {
fs: CacheFs fs: CacheFs
dev: boolean dev: boolean
appDir?: boolean appDir?: boolean
fetchCache?: boolean
minimalMode?: boolean
serverDistDir: string serverDistDir: string
flushToDisk?: boolean flushToDisk?: boolean
requestHeaders: IncrementalCache['requestHeaders']
maxMemoryCacheSize?: number maxMemoryCacheSize?: number
incrementalCacheHandlerPath?: string incrementalCacheHandlerPath?: string
getPrerenderManifest: () => PrerenderManifest getPrerenderManifest: () => PrerenderManifest
...@@ -71,11 +85,17 @@ export class IncrementalCache { ...@@ -71,11 +85,17 @@ export class IncrementalCache {
cacheHandlerMod = cacheHandlerMod.default || cacheHandlerMod cacheHandlerMod = cacheHandlerMod.default || cacheHandlerMod
} }
if (minimalMode && fetchCache) {
cacheHandlerMod = FetchCache
}
if (process.env.__NEXT_TEST_MAX_ISR_CACHE) { if (process.env.__NEXT_TEST_MAX_ISR_CACHE) {
// Allow cache size to be overridden for testing purposes // Allow cache size to be overridden for testing purposes
maxMemoryCacheSize = parseInt(process.env.__NEXT_TEST_MAX_ISR_CACHE, 10) maxMemoryCacheSize = parseInt(process.env.__NEXT_TEST_MAX_ISR_CACHE, 10)
} }
this.dev = dev this.dev = dev
this.minimalMode = minimalMode
this.requestHeaders = requestHeaders
this.prerenderManifest = getPrerenderManifest() this.prerenderManifest = getPrerenderManifest()
this.cacheHandler = new (cacheHandlerMod as typeof CacheHandler)({ this.cacheHandler = new (cacheHandlerMod as typeof CacheHandler)({
dev, dev,
...@@ -83,7 +103,8 @@ export class IncrementalCache { ...@@ -83,7 +103,8 @@ export class IncrementalCache {
flushToDisk, flushToDisk,
serverDistDir, serverDistDir,
maxMemoryCacheSize, maxMemoryCacheSize,
_appDir: appDir, _appDir: !!appDir,
_requestHeaders: requestHeaders,
}) })
} }
...@@ -110,19 +131,75 @@ export class IncrementalCache { ...@@ -110,19 +131,75 @@ export class IncrementalCache {
return revalidateAfter return revalidateAfter
} }
_getPathname(pathname: string) { _getPathname(pathname: string, fetchCache?: boolean) {
return normalizePagePath(pathname) return fetchCache ? pathname : normalizePagePath(pathname)
}
// x-ref: https://github.com/facebook/react/blob/2655c9354d8e1c54ba888444220f63e836925caa/packages/react/src/ReactFetch.js#L23
async fetchCacheKey(url: string, init: RequestInit = {}): Promise<string> {
const cacheString = JSON.stringify([
url,
init.method,
init.headers,
init.mode,
init.redirect,
init.credentials,
init.referrer,
init.referrerPolicy,
init.integrity,
init.next,
init.cache,
])
let cacheKey: string
if (process.env.NEXT_RUNTIME === 'edge') {
function bufferToHex(buffer: ArrayBuffer): string {
return Array.prototype.map
.call(new Uint8Array(buffer), (b) => b.toString(16).padStart(2, '0'))
.join('')
}
const buffer = new TextEncoder().encode(cacheString)
cacheKey = bufferToHex(await crypto.subtle.digest('SHA-256', buffer))
} else {
const crypto = require('crypto') as typeof import('crypto')
cacheKey = crypto.createHash('sha256').update(cacheString).digest('hex')
}
return cacheKey
} }
// get data from cache if available // get data from cache if available
async get(pathname: string): Promise<IncrementalCacheEntry | null> { async get(
pathname: string,
fetchCache?: boolean
): Promise<IncrementalCacheEntry | null> {
// we don't leverage the prerender cache in dev mode // we don't leverage the prerender cache in dev mode
// so that getStaticProps is always called for easier debugging // so that getStaticProps is always called for easier debugging
if (this.dev) return null if (this.dev) return null
pathname = this._getPathname(pathname) pathname = this._getPathname(pathname, fetchCache)
let entry: IncrementalCacheEntry | null = null let entry: IncrementalCacheEntry | null = null
const cacheData = await this.cacheHandler.get(pathname) const cacheData = await this.cacheHandler.get(pathname, fetchCache)
if (cacheData?.value?.kind === 'FETCH') {
const data = cacheData.value.data
const age = Math.round(
(Date.now() - (cacheData.lastModified || 0)) / 1000
)
const revalidate = cacheData.value.revalidate
return {
isStale: age > revalidate,
value: {
kind: 'FETCH',
data,
age,
revalidate,
isStale: age > revalidate,
},
revalidateAfter:
(cacheData.lastModified || Date.now()) + revalidate * 1000,
}
}
const curRevalidate = const curRevalidate =
this.prerenderManifest.routes[toRoute(pathname)]?.initialRevalidateSeconds this.prerenderManifest.routes[toRoute(pathname)]?.initialRevalidateSeconds
...@@ -159,7 +236,7 @@ export class IncrementalCache { ...@@ -159,7 +236,7 @@ export class IncrementalCache {
curRevalidate, curRevalidate,
revalidateAfter, revalidateAfter,
} }
this.set(pathname, entry.value, curRevalidate) this.set(pathname, entry.value, curRevalidate, fetchCache)
} }
return entry return entry
} }
...@@ -168,16 +245,17 @@ export class IncrementalCache { ...@@ -168,16 +245,17 @@ export class IncrementalCache {
async set( async set(
pathname: string, pathname: string,
data: IncrementalCacheValue | null, data: IncrementalCacheValue | null,
revalidateSeconds?: number | false revalidateSeconds?: number | false,
fetchCache?: boolean
) { ) {
if (this.dev) return if (this.dev) return
pathname = this._getPathname(pathname) pathname = this._getPathname(pathname, fetchCache)
try { try {
// we use the prerender manifest memory instance // we use the prerender manifest memory instance
// to store revalidate timings for calculating // to store revalidate timings for calculating
// revalidateAfter values so we update this on set // revalidateAfter values so we update this on set
if (typeof revalidateSeconds !== 'undefined') { if (typeof revalidateSeconds !== 'undefined' && !fetchCache) {
this.prerenderManifest.routes[pathname] = { this.prerenderManifest.routes[pathname] = {
dataRoute: path.posix.join( dataRoute: path.posix.join(
'/_next/data', '/_next/data',
...@@ -187,7 +265,7 @@ export class IncrementalCache { ...@@ -187,7 +265,7 @@ export class IncrementalCache {
initialRevalidateSeconds: revalidateSeconds, initialRevalidateSeconds: revalidateSeconds,
} }
} }
await this.cacheHandler.set(pathname, data) await this.cacheHandler.set(pathname, data, fetchCache)
} catch (error) { } catch (error) {
console.warn('Failed to update prerender cache for', pathname, error) console.warn('Failed to update prerender cache for', pathname, error)
} }
......
...@@ -228,15 +228,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -228,15 +228,7 @@ export default class NextNodeServer extends BaseServer {
} }
if (!this.minimalMode) { if (!this.minimalMode) {
const { ImageOptimizerCache } = this.imageResponseCache = new ResponseCache(this.minimalMode)
require('./image-optimizer') as typeof import('./image-optimizer')
this.imageResponseCache = new ResponseCache(
new ImageOptimizerCache({
distDir: this.distDir,
nextConfig: this.nextConfig,
}),
this.minimalMode
)
} }
if (!options.dev) { if (!options.dev) {
...@@ -278,12 +270,23 @@ export default class NextNodeServer extends BaseServer { ...@@ -278,12 +270,23 @@ export default class NextNodeServer extends BaseServer {
loadEnvConfig(this.dir, dev, Log, forceReload) loadEnvConfig(this.dir, dev, Log, forceReload)
} }
protected getResponseCache({ dev }: { dev: boolean }) { protected getIncrementalCache({
const incrementalCache = new IncrementalCache({ requestHeaders,
}: {
requestHeaders: IncrementalCache['requestHeaders']
}) {
const dev = !!this.renderOpts.dev
// incremental-cache is request specific with a shared
// although can have shared caches in module scope
// per-cache handler
return new IncrementalCache({
fs: this.getCacheFilesystem(), fs: this.getCacheFilesystem(),
dev, dev,
serverDistDir: this.serverDistDir, requestHeaders,
appDir: this.hasAppDir, appDir: this.hasAppDir,
minimalMode: this.minimalMode,
serverDistDir: this.serverDistDir,
fetchCache: this.nextConfig.experimental.fetchCache,
maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize, maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize,
flushToDisk: flushToDisk:
!this.minimalMode && this.nextConfig.experimental.isrFlushToDisk, !this.minimalMode && this.nextConfig.experimental.isrFlushToDisk,
...@@ -303,8 +306,10 @@ export default class NextNodeServer extends BaseServer { ...@@ -303,8 +306,10 @@ export default class NextNodeServer extends BaseServer {
} }
}, },
}) })
}
return new ResponseCache(incrementalCache, this.minimalMode) protected getResponseCache() {
return new ResponseCache(this.minimalMode)
} }
protected getPublicDir(): string { protected getPublicDir(): string {
...@@ -383,7 +388,15 @@ export default class NextNodeServer extends BaseServer { ...@@ -383,7 +388,15 @@ export default class NextNodeServer extends BaseServer {
finished: true, finished: true,
} }
} }
const { getHash, ImageOptimizerCache, sendResponse, ImageError } = const { ImageOptimizerCache } =
require('./image-optimizer') as typeof import('./image-optimizer')
const imageOptimizerCache = new ImageOptimizerCache({
distDir: this.distDir,
nextConfig: this.nextConfig,
})
const { getHash, sendResponse, ImageError } =
require('./image-optimizer') as typeof import('./image-optimizer') require('./image-optimizer') as typeof import('./image-optimizer')
if (!this.imageResponseCache) { if (!this.imageResponseCache) {
...@@ -391,7 +404,6 @@ export default class NextNodeServer extends BaseServer { ...@@ -391,7 +404,6 @@ export default class NextNodeServer extends BaseServer {
'invariant image optimizer cache was not initialized' 'invariant image optimizer cache was not initialized'
) )
} }
const imagesConfig = this.nextConfig.images const imagesConfig = this.nextConfig.images
if (imagesConfig.loader !== 'default') { if (imagesConfig.loader !== 'default') {
...@@ -434,7 +446,9 @@ export default class NextNodeServer extends BaseServer { ...@@ -434,7 +446,9 @@ export default class NextNodeServer extends BaseServer {
revalidate: maxAge, revalidate: maxAge,
} }
}, },
{} {
incrementalCache: imageOptimizerCache,
}
) )
if (cacheEntry?.value?.kind !== 'IMAGE') { if (cacheEntry?.value?.kind !== 'IMAGE') {
......
...@@ -10,7 +10,6 @@ import RenderResult from '../render-result' ...@@ -10,7 +10,6 @@ import RenderResult from '../render-result'
export * from './types' export * from './types'
export default class ResponseCache { export default class ResponseCache {
incrementalCache: IncrementalCache
pendingResponses: Map<string, Promise<ResponseCacheEntry | null>> pendingResponses: Map<string, Promise<ResponseCacheEntry | null>>
previousCacheItem?: { previousCacheItem?: {
key: string key: string
...@@ -19,8 +18,7 @@ export default class ResponseCache { ...@@ -19,8 +18,7 @@ export default class ResponseCache {
} }
minimalMode?: boolean minimalMode?: boolean
constructor(incrementalCache: IncrementalCache, minimalMode: boolean) { constructor(minimalMode: boolean) {
this.incrementalCache = incrementalCache
this.pendingResponses = new Map() this.pendingResponses = new Map()
this.minimalMode = minimalMode this.minimalMode = minimalMode
} }
...@@ -31,8 +29,10 @@ export default class ResponseCache { ...@@ -31,8 +29,10 @@ export default class ResponseCache {
context: { context: {
isManualRevalidate?: boolean isManualRevalidate?: boolean
isPrefetch?: boolean isPrefetch?: boolean
incrementalCache: IncrementalCache
} }
): Promise<ResponseCacheEntry | null> { ): Promise<ResponseCacheEntry | null> {
const { incrementalCache } = context
// ensure manual revalidate doesn't block normal requests // ensure manual revalidate doesn't block normal requests
const pendingResponseKey = key const pendingResponseKey = key
? `${key}-${context.isManualRevalidate ? '1' : '0'}` ? `${key}-${context.isManualRevalidate ? '1' : '0'}`
...@@ -41,6 +41,7 @@ export default class ResponseCache { ...@@ -41,6 +41,7 @@ export default class ResponseCache {
const pendingResponse = pendingResponseKey const pendingResponse = pendingResponseKey
? this.pendingResponses.get(pendingResponseKey) ? this.pendingResponses.get(pendingResponseKey)
: null : null
if (pendingResponse) { if (pendingResponse) {
return pendingResponse return pendingResponse
} }
...@@ -92,9 +93,15 @@ export default class ResponseCache { ...@@ -92,9 +93,15 @@ export default class ResponseCache {
let cachedResponse: IncrementalCacheItem = null let cachedResponse: IncrementalCacheItem = null
try { try {
cachedResponse = cachedResponse =
key && !this.minimalMode ? await this.incrementalCache.get(key) : null key && !this.minimalMode ? await incrementalCache.get(key) : null
if (cachedResponse && !context.isManualRevalidate) { if (cachedResponse && !context.isManualRevalidate) {
if (cachedResponse.value?.kind === 'FETCH') {
throw new Error(
`invariant: unexpected cachedResponse of kind fetch in response cache`
)
}
resolve({ resolve({
isStale: cachedResponse.isStale, isStale: cachedResponse.isStale,
revalidate: cachedResponse.curRevalidate, revalidate: cachedResponse.curRevalidate,
...@@ -136,7 +143,7 @@ export default class ResponseCache { ...@@ -136,7 +143,7 @@ export default class ResponseCache {
expiresAt: Date.now() + 1000, expiresAt: Date.now() + 1000,
} }
} else { } else {
await this.incrementalCache.set( await incrementalCache.set(
key, key,
cacheEntry.value?.kind === 'PAGE' cacheEntry.value?.kind === 'PAGE'
? { ? {
...@@ -159,7 +166,7 @@ export default class ResponseCache { ...@@ -159,7 +166,7 @@ export default class ResponseCache {
// when a getStaticProps path is erroring we automatically re-set the // when a getStaticProps path is erroring we automatically re-set the
// existing cache under a new expiration to prevent non-stop retrying // existing cache under a new expiration to prevent non-stop retrying
if (cachedResponse && key) { if (cachedResponse && key) {
await this.incrementalCache.set( await incrementalCache.set(
key, key,
cachedResponse.value, cachedResponse.value,
Math.min(Math.max(cachedResponse.revalidate || 3, 3), 30) Math.min(Math.max(cachedResponse.revalidate || 3, 3), 30)
......
...@@ -7,10 +7,19 @@ export interface ResponseCacheBase { ...@@ -7,10 +7,19 @@ export interface ResponseCacheBase {
context: { context: {
isManualRevalidate?: boolean isManualRevalidate?: boolean
isPrefetch?: boolean isPrefetch?: boolean
incrementalCache: IncrementalCache
} }
): Promise<ResponseCacheEntry | null> ): Promise<ResponseCacheEntry | null>
} }
export interface CachedFetchValue {
kind: 'FETCH'
data: any
isStale: boolean
age: number
revalidate: number
}
export interface CachedRedirectValue { export interface CachedRedirectValue {
kind: 'REDIRECT' kind: 'REDIRECT'
props: Object props: Object
...@@ -53,6 +62,7 @@ export type IncrementalCacheValue = ...@@ -53,6 +62,7 @@ export type IncrementalCacheValue =
| CachedRedirectValue | CachedRedirectValue
| IncrementalCachedPageValue | IncrementalCachedPageValue
| CachedImageValue | CachedImageValue
| CachedFetchValue
export type ResponseCacheValue = export type ResponseCacheValue =
| CachedRedirectValue | CachedRedirectValue
......
...@@ -24,6 +24,7 @@ export default class WebResponseCache { ...@@ -24,6 +24,7 @@ export default class WebResponseCache {
context: { context: {
isManualRevalidate?: boolean isManualRevalidate?: boolean
isPrefetch?: boolean isPrefetch?: boolean
incrementalCache: any
} }
): Promise<ResponseCacheEntry | null> { ): Promise<ResponseCacheEntry | null> {
// ensure manual revalidate doesn't block normal requests // ensure manual revalidate doesn't block normal requests
......
...@@ -54,6 +54,9 @@ export default class NextWebServer extends BaseServer<WebServerOptions> { ...@@ -54,6 +54,9 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
// For the web server layer, compression is automatically handled by the // For the web server layer, compression is automatically handled by the
// upstream proxy (edge runtime or node server) and we can simply skip here. // upstream proxy (edge runtime or node server) and we can simply skip here.
} }
protected getIncrementalCache() {
return {} as any
}
protected getResponseCache() { protected getResponseCache() {
return new WebResponseCache(this.minimalMode) return new WebResponseCache(this.minimalMode)
} }
......
...@@ -11,6 +11,7 @@ import { NextURL } from './next-url' ...@@ -11,6 +11,7 @@ import { NextURL } from './next-url'
import { stripInternalSearchParams } from '../internal-utils' import { stripInternalSearchParams } from '../internal-utils'
import { normalizeRscPath } from '../../shared/lib/router/utils/app-paths' import { normalizeRscPath } from '../../shared/lib/router/utils/app-paths'
import { import {
FETCH_CACHE_HEADER,
NEXT_ROUTER_PREFETCH, NEXT_ROUTER_PREFETCH,
NEXT_ROUTER_STATE_TREE, NEXT_ROUTER_STATE_TREE,
RSC, RSC,
...@@ -45,6 +46,7 @@ const FLIGHT_PARAMETERS = [ ...@@ -45,6 +46,7 @@ const FLIGHT_PARAMETERS = [
[RSC], [RSC],
[NEXT_ROUTER_STATE_TREE], [NEXT_ROUTER_STATE_TREE],
[NEXT_ROUTER_PREFETCH], [NEXT_ROUTER_PREFETCH],
[FETCH_CACHE_HEADER],
] as const ] as const
export async function adapter(params: { export async function adapter(params: {
......
...@@ -39,7 +39,7 @@ interface Window { ...@@ -39,7 +39,7 @@ interface Window {
} }
interface NextFetchRequestConfig { interface NextFetchRequestConfig {
revalidate?: number revalidate?: number | false
} }
interface RequestInit { interface RequestInit {
......
...@@ -76,6 +76,10 @@ describe('app-dir static/dynamic handling', () => { ...@@ -76,6 +76,10 @@ describe('app-dir static/dynamic handling', () => {
'ssr-auto/cache-no-store/page.js', 'ssr-auto/cache-no-store/page.js',
'ssr-auto/fetch-revalidate-zero/page.js', 'ssr-auto/fetch-revalidate-zero/page.js',
'ssr-forced/page.js', 'ssr-forced/page.js',
'variable-revalidate/no-store/page.js',
'variable-revalidate/revalidate-3.html',
'variable-revalidate/revalidate-3.rsc',
'variable-revalidate/revalidate-3/page.js',
]) ])
}) })
...@@ -167,6 +171,11 @@ describe('app-dir static/dynamic handling', () => { ...@@ -167,6 +171,11 @@ describe('app-dir static/dynamic handling', () => {
initialRevalidateSeconds: false, initialRevalidateSeconds: false,
srcRoute: '/ssg-preview/[[...route]]', srcRoute: '/ssg-preview/[[...route]]',
}, },
'/variable-revalidate/revalidate-3': {
dataRoute: '/variable-revalidate/revalidate-3.rsc',
initialRevalidateSeconds: 3,
srcRoute: '/variable-revalidate/revalidate-3',
},
}) })
expect(manifest.dynamicRoutes).toEqual({ expect(manifest.dynamicRoutes).toEqual({
'/blog/[author]/[slug]': { '/blog/[author]/[slug]': {
......
...@@ -19,6 +19,3 @@ export default function Page() { ...@@ -19,6 +19,3 @@ export default function Page() {
</> </>
) )
} }
// TODO-APP: remove revalidate config once next.revalidate is supported
export const revalidate = 0
import { cache, use } from 'react'
export default function Layout({ children }) {
const getData = cache(() =>
fetch('https://next-data-api-endpoint.vercel.app/api/random?layout', {
next: { revalidate: 10 },
}).then((res) => res.text())
)
const dataPromise = getData()
const data = use(dataPromise)
return (
<>
<p id="layout-data">revalidate 10: {data}</p>
{children}
</>
)
}
import { cache, use } from 'react'
export default function Page() {
const getData = cache(() =>
fetch('https://next-data-api-endpoint.vercel.app/api/random?page', {
cache: 'no-store',
}).then((res) => res.text())
)
const dataPromise = getData()
const data = use(dataPromise)
return (
<>
<p id="page">/variable-revalidate/no-cache</p>
<p id="page-data">no-store: {data}</p>
<p id="now">{Date.now()}</p>
</>
)
}
import { cache, use } from 'react'
export default function Page() {
const getData = cache(() =>
fetch('https://next-data-api-endpoint.vercel.app/api/random?page', {
next: { revalidate: 3 },
}).then((res) => res.text())
)
const dataPromise = getData()
const data = use(dataPromise)
return (
<>
<p id="page">/variable-revalidate/revalidate-3</p>
<p id="page-data">revalidate 3: {data}</p>
<p id="now">{Date.now()}</p>
</>
)
}
module.exports = { module.exports = {
experimental: { experimental: {
appDir: true, appDir: true,
fetchCache: true,
}, },
// assetPrefix: '/assets', // assetPrefix: '/assets',
rewrites: async () => { rewrites: async () => {
......
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