Skip to content

Commit

Permalink
use router basepath in client plugin (#53)
Browse files Browse the repository at this point in the history
* use router basepath in client plugin

This should fix #52 by building the fetch URI using publicPath and router.base

* Apply wished changes

* fix: fix linter issues
  • Loading branch information
chrisjung-dev authored Jun 9, 2020
1 parent 9be8a01 commit 2ad58de
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,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 {
Expand Down Expand Up @@ -178,6 +183,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
Expand Down

0 comments on commit 2ad58de

Please sign in to comment.