fix: handle router.base for crawler and serve (#7486)

Co-authored-by: pooya parsa <pyapar@gmail.com>
This commit is contained in:
Sébastien Chopin 2020-06-10 09:51:29 +02:00 committed by GitHub
parent 4ca3c769e3
commit 7c6e170a89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 4 deletions

View File

@ -39,6 +39,7 @@ export default {
const app = connect() const app = connect()
app.use(compression({ threshold: 0 })) app.use(compression({ threshold: 0 }))
app.use( app.use(
options.router.base,
serveStatic(options.generate.dir, { serveStatic(options.generate.dir, {
extensions: ['html'] extensions: ['html']
}) })

View File

@ -302,6 +302,7 @@ export default class Generator {
const possibleTrailingSlash = this.options.router.trailingSlash ? '/' : '' const possibleTrailingSlash = this.options.router.trailingSlash ? '/' : ''
parse(html).querySelectorAll('a').map((el) => { parse(html).querySelectorAll('a').map((el) => {
const sanitizedHref = (el.getAttribute('href') || '') const sanitizedHref = (el.getAttribute('href') || '')
.replace(this.options.router.base, '/')
.replace(/\/+$/, '') .replace(/\/+$/, '')
.split('?')[0] .split('?')[0]
.split('#')[0] .split('#')[0]

View File

@ -303,9 +303,14 @@ export default {
this._payloadFetchIndex = 0 this._payloadFetchIndex = 0
}, },
async fetchPayload(route) { async fetchPayload(route) {
const { staticAssetsBase } = window.<%= globals.context %>
const base = (this.$router.options.base || '').replace(/\/+$/, '')
if (base && route.startsWith(base)) {
route = route.substr(base.length)
}
route = (route.replace(/\/+$/, '') || '/').split('?')[0] route = (route.replace(/\/+$/, '') || '/').split('?')[0]
const src = urlJoin(base, staticAssetsBase, route, 'payload.js')
try { try {
const src = urlJoin(window.<%= globals.context %>.staticAssetsBase, route, 'payload.js')
const payload = await window.__NUXT_IMPORT__(route, src) const payload = await window.__NUXT_IMPORT__(route, src)
this.setPagePayload(payload) this.setPagePayload(payload)
return payload return payload

View File

@ -164,6 +164,7 @@ export default class SSRRenderer extends BaseRenderer {
if (renderContext.staticAssetsBase) { if (renderContext.staticAssetsBase) {
const preloadScripts = [] const preloadScripts = []
renderContext.staticAssets = [] renderContext.staticAssets = []
const routerBase = this.options.router.base
const { staticAssetsBase, url, nuxt, staticAssets } = renderContext const { staticAssetsBase, url, nuxt, staticAssets } = renderContext
const { data, fetch, mutations, ...state } = nuxt const { data, fetch, mutations, ...state } = nuxt
@ -177,9 +178,9 @@ export default class SSRRenderer extends BaseRenderer {
const stateScriptKb = (stateScript.length * 4 /* utf8 */) / 100 const stateScriptKb = (stateScript.length * 4 /* utf8 */) / 100
if (stateScriptKb > 10) { if (stateScriptKb > 10) {
const statePath = urlJoin(url, 'state.js') const statePath = urlJoin(url, 'state.js')
const stateUrl = urlJoin(staticAssetsBase, statePath) const stateUrl = urlJoin(routerBase, staticAssetsBase, statePath)
staticAssets.push({ path: statePath, src: stateScript }) staticAssets.push({ path: statePath, src: stateScript })
APP += `<script defer src="${staticAssetsBase}${statePath}"></script>` APP += `<script defer src="${stateUrl}"></script>`
preloadScripts.push(stateUrl) preloadScripts.push(stateUrl)
} else { } else {
APP += `<script>${stateScript}</script>` APP += `<script>${stateScript}</script>`
@ -187,7 +188,7 @@ export default class SSRRenderer extends BaseRenderer {
// Page level payload.js (async loaded for CSR) // Page level payload.js (async loaded for CSR)
const payloadPath = urlJoin(url, 'payload.js') const payloadPath = urlJoin(url, 'payload.js')
const payloadUrl = urlJoin(staticAssetsBase, payloadPath) const payloadUrl = urlJoin(routerBase, staticAssetsBase, payloadPath)
const routePath = (url.replace(/\/+$/, '') || '/').split('?')[0] // remove trailing slah and query params const routePath = (url.replace(/\/+$/, '') || '/').split('?')[0] // remove trailing slah and query params
const payloadScript = `__NUXT_JSONP__("${routePath}", ${devalue({ data, fetch, mutations })});` const payloadScript = `__NUXT_JSONP__("${routePath}", ${devalue({ data, fetch, mutations })});`
staticAssets.push({ path: payloadPath, src: payloadScript }) staticAssets.push({ path: payloadPath, src: payloadScript })

View File

@ -5,6 +5,9 @@ export default {
config: true config: true
} }
}, },
router: {
// base: '/test'
},
hooks: { hooks: {
export: { export: {
before ({ setPayload }) { before ({ setPayload }) {