fix(nuxt): handle relative baseURL in nitro runtime config (#23841)

This commit is contained in:
Daniel Roe 2023-10-21 19:17:53 +01:00 committed by GitHub
parent f4b7406e10
commit 7b8e7f176d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 10 deletions

View File

@ -1,10 +1,11 @@
import { joinURL } from 'ufo'
import type { MatcherExport, RouteMatcher } from 'radix3'
import { createMatcherFromExport } from 'radix3'
import { defu } from 'defu'
import { useAppConfig, useRuntimeConfig } from '#app'
import { useAppConfig } from '#app'
// @ts-expect-error virtual file
import { appManifest as isAppManifestEnabled } from '#build/nuxt.config.mjs'
// @ts-expect-error virtual file
import { buildAssetsURL } from '#build/paths.mjs'
export interface NuxtAppManifestMeta {
id: string
@ -23,10 +24,9 @@ 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 = $fetch<NuxtAppManifest>(buildAssetsURL(`builds/meta/${buildId}.json`))
manifest.then((m) => {
matcher = createMatcherFromExport(m.matcher)
})

View File

@ -1,18 +1,18 @@
import { joinURL } from 'ufo'
import type { NuxtAppManifestMeta } from '#app'
import { defineNuxtPlugin, getAppManifest, onNuxtReady, useRuntimeConfig } from '#app'
import { defineNuxtPlugin, getAppManifest, onNuxtReady } from '#app'
// @ts-expect-error virtual file
import { buildAssetsURL } from '#build/paths.mjs'
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'))
const meta = await $fetch<NuxtAppManifestMeta>(buildAssetsURL('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)

View File

@ -11,7 +11,7 @@ import escapeRE from 'escape-string-regexp'
import { defu } from 'defu'
import fsExtra from 'fs-extra'
import { dynamicEventHandler } from 'h3'
import type { Nuxt } from 'nuxt/schema'
import type { Nuxt, RuntimeConfig } from 'nuxt/schema'
// @ts-expect-error TODO: add legacy type support for subpath imports
import { template as defaultSpaLoadingTemplate } from '@nuxt/ui-templates/templates/spa-loading-icon.mjs'
@ -99,6 +99,12 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
},
runtimeConfig: {
...nuxt.options.runtimeConfig,
app: {
...nuxt.options.runtimeConfig.app,
baseURL: nuxt.options.runtimeConfig.app.baseURL.startsWith('./')
? nuxt.options.runtimeConfig.app.baseURL.slice(1)
: nuxt.options.runtimeConfig.app.baseURL
} satisfies RuntimeConfig['app'],
nitro: {
envPrefix: 'NUXT_',
...nuxt.options.runtimeConfig.nitro

View File

@ -19,7 +19,7 @@ describe.skipIf(process.env.SKIP_BUNDLE_SIZE === 'true' || process.env.ECOSYSTEM
for (const outputDir of ['.output', '.output-inline']) {
it('default client bundle size', async () => {
const clientStats = await analyzeSizes('**/*.js', join(rootDir, outputDir, 'public'))
expect.soft(roundToKilobytes(clientStats.totalBytes)).toMatchInlineSnapshot('"99.3k"')
expect.soft(roundToKilobytes(clientStats.totalBytes)).toMatchInlineSnapshot('"99.2k"')
expect(clientStats.files.map(f => f.replace(/\..*\.js/, '.js'))).toMatchInlineSnapshot(`
[
"_nuxt/entry.js",