Unverified Commit af89adbd authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update fetch cache memory handling (#47465)

This ensures we only honor cache entries from the in memory cache for up
to 2 seconds so that revalidates can correctly propagate and also
increases max fetch cache entry size to 2 MB. The `fetchCache` export is
also being detected in this PR but not yet honored which will be done in
a follow-up.
parent c7cbb4aa
...@@ -62,3 +62,5 @@ jobs: ...@@ -62,3 +62,5 @@ jobs:
- run: pnpm run build - run: pnpm run build
- run: node ./scripts/start-release.js --release-type ${{ github.event.inputs.releaseType }} --semver-type ${{ github.event.inputs.semverType }} - run: node ./scripts/start-release.js --release-type ${{ github.event.inputs.releaseType }} --semver-type ${{ github.event.inputs.semverType }}
env:
START_RELEASE_TOKEN: ${{ secrets.START_RELEASE_TOKEN }}
...@@ -9,6 +9,12 @@ export interface StaticGenerationStore { ...@@ -9,6 +9,12 @@ export interface StaticGenerationStore {
readonly isPrerendering?: boolean readonly isPrerendering?: boolean
forceDynamic?: boolean forceDynamic?: boolean
fetchCache?:
| 'only-cache'
| 'force-cache'
| 'force-no-store'
| 'default-no-store'
| 'only-no-store'
revalidate?: false | number revalidate?: false | number
forceStatic?: boolean forceStatic?: boolean
dynamicShouldError?: boolean dynamicShouldError?: boolean
......
...@@ -530,6 +530,10 @@ export async function renderToHTMLOrFlight( ...@@ -530,6 +530,10 @@ export async function renderToHTMLOrFlight(
} }
} }
if (typeof layoutOrPageMod?.fetchCache === 'string') {
staticGenerationStore.fetchCache = layoutOrPageMod?.fetchCache
}
if (typeof layoutOrPageMod?.revalidate === 'number') { if (typeof layoutOrPageMod?.revalidate === 'number') {
defaultRevalidate = layoutOrPageMod.revalidate as number defaultRevalidate = layoutOrPageMod.revalidate as number
......
...@@ -11,6 +11,7 @@ export type RequestContext = { ...@@ -11,6 +11,7 @@ export type RequestContext = {
isRevalidate?: boolean isRevalidate?: boolean
isBot?: boolean isBot?: boolean
nextExport?: boolean nextExport?: boolean
fetchCache?: StaticGenerationStore['fetchCache']
} }
} }
...@@ -55,6 +56,7 @@ export class StaticGenerationAsyncStorageWrapper ...@@ -55,6 +56,7 @@ export class StaticGenerationAsyncStorageWrapper
incrementalCache: renderOpts.incrementalCache, incrementalCache: renderOpts.incrementalCache,
isRevalidate: renderOpts.isRevalidate, isRevalidate: renderOpts.isRevalidate,
isPrerendering: renderOpts.nextExport, isPrerendering: renderOpts.nextExport,
fetchCache: renderOpts.fetchCache,
} }
;(renderOpts as any).store = store ;(renderOpts as any).store = store
......
...@@ -1485,6 +1485,15 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1485,6 +1485,15 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if (!headers['content-type'] && blob.type) { if (!headers['content-type'] && blob.type) {
headers['content-type'] = blob.type headers['content-type'] = blob.type
} }
let revalidate: number | false | undefined = (
(context as any).store as any as
| { revalidate?: number }
| undefined
)?.revalidate
if (typeof revalidate == 'undefined') {
revalidate = false
}
// Create the cache entry for the response. // Create the cache entry for the response.
const cacheEntry: ResponseCacheEntry = { const cacheEntry: ResponseCacheEntry = {
...@@ -1494,9 +1503,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1494,9 +1503,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
body: Buffer.from(await blob.arrayBuffer()), body: Buffer.from(await blob.arrayBuffer()),
headers, headers,
}, },
revalidate: revalidate,
((context as any).store as any as { revalidate?: number })
.revalidate || false,
} }
return cacheEntry return cacheEntry
......
...@@ -31,7 +31,10 @@ import type { ModuleLoader } from '../helpers/module-loader/module-loader' ...@@ -31,7 +31,10 @@ import type { ModuleLoader } from '../helpers/module-loader/module-loader'
import { RouteHandler } from './route-handler' import { RouteHandler } from './route-handler'
import * as Log from '../../../build/output/log' import * as Log from '../../../build/output/log'
import { patchFetch } from '../../lib/patch-fetch' import { patchFetch } from '../../lib/patch-fetch'
import { StaticGenerationAsyncStorage } from '../../../client/components/static-generation-async-storage' import {
StaticGenerationAsyncStorage,
StaticGenerationStore,
} from '../../../client/components/static-generation-async-storage'
import { StaticGenerationAsyncStorageWrapper } from '../../async-storage/static-generation-async-storage-wrapper' import { StaticGenerationAsyncStorageWrapper } from '../../async-storage/static-generation-async-storage-wrapper'
import { IncrementalCache } from '../../lib/incremental-cache' import { IncrementalCache } from '../../lib/incremental-cache'
import { AppConfig } from '../../../build/utils' import { AppConfig } from '../../../build/utils'
...@@ -74,7 +77,8 @@ export type AppRouteModule = { ...@@ -74,7 +77,8 @@ export type AppRouteModule = {
*/ */
handlers: Record<HTTP_METHOD, AppRouteHandlerFn> & handlers: Record<HTTP_METHOD, AppRouteHandlerFn> &
Record<'dynamic', AppConfig['dynamic']> & Record<'dynamic', AppConfig['dynamic']> &
Record<'revalidate', AppConfig['revalidate']> Record<'revalidate', AppConfig['revalidate']> &
Record<'fetchCache', AppConfig['fetchCache']>
/** /**
* The exported async storage object for this worker/module. * The exported async storage object for this worker/module.
...@@ -99,6 +103,7 @@ export type StaticGenerationContext = { ...@@ -99,6 +103,7 @@ export type StaticGenerationContext = {
incrementalCache?: IncrementalCache incrementalCache?: IncrementalCache
supportsDynamicHTML: boolean supportsDynamicHTML: boolean
nextExport?: boolean nextExport?: boolean
fetchCache?: StaticGenerationStore['fetchCache']
} }
/** /**
...@@ -473,6 +478,10 @@ export class AppRouteRouteHandler implements RouteHandler<AppRouteRouteMatch> { ...@@ -473,6 +478,10 @@ export class AppRouteRouteHandler implements RouteHandler<AppRouteRouteMatch> {
res: (res as NodeNextResponse).originalResponse, res: (res as NodeNextResponse).originalResponse,
} }
if (context) {
context.fetchCache = module.handlers.fetchCache
}
// Run the handler with the request AsyncLocalStorage to inject the helper // Run the handler with the request AsyncLocalStorage to inject the helper
// support. // support.
const response = await this.requestAsyncLocalStorageWrapper.wrap( const response = await this.requestAsyncLocalStorageWrapper.wrap(
...@@ -485,6 +494,7 @@ export class AppRouteRouteHandler implements RouteHandler<AppRouteRouteMatch> { ...@@ -485,6 +494,7 @@ export class AppRouteRouteHandler implements RouteHandler<AppRouteRouteMatch> {
pathname: definition.pathname, pathname: definition.pathname,
renderOpts: context ?? { renderOpts: context ?? {
supportsDynamicHTML: false, supportsDynamicHTML: false,
fetchCache: module.handlers.fetchCache,
}, },
}, },
(staticGenerationStore) => { (staticGenerationStore) => {
......
...@@ -71,6 +71,12 @@ export default class FetchCache implements CacheHandler { ...@@ -71,6 +71,12 @@ export default class FetchCache implements CacheHandler {
let data = memoryCache?.get(key) let data = memoryCache?.get(key)
// memory cache data is only leveraged for up to 1 seconds
// so that revalidation events can be pulled from source
if (Date.now() - (data?.lastModified || 0) > 2000) {
data = undefined
}
// get data from fetch cache // get data from fetch cache
if (!data && this.cacheEndpoint) { if (!data && this.cacheEndpoint) {
try { try {
......
...@@ -351,8 +351,8 @@ export class IncrementalCache { ...@@ -351,8 +351,8 @@ export class IncrementalCache {
fetchCache?: boolean fetchCache?: boolean
) { ) {
if (this.dev && !fetchCache) return if (this.dev && !fetchCache) return
// fetchCache has upper limit of 1MB per-entry currently // fetchCache has upper limit of 2MB per-entry currently
if (fetchCache && JSON.stringify(data).length > 1024 * 1024) { if (fetchCache && JSON.stringify(data).length > 2 * 1024 * 1024) {
if (this.dev) { if (this.dev) {
throw new Error(`fetch for over 1MB of data can not be cached`) throw new Error(`fetch for over 1MB of data can not be cached`)
} }
......
...@@ -99,6 +99,15 @@ export function patchFetch({ ...@@ -99,6 +99,15 @@ export function patchFetch({
? _headers ? _headers
: new Headers(_headers || {}) : new Headers(_headers || {})
// const isOnlyCache = staticGenerationStore.fetchCache === 'only-cache'
// const isForceCache = staticGenerationStore.fetchCache === 'force-cache'
// const isDefaultNoStore =
// staticGenerationStore.fetchCache === 'default-no-store'
// const isOnlyNoStore =
// staticGenerationStore.fetchCache === 'only-no-store'
// const isForceNoStore =
// staticGenerationStore.fetchCache === 'force-no-store'
const hasUnCacheableHeader = const hasUnCacheableHeader =
initHeaders.get('authorization') || initHeaders.get('cookie') initHeaders.get('authorization') || initHeaders.get('cookie')
......
...@@ -7,7 +7,7 @@ async function main() { ...@@ -7,7 +7,7 @@ async function main() {
const args = process.argv const args = process.argv
const releaseType = args[args.indexOf('--release-type') + 1] const releaseType = args[args.indexOf('--release-type') + 1]
const semverType = args[args.indexOf('--semver-type') + 1] const semverType = args[args.indexOf('--semver-type') + 1]
const isCanary = releaseType === 'canary' const isCanary = releaseType !== 'stable'
if (releaseType !== 'stable' && releaseType !== 'canary') { if (releaseType !== 'stable' && releaseType !== 'canary') {
console.log(`Invalid release type ${releaseType}, must be stable or canary`) console.log(`Invalid release type ${releaseType}, must be stable or canary`)
......
...@@ -127,20 +127,32 @@ function runTests(mode) { ...@@ -127,20 +127,32 @@ function runTests(mode) {
const imagesizes = await link.getAttribute('imagesizes') const imagesizes = await link.getAttribute('imagesizes')
entries.push({ fetchpriority, imagesrcset, imagesizes }) entries.push({ fetchpriority, imagesrcset, imagesizes })
} }
expect(entries).toEqual([
{ expect(
entries.find(
(item) =>
item.imagesrcset ===
'/_next/image?url=%2Ftest.jpg&w=640&q=75 1x, /_next/image?url=%2Ftest.jpg&w=828&q=75 2x'
)
).toEqual({
fetchpriority: 'high', fetchpriority: 'high',
imagesizes: '', imagesizes: '',
imagesrcset: imagesrcset:
'/_next/image?url=%2Ftest.jpg&w=640&q=75 1x, /_next/image?url=%2Ftest.jpg&w=828&q=75 2x', '/_next/image?url=%2Ftest.jpg&w=640&q=75 1x, /_next/image?url=%2Ftest.jpg&w=828&q=75 2x',
}, })
{
expect(
entries.find(
(item) =>
item.imagesrcset ===
'/_next/image?url=%2Fwide.png&w=640&q=75 640w, /_next/image?url=%2Fwide.png&w=750&q=75 750w, /_next/image?url=%2Fwide.png&w=828&q=75 828w, /_next/image?url=%2Fwide.png&w=1080&q=75 1080w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w, /_next/image?url=%2Fwide.png&w=1920&q=75 1920w, /_next/image?url=%2Fwide.png&w=2048&q=75 2048w, /_next/image?url=%2Fwide.png&w=3840&q=75 3840w'
)
).toEqual({
fetchpriority: 'high', fetchpriority: 'high',
imagesizes: '100vw', imagesizes: '100vw',
imagesrcset: imagesrcset:
'/_next/image?url=%2Fwide.png&w=640&q=75 640w, /_next/image?url=%2Fwide.png&w=750&q=75 750w, /_next/image?url=%2Fwide.png&w=828&q=75 828w, /_next/image?url=%2Fwide.png&w=1080&q=75 1080w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w, /_next/image?url=%2Fwide.png&w=1920&q=75 1920w, /_next/image?url=%2Fwide.png&w=2048&q=75 2048w, /_next/image?url=%2Fwide.png&w=3840&q=75 3840w', '/_next/image?url=%2Fwide.png&w=640&q=75 640w, /_next/image?url=%2Fwide.png&w=750&q=75 750w, /_next/image?url=%2Fwide.png&w=828&q=75 828w, /_next/image?url=%2Fwide.png&w=1080&q=75 1080w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w, /_next/image?url=%2Fwide.png&w=1920&q=75 1920w, /_next/image?url=%2Fwide.png&w=2048&q=75 2048w, /_next/image?url=%2Fwide.png&w=3840&q=75 3840w',
}, })
])
// When priority={true}, we should _not_ set loading="lazy" // When priority={true}, we should _not_ set loading="lazy"
expect( expect(
......
...@@ -126,20 +126,31 @@ function runTests(mode) { ...@@ -126,20 +126,31 @@ function runTests(mode) {
const imagesizes = await link.getAttribute('imagesizes') const imagesizes = await link.getAttribute('imagesizes')
entries.push({ fetchpriority, imagesrcset, imagesizes }) entries.push({ fetchpriority, imagesrcset, imagesizes })
} }
expect(entries).toEqual([ expect(
{ entries.find(
(item) =>
item.imagesrcset ===
'/_next/image?url=%2Ftest.jpg&w=640&q=75 1x, /_next/image?url=%2Ftest.jpg&w=828&q=75 2x'
)
).toEqual({
fetchpriority: 'high', fetchpriority: 'high',
imagesizes: '', imagesizes: '',
imagesrcset: imagesrcset:
'/_next/image?url=%2Ftest.jpg&w=640&q=75 1x, /_next/image?url=%2Ftest.jpg&w=828&q=75 2x', '/_next/image?url=%2Ftest.jpg&w=640&q=75 1x, /_next/image?url=%2Ftest.jpg&w=828&q=75 2x',
}, })
{
expect(
entries.find(
(item) =>
item.imagesrcset ===
'/_next/image?url=%2Fwide.png&w=640&q=75 640w, /_next/image?url=%2Fwide.png&w=750&q=75 750w, /_next/image?url=%2Fwide.png&w=828&q=75 828w, /_next/image?url=%2Fwide.png&w=1080&q=75 1080w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w, /_next/image?url=%2Fwide.png&w=1920&q=75 1920w, /_next/image?url=%2Fwide.png&w=2048&q=75 2048w, /_next/image?url=%2Fwide.png&w=3840&q=75 3840w'
)
).toEqual({
fetchpriority: 'high', fetchpriority: 'high',
imagesizes: '100vw', imagesizes: '100vw',
imagesrcset: imagesrcset:
'/_next/image?url=%2Fwide.png&w=640&q=75 640w, /_next/image?url=%2Fwide.png&w=750&q=75 750w, /_next/image?url=%2Fwide.png&w=828&q=75 828w, /_next/image?url=%2Fwide.png&w=1080&q=75 1080w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w, /_next/image?url=%2Fwide.png&w=1920&q=75 1920w, /_next/image?url=%2Fwide.png&w=2048&q=75 2048w, /_next/image?url=%2Fwide.png&w=3840&q=75 3840w', '/_next/image?url=%2Fwide.png&w=640&q=75 640w, /_next/image?url=%2Fwide.png&w=750&q=75 750w, /_next/image?url=%2Fwide.png&w=828&q=75 828w, /_next/image?url=%2Fwide.png&w=1080&q=75 1080w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w, /_next/image?url=%2Fwide.png&w=1920&q=75 1920w, /_next/image?url=%2Fwide.png&w=2048&q=75 2048w, /_next/image?url=%2Fwide.png&w=3840&q=75 3840w',
}, })
])
// When priority={true}, we should _not_ set loading="lazy" // When priority={true}, we should _not_ set loading="lazy"
expect( expect(
...@@ -187,10 +198,12 @@ function runTests(mode) { ...@@ -187,10 +198,12 @@ function runTests(mode) {
// should preload with crossorigin // should preload with crossorigin
expect( expect(
(
await browser.elementsByCss( await browser.elementsByCss(
'link[rel=preload][as=image][crossorigin=anonymous][imagesrcset*="test.jpg"]' 'link[rel=preload][as=image][crossorigin=anonymous][imagesrcset*="test.jpg"]'
) )
).toHaveLength(1) ).length
).toBeGreaterThanOrEqual(1)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
......
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