Skip to content

Commit

Permalink
fix: don't permamently cache fallback html
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Oct 10, 2024
1 parent dbe8dc7 commit 123991e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/run/handlers/request-context.cts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type RequestContext = {
responseCacheGetLastModified?: number
responseCacheKey?: string
responseCacheTags?: string[]
usedFsRead?: boolean
usedFsReadForNonFallback?: boolean
didPagesRouterOnDemandRevalidate?: boolean
serverTiming?: string
routeHandlerRevalidate?: NetlifyCachedRouteValue['revalidate']
Expand Down
2 changes: 1 addition & 1 deletion src/run/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export const setCacheControlHeaders = (
cacheControl === null &&
!headers.has('cdn-cache-control') &&
!headers.has('netlify-cdn-cache-control') &&
requestContext.usedFsRead
requestContext.usedFsReadForNonFallback
) {
// handle CDN Cache Control on static files
headers.set('cache-control', 'public, max-age=0, must-revalidate')
Expand Down
35 changes: 31 additions & 4 deletions src/run/next.cts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import fs from 'fs/promises'
import fs, { readFile } from 'fs/promises'
import { relative, resolve } from 'path'

// @ts-expect-error no types installed
import { patchFs } from 'fs-monkey'
import { type PrerenderManifest } from 'next/dist/build/index.js'

import { getRequestContext } from './handlers/request-context.cjs'
import { getTracer } from './handlers/tracer.cjs'
Expand Down Expand Up @@ -80,6 +81,30 @@ console.timeEnd('import next server')

type FS = typeof import('fs')

function normalizeStaticAssetPath(path: string) {
// just skip leading / for now
return path.replace(/^\/+/g, '')
}

let fallbacks: Array<string> | undefined
async function isFallbackHTML(relPath: string) {
if (!fallbacks) {
fallbacks = []

try {
const prerenderManifest = JSON.parse(
await readFile(resolve('.next/prerender-manifest.json'), 'utf-8'),
) as PrerenderManifest
fallbacks = Object.values(prerenderManifest.dynamicRoutes)
.map((route) => route.fallback)
.filter((fallback) => typeof fallback === 'string')
.map(normalizeStaticAssetPath)
} catch {}
}

return fallbacks.includes(normalizeStaticAssetPath(relPath))
}

export async function getMockedRequestHandlers(...args: Parameters<typeof getRequestHandlers>) {
const tracer = getTracer()
return tracer.withActiveSpan('mocked request handler', async () => {
Expand All @@ -100,9 +125,11 @@ export async function getMockedRequestHandlers(...args: Parameters<typeof getReq
const relPath = relative(resolve('.next/server/pages'), path)
const file = await store.get(await encodeBlobKey(relPath))
if (file !== null) {
const requestContext = getRequestContext()
if (requestContext) {
requestContext.usedFsRead = true
if (!(await isFallbackHTML(relPath))) {
const requestContext = getRequestContext()
if (requestContext) {
requestContext.usedFsReadForNonFallback = true
}
}

return file
Expand Down

0 comments on commit 123991e

Please sign in to comment.