fix: correct public path in generation and start (#5202)

This commit is contained in:
Xin Du (Clark) 2019-03-12 15:52:15 +00:00 committed by GitHub
parent c6565c97c6
commit 648062c308
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,7 +5,7 @@ import consola from 'consola'
import devalue from '@nuxt/devalue' import devalue from '@nuxt/devalue'
import invert from 'lodash/invert' import invert from 'lodash/invert'
import template from 'lodash/template' import template from 'lodash/template'
import { waitFor } from '@nuxt/utils' import { waitFor, isUrl, urlJoin } from '@nuxt/utils'
import { createBundleRenderer } from 'vue-server-renderer' import { createBundleRenderer } from 'vue-server-renderer'
import SPAMetaRenderer from './spa-meta' import SPAMetaRenderer from './spa-meta'
@ -14,6 +14,9 @@ export default class VueRenderer {
constructor(context) { constructor(context) {
this.context = context this.context = context
const { build: { publicPath }, router: { base } } = this.context.options
this.publicPath = isUrl(publicPath) ? publicPath : urlJoin(base, publicPath)
// Will be set by createRenderer // Will be set by createRenderer
this.renderer = { this.renderer = {
ssr: undefined, ssr: undefined,
@ -55,11 +58,11 @@ export default class VueRenderer {
renderScripts(context) { renderScripts(context) {
if (this.context.options.modern === 'client') { if (this.context.options.modern === 'client') {
const { publicPath, crossorigin } = this.context.options.build
const scriptPattern = /<script[^>]*?src="([^"]*?)"[^>]*?>[^<]*?<\/script>/g const scriptPattern = /<script[^>]*?src="([^"]*?)"[^>]*?>[^<]*?<\/script>/g
return context.renderScripts().replace(scriptPattern, (scriptTag, jsFile) => { return context.renderScripts().replace(scriptPattern, (scriptTag, jsFile) => {
const legacyJsFile = jsFile.replace(publicPath, '') const legacyJsFile = jsFile.replace(this.publicPath, '')
const modernJsFile = this.assetsMapping[legacyJsFile] const modernJsFile = this.assetsMapping[legacyJsFile]
const { build: { crossorigin } } = this.context.options
const cors = `${crossorigin ? ` crossorigin="${crossorigin}"` : ''}` const cors = `${crossorigin ? ` crossorigin="${crossorigin}"` : ''}`
const moduleTag = modernJsFile const moduleTag = modernJsFile
? scriptTag ? scriptTag
@ -95,14 +98,14 @@ export default class VueRenderer {
renderSsrResourceHints(context) { renderSsrResourceHints(context) {
if (this.context.options.modern === 'client') { if (this.context.options.modern === 'client') {
const { publicPath, crossorigin } = this.context.options.build
const linkPattern = /<link[^>]*?href="([^"]*?)"[^>]*?as="script"[^>]*?>/g const linkPattern = /<link[^>]*?href="([^"]*?)"[^>]*?as="script"[^>]*?>/g
return context.renderResourceHints().replace(linkPattern, (linkTag, jsFile) => { return context.renderResourceHints().replace(linkPattern, (linkTag, jsFile) => {
const legacyJsFile = jsFile.replace(publicPath, '') const legacyJsFile = jsFile.replace(this.publicPath, '')
const modernJsFile = this.assetsMapping[legacyJsFile] const modernJsFile = this.assetsMapping[legacyJsFile]
if (!modernJsFile) { if (!modernJsFile) {
return '' return ''
} }
const { crossorigin } = this.context.options.build
const cors = `${crossorigin ? ` crossorigin="${crossorigin}"` : ''}` const cors = `${crossorigin ? ` crossorigin="${crossorigin}"` : ''}`
return linkTag.replace('rel="preload"', `rel="modulepreload"${cors}`).replace(legacyJsFile, modernJsFile) return linkTag.replace('rel="preload"', `rel="modulepreload"${cors}`).replace(legacyJsFile, modernJsFile)
}) })