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

[turbopack]: Serve _devMiddlewareManifest.json from router #50241

Merged
merged 3 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 6 additions & 6 deletions packages/next-swc/crates/next-core/js/src/entry/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import 'next/dist/server/node-polyfill-fetch.js'
import middlewareChunkGroup from 'MIDDLEWARE_CHUNK_GROUP'
import middlewareConfig from 'MIDDLEWARE_CONFIG'

type Resolver = Awaited<
ReturnType<typeof import('next/dist/server/lib/route-resolver').makeResolver>
>

type RouterRequest = {
method: string
pathname: string
Expand Down Expand Up @@ -51,16 +55,12 @@ type MiddlewareHeadersResponse = {
headers: [string, string][]
}

let resolveRouteMemo: Promise<
(req: IncomingMessage, res: ServerResponse) => Promise<void>
>
let resolveRouteMemo: Promise<Resolver>

async function getResolveRoute(
dir: string,
serverInfo: ServerInfo
): ReturnType<
typeof import('next/dist/server/lib/route-resolver').makeResolver
> {
): Promise<Resolver> {
const nextConfig = await loadConfig(
PHASE_DEVELOPMENT_SERVER,
process.cwd(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,3 @@ globalThis.__webpack_require__ = (name) => {

// initialize() needs `__webpack_public_path__` to be defined.
globalThis.__webpack_public_path__ = undefined

// Avoids Next loading _next/static/[buildId]/_devMiddlewareManifest.json
globalThis.__DEV_MIDDLEWARE_MATCHERS = []
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is defined, then we'll never fetch the middleware matchers from the server. The fetcher will cache the fetched value afterwards.

6 changes: 5 additions & 1 deletion packages/next-swc/crates/next-core/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ impl ContentSource for DevManifestContentSource {
File::from(build_manifest.as_str()).with_content_type(APPLICATION_JAVASCRIPT_UTF_8)
}
"_next/static/development/_devMiddlewareManifest.json" => {
// empty middleware manifest
// If there is actual middleware, this request will have been handled by the
// node router in next-core/js/src/entry/router.ts and
// next/src/server/lib/route-resolver.ts.
// If we've reached this point, then there is no middleware and we need to
// respond with an empty `MiddlewareMatcher[]`.
File::from("[]").with_content_type(APPLICATION_JSON)
}
_ => return Ok(ContentSourceResultVc::not_found()),
Expand Down
17 changes: 3 additions & 14 deletions packages/next/src/server/lib/route-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getMiddlewareRouteMatcher } from '../../shared/lib/router/utils/middlew
import {
CLIENT_STATIC_FILES_PATH,
DEV_CLIENT_PAGES_MANIFEST,
DEV_MIDDLEWARE_MANIFEST,
} from '../../shared/lib/constants'
import type { BaseNextRequest } from '../base-http'

Expand Down Expand Up @@ -194,20 +195,6 @@ export async function makeResolver(
// @ts-expect-error protected
const buildId = devServer.buildId

const pagesManifestRoute = routes.fsRoutes.find(
(r) =>
r.name ===
`_next/${CLIENT_STATIC_FILES_PATH}/${buildId}/${DEV_CLIENT_PAGES_MANIFEST}`
)
if (pagesManifestRoute) {
// make sure turbopack serves this
pagesManifestRoute.fn = () => {
return {
finished: true,
}
}
}
Comment on lines -196 to -208
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary, because the router.compiledRoutes.filter below is going to remove this route handler regardless.


const router = new Router({
...routes,
catchAllMiddleware,
Expand Down Expand Up @@ -246,6 +233,8 @@ export async function makeResolver(
route.type === 'header' ||
route.name === 'catchall route' ||
route.name === 'middleware catchall' ||
route.name ===
`_next/${CLIENT_STATIC_FILES_PATH}/${buildId}/${DEV_MIDDLEWARE_MANIFEST}` ||
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows the router to handle this specific route, returning our MiddlewareMatcher[] JSON body.

route.name?.includes('check')
)
})
Expand Down