85e720a092
* initial commit for SPRv2 * Add initial SPR cache handling * update SPR handling * Implement SPR handling in render * Update tests, handle caching with serverless next start, add TODOs, and update manifest generating * Handle no prerender-manifest from not being used * Fix url.parse error * Apply suggestions from code review Co-Authored-By: Joe Haddad <joe.haddad@zeit.co> * Replace set with constants in next-page-config * simplify sprStatus.used * Add error if getStaticProps is used with getInitialProps * Remove stale TODO * Update revalidate values in SPR cache for non-seeded routes * Apply suggestions from code review * Remove concurrency type * Rename variable for clarity * Add copying prerender files during export * Add comment for clarity * Fix exporting * Update comment * Add additional note * Rename variable * Update to not re-export SPR pages from build * Hard navigate when fetching data fails * Remove default extension * Add brackets * Add checking output files to prerender tests * Adjust export move logic * Clarify behavior of export aggregation * Update variable names for clarity * Update tests * Add comment * s/an oxymoron/contradictory/ * rename * Extract error case * Add tests for exporting SPR pages and update /_next/data endpoint to end with .json * Relocate variable * Adjust route building * Rename to unstable * Rename unstable_getStaticParams * Fix linting * Only add this when a data request * Update prerender data tests * s/isServerless/isLikeServerless/ * Don't rely on query for `next start` in serverless mode * Rename var * Update renderedDuringBuild check * Add test for dynamic param with bracket * Fix serverless next start handling * remove todo * Adjust comment * Update calculateRevalidate * Remove cache logic from render.tsx * Remove extra imports * Move SPR cache logic to next-server * Remove old isDynamic prop * Add calling App getInitialProps for SPR pages * Update revalidate logic * Add isStale to SprCacheValue * Update headers for SPR * add awaiting pendingRevalidation * Dont return null for revalidation render * Adjust logic * Be sure to remove coalesced render * Fix data for serverless * Create a method coalescing utility * Remove TODO * Extract send payload helper * Wrap in-line * Move around some code * Add tests for de-duping and revalidating * Update prerender manifest test
163 lines
3.8 KiB
TypeScript
163 lines
3.8 KiB
TypeScript
import fs from 'fs'
|
|
import path from 'path'
|
|
import LRUCache from 'lru-cache'
|
|
import { promisify } from 'util'
|
|
import { PrerenderManifest } from '../../build'
|
|
import { PRERENDER_MANIFEST } from '../lib/constants'
|
|
import { normalizePagePath } from './normalize-page-path'
|
|
|
|
const readFile = promisify(fs.readFile)
|
|
const writeFile = promisify(fs.writeFile)
|
|
|
|
type SprCacheValue = {
|
|
html: string
|
|
pageData: any
|
|
isStale?: boolean
|
|
// milliseconds to revalidate after
|
|
revalidateAfter: number | false
|
|
}
|
|
|
|
let cache: LRUCache<string, SprCacheValue>
|
|
let prerenderManifest: PrerenderManifest
|
|
let sprOptions: {
|
|
flushToDisk?: boolean
|
|
pagesDir?: string
|
|
distDir?: string
|
|
dev?: boolean
|
|
} = {}
|
|
|
|
const getSeedPath = (pathname: string, ext: string): string => {
|
|
return path.join(sprOptions.pagesDir!, `${pathname}.${ext}`)
|
|
}
|
|
|
|
export const calculateRevalidate = (pathname: string): number | false => {
|
|
// in development we don't have a prerender-manifest
|
|
// and default to always revalidating to allow easier debugging
|
|
const curTime = new Date().getTime()
|
|
if (!sprOptions.dev) return curTime
|
|
|
|
const { initialRevalidateSeconds } = prerenderManifest.routes[pathname] || {
|
|
initialRevalidateSeconds: 1,
|
|
}
|
|
const revalidateAfter =
|
|
typeof initialRevalidateSeconds === 'number'
|
|
? initialRevalidateSeconds * 1000 + curTime
|
|
: initialRevalidateSeconds
|
|
|
|
return revalidateAfter
|
|
}
|
|
|
|
// initialize the SPR cache
|
|
export function initializeSprCache({
|
|
max,
|
|
dev,
|
|
distDir,
|
|
pagesDir,
|
|
flushToDisk,
|
|
}: {
|
|
dev: boolean
|
|
max?: number
|
|
distDir: string
|
|
pagesDir: string
|
|
flushToDisk?: boolean
|
|
}) {
|
|
sprOptions = {
|
|
dev,
|
|
distDir,
|
|
pagesDir,
|
|
flushToDisk:
|
|
!dev && (typeof flushToDisk !== 'undefined' ? flushToDisk : true),
|
|
}
|
|
|
|
try {
|
|
prerenderManifest = dev
|
|
? { routes: {} }
|
|
: JSON.parse(
|
|
fs.readFileSync(path.join(distDir, PRERENDER_MANIFEST), 'utf8')
|
|
)
|
|
} catch (_) {
|
|
prerenderManifest = { version: 1, routes: {} }
|
|
}
|
|
|
|
cache = new LRUCache({
|
|
// default to 50MB limit
|
|
max: max || 50 * 1024 * 1024,
|
|
length(val) {
|
|
// rough estimate of size of cache value
|
|
return val.html.length + JSON.stringify(val.pageData).length
|
|
},
|
|
})
|
|
}
|
|
|
|
// get data from SPR cache if available
|
|
export async function getSprCache(
|
|
pathname: string
|
|
): Promise<SprCacheValue | undefined> {
|
|
pathname = normalizePagePath(pathname)
|
|
|
|
let data: SprCacheValue | undefined = cache.get(pathname)
|
|
|
|
// let's check the disk for seed data
|
|
if (!data) {
|
|
try {
|
|
const html = await readFile(getSeedPath(pathname, 'html'), 'utf8')
|
|
const pageData = JSON.parse(
|
|
await readFile(getSeedPath(pathname, 'json'), 'utf8')
|
|
)
|
|
|
|
data = {
|
|
html,
|
|
pageData,
|
|
revalidateAfter: calculateRevalidate(pathname),
|
|
}
|
|
cache.set(pathname, data)
|
|
} catch (_) {
|
|
// unable to get data from disk
|
|
}
|
|
}
|
|
|
|
if (
|
|
data &&
|
|
data.revalidateAfter !== false &&
|
|
data.revalidateAfter < new Date().getTime()
|
|
) {
|
|
data.isStale = true
|
|
}
|
|
return data
|
|
}
|
|
|
|
// populate the SPR cache with new data
|
|
export async function setSprCache(
|
|
pathname: string,
|
|
data: {
|
|
html: string
|
|
pageData: any
|
|
},
|
|
revalidateSeconds?: number | false
|
|
) {
|
|
if (typeof revalidateSeconds !== 'undefined') {
|
|
prerenderManifest.routes[pathname] = {
|
|
initialRevalidateSeconds: revalidateSeconds,
|
|
}
|
|
}
|
|
|
|
pathname = normalizePagePath(pathname)
|
|
cache.set(pathname, {
|
|
...data,
|
|
revalidateAfter: calculateRevalidate(pathname),
|
|
})
|
|
|
|
if (sprOptions.flushToDisk) {
|
|
try {
|
|
await writeFile(getSeedPath(pathname, 'html'), data.html, 'utf8')
|
|
await writeFile(
|
|
getSeedPath(pathname, 'json'),
|
|
JSON.stringify(data.pageData),
|
|
'utf8'
|
|
)
|
|
} catch (error) {
|
|
// failed to flush to disk
|
|
console.warn('Failed to update prerender files for', pathname, error)
|
|
}
|
|
}
|
|
}
|