Unverified Commit c06d94ba authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Turbopack: Match proxy matchers with webpack implementation (#93594)

## What?

Applies
https://github.com/vercel/next.js/commit/d166096c399c4fc4e09cd2d1bf26dca6579a855d
for canary and handles the Turbopack-side implementation for
`middleware-manifest.json` which is used for edge runtime middleware.ts

---------
Co-authored-by: 's avatarZack Tanner <1939140+ztanner@users.noreply.github.com>
parent f088c3d4
...@@ -183,14 +183,20 @@ impl MiddlewareEndpoint { ...@@ -183,14 +183,20 @@ impl MiddlewareEndpoint {
source.insert_str(0, "/:nextInternalLocale((?!_next/)[^/.]{1,})"); source.insert_str(0, "/:nextInternalLocale((?!_next/)[^/.]{1,})");
} }
// Match transport-specific route forms that resolve to the
// same page:
// - Pages Router data routes: /_next/data/<build-id>/...
// - App Router transport routes: .rsc, ...segments/...segment.rsc
if is_root { if is_root {
source.push('('); source.push('(');
if has_i18n { if has_i18n {
source.push_str("|\\.json|"); source.push_str("|\\.json|");
} }
source.push_str("/?index|/?index\\.json)?") source.push_str("/?index|/?index\\.json|");
source.push_str("/?index(?:\\.rsc|\\.segments/.+\\.segment\\.rsc)");
source.push_str(")?");
} else { } else {
source.push_str("{(\\.json)}?") source.push_str("{(\\.json|\\.rsc|\\.segments/.+\\.segment\\.rsc)}?");
}; };
source.insert_str(0, "/:nextData(_next/data/[^/]{1,})?"); source.insert_str(0, "/:nextData(_next/data/[^/]{1,})?");
......
...@@ -8,7 +8,7 @@ describe('get-page-static-infos', () => { ...@@ -8,7 +8,7 @@ describe('get-page-static-infos', () => {
{ {
originalSource: '/middleware/path', originalSource: '/middleware/path',
regexp: regexp:
'^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/middleware\\/path(\\.json)?[\\/#\\?]?$', '^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/middleware\\/path(\\.json|\\.rsc|\\.segments\\/.+\\.segment\\.rsc)?[\\/#\\?]?$',
}, },
] ]
const result = getMiddlewareMatchers(matchers, { i18n: undefined }) const result = getMiddlewareMatchers(matchers, { i18n: undefined })
...@@ -21,25 +21,70 @@ describe('get-page-static-infos', () => { ...@@ -21,25 +21,70 @@ describe('get-page-static-infos', () => {
{ {
originalSource: '/middleware/path', originalSource: '/middleware/path',
regexp: regexp:
'^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/middleware\\/path(\\.json)?[\\/#\\?]?$', '^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/middleware\\/path(\\.json|\\.rsc|\\.segments\\/.+\\.segment\\.rsc)?[\\/#\\?]?$',
}, },
{ {
originalSource: '/middleware/another-path', originalSource: '/middleware/another-path',
regexp: regexp:
'^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/middleware\\/another-path(\\.json)?[\\/#\\?]?$', '^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/middleware\\/another-path(\\.json|\\.rsc|\\.segments\\/.+\\.segment\\.rsc)?[\\/#\\?]?$',
}, },
] ]
const result = getMiddlewareMatchers(matchers, { i18n: undefined }) const result = getMiddlewareMatchers(matchers, { i18n: undefined })
expect(result).toStrictEqual(expected) expect(result).toStrictEqual(expected)
}) })
it('matches /:id and /:id.json', () => { it('matches /:id and transport variants for the same route', () => {
const matchers = ['/:id'] const matchers = ['/:id']
const result = getMiddlewareMatchers(matchers, { i18n: undefined })[0] const result = getMiddlewareMatchers(matchers, { i18n: undefined })[0]
.regexp .regexp
const regex = new RegExp(result) const regex = new RegExp(result)
expect(regex.test('/apple')).toBe(true) expect(regex.test('/apple')).toBe(true)
expect(regex.test('/apple.json')).toBe(true) expect(regex.test('/apple.json')).toBe(true)
expect(regex.test('/apple.rsc')).toBe(true)
})
it('matches App Router segment-prefetch routes for static matchers', () => {
const regex = new RegExp(
getMiddlewareMatchers('/dashboard', { i18n: undefined })[0].regexp
)
expect(regex.test('/dashboard.rsc')).toBe(true)
expect(
regex.test('/dashboard.segments/$c$children/__PAGE__.segment.rsc')
).toBe(true)
expect(
regex.test('/settings.segments/$c$children/__PAGE__.segment.rsc')
).toBe(false)
})
it('matches App Router segment-prefetch routes for nested matchers', () => {
const regex = new RegExp(
getMiddlewareMatchers('/dashboard/:path*', {
i18n: undefined,
})[0].regexp
)
expect(
regex.test(
'/dashboard/settings.segments/$c$children/__PAGE__.segment.rsc'
)
).toBe(true)
expect(
regex.test(
'/marketing/settings.segments/$c$children/__PAGE__.segment.rsc'
)
).toBe(false)
})
it('matches the root App Router segment-prefetch transport route', () => {
const regex = new RegExp(
getMiddlewareMatchers('/', { i18n: undefined })[0].regexp
)
expect(regex.test('/index.rsc')).toBe(true)
expect(
regex.test('/index.segments/$c$children/__PAGE__.segment.rsc')
).toBe(true)
}) })
}) })
}) })
...@@ -11,6 +11,9 @@ import { ...@@ -11,6 +11,9 @@ import {
SERVER_RUNTIME, SERVER_RUNTIME,
MIDDLEWARE_FILENAME, MIDDLEWARE_FILENAME,
PROXY_FILENAME, PROXY_FILENAME,
RSC_SUFFIX,
RSC_SEGMENT_SUFFIX,
RSC_SEGMENTS_DIR_SUFFIX,
} from '../../lib/constants' } from '../../lib/constants'
import { tryToParsePath } from '../../lib/try-to-parse-path' import { tryToParsePath } from '../../lib/try-to-parse-path'
import { isAPIRoute } from '../../lib/is-api-route' import { isAPIRoute } from '../../lib/is-api-route'
...@@ -20,6 +23,7 @@ import { ...@@ -20,6 +23,7 @@ import {
warnAboutPreferredRegion, warnAboutPreferredRegion,
} from '../warn-about-edge-runtime' } from '../warn-about-edge-runtime'
import { RSC_MODULE_TYPES } from '../../shared/lib/constants' import { RSC_MODULE_TYPES } from '../../shared/lib/constants'
import { escapeStringRegexp } from '../../shared/lib/escape-regexp'
import type { RSCMeta } from '../webpack/loaders/get-module-build-info' import type { RSCMeta } from '../webpack/loaders/get-module-build-info'
import { PAGE_TYPES } from '../../lib/page-types' import { PAGE_TYPES } from '../../lib/page-types'
import { import {
...@@ -111,6 +115,13 @@ export interface PagesPageStaticInfo { ...@@ -111,6 +115,13 @@ export interface PagesPageStaticInfo {
export type PageStaticInfo = AppPageStaticInfo | PagesPageStaticInfo export type PageStaticInfo = AppPageStaticInfo | PagesPageStaticInfo
const APP_ROUTE_RSC_SUFFIX_MATCHER = escapeStringRegexp(RSC_SUFFIX)
const APP_ROUTE_SEGMENT_PREFETCH_SUFFIX_MATCHER = `${escapeStringRegexp(RSC_SEGMENTS_DIR_SUFFIX)}/.+${escapeStringRegexp(RSC_SEGMENT_SUFFIX)}`
const APP_ROUTE_TRANSPORT_SUFFIX_MATCHER = `${APP_ROUTE_RSC_SUFFIX_MATCHER}|${APP_ROUTE_SEGMENT_PREFETCH_SUFFIX_MATCHER}`
const ROOT_APP_ROUTE_TRANSPORT_MATCHER = `/?index(?:${APP_ROUTE_TRANSPORT_SUFFIX_MATCHER})`
const MIDDLEWARE_DATA_SUFFIX_MATCHER = `\\.json|${APP_ROUTE_TRANSPORT_SUFFIX_MATCHER}`
const OPTIONAL_MIDDLEWARE_NEXT_DATA_PREFIX = '/:nextData(_next/data/[^/]{1,})?'
const CLIENT_MODULE_LABEL = const CLIENT_MODULE_LABEL =
/\/\* __next_internal_client_entry_do_not_use__ ([^ ]*) (cjs|auto) \*\// /\/\* __next_internal_client_entry_do_not_use__ ([^ ]*) (cjs|auto) \*\//
...@@ -469,11 +480,17 @@ export function getMiddlewareMatchers( ...@@ -469,11 +480,17 @@ export function getMiddlewareMatchers(
}` }`
} }
source = `/:nextData(_next/data/[^/]{1,})?${source}${ // Match transport-specific route forms that resolve to the same page.
// - Pages Router data routes: /_next/data/<build-id>/...
// - App Router transport routes: .rsc, ...segments/...segment.rsc
const sourceSuffix = `${
isRoot isRoot
? `(${nextConfig.i18n ? '|\\.json|' : ''}/?index|/?index\\.json)?` ? `(${
: '{(\\.json)}?' nextConfig.i18n ? '|\\.json|' : ''
}/?index|/?index\\.json|${ROOT_APP_ROUTE_TRANSPORT_MATCHER})?`
: `{(${MIDDLEWARE_DATA_SUFFIX_MATCHER})}?`
}` }`
source = `${OPTIONAL_MIDDLEWARE_NEXT_DATA_PREFIX}${source}${sourceSuffix}`
if (nextConfig.basePath) { if (nextConfig.basePath) {
source = `${nextConfig.basePath}${source}` source = `${nextConfig.basePath}${source}`
......
...@@ -159,19 +159,19 @@ describe('Middleware can set the matcher in its config', () => { ...@@ -159,19 +159,19 @@ describe('Middleware can set the matcher in its config', () => {
"matchers": [ "matchers": [
{ {
"originalSource": "/", "originalSource": "/",
"regexp": "^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/(\\/?index|\\/?index\\.json))?[\\/#\\?]?$", "regexp": "^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/(\\/?index|\\/?index\\.json|\\/?index(?:\\.rsc|\\.segments\\/.+\\.segment\\.rsc)))?[\\/#\\?]?$",
}, },
{ {
"originalSource": "/with-middleware/:path*", "originalSource": "/with-middleware/:path*",
"regexp": "^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/with-middleware(?:\\/((?:[^\\/#\\?]+?)(?:\\/(?:[^\\/#\\?]+?))*))?(\\.json)?[\\/#\\?]?$", "regexp": "^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/with-middleware(?:\\/((?:[^\\/#\\?]+?)(?:\\/(?:[^\\/#\\?]+?))*))?(\\.json|\\.rsc|\\.segments\\/.+\\.segment\\.rsc)?[\\/#\\?]?$",
}, },
{ {
"originalSource": "/another-middleware/:path*", "originalSource": "/another-middleware/:path*",
"regexp": "^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/another-middleware(?:\\/((?:[^\\/#\\?]+?)(?:\\/(?:[^\\/#\\?]+?))*))?(\\.json)?[\\/#\\?]?$", "regexp": "^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/another-middleware(?:\\/((?:[^\\/#\\?]+?)(?:\\/(?:[^\\/#\\?]+?))*))?(\\.json|\\.rsc|\\.segments\\/.+\\.segment\\.rsc)?[\\/#\\?]?$",
}, },
{ {
"originalSource": "/_sites/:path((?![^/]*\\.json$)[^/]+$)", "originalSource": "/_sites/:path((?![^/]*\\.json$)[^/]+$)",
"regexp": "^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/_sites(?:\\/((?![^/]*\\.json$)[^/]+$))(\\.json)?[\\/#\\?]?$", "regexp": "^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/_sites(?:\\/((?![^/]*\\.json$)[^/]+$))(\\.json|\\.rsc|\\.segments\\/.+\\.segment\\.rsc)?[\\/#\\?]?$",
}, },
], ],
"name": "middleware", "name": "middleware",
......
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