refactor: inline staticAssetsBase in state chunk for full-static (#7401)

* refactor(vue-app): use NUXT.serverRendered instead of NUXT.spa

* refactor: inline staticAssetsBase into state.js chunk

* revert: unwanted change

[release]
This commit is contained in:
Pooya Parsa 2020-05-20 19:31:31 +02:00 committed by GitHub
parent 60ae0fb011
commit 1e53e38f8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 10 deletions

View File

@ -305,7 +305,7 @@ export default {
async fetchPayload(route) {
route = (route.replace(/\/+$/, '') || '/').split('?')[0]
try {
const src = urlJoin(window.__NUXT_STATIC__, route, 'payload.js')
const src = urlJoin(window.<%= globals.context %>.staticAssetsBase, route, 'payload.js')
const payload = await window.__NUXT_IMPORT__(route, src)
this.setPagePayload(payload)
return payload

View File

@ -805,7 +805,7 @@ async function mountApp (__app) {
<% if (isFullStatic) { %>
// Load page chunk
if (!NUXT.data && !NUXT.spa) {
if (!NUXT.data && NUXT.serverRendered) {
try {
const payload = await _app.fetchPayload(_app.context.route.path)
Object.assign(NUXT, payload)

View File

@ -152,12 +152,9 @@ export default class SPARenderer extends BaseRenderer {
// Serialize state (runtime config)
let APP = `${meta.BODY_SCRIPTS_PREPEND}<div id="${this.serverContext.globals.id}">${this.serverContext.resources.loadingHTML}</div>${meta.BODY_SCRIPTS}`
if (renderContext.staticAssetsBase) {
APP += `<script>window.__NUXT_STATIC__='${renderContext.staticAssetsBase}'</script>`
}
APP += `<script>window.${this.serverContext.globals.context}=${devalue({
config: renderContext.runtimeConfig.public,
spa: true
staticAssetsBase: renderContext.staticAssetsBase
})}</script>`
// Prepare template params

View File

@ -168,8 +168,10 @@ export default class SSRRenderer extends BaseRenderer {
const { data, fetch, mutations, ...state } = nuxt
// Initial state
const nuxtStaticScript = `window.__NUXT_STATIC__='${staticAssetsBase}';`
const stateScript = `window.${this.serverContext.globals.context}=${devalue(state)};`
const stateScript = `window.${this.serverContext.globals.context}=${devalue({
staticAssetsBase,
...state
})};`
// Make chunk for initial state > 10 KB
const stateScriptKb = (stateScript.length * 4 /* utf8 */) / 100
@ -177,11 +179,10 @@ export default class SSRRenderer extends BaseRenderer {
const statePath = urlJoin(url, 'state.js')
const stateUrl = urlJoin(staticAssetsBase, statePath)
staticAssets.push({ path: statePath, src: stateScript })
APP += `<script defer>${nuxtStaticScript}</script>`
APP += `<script defer src="${staticAssetsBase}${statePath}"></script>`
preloadScripts.push(stateUrl)
} else {
APP += `<script defer>${nuxtStaticScript}${stateScript}</script>`
APP += `<script>${stateScript}</script>`
}
// Page level payload.js (async loaded for CSR)