From a46f7897d3d28ef14aae89bf4058ca4694e2980f Mon Sep 17 00:00:00 2001 From: Chris Jung Date: Tue, 26 May 2020 22:45:34 +0200 Subject: [PATCH 1/3] use router basepath in client plugin This should fix #52 by building the fetch URI using publicPath and router.base --- lib/index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index ca2d522b0..c7c30eb06 100644 --- a/lib/index.js +++ b/lib/index.js @@ -118,17 +118,21 @@ 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' + dbPath: `${routerBasePath}${publicPath}` + 'content/db.json' } }) } else { From f85d2234cf8eb7f0428c88e4b31ea2036dcc527b Mon Sep 17 00:00:00 2001 From: Chris Jung Date: Wed, 3 Jun 2020 19:07:43 +0200 Subject: [PATCH 2/3] Apply wished changes --- lib/index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index c7c30eb06..961db0528 100644 --- a/lib/index.js +++ b/lib/index.js @@ -132,7 +132,8 @@ module.exports = async function () { fileName: 'content/plugin.client.js', src: join(__dirname, 'templates/plugin.static.js'), options: { - dbPath: `${routerBasePath}${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 { @@ -162,5 +163,14 @@ module.exports = async function () { }) } +function isUrl(string) { + try { + new URL(string) + } catch (_) { + return false + } + return true +} + module.exports.Database = Database module.exports.middleware = middleware From 5975291fccc050320cd9cc6bd86d5981e2f90938 Mon Sep 17 00:00:00 2001 From: Christian Jung Date: Wed, 3 Jun 2020 20:39:57 +0200 Subject: [PATCH 3/3] fix: fix linter issues --- lib/index.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/index.js b/lib/index.js index 961db0528..159648a2e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -161,15 +161,16 @@ module.exports = async function () { fileName: 'content/nuxt-content.js', src: join(__dirname, 'templates/nuxt-content.js') }) -} - -function isUrl(string) { - try { - new URL(string) - } catch (_) { - return false + 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 } - return true } module.exports.Database = Database