Skip to content

fix: follow-up to split-api revalidate behaviour #2113

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

Merged
merged 2 commits into from
May 15, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/runtime/src/helpers/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export const generateFunctions = async (
{ FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC, PUBLISH_DIR }: NetlifyPluginConstants,
appDir: string,
apiLambdas: APILambda[],
featureFlags: Record<string, unknown>,
): Promise<void> => {
const publish = resolve(PUBLISH_DIR)
const functionsDir = resolve(INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC)
Expand All @@ -70,7 +69,6 @@ export const generateFunctions = async (
publishDir,
appDir: relative(functionDir, appDir),
nextServerModuleRelativeLocation,
featureFlags,
})

await ensureDir(join(functionsDir, functionName))
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ const plugin: NetlifyPlugin = {
extendedRoutes.map(packSingleFunction),
)

await generateFunctions(constants, appDir, apiLambdas, featureFlags)
await generateFunctions(constants, appDir, apiLambdas)
await generatePagesResolver(constants)

await configureHandlerFunctions({
Expand Down
11 changes: 2 additions & 9 deletions packages/runtime/src/templates/getApiHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Bridge as NodeBridge } from '@vercel/node-bridge/bridge'
import { outdent as javascript } from 'outdent'

import type { NextConfig } from '../helpers/config'
import { splitApiRoutes as isSplitApiRoutesEnabled } from '../helpers/flags'

import type { NextServerType } from './handlerUtils'
import type { NetlifyNextServerType } from './server'
Expand All @@ -31,11 +30,10 @@ type MakeApiHandlerParams = {
app: string
pageRoot: string
NextServer: NextServerType
splitApiRoutes: boolean
}

// We return a function and then call `toString()` on it to serialise it as the launcher function
const makeApiHandler = ({ conf, app, pageRoot, NextServer, splitApiRoutes }: MakeApiHandlerParams) => {
const makeApiHandler = ({ conf, app, pageRoot, NextServer }: MakeApiHandlerParams) => {
// Change working directory into the site root, unless using Nx, which moves the
// dist directory and handles this itself
const dir = path.resolve(__dirname, app)
Expand Down Expand Up @@ -90,7 +88,6 @@ const makeApiHandler = ({ conf, app, pageRoot, NextServer, splitApiRoutes }: Mak
},
{
revalidateToken: customContext?.odb_refresh_hooks,
splitApiRoutes,
},
)
const requestHandler = nextServer.getRequestHandler()
Expand Down Expand Up @@ -137,13 +134,11 @@ export const getApiHandler = ({
publishDir = '../../../.next',
appDir = '../../..',
nextServerModuleRelativeLocation,
featureFlags,
}: {
schedule?: string
publishDir?: string
appDir?: string
nextServerModuleRelativeLocation: string | undefined
featureFlags: Record<string, unknown>
}): string =>
// 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)
javascript/* javascript */ `
Expand All @@ -166,8 +161,6 @@ export const getApiHandler = ({
let staticManifest
const path = require("path");
const pageRoot = path.resolve(path.join(__dirname, "${publishDir}", "server"));
const handler = (${makeApiHandler.toString()})({ conf: config, app: "${appDir}", pageRoot, NextServer, splitApiRoutes: ${isSplitApiRoutesEnabled(
featureFlags,
)} })
const handler = (${makeApiHandler.toString()})({ conf: config, app: "${appDir}", pageRoot, NextServer })
exports.handler = ${schedule ? `schedule(${JSON.stringify(schedule)}, handler);` : 'handler'}
`
1 change: 0 additions & 1 deletion packages/runtime/src/templates/getHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ const makeHandler = ({ conf, app, pageRoot, NextServer, staticManifest = [], mod
},
{
revalidateToken: customContext?.odb_refresh_hooks,
splitApiRoutes: false,
},
)
const requestHandler = nextServer.getRequestHandler()
Expand Down
26 changes: 8 additions & 18 deletions packages/runtime/src/templates/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {

interface NetlifyConfig {
revalidateToken?: string
splitApiRoutes: boolean
}

const getNetlifyNextServer = (NextServer: NextServerType) => {
Expand All @@ -39,25 +38,16 @@ const getNetlifyNextServer = (NextServer: NextServerType) => {
// preserve the URL before Next.js mutates it for i18n
const { url, headers } = req

if (this.netlifyConfig.splitApiRoutes) {
if (headers['x-prerender-revalidate'] && this.netlifyConfig.revalidateToken) {
// handle on-demand revalidation by purging the ODB cache
await this.netlifyRevalidate(url)
if (headers['x-prerender-revalidate'] && this.netlifyConfig.revalidateToken) {
// handle on-demand revalidation by purging the ODB cache
await this.netlifyRevalidate(url)

res = res as unknown as BaseNextResponse
res.statusCode = 200
res.setHeader('x-nextjs-cache', 'REVALIDATED')
res.send()
} else {
await handler(req, res, parsedUrl)
}
res = res as unknown as BaseNextResponse
res.statusCode = 200
res.setHeader('x-nextjs-cache', 'REVALIDATED')
res.send()
} else {
// handle the original res.revalidate() request
await handler(req, res, parsedUrl)
// handle on-demand revalidation by purging the ODB cache
if (res.statusCode === 200 && headers['x-prerender-revalidate'] && this.netlifyConfig.revalidateToken) {
await this.netlifyRevalidate(url)
}
return handler(req, res, parsedUrl)
}
}
}
Expand Down