refactor: move internal app runtime config to _app namespace (#9075)

This commit is contained in:
Daniel Roe 2021-03-30 17:38:51 +01:00 committed by GitHub
parent 589f3c0fa1
commit ca020cf9cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 18 additions and 13 deletions

View File

@ -459,9 +459,9 @@ export function getNuxtConfig (_options) {
assetsPath: isRelativePublicPath ? options.build.publicPath : useCDN ? '/' : joinURL(options.router.base, options.build.publicPath), assetsPath: isRelativePublicPath ? options.build.publicPath : useCDN ? '/' : joinURL(options.router.base, options.build.publicPath),
cdnURL: useCDN ? options.build.publicPath : null cdnURL: useCDN ? options.build.publicPath : null
}) })
// Expose app config to $config.app // Expose app config to $config._app
options.publicRuntimeConfig = options.publicRuntimeConfig || {} options.publicRuntimeConfig = options.publicRuntimeConfig || {}
options.publicRuntimeConfig.app = defu(options.publicRuntimeConfig.app, options.app) options.publicRuntimeConfig._app = options.app
// Generate staticAssets // Generate staticAssets
const { staticAssets } = options.generate const { staticAssets } = options.generate

View File

@ -323,7 +323,7 @@ Object {
"plugins": Array [], "plugins": Array [],
"privateRuntimeConfig": Object {}, "privateRuntimeConfig": Object {},
"publicRuntimeConfig": Object { "publicRuntimeConfig": Object {
"app": Object { "_app": Object {
"assetsPath": "/_nuxt/", "assetsPath": "/_nuxt/",
"basePath": "/", "basePath": "/",
"cdnURL": null, "cdnURL": null,

View File

@ -4,7 +4,12 @@
*/ */
export interface NuxtRuntimeConfig { export interface NuxtRuntimeConfig {
[key: string]: any [key: string]: any;
/**
* This is used internally by Nuxt for dynamic configuration and should not be used.
* @internal
*/
_app?: never;
} }
export type NuxtOptionsRuntimeConfig = NuxtRuntimeConfig | ((env: typeof process.env) => NuxtRuntimeConfig) export type NuxtOptionsRuntimeConfig = NuxtRuntimeConfig | ((env: typeof process.env) => NuxtRuntimeConfig)

View File

@ -49,8 +49,8 @@ let router
const NUXT = window.<%= globals.context %> || {} const NUXT = window.<%= globals.context %> || {}
const $config = NUXT.config || {} const $config = NUXT.config || {}
if ($config.app) { if ($config._app) {
__webpack_public_path__ = urlJoin($config.app.cdnURL, $config.app.assetsPath) __webpack_public_path__ = urlJoin($config._app.cdnURL, $config._app.assetsPath)
} }
Object.assign(Vue.config, <%= serialize(vue.config) %>)<%= isTest ? '// eslint-disable-line' : '' %> Object.assign(Vue.config, <%= serialize(vue.config) %>)<%= isTest ? '// eslint-disable-line' : '' %>

View File

@ -102,7 +102,7 @@ export const routerOptions = {
} }
export function createRouter (ssrContext, config) { export function createRouter (ssrContext, config) {
const base = (config.app && config.app.basePath) || routerOptions.base const base = (config._app && config._app.basePath) || routerOptions.base
const router = new Router({ ...routerOptions, base }) const router = new Router({ ...routerOptions, base })
// TODO: remove in Nuxt 3 // TODO: remove in Nuxt 3

View File

@ -59,7 +59,7 @@ const createNext = ssrContext => (opts) => {
} }
let fullPath = withQuery(opts.path, opts.query) let fullPath = withQuery(opts.path, opts.query)
const $config = ssrContext.runtimeConfig || {} const $config = ssrContext.runtimeConfig || {}
const routerBase = ($config.app && $config.app.basePath) || '<%= router.base %>' const routerBase = ($config._app && $config._app.basePath) || '<%= router.base %>'
if (!fullPath.startsWith('http') && (routerBase !== '/' && !fullPath.startsWith(routerBase))) { if (!fullPath.startsWith('http') && (routerBase !== '/' && !fullPath.startsWith(routerBase))) {
fullPath = joinURL(routerBase, fullPath) fullPath = joinURL(routerBase, fullPath)
} }
@ -99,8 +99,8 @@ export default async (ssrContext) => {
<% } %> <% } %>
// Public runtime config // Public runtime config
ssrContext.nuxt.config = ssrContext.runtimeConfig.public ssrContext.nuxt.config = ssrContext.runtimeConfig.public
if (ssrContext.nuxt.config.app) { if (ssrContext.nuxt.config._app) {
__webpack_public_path__ = joinURL(ssrContext.nuxt.config.app.cdnURL, ssrContext.nuxt.config.app.assetsPath) __webpack_public_path__ = joinURL(ssrContext.nuxt.config._app.cdnURL, ssrContext.nuxt.config._app.assetsPath)
} }
// Create the app definition and the instance (created for each request) // Create the app definition and the instance (created for each request)
const { app, router<%= (store ? ', store' : '') %> } = await createApp(ssrContext, ssrContext.runtimeConfig.private) const { app, router<%= (store ? ', store' : '') %> } = await createApp(ssrContext, ssrContext.runtimeConfig.private)

View File

@ -55,9 +55,9 @@ describe('basic ssr', () => {
expect(window.document.body.innerHTML).toContain('<h1>Index page</h1>') expect(window.document.body.innerHTML).toContain('<h1>Index page</h1>')
expect(window.__NUXT__.config.app.basePath).toBe('/path/') expect(window.__NUXT__.config._app.basePath).toBe('/path/')
expect(window.__NUXT__.config.app.cdnURL).toBe('https://cdn.nuxtjs.org/') expect(window.__NUXT__.config._app.cdnURL).toBe('https://cdn.nuxtjs.org/')
expect(window.__NUXT__.config.app.assetsPath).toBe('/') expect(window.__NUXT__.config._app.assetsPath).toBe('/')
expect(fetchCount).toBeGreaterThan(0) expect(fetchCount).toBeGreaterThan(0)
}) })