Skip to content

Commit

Permalink
Update experimental compile cache handling (#56139)
Browse files Browse the repository at this point in the history
This ensures we properly set a cache-control header for automatically statically optimized pages so we aren't re-re-rendering every time for them when leveraging this mode.
  • Loading branch information
ijjk committed Sep 28, 2023
1 parent fe90b04 commit 403cb4a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/next/src/build/index.ts
Expand Up @@ -920,6 +920,8 @@ export default async function build(
incrementalCacheHandlerPath: incrementalCacheHandlerPath
? path.relative(distDir, incrementalCacheHandlerPath)
: undefined,

isExperimentalCompile: true,
},
},
appDir: dir,
Expand Down
4 changes: 4 additions & 0 deletions packages/next/src/server/base-server.ts
Expand Up @@ -286,6 +286,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
largePageDataBytes?: number
appDirDevErrorLogger?: (err: any) => Promise<void>
strictNextHead: boolean
isExperimentalCompile?: boolean
}
protected readonly serverOptions: Readonly<ServerOptions>
protected readonly appPathRoutes?: Record<string, string[]>
Expand Down Expand Up @@ -477,6 +478,9 @@ export default abstract class Server<ServerOptions extends Options = Options> {
Object.keys(publicRuntimeConfig).length > 0
? publicRuntimeConfig
: undefined,

// @ts-expect-error internal field not publicly exposed
isExperimentalCompile: this.nextConfig.experimental.isExperimentalCompile,
}

// Initialize next/config with the environment configuration
Expand Down
16 changes: 16 additions & 0 deletions packages/next/src/server/render.tsx
Expand Up @@ -47,6 +47,7 @@ import {
SERVER_PROPS_SSG_CONFLICT,
SSG_GET_INITIAL_PROPS_CONFLICT,
UNSTABLE_REVALIDATE_RENAME_ERROR,
CACHE_ONE_YEAR,
} from '../lib/constants'
import {
COMPILER_NAMES,
Expand Down Expand Up @@ -101,6 +102,7 @@ import {
import { getTracer } from './lib/trace/tracer'
import { RenderSpan } from './lib/trace/constants'
import { ReflectAdapter } from './web/spec-extension/adapters/reflect'
import { setRevalidateHeaders } from './send-payload'

let tryGetPreviewData: typeof import('./api-utils/node/try-get-preview-data').tryGetPreviewData
let warn: typeof import('../build/output/log').warn
Expand Down Expand Up @@ -280,6 +282,7 @@ export type RenderOptsPartial = {
isDraftMode?: boolean
deploymentId?: string
isServerAction?: boolean
isExperimentalCompile?: boolean
}

export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial
Expand Down Expand Up @@ -441,6 +444,7 @@ export async function renderToHTMLImpl(
basePath,
images,
runtime: globalRuntime,
isExperimentalCompile,
} = renderOpts
const { App } = extra

Expand Down Expand Up @@ -497,6 +501,18 @@ export async function renderToHTMLImpl(
!isSSG &&
!getServerSideProps

// if we are running from experimental compile and the page
// would normally be automatically statically optimized
// ensure we set cache header so it's not rendered on-demand
// every request
if (isAutoExport && !dev && isExperimentalCompile) {
setRevalidateHeaders(res, {
revalidate: CACHE_ONE_YEAR,
private: false,
stateful: false,
})
}

if (hasPageGetInitialProps && isSSG) {
throw new Error(SSG_GET_INITIAL_PROPS_CONFLICT + ` ${pathname}`)
}
Expand Down

0 comments on commit 403cb4a

Please sign in to comment.