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

Fix turbopack route resolver (#46206 #46206

Merged
merged 1 commit into from
Feb 21, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions packages/next/src/server/lib/route-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { IncomingMessage, ServerResponse } from 'http'
import type { NextConfig } from '../config'
import type { Route } from '../router'
import { RouteDefinition } from '../future/route-definitions/route-definition'
import { RouteKind } from '../future/route-kind'
import { DefaultRouteMatcherManager } from '../future/route-matcher-managers/default-route-matcher-manager'
import { RouteMatch } from '../future/route-matches/route-match'
import type { PageChecker, Route } from '../router'

type RouteResult =
| {
Expand All @@ -13,6 +17,37 @@ type RouteResult =
type: 'none'
}

class DevRouteMatcherManager extends DefaultRouteMatcherManager {
private hasPage: PageChecker

constructor(hasPage: PageChecker) {
super()
this.hasPage = hasPage
}

async match(
pathname: string
): Promise<RouteMatch<RouteDefinition<RouteKind>> | null> {
if (await this.hasPage(pathname)) {
return {
definition: {
kind: RouteKind.PAGES,
page: '',
pathname,
filename: '',
bundlePath: '',
},
params: {},
}
}
return null
}

async test(pathname: string) {
return (await this.match(pathname)) !== null
}
}

export async function makeResolver(dir: string, nextConfig: NextConfig) {
const url = require('url') as typeof import('url')
const { default: Router } = require('../router') as typeof import('../router')
Expand All @@ -37,6 +72,12 @@ export async function makeResolver(dir: string, nextConfig: NextConfig) {

const routeResults = new WeakMap<any, string>()
const routes = devServer.generateRoutes.bind(devServer)()

routes.matchers = new DevRouteMatcherManager(
// @ts-expect-error internal method
devServer.hasPage.bind(devServer)
)

const router = new Router({
...routes,
catchAllRoute: {
Expand Down Expand Up @@ -98,7 +139,7 @@ export async function makeResolver(dir: string, nextConfig: NextConfig) {
type: 'rewrite',
url: resolvedUrl,
statusCode: 200,
headers: {},
headers: res.originalResponse.getHeaders(),
}

res.body(JSON.stringify(routeResult)).send()
Expand Down
Loading