File size: 4,776 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | import type { FileReader } from './helpers/file-reader/file-reader'
import type { Normalizer } from '../../normalizers/normalizer'
import { AppRouteRouteMatcher } from '../../route-matchers/app-route-route-matcher'
import { RouteKind } from '../../route-kind'
import { FileCacheRouteMatcherProvider } from './file-cache-route-matcher-provider'
import { isAppRouteRoute } from '../../../lib/is-app-route-route'
import { DevAppNormalizers } from '../../normalizers/built/app'
import {
isMetadataRouteFile,
isStaticMetadataRoute,
} from '../../../lib/metadata/is-metadata-route'
import { normalizeMetadataPageToRoute } from '../../../lib/metadata/get-metadata-route'
import path from '../../../shared/lib/isomorphic/path'
export class DevAppRouteRouteMatcherProvider extends FileCacheRouteMatcherProvider<AppRouteRouteMatcher> {
private readonly normalizers: {
page: Normalizer
pathname: Normalizer
bundlePath: Normalizer
}
private readonly appDir: string
private readonly isTurbopack: boolean
constructor(
appDir: string,
extensions: ReadonlyArray<string>,
reader: FileReader,
isTurbopack: boolean
) {
super(appDir, reader)
this.appDir = appDir
this.isTurbopack = isTurbopack
this.normalizers = new DevAppNormalizers(appDir, extensions, isTurbopack)
}
protected async transform(
files: ReadonlyArray<string>
): Promise<ReadonlyArray<AppRouteRouteMatcher>> {
const matchers: Array<AppRouteRouteMatcher> = []
for (const filename of files) {
let page = this.normalizers.page.normalize(filename)
// If the file isn't a match for this matcher, then skip it.
if (!isAppRouteRoute(page)) continue
// Validate that this is not an ignored page.
if (page.includes('/_')) continue
// Turbopack uses the correct page name with the underscore normalized.
// TODO: Move implementation to packages/next/src/server/normalizers/built/app/app-page-normalizer.ts.
// The `includes('/_')` check above needs to be moved for that to work as otherwise `%5Fsegmentname`
// will result in `_segmentname` which hits that includes check and be skipped.
if (this.isTurbopack) {
page = page.replace(/%5F/g, '_')
}
const pathname = this.normalizers.pathname.normalize(filename)
const bundlePath = this.normalizers.bundlePath.normalize(filename)
const ext = path.extname(filename).slice(1)
const isEntryMetadataRouteFile = isMetadataRouteFile(
filename.replace(this.appDir, ''),
[ext],
true
)
if (isEntryMetadataRouteFile && !isStaticMetadataRoute(page)) {
// Matching dynamic metadata routes.
// Add 2 possibilities for both single and multiple routes:
{
// single:
// /sitemap.ts -> /sitemap.xml/route
// /icon.ts -> /icon/route
// We'll map the filename before normalization:
// sitemap.ts -> sitemap.xml/route.ts
// icon.ts -> icon/route.ts
const metadataPage = normalizeMetadataPageToRoute(page, false)
const metadataPathname = normalizeMetadataPageToRoute(pathname, false)
const metadataBundlePath = normalizeMetadataPageToRoute(
bundlePath,
false
)
const matcher = new AppRouteRouteMatcher({
kind: RouteKind.APP_ROUTE,
page: metadataPage,
pathname: metadataPathname,
bundlePath: metadataBundlePath,
filename,
})
matchers.push(matcher)
}
{
// multiple:
// /sitemap.ts -> /sitemap/[__metadata_id__]/route
// /icon.ts -> /icon/[__metadata_id__]/route
// We'll map the filename before normalization:
// sitemap.ts -> sitemap.xml/[__metadata_id__].ts
// icon.ts -> icon/[__metadata_id__].ts
const metadataPage = normalizeMetadataPageToRoute(page, true)
const metadataPathname = normalizeMetadataPageToRoute(pathname, true)
const metadataBundlePath = normalizeMetadataPageToRoute(
bundlePath,
true
)
const matcher = new AppRouteRouteMatcher({
kind: RouteKind.APP_ROUTE,
page: metadataPage,
pathname: metadataPathname,
bundlePath: metadataBundlePath,
filename,
})
matchers.push(matcher)
}
} else {
// Normal app routes and static metadata routes.
matchers.push(
new AppRouteRouteMatcher({
kind: RouteKind.APP_ROUTE,
page,
pathname,
bundlePath,
filename,
})
)
}
}
return matchers
}
}
|