From 1e9e8b2666def724f9345a03819cd585c117719e Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Wed, 3 May 2023 12:53:58 +0200 Subject: [PATCH] Revert "Fix cross-worker revalidate API" (#49138) Reverts vercel/next.js#49101 It breaks Turbopack --- packages/next/src/server/api-utils/node.ts | 57 +++++-------------- packages/next/src/server/next-server.ts | 38 ++----------- .../app-dir/revalidate/app/layout.js | 7 --- .../production/app-dir/revalidate/app/page.js | 4 -- .../app-dir/revalidate/next.config.js | 5 -- .../revalidate/pages/api/revalidate.js | 8 --- .../app-dir/revalidate/revalidate.test.ts | 20 ------- 7 files changed, 18 insertions(+), 121 deletions(-) delete mode 100644 test/production/app-dir/revalidate/app/layout.js delete mode 100644 test/production/app-dir/revalidate/app/page.js delete mode 100644 test/production/app-dir/revalidate/next.config.js delete mode 100644 test/production/app-dir/revalidate/pages/api/revalidate.js delete mode 100644 test/production/app-dir/revalidate/revalidate.test.ts diff --git a/packages/next/src/server/api-utils/node.ts b/packages/next/src/server/api-utils/node.ts index 2eb8ba4e2dcf0..fb8a87e879983 100644 --- a/packages/next/src/server/api-utils/node.ts +++ b/packages/next/src/server/api-utils/node.ts @@ -27,6 +27,7 @@ import { SYMBOL_PREVIEW_DATA, RESPONSE_LIMIT_DEFAULT, } from './index' +import { createRequestResponseMocks } from '../lib/mock-request' import { getTracer } from '../lib/trace/tracer' import { NodeSpan } from '../lib/trace/constants' import { RequestCookies } from '../web/spec-extension/cookies' @@ -35,7 +36,6 @@ import { PRERENDER_REVALIDATE_HEADER, PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER, } from '../../lib/constants' -import { invokeRequest } from '../lib/server-ipc' export function tryGetPreviewData( req: IncomingMessage | BaseNextRequest | Request, @@ -194,14 +194,7 @@ export async function parseBody( type ApiContext = __ApiPreviewProps & { trustHostHeader?: boolean allowedRevalidateHeaderKeys?: string[] - hostname?: string - revalidate?: (config: { - urlPath: string - revalidateHeaders: { [key: string]: string | string[] } - opts: { unstable_onlyGenerated?: boolean } - }) => Promise - - // (_req: IncomingMessage, _res: ServerResponse) => Promise + revalidate?: (_req: IncomingMessage, _res: ServerResponse) => Promise } function getMaxContentLength(responseLimit?: ResponseLimit) { @@ -460,44 +453,20 @@ async function revalidate( throw new Error(`Invalid response ${res.status}`) } } else if (context.revalidate) { - // We prefer to use the IPC call if running under the workers mode. - const ipcPort = process.env.__NEXT_PRIVATE_ROUTER_IPC_PORT - if (ipcPort) { - const ipcKey = process.env.__NEXT_PRIVATE_ROUTER_IPC_KEY - const res = await invokeRequest( - `http://${ - context.hostname - }:${ipcPort}?key=${ipcKey}&method=revalidate&args=${encodeURIComponent( - JSON.stringify([{ urlPath, revalidateHeaders }]) - )}`, - { - method: 'GET', - headers: {}, - } - ) - - const chunks = [] + const mocked = createRequestResponseMocks({ + url: urlPath, + headers: revalidateHeaders, + }) - for await (const chunk of res) { - if (chunk) { - chunks.push(chunk) - } - } - const body = Buffer.concat(chunks).toString() - const result = JSON.parse(body) + await context.revalidate(mocked.req, mocked.res) + await mocked.res.hasStreamed - if (result.err) { - throw new Error(result.err.message) - } - - return + if ( + mocked.res.getHeader('x-nextjs-cache') !== 'REVALIDATED' && + !(mocked.res.statusCode === 404 && opts.unstable_onlyGenerated) + ) { + throw new Error(`Invalid response ${mocked.res.statusCode}`) } - - await context.revalidate({ - urlPath, - revalidateHeaders, - opts, - }) } else { throw new Error( `Invariant: required internal revalidate method not passed to api-utils` diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index 4dfa50e77b5d8..0ebce2758ae79 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -107,7 +107,6 @@ import { removePathPrefix } from '../shared/lib/router/utils/remove-path-prefix' import { addPathPrefix } from '../shared/lib/router/utils/add-path-prefix' import { pathHasPrefix } from '../shared/lib/router/utils/path-has-prefix' import { filterReqHeaders, invokeRequest } from './lib/server-ipc' -import { createRequestResponseMocks } from './lib/mock-request' export * from './base-server' @@ -907,13 +906,16 @@ export default class NextNodeServer extends BaseServer { pageModule, { ...this.renderOpts.previewProps, - revalidate: this.revalidate.bind(this), + revalidate: (newReq: IncomingMessage, newRes: ServerResponse) => + this.getRequestHandler()( + new NodeNextRequest(newReq), + new NodeNextResponse(newRes) + ), // internal config so is not typed trustHostHeader: (this.nextConfig.experimental as Record) .trustHostHeader, allowedRevalidateHeaderKeys: this.nextConfig.experimental.allowedRevalidateHeaderKeys, - hostname: this.hostname, }, this.minimalMode, this.renderOpts.dev, @@ -1670,36 +1672,6 @@ export default class NextNodeServer extends BaseServer { } } - public async revalidate({ - urlPath, - revalidateHeaders, - opts, - }: { - urlPath: string - revalidateHeaders: { [key: string]: string | string[] } - opts: { unstable_onlyGenerated?: boolean } - }) { - const mocked = createRequestResponseMocks({ - url: urlPath, - headers: revalidateHeaders, - }) - - const handler = this.getRequestHandler() - await handler( - new NodeNextRequest(mocked.req), - new NodeNextResponse(mocked.res) - ) - await mocked.res.hasStreamed - - if ( - mocked.res.getHeader('x-nextjs-cache') !== 'REVALIDATED' && - !(mocked.res.statusCode === 404 && opts.unstable_onlyGenerated) - ) { - throw new Error(`Invalid response ${mocked.res.statusCode}`) - } - return {} - } - public async render( req: BaseNextRequest | IncomingMessage, res: BaseNextResponse | ServerResponse, diff --git a/test/production/app-dir/revalidate/app/layout.js b/test/production/app-dir/revalidate/app/layout.js deleted file mode 100644 index a3a86a5ca1e12..0000000000000 --- a/test/production/app-dir/revalidate/app/layout.js +++ /dev/null @@ -1,7 +0,0 @@ -export default function Root({ children }) { - return ( - - {children} - - ) -} diff --git a/test/production/app-dir/revalidate/app/page.js b/test/production/app-dir/revalidate/app/page.js deleted file mode 100644 index 6cc12f77ff9cd..0000000000000 --- a/test/production/app-dir/revalidate/app/page.js +++ /dev/null @@ -1,4 +0,0 @@ -export default async function Page() { - const data = Math.random() - return

{data}

-} diff --git a/test/production/app-dir/revalidate/next.config.js b/test/production/app-dir/revalidate/next.config.js deleted file mode 100644 index cfa3ac3d7aa94..0000000000000 --- a/test/production/app-dir/revalidate/next.config.js +++ /dev/null @@ -1,5 +0,0 @@ -module.exports = { - experimental: { - appDir: true, - }, -} diff --git a/test/production/app-dir/revalidate/pages/api/revalidate.js b/test/production/app-dir/revalidate/pages/api/revalidate.js deleted file mode 100644 index a3e4482a2bcbc..0000000000000 --- a/test/production/app-dir/revalidate/pages/api/revalidate.js +++ /dev/null @@ -1,8 +0,0 @@ -export default async function (_req, res) { - try { - await res.revalidate('/') - return res.json({ revalidated: true }) - } catch (err) { - return res.status(500).send('Error') - } -} diff --git a/test/production/app-dir/revalidate/revalidate.test.ts b/test/production/app-dir/revalidate/revalidate.test.ts deleted file mode 100644 index 1922eb6c3bf31..0000000000000 --- a/test/production/app-dir/revalidate/revalidate.test.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { createNextDescribe } from 'e2e-utils' - -createNextDescribe( - 'app-dir revalidate', - { - files: __dirname, - skipDeployment: true, - }, - ({ next }) => { - it('should be able to revalidate the cache via pages/api', async () => { - const $ = await next.render$('/') - const id = $('h1').text() - const res = await next.fetch('/api/revalidate') - expect(res.status).toBe(200) - const $2 = await next.render$('/') - const id2 = $2('h1').text() - expect(id).not.toBe(id2) - }) - } -)