-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(nuxt): experimental build manifest + client route rules (#21641)
- Loading branch information
Showing
28 changed files
with
368 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { joinURL } from 'ufo' | ||
import type { MatcherExport, RouteMatcher } from 'radix3' | ||
import { createMatcherFromExport } from 'radix3' | ||
import { defu } from 'defu' | ||
import { useAppConfig, useRuntimeConfig } from '#app' | ||
// @ts-expect-error virtual file | ||
import { appManifest as isAppManifestEnabled } from '#build/nuxt.config.mjs' | ||
|
||
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`') | ||
} | ||
const config = useRuntimeConfig() | ||
// @ts-expect-error private property | ||
const buildId = useAppConfig().nuxt?.buildId | ||
manifest = $fetch<NuxtAppManifest>(joinURL(config.app.cdnURL || config.app.baseURL, config.app.buildAssetsDir, `builds/meta/${buildId}.json`)) | ||
manifest.then((m) => { | ||
matcher = createMatcherFromExport(m.matcher) | ||
}) | ||
return manifest | ||
} | ||
|
||
export function getAppManifest (): Promise<NuxtAppManifest> { | ||
if (!isAppManifestEnabled) { | ||
throw new Error('[nuxt] app manifest should be enabled with `experimental.appManifest`') | ||
} | ||
return manifest || fetchManifest() | ||
} | ||
|
||
export async function getRouteRules (url: string) { | ||
await getAppManifest() | ||
return defu({} as Record<string, any>, ...matcher.matchAll(url).reverse()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { defineNuxtRouteMiddleware } from '#app/composables/router' | ||
import { getRouteRules } from '#app/composables/manifest' | ||
|
||
export default defineNuxtRouteMiddleware(async (to) => { | ||
if (import.meta.server || import.meta.test) { return } | ||
const rules = await getRouteRules(to.path) | ||
if (rules.redirect) { | ||
return rules.redirect | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/nuxt/src/app/plugins/check-outdated-build.client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { joinURL } from 'ufo' | ||
import type { NuxtAppManifestMeta } from '#app' | ||
import { defineNuxtPlugin, getAppManifest, onNuxtReady, useRuntimeConfig } from '#app' | ||
|
||
export default defineNuxtPlugin((nuxtApp) => { | ||
if (import.meta.test) { return } | ||
|
||
let timeout: NodeJS.Timeout | ||
const config = useRuntimeConfig() | ||
|
||
async function getLatestManifest () { | ||
const currentManifest = await getAppManifest() | ||
if (timeout) { clearTimeout(timeout) } | ||
timeout = setTimeout(getLatestManifest, 1000 * 60 * 60) | ||
const meta = await $fetch<NuxtAppManifestMeta>(joinURL(config.app.cdnURL || config.app.baseURL, config.app.buildAssetsDir, 'builds/latest.json')) | ||
if (meta.id !== currentManifest.id) { | ||
// There is a newer build which we will let the user handle | ||
nuxtApp.hooks.callHook('app:manifest:update', meta) | ||
} | ||
} | ||
|
||
onNuxtReady(() => { timeout = setTimeout(getLatestManifest, 1000 * 60 * 60) }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.