Unverified Commit 2f8319b5 authored by Sebastian Markbåge's avatar Sebastian Markbåge Committed by GitHub

Run Middleware within WorkStore (#70808)

The data structure inside WorkStore (formerly StaticGenerationStore) is
a mess with a bunch of options (what hidden class?). It's also there to
support Route Handlers, App Router Pages and Actions. It should probably
be a more specific disjoint union for each use case rather than just
optional fields.

We could have a separate kind of store for Middleware but it seems like
we're going towards Middleware becoming something with same semantics as
an App Route Handler. For example, we already support `cookies()`,
`headers()`, `after()`, `revalidate` etc. That's why it already has a
RequestStore.

This becomes a requirement for [moving `AfterContext` context to
`WorkStore`](https://github.com/vercel/next.js/pull/70806).
parent 98130616
...@@ -17,6 +17,9 @@ import { ...@@ -17,6 +17,9 @@ import {
type WrapperRenderOpts, type WrapperRenderOpts,
} from '../async-storage/with-request-store' } from '../async-storage/with-request-store'
import { requestAsyncStorage } from '../../client/components/request-async-storage.external' import { requestAsyncStorage } from '../../client/components/request-async-storage.external'
import { withWorkStore } from '../async-storage/with-work-store'
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
import { NEXT_ROUTER_PREFETCH_HEADER } from '../../client/components/app-router-headers'
import { getTracer } from '../lib/trace/tracer' import { getTracer } from '../lib/trace/tracer'
import type { TextMapGetter } from 'next/dist/compiled/@opentelemetry/api' import type { TextMapGetter } from 'next/dist/compiled/@opentelemetry/api'
import { MiddlewareSpan } from '../lib/trace/constants' import { MiddlewareSpan } from '../lib/trace/constants'
...@@ -234,27 +237,48 @@ export async function adapter( ...@@ -234,27 +237,48 @@ export async function adapter(
try { try {
const previewProps = getEdgePreviewProps() const previewProps = getEdgePreviewProps()
return await withRequestStore( return await withWorkStore(
requestAsyncStorage, workAsyncStorage,
{ {
req: request, page: '/', // Fake Work
res: undefined, fallbackRouteParams: null,
url: request.nextUrl,
renderOpts: { renderOpts: {
onUpdateCookies: (cookies) => {
cookiesFromResponse = cookies
},
previewProps,
waitUntil,
onClose: closeController
? closeController.onClose.bind(closeController)
: undefined,
experimental: { experimental: {
after: isAfterEnabled, after: isAfterEnabled,
isRoutePPREnabled: false,
dynamicIO: false,
}, },
buildId: buildId ?? '',
supportsDynamicResponse: true,
}, },
requestEndedState: { ended: false },
isPrefetchRequest: request.headers.has(
NEXT_ROUTER_PREFETCH_HEADER
),
}, },
() => params.handler(request, event) () =>
withRequestStore(
requestAsyncStorage,
{
req: request,
res: undefined,
url: request.nextUrl,
renderOpts: {
onUpdateCookies: (cookies) => {
cookiesFromResponse = cookies
},
previewProps,
waitUntil,
onClose: closeController
? closeController.onClose.bind(closeController)
: undefined,
experimental: {
after: isAfterEnabled,
},
},
},
() => params.handler(request, event)
)
) )
} finally { } finally {
// middleware cannot stream, so we can consider the response closed // middleware cannot stream, so we can consider the response closed
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment