Unverified Commit 2eef7754 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update tag handling for app cache (#53321)

Optimizes how we handle cache tags for soft tags (auto-added by Next.js)
and normal tags (added manually) and adds differentiating between
`revalidatePath('/blog/first')` and page/layout.

Soft tags are not stored across cache entry and instead auto sent along
when checking cache entries. This allows us to prevent storing
exponential amounts of tags across cache entries while still having the
relationship between them so that single path revalidation can work
properly.

x-ref: [slack
thread](https://vercel.slack.com/archives/C042LHPJ1NX/p1690586837903309)
parent acbbf14a
...@@ -1241,7 +1241,7 @@ export async function buildAppStaticPaths({ ...@@ -1241,7 +1241,7 @@ export async function buildAppStaticPaths({
return StaticGenerationAsyncStorageWrapper.wrap( return StaticGenerationAsyncStorageWrapper.wrap(
staticGenerationAsyncStorage, staticGenerationAsyncStorage,
{ {
pathname: page, urlPathname: page,
renderOpts: { renderOpts: {
originalPathname: page, originalPathname: page,
incrementalCache, incrementalCache,
......
...@@ -4,7 +4,6 @@ export const ACTION = 'Next-Action' as const ...@@ -4,7 +4,6 @@ export const ACTION = 'Next-Action' 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 NEXT_URL = 'Next-Url' as const export const NEXT_URL = 'Next-Url' as const
export const FETCH_CACHE_HEADER = 'x-vercel-sc-headers' as const
export const RSC_CONTENT_TYPE_HEADER = 'text/x-component' as const export const RSC_CONTENT_TYPE_HEADER = 'text/x-component' as const
export const RSC_VARY_HEADER = export const RSC_VARY_HEADER =
`${RSC}, ${NEXT_ROUTER_STATE_TREE}, ${NEXT_ROUTER_PREFETCH}, ${NEXT_URL}` as const `${RSC}, ${NEXT_ROUTER_STATE_TREE}, ${NEXT_ROUTER_PREFETCH}, ${NEXT_URL}` as const
......
...@@ -5,8 +5,8 @@ import { createAsyncLocalStorage } from './async-local-storage' ...@@ -5,8 +5,8 @@ import { createAsyncLocalStorage } from './async-local-storage'
export interface StaticGenerationStore { export interface StaticGenerationStore {
readonly isStaticGeneration: boolean readonly isStaticGeneration: boolean
readonly pathname: string readonly pagePath?: string
readonly originalPathname?: string readonly urlPathname: string
readonly incrementalCache?: IncrementalCache readonly incrementalCache?: IncrementalCache
readonly isOnDemandRevalidate?: boolean readonly isOnDemandRevalidate?: boolean
readonly isPrerendering?: boolean readonly isPrerendering?: boolean
......
...@@ -25,7 +25,10 @@ import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic' ...@@ -25,7 +25,10 @@ import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic'
import { getRouteMatcher } from '../shared/lib/router/utils/route-matcher' import { getRouteMatcher } from '../shared/lib/router/utils/route-matcher'
import { getRouteRegex } from '../shared/lib/router/utils/route-regex' import { getRouteRegex } from '../shared/lib/router/utils/route-regex'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path' import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { SERVER_PROPS_EXPORT_ERROR } from '../lib/constants' import {
NEXT_CACHE_TAGS_HEADER,
SERVER_PROPS_EXPORT_ERROR,
} from '../lib/constants'
import { requireFontManifest } from '../server/require' import { requireFontManifest } from '../server/require'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path' import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { trace } from '../trace' import { trace } from '../trace'
...@@ -499,7 +502,7 @@ export default async function exportPage({ ...@@ -499,7 +502,7 @@ export default async function exportPage({
.fetchTags .fetchTags
if (cacheTags) { if (cacheTags) {
headers['x-next-cache-tags'] = cacheTags headers[NEXT_CACHE_TAGS_HEADER] = cacheTags
} }
if (!headers['content-type'] && body.type) { if (!headers['content-type'] && body.type) {
...@@ -554,7 +557,7 @@ export default async function exportPage({ ...@@ -554,7 +557,7 @@ export default async function exportPage({
const cacheTags = (curRenderOpts as any).fetchTags const cacheTags = (curRenderOpts as any).fetchTags
const headers = cacheTags const headers = cacheTags
? { ? {
'x-next-cache-tags': cacheTags, [NEXT_CACHE_TAGS_HEADER]: cacheTags,
} }
: undefined : undefined
......
...@@ -6,6 +6,14 @@ export const PRERENDER_REVALIDATE_HEADER = 'x-prerender-revalidate' ...@@ -6,6 +6,14 @@ export const PRERENDER_REVALIDATE_HEADER = 'x-prerender-revalidate'
export const PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER = export const PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER =
'x-prerender-revalidate-if-generated' 'x-prerender-revalidate-if-generated'
export const NEXT_CACHE_TAGS_HEADER = 'x-next-cache-tags'
export const NEXT_CACHE_SOFT_TAGS_HEADER = 'x-next-cache-soft-tags'
export const NEXT_CACHE_REVALIDATED_TAGS_HEADER = 'x-next-revalidated-tags'
export const NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER =
'x-next-revalidate-tag-token'
export const NEXT_CACHE_IMPLICIT_TAG_ID = '_N_T_'
// in seconds // in seconds
export const CACHE_ONE_YEAR = 31536000 export const CACHE_ONE_YEAR = 31536000
......
...@@ -32,6 +32,10 @@ import { ...@@ -32,6 +32,10 @@ import {
getModifiedCookieValues, getModifiedCookieValues,
} from '../web/spec-extension/adapters/request-cookies' } from '../web/spec-extension/adapters/request-cookies'
import { RequestStore } from '../../client/components/request-async-storage' import { RequestStore } from '../../client/components/request-async-storage'
import {
NEXT_CACHE_REVALIDATED_TAGS_HEADER,
NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER,
} from '../../lib/constants'
function nodeToWebReadableStream(nodeReadable: import('stream').Readable) { function nodeToWebReadableStream(nodeReadable: import('stream').Readable) {
if (process.env.NEXT_RUNTIME !== 'edge') { if (process.env.NEXT_RUNTIME !== 'edge') {
...@@ -178,11 +182,11 @@ async function createRedirectRenderResult( ...@@ -178,11 +182,11 @@ async function createRedirectRenderResult(
if (staticGenerationStore.revalidatedTags) { if (staticGenerationStore.revalidatedTags) {
forwardedHeaders.set( forwardedHeaders.set(
'x-next-revalidated-tags', NEXT_CACHE_REVALIDATED_TAGS_HEADER,
staticGenerationStore.revalidatedTags.join(',') staticGenerationStore.revalidatedTags.join(',')
) )
forwardedHeaders.set( forwardedHeaders.set(
'x-next-revalidate-tag-token', NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER,
staticGenerationStore.incrementalCache?.prerenderManifest?.preview staticGenerationStore.incrementalCache?.prerenderManifest?.preview
?.previewModeId || '' ?.previewModeId || ''
) )
......
...@@ -1786,7 +1786,7 @@ export async function renderToHTMLOrFlight( ...@@ -1786,7 +1786,7 @@ export async function renderToHTMLOrFlight(
() => () =>
StaticGenerationAsyncStorageWrapper.wrap( StaticGenerationAsyncStorageWrapper.wrap(
staticGenerationAsyncStorage, staticGenerationAsyncStorage,
{ pathname: pagePath, renderOpts }, { urlPathname: pathname, renderOpts },
() => wrappedRender() () => wrappedRender()
) )
) )
......
...@@ -4,7 +4,7 @@ import type { AsyncLocalStorage } from 'async_hooks' ...@@ -4,7 +4,7 @@ import type { AsyncLocalStorage } from 'async_hooks'
import type { IncrementalCache } from '../lib/incremental-cache' import type { IncrementalCache } from '../lib/incremental-cache'
export type StaticGenerationContext = { export type StaticGenerationContext = {
pathname: string urlPathname: string
renderOpts: { renderOpts: {
originalPathname?: string originalPathname?: string
incrementalCache?: IncrementalCache incrementalCache?: IncrementalCache
...@@ -34,7 +34,7 @@ export const StaticGenerationAsyncStorageWrapper: AsyncStorageWrapper< ...@@ -34,7 +34,7 @@ export const StaticGenerationAsyncStorageWrapper: AsyncStorageWrapper<
> = { > = {
wrap<Result>( wrap<Result>(
storage: AsyncLocalStorage<StaticGenerationStore>, storage: AsyncLocalStorage<StaticGenerationStore>,
{ pathname, renderOpts }: StaticGenerationContext, { urlPathname, renderOpts }: StaticGenerationContext,
callback: (store: StaticGenerationStore) => Result callback: (store: StaticGenerationStore) => Result
): Result { ): Result {
/** /**
...@@ -57,8 +57,8 @@ export const StaticGenerationAsyncStorageWrapper: AsyncStorageWrapper< ...@@ -57,8 +57,8 @@ export const StaticGenerationAsyncStorageWrapper: AsyncStorageWrapper<
const store: StaticGenerationStore = { const store: StaticGenerationStore = {
isStaticGeneration, isStaticGeneration,
pathname, urlPathname,
originalPathname: renderOpts.originalPathname, pagePath: renderOpts.originalPathname,
incrementalCache: incrementalCache:
// we fallback to a global incremental cache for edge-runtime locally // we fallback to a global incremental cache for edge-runtime locally
// so that it can access the fs cache without mocks // so that it can access the fs cache without mocks
......
...@@ -113,7 +113,10 @@ import { ...@@ -113,7 +113,10 @@ import {
fromNodeOutgoingHttpHeaders, fromNodeOutgoingHttpHeaders,
toNodeOutgoingHttpHeaders, toNodeOutgoingHttpHeaders,
} from './web/utils' } from './web/utils'
import { NEXT_QUERY_PARAM_PREFIX } from '../lib/constants' import {
NEXT_CACHE_TAGS_HEADER,
NEXT_QUERY_PARAM_PREFIX,
} from '../lib/constants'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path' import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { import {
NextRequestAdapter, NextRequestAdapter,
...@@ -1997,7 +2000,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1997,7 +2000,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
headers = toNodeOutgoingHttpHeaders(response.headers) headers = toNodeOutgoingHttpHeaders(response.headers)
if (cacheTags) { if (cacheTags) {
headers['x-next-cache-tags'] = cacheTags headers[NEXT_CACHE_TAGS_HEADER] = cacheTags
} }
if (!headers['content-type'] && blob.type) { if (!headers['content-type'] && blob.type) {
...@@ -2116,7 +2119,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -2116,7 +2119,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
const cacheTags = (renderOpts as any).fetchTags const cacheTags = (renderOpts as any).fetchTags
if (cacheTags) { if (cacheTags) {
headers = { headers = {
'x-next-cache-tags': cacheTags, [NEXT_CACHE_TAGS_HEADER]: cacheTags,
} }
} }
...@@ -2407,7 +2410,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -2407,7 +2410,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
const headers = { ...cachedData.headers } const headers = { ...cachedData.headers }
if (!(this.minimalMode && isSSG)) { if (!(this.minimalMode && isSSG)) {
delete headers['x-next-cache-tags'] delete headers[NEXT_CACHE_TAGS_HEADER]
} }
await sendResponse( await sendResponse(
...@@ -2424,11 +2427,11 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -2424,11 +2427,11 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if ( if (
this.minimalMode && this.minimalMode &&
isSSG && isSSG &&
cachedData.headers?.['x-next-cache-tags'] cachedData.headers?.[NEXT_CACHE_TAGS_HEADER]
) { ) {
res.setHeader( res.setHeader(
'x-next-cache-tags', NEXT_CACHE_TAGS_HEADER,
cachedData.headers['x-next-cache-tags'] as string cachedData.headers[NEXT_CACHE_TAGS_HEADER] as string
) )
} }
if (isDataReq && typeof cachedData.pageData !== 'string') { if (isDataReq && typeof cachedData.pageData !== 'string') {
......
...@@ -264,12 +264,13 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -264,12 +264,13 @@ export class AppRouteRouteModule extends RouteModule<
// Get the context for the static generation. // Get the context for the static generation.
const staticGenerationContext: StaticGenerationContext = { const staticGenerationContext: StaticGenerationContext = {
pathname: this.definition.pathname, urlPathname: request.nextUrl.pathname,
renderOpts: renderOpts:
// If the staticGenerationContext is not provided then we default to // If the staticGenerationContext is not provided then we default to
// the default values. // the default values.
context.staticGenerationContext ?? { context.staticGenerationContext ?? {
supportsDynamicHTML: false, supportsDynamicHTML: false,
originalPathname: this.definition.pathname,
}, },
} }
......
...@@ -321,7 +321,11 @@ export class ImageOptimizerCache { ...@@ -321,7 +321,11 @@ export class ImageOptimizerCache {
async set( async set(
cacheKey: string, cacheKey: string,
value: IncrementalCacheValue | null, value: IncrementalCacheValue | null,
revalidate?: number | false {
revalidate,
}: {
revalidate?: number | false
}
) { ) {
if (value?.kind !== 'IMAGE') { if (value?.kind !== 'IMAGE') {
throw new Error('invariant attempted to set non-image to image-cache') throw new Error('invariant attempted to set non-image to image-cache')
......
import LRUCache from 'next/dist/compiled/lru-cache'
import { FETCH_CACHE_HEADER } from '../../../client/components/app-router-headers'
import { CACHE_ONE_YEAR } from '../../../lib/constants'
import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './' import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './'
import { getDerivedTags } from './utils'
let memoryCache: LRUCache<string, CacheHandlerValue> | undefined import LRUCache from 'next/dist/compiled/lru-cache'
import {
CACHE_ONE_YEAR,
NEXT_CACHE_SOFT_TAGS_HEADER,
} from '../../../lib/constants'
let rateLimitedUntil = 0 let rateLimitedUntil = 0
let memoryCache: LRUCache<string, CacheHandlerValue> | undefined
interface NextFetchCacheParams { interface NextFetchCacheParams {
internal?: boolean internal?: boolean
...@@ -15,11 +16,17 @@ interface NextFetchCacheParams { ...@@ -15,11 +16,17 @@ interface NextFetchCacheParams {
fetchUrl?: string fetchUrl?: string
} }
const CACHE_TAGS_HEADER = 'x-vercel-cache-tags' as const
const CACHE_HEADERS_HEADER = 'x-vercel-sc-headers' as const
const CACHE_STATE_HEADER = 'x-vercel-cache-state' as const
const CACHE_REVALIDATE_HEADER = 'x-vercel-revalidate' as const
const CACHE_FETCH_URL_HEADER = 'x-vercel-cache-item-name' as const
const CACHE_CONTROL_VALUE_HEADER = 'x-vercel-cache-control' as const
export default class FetchCache implements CacheHandler { export default class FetchCache implements CacheHandler {
private headers: Record<string, string> private headers: Record<string, string>
private cacheEndpoint?: string private cacheEndpoint?: string
private debug: boolean private debug: boolean
private revalidatedTags: string[]
static isAvailable(ctx: { static isAvailable(ctx: {
_requestHeaders: CacheHandlerContext['_requestHeaders'] _requestHeaders: CacheHandlerContext['_requestHeaders']
...@@ -32,17 +39,16 @@ export default class FetchCache implements CacheHandler { ...@@ -32,17 +39,16 @@ export default class FetchCache implements CacheHandler {
constructor(ctx: CacheHandlerContext) { constructor(ctx: CacheHandlerContext) {
this.debug = !!process.env.NEXT_PRIVATE_DEBUG_CACHE this.debug = !!process.env.NEXT_PRIVATE_DEBUG_CACHE
this.headers = {} this.headers = {}
this.revalidatedTags = ctx.revalidatedTags
this.headers['Content-Type'] = 'application/json' this.headers['Content-Type'] = 'application/json'
if (FETCH_CACHE_HEADER in ctx._requestHeaders) { if (CACHE_HEADERS_HEADER in ctx._requestHeaders) {
const newHeaders = JSON.parse( const newHeaders = JSON.parse(
ctx._requestHeaders[FETCH_CACHE_HEADER] as string ctx._requestHeaders[CACHE_HEADERS_HEADER] as string
) )
for (const k in newHeaders) { for (const k in newHeaders) {
this.headers[k] = newHeaders[k] this.headers[k] = newHeaders[k]
} }
delete ctx._requestHeaders[FETCH_CACHE_HEADER] delete ctx._requestHeaders[CACHE_HEADERS_HEADER]
} }
const scHost = const scHost =
ctx._requestHeaders['x-vercel-sc-host'] || process.env.SUSPENSE_CACHE_URL ctx._requestHeaders['x-vercel-sc-host'] || process.env.SUSPENSE_CACHE_URL
...@@ -118,7 +124,7 @@ export default class FetchCache implements CacheHandler { ...@@ -118,7 +124,7 @@ export default class FetchCache implements CacheHandler {
{ {
method: 'POST', method: 'POST',
headers: this.headers, headers: this.headers,
// @ts-expect-error // @ts-expect-error not on public type
next: { internal: true }, next: { internal: true },
} }
) )
...@@ -138,10 +144,16 @@ export default class FetchCache implements CacheHandler { ...@@ -138,10 +144,16 @@ export default class FetchCache implements CacheHandler {
public async get( public async get(
key: string, key: string,
fetchCache?: boolean, ctx: {
fetchUrl?: string, tags?: string[]
fetchIdx?: number softTags?: string[]
fetchCache?: boolean
fetchUrl?: string
fetchIdx?: number
}
) { ) {
const { tags, softTags, fetchCache, fetchIdx, fetchUrl } = ctx
if (!fetchCache) return null if (!fetchCache) return null
if (Date.now() < rateLimitedUntil) { if (Date.now() < rateLimitedUntil) {
...@@ -175,7 +187,9 @@ export default class FetchCache implements CacheHandler { ...@@ -175,7 +187,9 @@ export default class FetchCache implements CacheHandler {
method: 'GET', method: 'GET',
headers: { headers: {
...this.headers, ...this.headers,
'X-Vercel-Cache-Item-Name': fetchUrl, [CACHE_FETCH_URL_HEADER]: fetchUrl,
[CACHE_TAGS_HEADER]: tags?.join(',') || '',
[NEXT_CACHE_SOFT_TAGS_HEADER]: softTags?.join(',') || '',
} as any, } as any,
next: fetchParams as NextFetchRequestConfig, next: fetchParams as NextFetchRequestConfig,
} }
...@@ -209,7 +223,7 @@ export default class FetchCache implements CacheHandler { ...@@ -209,7 +223,7 @@ export default class FetchCache implements CacheHandler {
throw new Error(`invalid cache value`) throw new Error(`invalid cache value`)
} }
const cacheState = res.headers.get('x-vercel-cache-state') const cacheState = res.headers.get(CACHE_STATE_HEADER)
const age = res.headers.get('age') const age = res.headers.get('age')
data = { data = {
...@@ -221,13 +235,16 @@ export default class FetchCache implements CacheHandler { ...@@ -221,13 +235,16 @@ export default class FetchCache implements CacheHandler {
? Date.now() - CACHE_ONE_YEAR ? Date.now() - CACHE_ONE_YEAR
: Date.now() - parseInt(age || '0', 10) * 1000, : Date.now() - parseInt(age || '0', 10) * 1000,
} }
if (this.debug) { if (this.debug) {
console.log( console.log(
`got fetch cache entry for ${key}, duration: ${ `got fetch cache entry for ${key}, duration: ${
Date.now() - start Date.now() - start
}ms, size: ${ }ms, size: ${
Object.keys(cached).length Object.keys(cached).length
}, cache-state: ${cacheState}` }, cache-state: ${cacheState} tags: ${tags?.join(
','
)} softTags: ${softTags?.join(',')}`
) )
} }
...@@ -242,29 +259,23 @@ export default class FetchCache implements CacheHandler { ...@@ -242,29 +259,23 @@ export default class FetchCache implements CacheHandler {
} }
} }
// if a tag was revalidated we don't return stale data
if (data?.value?.kind === 'FETCH') {
const innerData = data.value.data
const derivedTags = getDerivedTags(innerData.tags || [])
if (
derivedTags.some((tag) => {
return this.revalidatedTags.includes(tag)
})
) {
data = undefined
}
}
return data || null return data || null
} }
public async set( public async set(
key: string, key: string,
data: CacheHandlerValue['value'], data: CacheHandlerValue['value'],
fetchCache?: boolean, {
fetchUrl?: string, fetchCache,
fetchIdx?: number fetchIdx,
fetchUrl,
tags,
}: {
tags?: string[]
fetchCache?: boolean
fetchUrl?: string
fetchIdx?: number
}
) { ) {
if (!fetchCache) return if (!fetchCache) return
...@@ -284,26 +295,25 @@ export default class FetchCache implements CacheHandler { ...@@ -284,26 +295,25 @@ export default class FetchCache implements CacheHandler {
try { try {
const start = Date.now() const start = Date.now()
if (data !== null && 'revalidate' in data) { if (data !== null && 'revalidate' in data) {
this.headers['x-vercel-revalidate'] = data.revalidate.toString() this.headers[CACHE_REVALIDATE_HEADER] = data.revalidate.toString()
} }
if ( if (
!this.headers['x-vercel-revalidate'] && !this.headers[CACHE_REVALIDATE_HEADER] &&
data !== null && data !== null &&
'data' in data 'data' in data
) { ) {
this.headers['x-vercel-cache-control'] = this.headers[CACHE_CONTROL_VALUE_HEADER] =
data.data.headers['cache-control'] data.data.headers['cache-control']
} }
const body = JSON.stringify(data) const body = JSON.stringify({
const headers = { ...this.headers } ...data,
if (data !== null && 'data' in data && data.data.tags) { // we send the tags in the header instead
headers['x-vercel-cache-tags'] = data.data.tags.join(',') // of in the body here
} tags: undefined,
})
if (this.debug) { if (this.debug) {
console.log('set cache', key, { console.log('set cache', key)
tags: headers['x-vercel-cache-tags'],
})
} }
const fetchParams: NextFetchCacheParams = { const fetchParams: NextFetchCacheParams = {
internal: true, internal: true,
...@@ -316,8 +326,9 @@ export default class FetchCache implements CacheHandler { ...@@ -316,8 +326,9 @@ export default class FetchCache implements CacheHandler {
{ {
method: 'POST', method: 'POST',
headers: { headers: {
...headers, ...this.headers,
'X-Vercel-Cache-Item-Name': fetchUrl || '', [CACHE_FETCH_URL_HEADER]: fetchUrl || '',
[CACHE_TAGS_HEADER]: tags?.join(',') || '',
}, },
body: body, body: body,
next: fetchParams as NextFetchRequestConfig, next: fetchParams as NextFetchRequestConfig,
......
import type { OutgoingHttpHeaders } from 'http' import type { OutgoingHttpHeaders } from 'http'
import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './' import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './'
import LRUCache from 'next/dist/compiled/lru-cache' import LRUCache from 'next/dist/compiled/lru-cache'
import { CacheFs } from '../../../shared/lib/utils' import { CacheFs } from '../../../shared/lib/utils'
import path from '../../../shared/lib/isomorphic/path' import path from '../../../shared/lib/isomorphic/path'
import { CachedFetchValue } from '../../response-cache' import { CachedFetchValue } from '../../response-cache'
import { getDerivedTags } from './utils' import { NEXT_CACHE_TAGS_HEADER } from '../../../lib/constants'
type FileSystemCacheContext = Omit< type FileSystemCacheContext = Omit<
CacheHandlerContext, CacheHandlerContext,
...@@ -16,7 +17,7 @@ type FileSystemCacheContext = Omit< ...@@ -16,7 +17,7 @@ type FileSystemCacheContext = Omit<
type TagsManifest = { type TagsManifest = {
version: 1 version: 1
items: { [tag: string]: { keys: string[]; revalidatedAt: number } } items: { [tag: string]: { revalidatedAt: number } }
} }
let memoryCache: LRUCache<string, CacheHandlerValue> | undefined let memoryCache: LRUCache<string, CacheHandlerValue> | undefined
let tagsManifest: TagsManifest | undefined let tagsManifest: TagsManifest | undefined
...@@ -81,31 +82,6 @@ export default class FileSystemCache implements CacheHandler { ...@@ -81,31 +82,6 @@ export default class FileSystemCache implements CacheHandler {
} }
} }
async setTags(key: string, tags: string[]) {
this.loadTagsManifest()
if (!tagsManifest || !this.tagsManifestPath) {
return
}
for (const tag of tags) {
const data = tagsManifest.items[tag] || { keys: [] }
if (!data.keys.includes(key)) {
data.keys.push(key)
}
tagsManifest.items[tag] = data
}
try {
await this.fs.mkdir(path.dirname(this.tagsManifestPath))
await this.fs.writeFile(
this.tagsManifestPath,
JSON.stringify(tagsManifest || {})
)
} catch (err: any) {
console.warn('Failed to update tags manifest.', err)
}
}
public async revalidateTag(tag: string) { public async revalidateTag(tag: string) {
// we need to ensure the tagsManifest is refreshed // we need to ensure the tagsManifest is refreshed
// since separate workers can be updating it at the same // since separate workers can be updating it at the same
...@@ -115,7 +91,7 @@ export default class FileSystemCache implements CacheHandler { ...@@ -115,7 +91,7 @@ export default class FileSystemCache implements CacheHandler {
return return
} }
const data = tagsManifest.items[tag] || { keys: [] } const data = tagsManifest.items[tag] || {}
data.revalidatedAt = Date.now() data.revalidatedAt = Date.now()
tagsManifest.items[tag] = data tagsManifest.items[tag] = data
...@@ -130,7 +106,18 @@ export default class FileSystemCache implements CacheHandler { ...@@ -130,7 +106,18 @@ export default class FileSystemCache implements CacheHandler {
} }
} }
public async get(key: string, fetchCache?: boolean) { public async get(
key: string,
{
tags,
softTags,
fetchCache,
}: {
tags?: string[]
softTags?: string[]
fetchCache?: boolean
} = {}
) {
let data = memoryCache?.get(key) let data = memoryCache?.get(key)
// let's check the disk for seed data // let's check the disk for seed data
...@@ -178,6 +165,17 @@ export default class FileSystemCache implements CacheHandler { ...@@ -178,6 +165,17 @@ export default class FileSystemCache implements CacheHandler {
lastModified, lastModified,
value: parsedData, value: parsedData,
} }
if (data.value?.kind === 'FETCH') {
const storedTags = data.value?.data?.tags
// update stored tags if a new one is being added
// TODO: remove this when we can send the tags
// via header on GET same as SET
if (!tags?.every((tag) => storedTags?.includes(tag))) {
await this.set(key, data.value, { tags })
}
}
} else { } else {
const pageData = isAppPath const pageData = isAppPath
? ( ? (
...@@ -234,42 +232,41 @@ export default class FileSystemCache implements CacheHandler { ...@@ -234,42 +232,41 @@ export default class FileSystemCache implements CacheHandler {
// unable to get data from disk // unable to get data from disk
} }
} }
let cacheTags: undefined | string[]
if (data?.value?.kind === 'PAGE') { if (data?.value?.kind === 'PAGE') {
const tagsHeader = data.value.headers?.['x-next-cache-tags'] let cacheTags: undefined | string[]
const tagsHeader = data.value.headers?.[NEXT_CACHE_TAGS_HEADER]
if (typeof tagsHeader === 'string') { if (typeof tagsHeader === 'string') {
cacheTags = tagsHeader.split(',') cacheTags = tagsHeader.split(',')
} }
}
if (data?.value?.kind === 'PAGE' && cacheTags?.length) { if (cacheTags?.length) {
this.loadTagsManifest() this.loadTagsManifest()
const derivedTags = getDerivedTags(cacheTags || [])
const isStale = derivedTags.some((tag) => { const isStale = cacheTags.some((tag) => {
return ( return (
tagsManifest?.items[tag]?.revalidatedAt && tagsManifest?.items[tag]?.revalidatedAt &&
tagsManifest?.items[tag].revalidatedAt >= tagsManifest?.items[tag].revalidatedAt >=
(data?.lastModified || Date.now()) (data?.lastModified || Date.now())
) )
}) })
// we trigger a blocking validation if an ISR page // we trigger a blocking validation if an ISR page
// had a tag revalidated, if we want to be a background // had a tag revalidated, if we want to be a background
// revalidation instead we return data.lastModified = -1 // revalidation instead we return data.lastModified = -1
if (isStale) { if (isStale) {
data = undefined data = undefined
}
} }
} }
if (data && data?.value?.kind === 'FETCH') { if (data && data?.value?.kind === 'FETCH') {
this.loadTagsManifest() this.loadTagsManifest()
const innerData = data.value.data
const derivedTags = getDerivedTags(innerData.tags || [])
const wasRevalidated = derivedTags.some((tag) => { const combinedTags = [...(tags || []), ...(softTags || [])]
const wasRevalidated = combinedTags.some((tag) => {
if (this.revalidatedTags.includes(tag)) { if (this.revalidatedTags.includes(tag)) {
return true return true
} }
...@@ -290,7 +287,13 @@ export default class FileSystemCache implements CacheHandler { ...@@ -290,7 +287,13 @@ export default class FileSystemCache implements CacheHandler {
return data || null return data || null
} }
public async set(key: string, data: CacheHandlerValue['value']) { public async set(
key: string,
data: CacheHandlerValue['value'],
ctx: {
tags?: string[]
}
) {
memoryCache?.set(key, { memoryCache?.set(key, {
value: data, value: data,
lastModified: Date.now(), lastModified: Date.now(),
...@@ -345,8 +348,13 @@ export default class FileSystemCache implements CacheHandler { ...@@ -345,8 +348,13 @@ export default class FileSystemCache implements CacheHandler {
fetchCache: true, fetchCache: true,
}) })
await this.fs.mkdir(path.dirname(filePath)) await this.fs.mkdir(path.dirname(filePath))
await this.fs.writeFile(filePath, JSON.stringify(data)) await this.fs.writeFile(
await this.setTags(key, data.data.tags || []) filePath,
JSON.stringify({
...data,
tags: ctx.tags,
})
)
} }
} }
......
import type { CacheFs } from '../../../shared/lib/utils' import type { CacheFs } from '../../../shared/lib/utils'
import FetchCache from './fetch-cache'
import FileSystemCache from './file-system-cache' 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 { encodeText } from '../../stream-utils/encode-decode'
import { encode } from '../../../shared/lib/base64-arraybuffer'
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,
} from '../../response-cache' } from '../../response-cache'
import { encode } from '../../../shared/lib/base64-arraybuffer'
import { encodeText } from '../../stream-utils/encode-decode'
import { import {
CACHE_ONE_YEAR, CACHE_ONE_YEAR,
NEXT_CACHE_REVALIDATED_TAGS_HEADER,
NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER,
PRERENDER_REVALIDATE_HEADER, PRERENDER_REVALIDATE_HEADER,
} from '../../../lib/constants' } from '../../../lib/constants'
...@@ -44,20 +47,13 @@ export class CacheHandler { ...@@ -44,20 +47,13 @@ export class CacheHandler {
constructor(_ctx: CacheHandlerContext) {} constructor(_ctx: CacheHandlerContext) {}
public async get( public async get(
_key: string, ..._args: Parameters<IncrementalCache['get']>
_fetchCache?: boolean,
_fetchUrl?: string,
_fetchIdx?: number
): Promise<CacheHandlerValue | null> { ): Promise<CacheHandlerValue | null> {
return {} as any return {} as any
} }
public async set( public async set(
_key: string, ..._args: Parameters<IncrementalCache['set']>
_data: IncrementalCacheValue | null,
_fetchCache?: boolean,
_fetchUrl?: string,
_fetchIdx?: number
): Promise<void> {} ): Promise<void> {}
public async revalidateTag(_tag: string): Promise<void> {} public async revalidateTag(_tag: string): Promise<void> {}
...@@ -152,11 +148,12 @@ export class IncrementalCache { ...@@ -152,11 +148,12 @@ export class IncrementalCache {
if ( if (
minimalMode && minimalMode &&
typeof requestHeaders['x-next-revalidated-tags'] === 'string' && typeof requestHeaders[NEXT_CACHE_REVALIDATED_TAGS_HEADER] === 'string' &&
requestHeaders['x-next-revalidate-tag-token'] === requestHeaders[NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER] ===
this.prerenderManifest?.preview?.previewModeId this.prerenderManifest?.preview?.previewModeId
) { ) {
revalidatedTags = requestHeaders['x-next-revalidated-tags'].split(',') revalidatedTags =
requestHeaders[NEXT_CACHE_REVALIDATED_TAGS_HEADER].split(',')
} }
if (CurCacheHandler) { if (CurCacheHandler) {
...@@ -393,11 +390,15 @@ export class IncrementalCache { ...@@ -393,11 +390,15 @@ export class IncrementalCache {
// get data from cache if available // get data from cache if available
async get( async get(
pathname: string, cacheKey: string,
fetchCache?: boolean, ctx: {
revalidate?: number | false, fetchCache?: boolean
fetchUrl?: string, revalidate?: number | false
fetchIdx?: number fetchUrl?: string
fetchIdx?: number
tags?: string[]
softTags?: string[]
} = {}
): Promise<IncrementalCacheEntry | null> { ): Promise<IncrementalCacheEntry | null> {
if ( if (
process.env.__NEXT_INCREMENTAL_CACHE_IPC_PORT && process.env.__NEXT_INCREMENTAL_CACHE_IPC_PORT &&
...@@ -419,22 +420,28 @@ export class IncrementalCache { ...@@ -419,22 +420,28 @@ export class IncrementalCache {
// so that getStaticProps is always called for easier debugging // so that getStaticProps is always called for easier debugging
if ( if (
this.dev && this.dev &&
(!fetchCache || this.requestHeaders['cache-control'] === 'no-cache') (!ctx.fetchCache || this.requestHeaders['cache-control'] === 'no-cache')
) { ) {
return null return null
} }
pathname = this._getPathname(pathname, fetchCache) cacheKey = this._getPathname(cacheKey, ctx.fetchCache)
let entry: IncrementalCacheEntry | null = null let entry: IncrementalCacheEntry | null = null
let revalidate = ctx.revalidate
const cacheData = await this.cacheHandler?.get( const cacheData = await this.cacheHandler?.get(cacheKey, ctx)
pathname,
fetchCache,
fetchUrl,
fetchIdx
)
if (cacheData?.value?.kind === 'FETCH') { if (cacheData?.value?.kind === 'FETCH') {
const combinedTags = [...(ctx.tags || []), ...(ctx.softTags || [])]
// if a tag was revalidated we don't return stale data
if (
combinedTags.some((tag) => {
return this.revalidatedTags?.includes(tag)
})
) {
return null
}
revalidate = revalidate || cacheData.value.revalidate revalidate = revalidate || cacheData.value.revalidate
const age = Math.round( const age = Math.round(
(Date.now() - (cacheData.lastModified || 0)) / 1000 (Date.now() - (cacheData.lastModified || 0)) / 1000
...@@ -455,7 +462,7 @@ export class IncrementalCache { ...@@ -455,7 +462,7 @@ export class IncrementalCache {
} }
const curRevalidate = const curRevalidate =
this.prerenderManifest.routes[toRoute(pathname)]?.initialRevalidateSeconds this.prerenderManifest.routes[toRoute(cacheKey)]?.initialRevalidateSeconds
let isStale: boolean | -1 | undefined let isStale: boolean | -1 | undefined
let revalidateAfter: false | number let revalidateAfter: false | number
...@@ -465,9 +472,9 @@ export class IncrementalCache { ...@@ -465,9 +472,9 @@ export class IncrementalCache {
revalidateAfter = -1 * CACHE_ONE_YEAR revalidateAfter = -1 * CACHE_ONE_YEAR
} else { } else {
revalidateAfter = this.calculateRevalidate( revalidateAfter = this.calculateRevalidate(
pathname, cacheKey,
cacheData?.lastModified || Date.now(), cacheData?.lastModified || Date.now(),
this.dev && !fetchCache this.dev && !ctx.fetchCache
) )
isStale = isStale =
revalidateAfter !== false && revalidateAfter < Date.now() revalidateAfter !== false && revalidateAfter < Date.now()
...@@ -486,7 +493,7 @@ export class IncrementalCache { ...@@ -486,7 +493,7 @@ export class IncrementalCache {
if ( if (
!cacheData && !cacheData &&
this.prerenderManifest.notFoundRoutes.includes(pathname) this.prerenderManifest.notFoundRoutes.includes(cacheKey)
) { ) {
// for the first hit after starting the server the cache // for the first hit after starting the server the cache
// may not have a way to save notFound: true so if // may not have a way to save notFound: true so if
...@@ -499,14 +506,7 @@ export class IncrementalCache { ...@@ -499,14 +506,7 @@ export class IncrementalCache {
curRevalidate, curRevalidate,
revalidateAfter, revalidateAfter,
} }
this.set( this.set(cacheKey, entry.value, ctx)
pathname,
entry.value,
curRevalidate,
fetchCache,
fetchUrl,
fetchIdx
)
} }
return entry return entry
} }
...@@ -515,10 +515,13 @@ export class IncrementalCache { ...@@ -515,10 +515,13 @@ export class IncrementalCache {
async set( async set(
pathname: string, pathname: string,
data: IncrementalCacheValue | null, data: IncrementalCacheValue | null,
revalidateSeconds?: number | false, ctx: {
fetchCache?: boolean, revalidate?: number | false
fetchUrl?: string, fetchCache?: boolean
fetchIdx?: number fetchUrl?: string
fetchIdx?: number
tags?: string[]
}
) { ) {
if ( if (
process.env.__NEXT_INCREMENTAL_CACHE_IPC_PORT && process.env.__NEXT_INCREMENTAL_CACHE_IPC_PORT &&
...@@ -536,38 +539,32 @@ export class IncrementalCache { ...@@ -536,38 +539,32 @@ export class IncrementalCache {
}) })
} }
if (this.dev && !fetchCache) return if (this.dev && !ctx.fetchCache) return
// fetchCache has upper limit of 2MB per-entry currently // fetchCache has upper limit of 2MB per-entry currently
if (fetchCache && JSON.stringify(data).length > 2 * 1024 * 1024) { if (ctx.fetchCache && JSON.stringify(data).length > 2 * 1024 * 1024) {
if (this.dev) { if (this.dev) {
throw new Error(`fetch for over 2MB of data can not be cached`) throw new Error(`fetch for over 2MB of data can not be cached`)
} }
return return
} }
pathname = this._getPathname(pathname, fetchCache) pathname = this._getPathname(pathname, ctx.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' && !fetchCache) { if (typeof ctx.revalidate !== 'undefined' && !ctx.fetchCache) {
this.prerenderManifest.routes[pathname] = { this.prerenderManifest.routes[pathname] = {
dataRoute: path.posix.join( dataRoute: path.posix.join(
'/_next/data', '/_next/data',
`${normalizePagePath(pathname)}.json` `${normalizePagePath(pathname)}.json`
), ),
srcRoute: null, // FIXME: provide actual source route, however, when dynamically appending it doesn't really matter srcRoute: null, // FIXME: provide actual source route, however, when dynamically appending it doesn't really matter
initialRevalidateSeconds: revalidateSeconds, initialRevalidateSeconds: ctx.revalidate,
} }
} }
await this.cacheHandler?.set( await this.cacheHandler?.set(pathname, data, ctx)
pathname,
data,
fetchCache,
fetchUrl,
fetchIdx
)
} catch (error) { } catch (error) {
console.warn('Failed to update prerender cache for', pathname, error) console.warn('Failed to update prerender cache for', pathname, error)
} }
......
export const getDerivedTags = (tags: string[]): string[] => {
const derivedTags: string[] = ['/']
for (const tag of tags || []) {
if (tag.startsWith('/')) {
const pathnameParts = tag.split('/')
// we automatically add the current path segments as tags
// for revalidatePath handling
for (let i = 1; i < pathnameParts.length + 1; i++) {
const curPathname = pathnameParts.slice(0, i).join('/')
if (curPathname) {
derivedTags.push(curPathname)
if (!derivedTags.includes(curPathname)) {
derivedTags.push(curPathname)
}
}
}
} else if (!derivedTags.includes(tag)) {
derivedTags.push(tag)
}
}
return derivedTags
}
...@@ -3,26 +3,67 @@ import type * as ServerHooks from '../../client/components/hooks-server-context' ...@@ -3,26 +3,67 @@ import type * as ServerHooks from '../../client/components/hooks-server-context'
import { AppRenderSpan, NextNodeServerSpan } from './trace/constants' import { AppRenderSpan, NextNodeServerSpan } from './trace/constants'
import { getTracer, SpanKind } from './trace/tracer' import { getTracer, SpanKind } from './trace/tracer'
import { CACHE_ONE_YEAR } from '../../lib/constants' import { CACHE_ONE_YEAR, NEXT_CACHE_IMPLICIT_TAG_ID } from '../../lib/constants'
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge' const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
const getDerivedTags = (pathname: string): string[] => {
const derivedTags: string[] = [`/layout`]
// we automatically add the current path segments as tags
// for revalidatePath handling
if (pathname.startsWith('/')) {
const pathnameParts = pathname.split('/')
for (let i = 1; i < pathnameParts.length + 1; i++) {
let curPathname = pathnameParts.slice(0, i).join('/')
if (curPathname) {
// all derived tags other than the page are layout tags
if (!curPathname.endsWith('/page') && !curPathname.endsWith('/route')) {
curPathname = `${curPathname}${
!curPathname.endsWith('/') ? '/' : ''
}layout`
}
derivedTags.push(curPathname)
}
}
}
return derivedTags
}
export function addImplicitTags( export function addImplicitTags(
staticGenerationStore: ReturnType<StaticGenerationAsyncStorage['getStore']> staticGenerationStore: ReturnType<StaticGenerationAsyncStorage['getStore']>
) { ) {
const newTags: string[] = [] const newTags: string[] = []
const pathname = staticGenerationStore?.originalPathname if (!staticGenerationStore) {
if (!pathname) {
return newTags return newTags
} }
const { pagePath, urlPathname } = staticGenerationStore
if (!Array.isArray(staticGenerationStore.tags)) { if (!Array.isArray(staticGenerationStore.tags)) {
staticGenerationStore.tags = [] staticGenerationStore.tags = []
} }
if (!staticGenerationStore.tags.includes(pathname)) {
staticGenerationStore.tags.push(pathname) if (pagePath) {
const derivedTags = getDerivedTags(pagePath)
for (let tag of derivedTags) {
tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${tag}`
if (!staticGenerationStore.tags?.includes(tag)) {
staticGenerationStore.tags.push(tag)
}
newTags.push(tag)
}
}
if (urlPathname) {
const tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${urlPathname}`
if (!staticGenerationStore.tags?.includes(tag)) {
staticGenerationStore.tags.push(tag)
}
newTags.push(tag)
} }
newTags.push(pathname)
return newTags return newTags
} }
...@@ -166,11 +207,6 @@ export function patchFetch({ ...@@ -166,11 +207,6 @@ export function patchFetch({
} }
const implicitTags = addImplicitTags(staticGenerationStore) const implicitTags = addImplicitTags(staticGenerationStore)
for (const tag of implicitTags || []) {
if (!tags.includes(tag)) {
tags.push(tag)
}
}
const isOnlyCache = staticGenerationStore.fetchCache === 'only-cache' const isOnlyCache = staticGenerationStore.fetchCache === 'only-cache'
const isForceCache = staticGenerationStore.fetchCache === 'force-cache' const isForceCache = staticGenerationStore.fetchCache === 'force-cache'
const isDefaultCache = const isDefaultCache =
...@@ -190,7 +226,7 @@ export function patchFetch({ ...@@ -190,7 +226,7 @@ export function patchFetch({
typeof curRevalidate !== 'undefined' typeof curRevalidate !== 'undefined'
) { ) {
console.warn( console.warn(
`Warning: fetch for ${fetchUrl} on ${staticGenerationStore.pathname} specified "cache: ${_cache}" and "revalidate: ${curRevalidate}", only one should be specified.` `Warning: fetch for ${fetchUrl} on ${staticGenerationStore.urlPathname} specified "cache: ${_cache}" and "revalidate: ${curRevalidate}", only one should be specified.`
) )
_cache = undefined _cache = undefined
} }
...@@ -392,15 +428,17 @@ export function patchFetch({ ...@@ -392,15 +428,17 @@ export function patchFetch({
headers: Object.fromEntries(res.headers.entries()), headers: Object.fromEntries(res.headers.entries()),
body: bodyBuffer.toString('base64'), body: bodyBuffer.toString('base64'),
status: res.status, status: res.status,
tags,
url: res.url, url: res.url,
}, },
revalidate: normalizedRevalidate, revalidate: normalizedRevalidate,
}, },
revalidate, {
true, fetchCache: true,
fetchUrl, revalidate,
fetchIdx fetchUrl,
fetchIdx,
tags,
}
) )
} catch (err) { } catch (err) {
console.warn(`Failed to set fetch cache`, input, err) console.warn(`Failed to set fetch cache`, input, err)
...@@ -427,13 +465,14 @@ export function patchFetch({ ...@@ -427,13 +465,14 @@ export function patchFetch({
const entry = staticGenerationStore.isOnDemandRevalidate const entry = staticGenerationStore.isOnDemandRevalidate
? null ? null
: await staticGenerationStore.incrementalCache.get( : await staticGenerationStore.incrementalCache.get(cacheKey, {
cacheKey, fetchCache: true,
true,
revalidate, revalidate,
fetchUrl, fetchUrl,
fetchIdx fetchIdx,
) tags,
softTags: implicitTags,
})
if (entry) { if (entry) {
await handleUnlock() await handleUnlock()
...@@ -443,7 +482,6 @@ export function patchFetch({ ...@@ -443,7 +482,6 @@ export function patchFetch({
} }
if (entry?.value && entry.value.kind === 'FETCH') { if (entry?.value && entry.value.kind === 'FETCH') {
const currentTags = entry.value.data.tags
// when stale and is revalidating we wait for fresh data // when stale and is revalidating we wait for fresh data
// so the revalidated entry has the updated data // so the revalidated entry has the updated data
if (!(staticGenerationStore.isRevalidate && entry.isStale)) { if (!(staticGenerationStore.isRevalidate && entry.isStale)) {
...@@ -454,31 +492,7 @@ export function patchFetch({ ...@@ -454,31 +492,7 @@ export function patchFetch({
staticGenerationStore.pendingRevalidates.push( staticGenerationStore.pendingRevalidates.push(
doOriginalFetch(true).catch(console.error) doOriginalFetch(true).catch(console.error)
) )
} else if (
tags &&
!tags.every((tag) => currentTags?.includes(tag))
) {
// if new tags are being added we need to set even if
// the data isn't stale
if (!entry.value.data.tags) {
entry.value.data.tags = []
}
for (const tag of tags) {
if (!entry.value.data.tags.includes(tag)) {
entry.value.data.tags.push(tag)
}
}
staticGenerationStore.incrementalCache?.set(
cacheKey,
entry.value,
revalidate,
true,
fetchUrl,
fetchIdx
)
} }
const resData = entry.value.data const resData = entry.value.data
let decodedBody: ArrayBuffer let decodedBody: ArrayBuffer
...@@ -521,8 +535,8 @@ export function patchFetch({ ...@@ -521,8 +535,8 @@ export function patchFetch({
if (cache === 'no-store') { if (cache === 'no-store') {
staticGenerationStore.revalidate = 0 staticGenerationStore.revalidate = 0
const dynamicUsageReason = `no-store fetch ${input}${ const dynamicUsageReason = `no-store fetch ${input}${
staticGenerationStore.pathname staticGenerationStore.urlPathname
? ` ${staticGenerationStore.pathname}` ? ` ${staticGenerationStore.urlPathname}`
: '' : ''
}` }`
const err = new DynamicServerError(dynamicUsageReason) const err = new DynamicServerError(dynamicUsageReason)
...@@ -549,8 +563,8 @@ export function patchFetch({ ...@@ -549,8 +563,8 @@ export function patchFetch({
const dynamicUsageReason = `revalidate: ${ const dynamicUsageReason = `revalidate: ${
next.revalidate next.revalidate
} fetch ${input}${ } fetch ${input}${
staticGenerationStore.pathname staticGenerationStore.urlPathname
? ` ${staticGenerationStore.pathname}` ? ` ${staticGenerationStore.urlPathname}`
: '' : ''
}` }`
const err = new DynamicServerError(dynamicUsageReason) const err = new DynamicServerError(dynamicUsageReason)
......
...@@ -156,7 +156,9 @@ export default class ResponseCache { ...@@ -156,7 +156,9 @@ export default class ResponseCache {
status: cacheEntry.value.status, status: cacheEntry.value.status,
} }
: cacheEntry.value, : cacheEntry.value,
cacheEntry.revalidate {
revalidate: cacheEntry.revalidate,
}
) )
} }
} else { } else {
...@@ -170,11 +172,12 @@ export default class ResponseCache { ...@@ -170,11 +172,12 @@ 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 incrementalCache.set( await incrementalCache.set(key, cachedResponse.value, {
key, revalidate: Math.min(
cachedResponse.value, Math.max(cachedResponse.revalidate || 3, 3),
Math.min(Math.max(cachedResponse.revalidate || 3, 3), 30) 30
) ),
})
} }
// while revalidating in the background we can't reject as // while revalidating in the background we can't reject as
// we already resolved the cache entry so log the error here // we already resolved the cache entry so log the error here
......
...@@ -20,6 +20,8 @@ export interface CachedFetchValue { ...@@ -20,6 +20,8 @@ export interface CachedFetchValue {
body: string body: string
url: string url: string
status?: number status?: number
// tags are only present with file-system-cache
// fetch cache stores tags outside of cache entry
tags?: string[] tags?: string[]
} }
revalidate: number revalidate: number
...@@ -112,10 +114,13 @@ export type IncrementalCacheItem = { ...@@ -112,10 +114,13 @@ export type IncrementalCacheItem = {
} | null } | null
export interface IncrementalCache { export interface IncrementalCache {
get: (key: string) => Promise<IncrementalCacheItem> get: (
key: string,
ctx?: { fetchCache?: boolean }
) => Promise<IncrementalCacheItem>
set: ( set: (
key: string, key: string,
data: IncrementalCacheValue | null, data: IncrementalCacheValue | null,
revalidate?: number | false ctx: { revalidate: number | false }
) => Promise<void> ) => Promise<void>
} }
...@@ -11,7 +11,6 @@ import { NextURL } from './next-url' ...@@ -11,7 +11,6 @@ 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,
...@@ -51,7 +50,6 @@ const FLIGHT_PARAMETERS = [ ...@@ -51,7 +50,6 @@ 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 type AdapterOptions = { export type AdapterOptions = {
......
import { revalidateTag } from './revalidate-tag' import { revalidateTag } from './revalidate-tag'
import { NEXT_CACHE_IMPLICIT_TAG_ID } from '../../../lib/constants'
export function revalidatePath(path: string) { export function revalidatePath(path: string, type?: 'layout' | 'page') {
path = `${NEXT_CACHE_IMPLICIT_TAG_ID}${path}`
if (type) {
path += `${path.endsWith('/') ? '' : '/'}${type}`
}
return revalidateTag(path) return revalidateTag(path)
} }
...@@ -19,19 +19,6 @@ export function unstable_cache<T extends Callback>( ...@@ -19,19 +19,6 @@ export function unstable_cache<T extends Callback>(
const staticGenerationAsyncStorage: StaticGenerationAsyncStorage = const staticGenerationAsyncStorage: StaticGenerationAsyncStorage =
(fetch as any).__nextGetStaticStore?.() || _staticGenerationAsyncStorage (fetch as any).__nextGetStaticStore?.() || _staticGenerationAsyncStorage
const store: undefined | StaticGenerationStore =
staticGenerationAsyncStorage?.getStore()
const incrementalCache:
| import('../../lib/incremental-cache').IncrementalCache
| undefined =
store?.incrementalCache || (globalThis as any).__incrementalCache
if (!incrementalCache) {
throw new Error(
`Invariant: incrementalCache missing in unstable_cache ${cb.toString()}`
)
}
if (options.revalidate === 0) { if (options.revalidate === 0) {
throw new Error( throw new Error(
`Invariant revalidate: 0 can not be passed to unstable_cache(), must be "false" or "> 0" ${cb.toString()}` `Invariant revalidate: 0 can not be passed to unstable_cache(), must be "false" or "> 0" ${cb.toString()}`
...@@ -39,6 +26,20 @@ export function unstable_cache<T extends Callback>( ...@@ -39,6 +26,20 @@ export function unstable_cache<T extends Callback>(
} }
const cachedCb = async (...args: any[]) => { const cachedCb = async (...args: any[]) => {
const store: undefined | StaticGenerationStore =
staticGenerationAsyncStorage?.getStore()
const incrementalCache:
| import('../../lib/incremental-cache').IncrementalCache
| undefined =
store?.incrementalCache || (globalThis as any).__incrementalCache
if (!incrementalCache) {
throw new Error(
`Invariant: incrementalCache missing in unstable_cache ${cb.toString()}`
)
}
const joinedKey = `${cb.toString()}-${ const joinedKey = `${cb.toString()}-${
Array.isArray(keyParts) && keyParts.join(',') Array.isArray(keyParts) && keyParts.join(',')
}-${JSON.stringify(args)}` }-${JSON.stringify(args)}`
...@@ -51,18 +52,10 @@ export function unstable_cache<T extends Callback>( ...@@ -51,18 +52,10 @@ export function unstable_cache<T extends Callback>(
{ {
...store, ...store,
fetchCache: 'only-no-store', fetchCache: 'only-no-store',
urlPathname: store?.urlPathname || '/',
isStaticGeneration: !!store?.isStaticGeneration, isStaticGeneration: !!store?.isStaticGeneration,
pathname: store?.pathname || '/',
}, },
async () => { async () => {
const cacheKey = await incrementalCache?.fetchCacheKey(joinedKey)
const cacheEntry =
cacheKey &&
!(
store?.isOnDemandRevalidate || incrementalCache.isOnDemandRevalidate
) &&
(await incrementalCache?.get(cacheKey, true, options.revalidate))
const tags = options.tags || [] const tags = options.tags || []
if (Array.isArray(tags) && store) { if (Array.isArray(tags) && store) {
...@@ -77,11 +70,18 @@ export function unstable_cache<T extends Callback>( ...@@ -77,11 +70,18 @@ export function unstable_cache<T extends Callback>(
} }
const implicitTags = addImplicitTags(store) const implicitTags = addImplicitTags(store)
for (const tag of implicitTags) { const cacheKey = await incrementalCache?.fetchCacheKey(joinedKey)
if (!tags.includes(tag)) { const cacheEntry =
tags.push(tag) cacheKey &&
} !(
} store?.isOnDemandRevalidate || incrementalCache.isOnDemandRevalidate
) &&
(await incrementalCache?.get(cacheKey, {
fetchCache: true,
revalidate: options.revalidate,
tags,
softTags: implicitTags,
}))
const invokeCallback = async () => { const invokeCallback = async () => {
const result = await cb(...args) const result = await cb(...args)
...@@ -96,7 +96,6 @@ export function unstable_cache<T extends Callback>( ...@@ -96,7 +96,6 @@ export function unstable_cache<T extends Callback>(
// TODO: handle non-JSON values? // TODO: handle non-JSON values?
body: JSON.stringify(result), body: JSON.stringify(result),
status: 200, status: 200,
tags,
url: '', url: '',
}, },
revalidate: revalidate:
...@@ -104,8 +103,11 @@ export function unstable_cache<T extends Callback>( ...@@ -104,8 +103,11 @@ export function unstable_cache<T extends Callback>(
? CACHE_ONE_YEAR ? CACHE_ONE_YEAR
: options.revalidate, : options.revalidate,
}, },
options.revalidate, {
true revalidate: options.revalidate,
fetchCache: true,
tags,
}
) )
} }
return result return result
...@@ -128,7 +130,6 @@ export function unstable_cache<T extends Callback>( ...@@ -128,7 +130,6 @@ export function unstable_cache<T extends Callback>(
const resData = cacheEntry.value.data const resData = cacheEntry.value.data
cachedValue = JSON.parse(resData.body) cachedValue = JSON.parse(resData.body)
} }
const currentTags = cacheEntry.value.data.tags
if (isStale) { if (isStale) {
if (!store) { if (!store) {
...@@ -143,22 +144,6 @@ export function unstable_cache<T extends Callback>( ...@@ -143,22 +144,6 @@ export function unstable_cache<T extends Callback>(
) )
) )
} }
} else if (tags && !tags.every((tag) => currentTags?.includes(tag))) {
if (!cacheEntry.value.data.tags) {
cacheEntry.value.data.tags = []
}
for (const tag of tags) {
if (!cacheEntry.value.data.tags.includes(tag)) {
cacheEntry.value.data.tags.push(tag)
}
}
incrementalCache?.set(
cacheKey,
cacheEntry.value,
options.revalidate,
true
)
} }
return cachedValue return cachedValue
} }
......
...@@ -730,20 +730,20 @@ createNextDescribe( ...@@ -730,20 +730,20 @@ createNextDescribe(
await browser.elementByCss('#back').click() await browser.elementByCss('#back').click()
switch (type) {
case 'tag':
await browser.elementByCss('#revalidate-thankyounext').click()
break
case 'path':
await browser.elementByCss('#revalidate-path').click()
break
default:
throw new Error(`Invalid type: ${type}`)
}
// Should be different // Should be different
let revalidatedThankYouNext let revalidatedThankYouNext
await check(async () => { await check(async () => {
switch (type) {
case 'tag':
await browser.elementByCss('#revalidate-thankyounext').click()
break
case 'path':
await browser.elementByCss('#revalidate-path').click()
break
default:
throw new Error(`Invalid type: ${type}`)
}
revalidatedThankYouNext = await browser revalidatedThankYouNext = await browser
.elementByCss('#thankyounext') .elementByCss('#thankyounext')
.text() .text()
......
...@@ -895,7 +895,7 @@ createNextDescribe( ...@@ -895,7 +895,7 @@ createNextDescribe(
initialHeaders: { initialHeaders: {
'content-type': 'application/json', 'content-type': 'application/json',
'x-next-cache-tags': 'x-next-cache-tags':
'thankyounext,/route-handler/revalidate-360-isr/route', 'thankyounext,_N_T_/layout,_N_T_/route-handler/layout,_N_T_/route-handler/revalidate-360-isr/layout,_N_T_/route-handler/revalidate-360-isr/route,_N_T_/route-handler/revalidate-360-isr',
}, },
initialRevalidateSeconds: 10, initialRevalidateSeconds: 10,
srcRoute: '/route-handler/revalidate-360-isr', srcRoute: '/route-handler/revalidate-360-isr',
...@@ -904,7 +904,8 @@ createNextDescribe( ...@@ -904,7 +904,8 @@ createNextDescribe(
dataRoute: null, dataRoute: null,
initialHeaders: { initialHeaders: {
'set-cookie': 'theme=light; Path=/,my_company=ACME; Path=/', 'set-cookie': 'theme=light; Path=/,my_company=ACME; Path=/',
'x-next-cache-tags': '/route-handler/static-cookies/route', 'x-next-cache-tags':
'_N_T_/layout,_N_T_/route-handler/layout,_N_T_/route-handler/static-cookies/layout,_N_T_/route-handler/static-cookies/route,_N_T_/route-handler/static-cookies',
}, },
initialRevalidateSeconds: false, initialRevalidateSeconds: false,
srcRoute: '/route-handler/static-cookies', srcRoute: '/route-handler/static-cookies',
......
...@@ -156,10 +156,22 @@ describe('should set-up next', () => { ...@@ -156,10 +156,22 @@ describe('should set-up next', () => {
it('should send cache tags in minimal mode for ISR', async () => { it('should send cache tags in minimal mode for ISR', async () => {
for (const [path, tags] of [ for (const [path, tags] of [
['/isr/first', 'isr-page,/isr/[slug]/page'], [
['/isr/second', 'isr-page,/isr/[slug]/page'], '/isr/first',
['/api/isr/first', 'isr-page,/api/isr/[slug]/route'], 'isr-page,_N_T_/layout,_N_T_/isr/layout,_N_T_/isr/[slug]/layout,_N_T_/isr/[slug]/page,_N_T_/isr/first',
['/api/isr/second', 'isr-page,/api/isr/[slug]/route'], ],
[
'/isr/second',
'isr-page,_N_T_/layout,_N_T_/isr/layout,_N_T_/isr/[slug]/layout,_N_T_/isr/[slug]/page,_N_T_/isr/second',
],
[
'/api/isr/first',
'isr-page,_N_T_/layout,_N_T_/api/layout,_N_T_/api/isr/layout,_N_T_/api/isr/[slug]/layout,_N_T_/api/isr/[slug]/route,_N_T_/api/isr/first',
],
[
'/api/isr/second',
'isr-page,_N_T_/layout,_N_T_/api/layout,_N_T_/api/isr/layout,_N_T_/api/isr/[slug]/layout,_N_T_/api/isr/[slug]/route,_N_T_/api/isr/second',
],
]) { ]) {
require('console').error('checking', { path, tags }) require('console').error('checking', { path, tags })
const res = await fetchViaHTTP(appPort, path, undefined, { const res = await fetchViaHTTP(appPort, path, undefined, {
......
...@@ -20,14 +20,18 @@ describe('FileSystemCache', () => { ...@@ -20,14 +20,18 @@ describe('FileSystemCache', () => {
fileURLToPath(new URL('./images/icon.png', import.meta.url)) fileURLToPath(new URL('./images/icon.png', import.meta.url))
) )
await fsCache.set('icon.png', { await fsCache.set(
body: binary, 'icon.png',
headers: { {
'Content-Type': 'image/png', body: binary,
headers: {
'Content-Type': 'image/png',
},
status: 200,
kind: 'ROUTE',
}, },
status: 200, {}
kind: 'ROUTE', )
})
expect((await fsCache.get('icon.png')).value).toEqual({ expect((await fsCache.get('icon.png')).value).toEqual({
body: binary, body: binary,
......
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