diff --git a/lib/index.js b/lib/index.js index ca2d522b0..159648a2e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -118,17 +118,22 @@ module.exports = async function () { dirs: database.dirs } }) - let publicPath = this.options.build.publicPath // can be an url + let routerBasePath = this.options.router.base + /* istanbul ignore if */ if (publicPath[publicPath.length - 1] !== '/') { publicPath += '/' } + if (routerBasePath[routerBasePath.length - 1] === '/') { + routerBasePath = routerBasePath.slice(0, -1) + } this.addPlugin({ fileName: 'content/plugin.client.js', src: join(__dirname, 'templates/plugin.static.js'), options: { - dbPath: publicPath + 'content/db.json' + // if publicPath is an URL, use public path, if not, add basepath before it + dbPath: isUrl(publicPath) ? `${publicPath}content/db.json` : `${routerBasePath}${publicPath}content/db.json` } }) } else { @@ -156,6 +161,16 @@ module.exports = async function () { fileName: 'content/nuxt-content.js', src: join(__dirname, 'templates/nuxt-content.js') }) + function isUrl (string) { + try { + // quick test if the string is an URL + // eslint-disable-next-line no-new + new URL(string) + } catch (_) { + return false + } + return true + } } module.exports.Database = Database