Skip to content

Commit

Permalink
Revert "Fix cross-worker revalidate API (#49101)"
Browse files Browse the repository at this point in the history
This reverts commit 931c101.
  • Loading branch information
sokra committed May 3, 2023
1 parent cfd27e9 commit 600980d
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 121 deletions.
57 changes: 13 additions & 44 deletions packages/next/src/server/api-utils/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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,
Expand Down Expand Up @@ -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<any>

// (_req: IncomingMessage, _res: ServerResponse) => Promise<any>
revalidate?: (_req: IncomingMessage, _res: ServerResponse) => Promise<any>
}

function getMaxContentLength(responseLimit?: ResponseLimit) {
Expand Down Expand Up @@ -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`
Expand Down
38 changes: 5 additions & 33 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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<string, any>)
.trustHostHeader,
allowedRevalidateHeaderKeys:
this.nextConfig.experimental.allowedRevalidateHeaderKeys,
hostname: this.hostname,
},
this.minimalMode,
this.renderOpts.dev,
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions test/production/app-dir/revalidate/app/layout.js

This file was deleted.

4 changes: 0 additions & 4 deletions test/production/app-dir/revalidate/app/page.js

This file was deleted.

5 changes: 0 additions & 5 deletions test/production/app-dir/revalidate/next.config.js

This file was deleted.

8 changes: 0 additions & 8 deletions test/production/app-dir/revalidate/pages/api/revalidate.js

This file was deleted.

20 changes: 0 additions & 20 deletions test/production/app-dir/revalidate/revalidate.test.ts

This file was deleted.

0 comments on commit 600980d

Please sign in to comment.