From 1196c17a2a3db747a93765a80e29c6863c047fd4 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 4 Feb 2021 20:56:18 +0000 Subject: [PATCH] fix(vue-app): enable dynamic publicPath on server-side (#8759) --- packages/vue-app/template/server.js | 11 ++-- test/dev/basic.dynamic.test.js | 8 +++ test/fixtures/basic/assets/test.svg | 98 +++++++++++++++++++++++++++++ test/fixtures/basic/pages/index.vue | 5 +- 4 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/basic/assets/test.svg diff --git a/packages/vue-app/template/server.js b/packages/vue-app/template/server.js index 61953953b6..7b0c3a7b9b 100644 --- a/packages/vue-app/template/server.js +++ b/packages/vue-app/template/server.js @@ -1,6 +1,6 @@ import { stringify } from 'querystring' import Vue from 'vue' -import { normalizeURL } from 'ufo' +import { normalizeURL, joinURL } from 'ufo' <% if (fetch.server) { %>import fetch from 'node-fetch'<% } %> <% if (features.middleware) { %>import middleware from './middleware.js'<% } %> import { @@ -51,10 +51,6 @@ Vue.component(NuxtLink.name, NuxtLink) const noopApp = () => new Vue({ render: h => h('div', { domProps: { id: '<%= globals.id %>' } }) }) -function urlJoin () { - return Array.prototype.slice.call(arguments).join('/').replace(/\/+/g, '/') -} - const createNext = ssrContext => (opts) => { // If static target, render on client-side ssrContext.redirected = opts @@ -67,7 +63,7 @@ const createNext = ssrContext => (opts) => { const $config = ssrContext.runtimeConfig || {} const routerBase = ($config.app && $config.app.basePath) || '<%= router.base %>' if (!opts.path.startsWith('http') && (routerBase !== '/' && !opts.path.startsWith(routerBase))) { - opts.path = urlJoin(routerBase, opts.path) + opts.path = joinURL(routerBase, opts.path) } // Avoid loop redirect if (decodeURI(opts.path) === decodeURI(ssrContext.url)) { @@ -105,6 +101,9 @@ export default async (ssrContext) => { <% } %> // Public runtime config ssrContext.nuxt.config = ssrContext.runtimeConfig.public + if (ssrContext.nuxt.config.app) { + __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) const { app, router<%= (store ? ', store' : '') %> } = await createApp(ssrContext, { ...ssrContext.runtimeConfig.public, ...ssrContext.runtimeConfig.private }) const _app = new Vue(app) diff --git a/test/dev/basic.dynamic.test.js b/test/dev/basic.dynamic.test.js index 70e4fedea5..c05803adf6 100644 --- a/test/dev/basic.dynamic.test.js +++ b/test/dev/basic.dynamic.test.js @@ -62,6 +62,14 @@ describe('basic ssr', () => { expect(fetchCount).toBeGreaterThan(0) }) + test('dynamic publicPath is used in ssr html', async () => { + const { html } = await nuxt.server.renderRoute('/', { + resources: resourceLoader + }) + + expect(html).toContain(' + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/fixtures/basic/pages/index.vue b/test/fixtures/basic/pages/index.vue index 53e890d488..a6a5d328a0 100644 --- a/test/fixtures/basic/pages/index.vue +++ b/test/fixtures/basic/pages/index.vue @@ -1,3 +1,6 @@