Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update revalidate handling for app #49062

Merged
merged 8 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/next/cache.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { unstable_cache } from 'next/dist/server/web/spec-extension/unstable-cache'
export { unstable_revalidatePath } from 'next/dist/server/web/spec-extension/unstable-revalidate-path'
export { unstable_revalidateTag } from 'next/dist/server/web/spec-extension/unstable-revalidate-tag'
19 changes: 19 additions & 0 deletions packages/next/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const cacheExports = {
unstable_cache: require('next/dist/server/web/spec-extension/unstable-cache')
.unstable_cache,
unstable_revalidateTag:
require('next/dist/server/web/spec-extension/unstable-revalidate-tag')
.unstable_revalidateTag,
unstable_revalidatePath:
require('next/dist/server/web/spec-extension/unstable-revalidate-path')
.unstable_revalidatePath,
}

// https://nodejs.org/api/esm.html#commonjs-namespaces
// When importing CommonJS modules, the module.exports object is provided as the default export
module.exports = cacheExports

// make import { xxx } from 'next/server' work
exports.unstable_cache = cacheExports.unstable_cache
exports.unstable_revalidatePath = cacheExports.unstable_revalidatePath
exports.unstable_revalidateTag = cacheExports.unstable_revalidateTag
1 change: 1 addition & 0 deletions packages/next/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/// <reference path="./dist/styled-jsx/types/global.d.ts" />
/// <reference path="./amp.d.ts" />
/// <reference path="./app.d.ts" />
/// <reference path="./cache.d.ts" />
/// <reference path="./config.d.ts" />
/// <reference path="./document.d.ts" />
/// <reference path="./dynamic.d.ts" />
Expand Down
2 changes: 2 additions & 0 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"client.js",
"client.d.ts",
"compat",
"cache.js",
"cache.d.ts",
"config.js",
"config.d.ts",
"constants.js",
Expand Down
3 changes: 0 additions & 3 deletions packages/next/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,3 @@ export { userAgent } from 'next/dist/server/web/spec-extension/user-agent'
export { URLPattern } from 'next/dist/compiled/@edge-runtime/primitives/url'
export { ImageResponse } from 'next/dist/server/web/spec-extension/image-response'
export type { ImageResponseOptions } from 'next/dist/compiled/@vercel/og/types'
export { unstable_cache } from 'next/dist/server/web/spec-extension/unstable-cache'
export { unstable_revalidatePath } from 'next/dist/server/web/spec-extension/unstable-revalidate-path'
export { unstable_revalidateTag } from 'next/dist/server/web/spec-extension/unstable-revalidate-tag'
11 changes: 0 additions & 11 deletions packages/next/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ const serverExports = {
.NextResponse,
ImageResponse: require('next/dist/server/web/spec-extension/image-response')
.ImageResponse,
unstable_cache: require('next/dist/server/web/spec-extension/unstable-cache')
.unstable_cache,
unstable_revalidateTag:
require('next/dist/server/web/spec-extension/unstable-revalidate-tag')
.unstable_revalidateTag,
unstable_revalidatePath:
require('next/dist/server/web/spec-extension/unstable-revalidate-path')
.unstable_revalidatePath,
userAgentFromString: require('next/dist/server/web/spec-extension/user-agent')
.userAgentFromString,
userAgent: require('next/dist/server/web/spec-extension/user-agent')
Expand All @@ -32,9 +24,6 @@ module.exports = serverExports
exports.NextRequest = serverExports.NextRequest
exports.NextResponse = serverExports.NextResponse
exports.ImageResponse = serverExports.ImageResponse
exports.unstable_cache = serverExports.unstable_cache
exports.unstable_revalidatePath = serverExports.unstable_revalidatePath
exports.unstable_revalidateTag = serverExports.unstable_revalidateTag
exports.userAgentFromString = serverExports.userAgentFromString
exports.userAgent = serverExports.userAgent
exports.URLPattern = serverExports.URLPattern
18 changes: 9 additions & 9 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2377,16 +2377,16 @@ export default async function build(
initialHeaders?: SsgRoute['initialHeaders']
} = {}

if (isRouteHandler) {
const exportRouteMeta =
exportConfig.initialPageMetaMap[route] || {}
const exportRouteMeta: {
status?: number
headers?: Record<string, string>
} = exportConfig.initialPageMetaMap[route] || {}

if (exportRouteMeta.status !== 200) {
routeMeta.initialStatus = exportRouteMeta.status
}
if (Object.keys(exportRouteMeta.headers).length) {
routeMeta.initialHeaders = exportRouteMeta.headers
}
if (exportRouteMeta.status !== 200) {
routeMeta.initialStatus = exportRouteMeta.status
}
if (Object.keys(exportRouteMeta.headers || {}).length) {
routeMeta.initialHeaders = exportRouteMeta.headers
}

finalPrerenderRoutes[route] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface StaticGenerationStore {
readonly pathname: string
readonly incrementalCache?: IncrementalCache
readonly isRevalidate?: boolean
readonly isMinimalMode?: boolean
readonly isOnDemandRevalidate?: boolean
readonly isPrerendering?: boolean

Expand All @@ -17,6 +18,7 @@ export interface StaticGenerationStore {
| 'force-no-store'
| 'default-no-store'
| 'only-no-store'

revalidate?: false | number
forceStatic?: boolean
dynamicShouldError?: boolean
Expand All @@ -26,6 +28,8 @@ export interface StaticGenerationStore {
dynamicUsageStack?: string

nextFetchId?: number

tags?: string[]
}

export type StaticGenerationAsyncStorage =
Expand Down
38 changes: 38 additions & 0 deletions packages/next/src/export/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { isAppRouteRoute } from '../lib/is-app-route-route'
import { toNodeHeaders } from '../server/web/utils'
import { RouteModuleLoader } from '../server/future/helpers/module-loader/route-module-loader'
import { NextRequestAdapter } from '../server/web/spec-extension/adapters/next-request'
import * as ciEnvironment from '../telemetry/ci-info'

const envConfig = require('../shared/lib/runtime-config')

Expand Down Expand Up @@ -322,6 +323,12 @@ export default async function exportPage({
fontManifest: optimizeFonts ? requireFontManifest(distDir) : null,
locale: locale as string,
supportsDynamicHTML: false,
...(ciEnvironment.hasNextSupport
? {
isMinimalMode: true,
isRevalidate: true,
}
: {}),
}
}

Expand Down Expand Up @@ -400,6 +407,12 @@ export default async function exportPage({
nextExport: true,
supportsDynamicHTML: false,
incrementalCache: curRenderOpts.incrementalCache,
...(ciEnvironment.hasNextSupport
? {
isMinimalMode: true,
isRevalidate: true,
}
: {}),
},
}

Expand Down Expand Up @@ -429,6 +442,12 @@ export default async function exportPage({

results.fromBuildExportRevalidate = revalidate
const headers = toNodeHeaders(response.headers)
const cacheTags = (context.staticGenerationContext as any)
.fetchTags

if (cacheTags) {
headers['x-next-cache-tags'] = cacheTags
}

if (!headers['content-type'] && body.type) {
headers['content-type'] = body.type
Expand Down Expand Up @@ -481,7 +500,26 @@ export default async function exportPage({
results.fromBuildExportRevalidate = revalidate

if (revalidate !== 0) {
const cacheTags = (curRenderOpts as any).fetchTags
const headers = cacheTags
? {
'x-next-cache-tags': cacheTags,
}
: undefined

if (ciEnvironment.hasNextSupport) {
if (cacheTags) {
results.fromBuildExportMeta = {
headers,
}
}
}

await promises.writeFile(htmlFilepath, html ?? '', 'utf8')
await promises.writeFile(
htmlFilepath.replace(/\.html$/, '.meta'),
JSON.stringify({ headers })
)
await promises.writeFile(
htmlFilepath.replace(/\.html$/, '.rsc'),
flightData
Expand Down
4 changes: 3 additions & 1 deletion packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import {
getURLFromRedirectError,
isRedirectError,
} from '../../client/components/redirect'
import { patchFetch } from '../lib/patch-fetch'
import { addImplicitTags, patchFetch } from '../lib/patch-fetch'
import { AppRenderSpan } from '../lib/trace/constants'
import { getTracer } from '../lib/trace/tracer'
import { interopDefault } from './interop-default'
Expand Down Expand Up @@ -1571,6 +1571,8 @@ export async function renderToHTMLOrFlight(
if (staticGenerationStore.pendingRevalidates) {
await Promise.all(staticGenerationStore.pendingRevalidates)
}
addImplicitTags(staticGenerationStore)
;(renderOpts as any).fetchTags = staticGenerationStore.tags?.join(',')

if (staticGenerationStore.isStaticGeneration) {
const htmlResult = await streamToBufferedResult(renderResult)
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/server/base-http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export abstract class BaseNextResponse<Destination = any> {
*/
abstract setHeader(name: string, value: string | string[]): this

/**
* Removes a header
*/
abstract removeHeader(name: string): this

/**
* Appends value for the given header name
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/server/base-http/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ export class NodeNextResponse extends BaseNextResponse<Writable> {
return this
}

removeHeader(name: string): this {
this._res.removeHeader(name)
return this
}

getHeaderValues(name: string): string[] | undefined {
const values = this._res.getHeader(name)

Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/server/base-http/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export class WebNextResponse extends BaseNextResponse<WritableStream> {
return this
}

removeHeader(name: string): this {
this.headers.delete(name)
return this
}

getHeaderValues(name: string): string[] | undefined {
// https://developer.mozilla.org/en-US/docs/Web/API/Headers/get#example
return this.getHeader(name)
Expand Down