Skip to content

Commit 30db86a

Browse files
committed
fix: put updated revalidate behaviour behind flag
1 parent 6089296 commit 30db86a

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

packages/runtime/src/helpers/functions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const generateFunctions = async (
4848
{ FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC, PUBLISH_DIR }: NetlifyPluginConstants,
4949
appDir: string,
5050
apiLambdas: APILambda[],
51+
featureFlags: Record<string, unknown>,
5152
): Promise<void> => {
5253
const publish = resolve(PUBLISH_DIR)
5354
const functionsDir = resolve(INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC)
@@ -69,6 +70,7 @@ export const generateFunctions = async (
6970
publishDir,
7071
appDir: relative(functionDir, appDir),
7172
nextServerModuleRelativeLocation,
73+
featureFlags,
7274
})
7375

7476
await ensureDir(join(functionsDir, functionName))

packages/runtime/src/templates/getApiHandler.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Bridge as NodeBridge } from '@vercel/node-bridge/bridge'
44
import { outdent as javascript } from 'outdent'
55

66
import type { NextConfig } from '../helpers/config'
7+
import { splitApiRoutes as isSplitApiRoutesEnabled } from '../helpers/flags'
78

89
import type { NextServerType } from './handlerUtils'
910
import type { NetlifyNextServerType } from './server'
@@ -30,10 +31,11 @@ type MakeApiHandlerParams = {
3031
app: string
3132
pageRoot: string
3233
NextServer: NextServerType
34+
splitApiRoutes: boolean
3335
}
3436

3537
// We return a function and then call `toString()` on it to serialise it as the launcher function
36-
const makeApiHandler = ({ conf, app, pageRoot, NextServer }: MakeApiHandlerParams) => {
38+
const makeApiHandler = ({ conf, app, pageRoot, NextServer, splitApiRoutes }: MakeApiHandlerParams) => {
3739
// Change working directory into the site root, unless using Nx, which moves the
3840
// dist directory and handles this itself
3941
const dir = path.resolve(__dirname, app)
@@ -88,6 +90,7 @@ const makeApiHandler = ({ conf, app, pageRoot, NextServer }: MakeApiHandlerParam
8890
},
8991
{
9092
revalidateToken: customContext?.odb_refresh_hooks,
93+
splitApiRoutes,
9194
},
9295
)
9396
const requestHandler = nextServer.getRequestHandler()
@@ -134,11 +137,13 @@ export const getApiHandler = ({
134137
publishDir = '../../../.next',
135138
appDir = '../../..',
136139
nextServerModuleRelativeLocation,
140+
featureFlags,
137141
}: {
138142
schedule?: string
139143
publishDir?: string
140144
appDir?: string
141145
nextServerModuleRelativeLocation: string | undefined
146+
featureFlags: Record<string, unknown>
142147
}): string =>
143148
// This is a string, but if you have the right editor plugin it should format as js (e.g. bierner.comment-tagged-templates in VS Code)
144149
javascript/* javascript */ `
@@ -161,6 +166,8 @@ export const getApiHandler = ({
161166
let staticManifest
162167
const path = require("path");
163168
const pageRoot = path.resolve(path.join(__dirname, "${publishDir}", "server"));
164-
const handler = (${makeApiHandler.toString()})({ conf: config, app: "${appDir}", pageRoot, NextServer})
169+
const handler = (${makeApiHandler.toString()})({ conf: config, app: "${appDir}", pageRoot, NextServer, splitApiRoutes: ${isSplitApiRoutesEnabled(
170+
featureFlags,
171+
)} })
165172
exports.handler = ${schedule ? `schedule(${JSON.stringify(schedule)}, handler);` : 'handler'}
166173
`

packages/runtime/src/templates/server.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313

1414
interface NetlifyConfig {
1515
revalidateToken?: string
16+
splitApiRoutes: boolean
1617
}
1718

1819
const getNetlifyNextServer = (NextServer: NextServerType) => {
@@ -37,16 +38,26 @@ const getNetlifyNextServer = (NextServer: NextServerType) => {
3738
return async (req, res, parsedUrl) => {
3839
// preserve the URL before Next.js mutates it for i18n
3940
const { url, headers } = req
40-
if (headers['x-prerender-revalidate'] && this.netlifyConfig.revalidateToken) {
41-
// handle on-demand revalidation by purging the ODB cache
42-
await this.netlifyRevalidate(url)
4341

44-
res = res as unknown as BaseNextResponse
45-
res.statusCode = 200
46-
res.setHeader('x-nextjs-cache', 'REVALIDATED')
47-
res.send()
42+
if (this.netlifyConfig.splitApiRoutes) {
43+
if (headers['x-prerender-revalidate'] && this.netlifyConfig.revalidateToken) {
44+
// handle on-demand revalidation by purging the ODB cache
45+
await this.netlifyRevalidate(url)
46+
47+
res = res as unknown as BaseNextResponse
48+
res.statusCode = 200
49+
res.setHeader('x-nextjs-cache', 'REVALIDATED')
50+
res.send()
51+
} else {
52+
await handler(req, res, parsedUrl)
53+
}
4854
} else {
55+
// handle the original res.revalidate() request
4956
await handler(req, res, parsedUrl)
57+
// handle on-demand revalidation by purging the ODB cache
58+
if (res.statusCode === 200 && headers['x-prerender-revalidate'] && this.netlifyConfig.revalidateToken) {
59+
await this.netlifyRevalidate(url)
60+
}
5061
}
5162
}
5263
}

0 commit comments

Comments
 (0)