From 4c93fadd3675dfe546279fccb5b748ffa66ff9f5 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Mon, 15 May 2023 09:18:15 +0200 Subject: [PATCH 1/2] fix: don't flag revalidate behaviour --- packages/runtime/src/helpers/functions.ts | 2 -- packages/runtime/src/index.ts | 2 +- .../runtime/src/templates/getApiHandler.ts | 11 ++------- packages/runtime/src/templates/getHandler.ts | 1 - packages/runtime/src/templates/server.ts | 24 ++++++------------- 5 files changed, 10 insertions(+), 30 deletions(-) diff --git a/packages/runtime/src/helpers/functions.ts b/packages/runtime/src/helpers/functions.ts index 167653a044..4b0e6b4a73 100644 --- a/packages/runtime/src/helpers/functions.ts +++ b/packages/runtime/src/helpers/functions.ts @@ -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, ): Promise => { const publish = resolve(PUBLISH_DIR) const functionsDir = resolve(INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC) @@ -70,7 +69,6 @@ export const generateFunctions = async ( publishDir, appDir: relative(functionDir, appDir), nextServerModuleRelativeLocation, - featureFlags, }) await ensureDir(join(functionsDir, functionName)) diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.ts index 8a28c4204a..07b160e24d 100644 --- a/packages/runtime/src/index.ts +++ b/packages/runtime/src/index.ts @@ -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({ diff --git a/packages/runtime/src/templates/getApiHandler.ts b/packages/runtime/src/templates/getApiHandler.ts index f35a428f2a..00321824ee 100644 --- a/packages/runtime/src/templates/getApiHandler.ts +++ b/packages/runtime/src/templates/getApiHandler.ts @@ -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' @@ -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) @@ -90,7 +88,6 @@ const makeApiHandler = ({ conf, app, pageRoot, NextServer, splitApiRoutes }: Mak }, { revalidateToken: customContext?.odb_refresh_hooks, - splitApiRoutes, }, ) const requestHandler = nextServer.getRequestHandler() @@ -137,13 +134,11 @@ export const getApiHandler = ({ publishDir = '../../../.next', appDir = '../../..', nextServerModuleRelativeLocation, - featureFlags, }: { schedule?: string publishDir?: string appDir?: string nextServerModuleRelativeLocation: string | undefined - featureFlags: Record }): 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 */ ` @@ -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'} ` diff --git a/packages/runtime/src/templates/getHandler.ts b/packages/runtime/src/templates/getHandler.ts index 7e1d2b8f36..176ddce22c 100644 --- a/packages/runtime/src/templates/getHandler.ts +++ b/packages/runtime/src/templates/getHandler.ts @@ -101,7 +101,6 @@ const makeHandler = ({ conf, app, pageRoot, NextServer, staticManifest = [], mod }, { revalidateToken: customContext?.odb_refresh_hooks, - splitApiRoutes: false, }, ) const requestHandler = nextServer.getRequestHandler() diff --git a/packages/runtime/src/templates/server.ts b/packages/runtime/src/templates/server.ts index e5fb2e742c..c7cc45f796 100644 --- a/packages/runtime/src/templates/server.ts +++ b/packages/runtime/src/templates/server.ts @@ -13,7 +13,6 @@ import { interface NetlifyConfig { revalidateToken?: string - splitApiRoutes: boolean } const getNetlifyNextServer = (NextServer: NextServerType) => { @@ -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) - } } } } From 4bc8d9379b8e101aca6025598ed5a125a3b8cd74 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Mon, 15 May 2023 09:18:59 +0200 Subject: [PATCH 2/2] fix: return handler, don't await --- packages/runtime/src/templates/server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime/src/templates/server.ts b/packages/runtime/src/templates/server.ts index c7cc45f796..fbbb0bc1c4 100644 --- a/packages/runtime/src/templates/server.ts +++ b/packages/runtime/src/templates/server.ts @@ -47,7 +47,7 @@ const getNetlifyNextServer = (NextServer: NextServerType) => { res.setHeader('x-nextjs-cache', 'REVALIDATED') res.send() } else { - await handler(req, res, parsedUrl) + return handler(req, res, parsedUrl) } } }