2023-09-19 21:31:18 +00:00
|
|
|
import type { MatcherExport, RouteMatcher } from 'radix3'
|
|
|
|
import { createMatcherFromExport } from 'radix3'
|
|
|
|
import { defu } from 'defu'
|
2023-10-30 21:05:02 +00:00
|
|
|
import { useAppConfig } from '../config'
|
2023-09-19 21:31:18 +00:00
|
|
|
// @ts-expect-error virtual file
|
|
|
|
import { appManifest as isAppManifestEnabled } from '#build/nuxt.config.mjs'
|
2023-10-21 18:17:53 +00:00
|
|
|
// @ts-expect-error virtual file
|
|
|
|
import { buildAssetsURL } from '#build/paths.mjs'
|
2023-09-19 21:31:18 +00:00
|
|
|
|
|
|
|
export interface NuxtAppManifestMeta {
|
|
|
|
id: string
|
|
|
|
timestamp: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface NuxtAppManifest extends NuxtAppManifestMeta {
|
|
|
|
matcher: MatcherExport
|
|
|
|
prerendered: string[]
|
|
|
|
}
|
|
|
|
|
|
|
|
let manifest: Promise<NuxtAppManifest>
|
|
|
|
let matcher: RouteMatcher
|
|
|
|
|
|
|
|
function fetchManifest () {
|
|
|
|
if (!isAppManifestEnabled) {
|
|
|
|
throw new Error('[nuxt] app manifest should be enabled with `experimental.appManifest`')
|
|
|
|
}
|
|
|
|
// @ts-expect-error private property
|
|
|
|
const buildId = useAppConfig().nuxt?.buildId
|
2023-10-21 18:17:53 +00:00
|
|
|
manifest = $fetch<NuxtAppManifest>(buildAssetsURL(`builds/meta/${buildId}.json`))
|
2023-09-19 21:31:18 +00:00
|
|
|
manifest.then((m) => {
|
|
|
|
matcher = createMatcherFromExport(m.matcher)
|
|
|
|
})
|
|
|
|
return manifest
|
|
|
|
}
|
|
|
|
|
2024-01-19 17:03:30 +00:00
|
|
|
/** @since 3.7.4 */
|
2023-09-19 21:31:18 +00:00
|
|
|
export function getAppManifest (): Promise<NuxtAppManifest> {
|
|
|
|
if (!isAppManifestEnabled) {
|
|
|
|
throw new Error('[nuxt] app manifest should be enabled with `experimental.appManifest`')
|
|
|
|
}
|
|
|
|
return manifest || fetchManifest()
|
|
|
|
}
|
|
|
|
|
2024-01-19 17:03:30 +00:00
|
|
|
/** @since 3.7.4 */
|
2023-09-19 21:31:18 +00:00
|
|
|
export async function getRouteRules (url: string) {
|
|
|
|
await getAppManifest()
|
|
|
|
return defu({} as Record<string, any>, ...matcher.matchAll(url).reverse())
|
|
|
|
}
|