fix(nuxt): use portal to sync nitro/nuxt runtimeConfig + routeRules (#27596)

This commit is contained in:
Daniel Roe 2024-06-13 18:51:44 +01:00 committed by GitHub
parent 220cc502a1
commit fbf88f7b10
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 4 deletions

View File

@ -4,7 +4,7 @@ import ignore from 'ignore'
import type { LoadNuxtOptions } from '@nuxt/kit' import type { LoadNuxtOptions } from '@nuxt/kit'
import { addBuildPlugin, addComponent, addPlugin, addRouteMiddleware, addServerPlugin, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolveIgnorePatterns, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit' import { addBuildPlugin, addComponent, addPlugin, addRouteMiddleware, addServerPlugin, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolveIgnorePatterns, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit'
import { resolvePath as _resolvePath } from 'mlly' import { resolvePath as _resolvePath } from 'mlly'
import type { Nuxt, NuxtHooks, NuxtModule, NuxtOptions, RuntimeConfig } from 'nuxt/schema' import type { Nuxt, NuxtHooks, NuxtModule, NuxtOptions } from 'nuxt/schema'
import type { PackageJson } from 'pkg-types' import type { PackageJson } from 'pkg-types'
import { readPackageJSON, resolvePackageJSON } from 'pkg-types' import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
import { hash } from 'ohash' import { hash } from 'ohash'
@ -645,8 +645,22 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
options._modules.push('@nuxt/telemetry') options._modules.push('@nuxt/telemetry')
} }
// Ensure we share runtime config between Nuxt and Nitro // Ensure we share key config between Nuxt and Nitro
options.runtimeConfig = options.nitro.runtimeConfig as RuntimeConfig createPortalProperties(options.nitro.runtimeConfig, options, ['nitro.runtimeConfig', 'runtimeConfig'])
createPortalProperties(options.nitro.routeRules, options, ['nitro.routeRules', 'routeRules'])
// prevent replacement of options.nitro
const nitroOptions = options.nitro
Object.defineProperties(options, {
nitro: {
configurable: false,
enumerable: true,
get: () => nitroOptions,
set (value) {
Object.assign(nitroOptions, value)
},
},
})
const nuxt = createNuxt(options) const nuxt = createNuxt(options)
@ -695,3 +709,31 @@ function deduplicateArray<T = unknown> (maybeArray: T): T {
} }
return fresh as T return fresh as T
} }
function createPortalProperties (sourceValue: any, options: NuxtOptions, paths: string[]) {
let sharedValue = sourceValue
for (const path of paths) {
const segments = path.split('.')
const key = segments.pop()!
let parent: Record<string, any> = options
while (segments.length) {
const key = segments.shift()!
parent = parent[key] || (parent[key] = {})
}
delete parent[key]
Object.defineProperties(parent, {
[key]: {
configurable: false,
enumerable: true,
get: () => sharedValue,
set (value) {
sharedValue = value
},
},
})
}
}

View File

@ -1,5 +1,6 @@
import { addBuildPlugin, addComponent } from 'nuxt/kit' import { addBuildPlugin, addComponent } from 'nuxt/kit'
import type { NuxtPage } from 'nuxt/schema' import type { NuxtPage } from 'nuxt/schema'
import { defu } from 'defu'
import { createUnplugin } from 'unplugin' import { createUnplugin } from 'unplugin'
import { withoutLeadingSlash } from 'ufo' import { withoutLeadingSlash } from 'ufo'
@ -88,10 +89,17 @@ export default defineNuxtConfig({
runtimeConfig: { runtimeConfig: {
public: { public: {
needsFallback: undefined, needsFallback: undefined,
testConfig: 123,
}, },
}, },
modules: [ modules: [
function (_options, nuxt) {
// ensure setting `runtimeConfig` also sets `nitro.runtimeConfig`
nuxt.options.runtimeConfig = defu(nuxt.options.runtimeConfig, {
public: {
testConfig: 123,
},
})
},
function (_options, nuxt) { function (_options, nuxt) {
nuxt.hook('modules:done', () => { nuxt.hook('modules:done', () => {
// @ts-expect-error not valid nuxt option // @ts-expect-error not valid nuxt option