Unverified Commit 53123ad6 authored by Gerald Monaco's avatar Gerald Monaco Committed by GitHub

More explicit typing for `IncrementalCache` API (#26941)

Make the typing for `IncrementalCache` more explicit. With streaming, we’ll want to stream page data as well as HTML. This is a bit complicated now because we’re overloading `pageData` for both redirects and pages.

This PR makes the different types explicit. With streaming, the data for redirects is synchronously available, while the data for pages will become a stream.

A follow up PR will add a “stream through” cache in front of `IncrementalCache` 
parent d476c91a
...@@ -9,15 +9,25 @@ function toRoute(pathname: string): string { ...@@ -9,15 +9,25 @@ function toRoute(pathname: string): string {
return pathname.replace(/\/$/, '').replace(/\/index$/, '') || '/' return pathname.replace(/\/$/, '').replace(/\/index$/, '') || '/'
} }
type IncrementalCacheValue = { interface CachedRedirectValue {
html?: string kind: 'REDIRECT'
pageData?: any props: Object
isStale?: boolean }
isNotFound?: boolean
isRedirect?: boolean interface CachedPageValue {
kind: 'PAGE'
html: string
pageData: Object
}
export type IncrementalCacheValue = CachedRedirectValue | CachedPageValue
type IncrementalCacheEntry = {
curRevalidate?: number | false curRevalidate?: number | false
// milliseconds to revalidate after // milliseconds to revalidate after
revalidateAfter: number | false revalidateAfter: number | false
isStale?: boolean
value: IncrementalCacheValue | null
} }
export class IncrementalCache { export class IncrementalCache {
...@@ -29,7 +39,7 @@ export class IncrementalCache { ...@@ -29,7 +39,7 @@ export class IncrementalCache {
} }
prerenderManifest: PrerenderManifest prerenderManifest: PrerenderManifest
cache: LRUCache<string, IncrementalCacheValue> cache: LRUCache<string, IncrementalCacheEntry>
locales?: string[] locales?: string[]
constructor({ constructor({
...@@ -73,10 +83,10 @@ export class IncrementalCache { ...@@ -73,10 +83,10 @@ export class IncrementalCache {
this.cache = new LRUCache({ this.cache = new LRUCache({
// default to 50MB limit // default to 50MB limit
max: max || 50 * 1024 * 1024, max: max || 50 * 1024 * 1024,
length(val) { length({ value }) {
if (val.isNotFound || val.isRedirect) return 25 if (!value || value.kind === 'REDIRECT') return 25
// rough estimate of size of cache value // rough estimate of size of cache value
return val.html!.length + JSON.stringify(val.pageData).length return value.html.length + JSON.stringify(value.pageData).length
}, },
}) })
} }
...@@ -112,8 +122,8 @@ export class IncrementalCache { ...@@ -112,8 +122,8 @@ export class IncrementalCache {
} }
// get data from cache if available // get data from cache if available
async get(pathname: string): Promise<IncrementalCacheValue | void> { async get(pathname: string): Promise<IncrementalCacheEntry | null> {
if (this.incrementalOptions.dev) return if (this.incrementalOptions.dev) return null
pathname = normalizePagePath(pathname) pathname = normalizePagePath(pathname)
let data = this.cache.get(pathname) let data = this.cache.get(pathname)
...@@ -121,7 +131,7 @@ export class IncrementalCache { ...@@ -121,7 +131,7 @@ export class IncrementalCache {
// let's check the disk for seed data // let's check the disk for seed data
if (!data) { if (!data) {
if (this.prerenderManifest.notFoundRoutes.includes(pathname)) { if (this.prerenderManifest.notFoundRoutes.includes(pathname)) {
return { isNotFound: true, revalidateAfter: false } return { revalidateAfter: false, value: null }
} }
try { try {
...@@ -134,15 +144,21 @@ export class IncrementalCache { ...@@ -134,15 +144,21 @@ export class IncrementalCache {
) )
data = { data = {
html,
pageData,
revalidateAfter: this.calculateRevalidate(pathname), revalidateAfter: this.calculateRevalidate(pathname),
value: {
kind: 'PAGE',
html,
pageData,
},
} }
this.cache.set(pathname, data) this.cache.set(pathname, data)
} catch (_) { } catch (_) {
// unable to get data from disk // unable to get data from disk
} }
} }
if (!data) {
return null
}
if ( if (
data && data &&
...@@ -164,12 +180,7 @@ export class IncrementalCache { ...@@ -164,12 +180,7 @@ export class IncrementalCache {
// populate the incremental cache with new data // populate the incremental cache with new data
async set( async set(
pathname: string, pathname: string,
data: { data: IncrementalCacheValue | null,
html?: string
pageData?: any
isNotFound?: boolean
isRedirect?: boolean
},
revalidateSeconds?: number | false revalidateSeconds?: number | false
) { ) {
if (this.incrementalOptions.dev) return if (this.incrementalOptions.dev) return
...@@ -188,17 +199,13 @@ export class IncrementalCache { ...@@ -188,17 +199,13 @@ export class IncrementalCache {
pathname = normalizePagePath(pathname) pathname = normalizePagePath(pathname)
this.cache.set(pathname, { this.cache.set(pathname, {
...data,
revalidateAfter: this.calculateRevalidate(pathname), revalidateAfter: this.calculateRevalidate(pathname),
value: data,
}) })
// TODO: This option needs to cease to exist unless it stops mutating the // TODO: This option needs to cease to exist unless it stops mutating the
// `next build` output's manifest. // `next build` output's manifest.
if ( if (this.incrementalOptions.flushToDisk && data?.kind === 'PAGE') {
this.incrementalOptions.flushToDisk &&
!data.isNotFound &&
!data.isRedirect
) {
try { try {
const seedPath = this.getSeedPath(pathname, 'html') const seedPath = this.getSeedPath(pathname, 'html')
await promises.mkdir(path.dirname(seedPath), { recursive: true }) await promises.mkdir(path.dirname(seedPath), { recursive: true })
......
...@@ -73,7 +73,7 @@ import prepareDestination, { ...@@ -73,7 +73,7 @@ import prepareDestination, {
} from '../shared/lib/router/utils/prepare-destination' } from '../shared/lib/router/utils/prepare-destination'
import { sendPayload, setRevalidateHeaders } from './send-payload' import { sendPayload, setRevalidateHeaders } from './send-payload'
import { serveStatic } from './serve-static' import { serveStatic } from './serve-static'
import { IncrementalCache } from './incremental-cache' import { IncrementalCache, IncrementalCacheValue } from './incremental-cache'
import { execOnce } from '../shared/lib/utils' import { execOnce } from '../shared/lib/utils'
import { isBlockedPage } from './utils' import { isBlockedPage } from './utils'
import { loadEnvConfig } from '@next/env' import { loadEnvConfig } from '@next/env'
...@@ -1653,30 +1653,25 @@ export default class Server { ...@@ -1653,30 +1653,25 @@ export default class Server {
} }
// Complete the response with cached data if its present // Complete the response with cached data if its present
const cachedData = ssgCacheKey const cacheEntry = ssgCacheKey
? await this.incrementalCache.get(ssgCacheKey) ? await this.incrementalCache.get(ssgCacheKey)
: undefined : undefined
if (cachedData) { if (cacheEntry) {
const data = isDataReq const cachedData = cacheEntry.value
? JSON.stringify(cachedData.pageData)
: cachedData.html
const revalidateOptions = !this.renderOpts.dev const revalidateOptions = !this.renderOpts.dev
? { ? {
// When the page is 404 cache-control should not be added // When the page is 404 cache-control should not be added
private: isPreviewMode || is404Page, private: isPreviewMode || is404Page,
stateful: false, // GSP response stateful: false, // GSP response
revalidate: revalidate:
cachedData.curRevalidate !== undefined cacheEntry.curRevalidate !== undefined
? cachedData.curRevalidate ? cacheEntry.curRevalidate
: /* default to minimum revalidate (this should be an invariant) */ 1, : /* default to minimum revalidate (this should be an invariant) */ 1,
} }
: undefined : undefined
if (!isDataReq && cachedData.pageData?.pageProps?.__N_REDIRECT) { if (!cachedData) {
await handleRedirect(cachedData.pageData)
} else if (cachedData.isNotFound) {
if (revalidateOptions) { if (revalidateOptions) {
setRevalidateHeaders(res, revalidateOptions) setRevalidateHeaders(res, revalidateOptions)
} }
...@@ -1689,16 +1684,34 @@ export default class Server { ...@@ -1689,16 +1684,34 @@ export default class Server {
query, query,
} as UrlWithParsedQuery) } as UrlWithParsedQuery)
} }
} else if (cachedData.kind === 'REDIRECT') {
if (isDataReq) {
sendPayload(
req,
res,
JSON.stringify(cachedData.props),
'json',
{
generateEtags: this.renderOpts.generateEtags,
poweredByHeader: this.renderOpts.poweredByHeader,
},
revalidateOptions
)
} else {
await handleRedirect(cachedData.props)
}
} else { } else {
respondWith({ respondWith({
type: isDataReq ? 'json' : 'html', type: isDataReq ? 'json' : 'html',
body: data!, body: isDataReq
? JSON.stringify(cachedData.pageData)
: cachedData.html,
revalidateOptions, revalidateOptions,
}) })
} }
// Stop the request chain here if the data we sent was up-to-date // Stop the request chain here if the data we sent was up-to-date
if (!cachedData.isStale) { if (!cacheEntry.isStale) {
return return
} }
} }
...@@ -1912,11 +1925,15 @@ export default class Server { ...@@ -1912,11 +1925,15 @@ export default class Server {
// Update the cache if the head request and cacheable // Update the cache if the head request and cacheable
if (isOrigin && ssgCacheKey) { if (isOrigin && ssgCacheKey) {
await this.incrementalCache.set( let cacheValue: IncrementalCacheValue | null
ssgCacheKey, if (isNotFound) {
{ html: html!, pageData, isNotFound, isRedirect }, cacheValue = null
sprRevalidate } else if (isRedirect) {
) cacheValue = { kind: 'REDIRECT', props: pageData }
} else {
cacheValue = { kind: 'PAGE', html, pageData }
}
await this.incrementalCache.set(ssgCacheKey, cacheValue, sprRevalidate)
} }
if (!hasResponded() && isNotFound) { if (!hasResponded() && isNotFound) {
......
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