fix: fix asset loading on subfolder deployments (#287)

uci-hugo-doc
Robert Kaussow 2022-01-16 16:03:31 +01:00 committed by GitHub
parent 367d2b8a7c
commit cdc8c1faed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View File

@ -8,6 +8,7 @@
placeholder="Search..." placeholder="Search..."
aria-label="Search" aria-label="Search"
maxlength="64" maxlength="64"
data-site-base-url="{{ .Site.BaseURL }}"
/> />
<div class="gdoc-search__spinner spinner hidden"></div> <div class="gdoc-search__spinner spinner hidden"></div>
<ul id="gdoc-search-results" class="gdoc-search__list"></ul> <ul id="gdoc-search-results" class="gdoc-search__list"></ul>

View File

@ -5,6 +5,7 @@ const { Validator } = require("@cfworker/json-schema")
document.addEventListener("DOMContentLoaded", function (event) { document.addEventListener("DOMContentLoaded", function (event) {
const input = document.querySelector("#gdoc-search-input") const input = document.querySelector("#gdoc-search-input")
const results = document.querySelector("#gdoc-search-results") const results = document.querySelector("#gdoc-search-results")
const basePath = urlPath(input ? input.dataset.siteBaseUrl : "")
const configSchema = { const configSchema = {
type: "object", type: "object",
@ -23,7 +24,9 @@ document.addEventListener("DOMContentLoaded", function (event) {
} }
const validator = new Validator(configSchema) const validator = new Validator(configSchema)
getJson("/search/config.min.json", function (searchConfig) { if (!input) return
getJson(combineURLs(basePath, "/search/config.min.json"), function (searchConfig) {
const validationResult = validator.validate(searchConfig) const validationResult = validator.validate(searchConfig)
if (!validationResult.valid) if (!validationResult.valid)
@ -193,3 +196,24 @@ function flattenHits(results) {
return items return items
} }
function urlPath(rawURL) {
var parser = document.createElement("a")
parser.href = rawURL
return parser.pathname
}
/**
* Part of [axios](https://github.com/axios/axios/blob/master/lib/helpers/combineURLs.js).
* Creates a new URL by combining the specified URLs
*
* @param {string} baseURL The base URL
* @param {string} relativeURL The relative URL
* @returns {string} The combined URL
*/
function combineURLs(baseURL, relativeURL) {
return relativeURL
? baseURL.replace(/\/+$/, "") + "/" + relativeURL.replace(/^\/+/, "")
: baseURL
}

View File

@ -27,7 +27,6 @@ var config = {
filename: "js/[name]-[contenthash:8].bundle.min.js", filename: "js/[name]-[contenthash:8].bundle.min.js",
chunkFilename: "js/[name]-[contenthash:8].chunk.min.js", chunkFilename: "js/[name]-[contenthash:8].chunk.min.js",
path: path.join(__dirname, "static"), path: path.join(__dirname, "static"),
publicPath: "/",
clean: true clean: true
}, },
watchOptions: { watchOptions: {