Skip to content
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

Revert "Fix cross-worker revalidate API" #49138

Merged
merged 1 commit into from May 3, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 13 additions & 44 deletions packages/next/src/server/api-utils/node.ts
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
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.