Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down