Refactor routing and resource fetching to use BASE_URL for improved environment compatibility

This commit is contained in:
Supertiger
2025-08-01 18:20:47 +01:00
parent e9f2ac6063
commit 224e6ed9fd
6 changed files with 17 additions and 8 deletions

View File

@@ -1,9 +1,9 @@
if (location.pathname === "/waybar") { if (location.pathname === import.meta.env.BASE_URL + "/waybar") {
import("./routes/waybar/waybar").then(({ createWaybarPage }) => import("./routes/waybar/waybar").then(({ createWaybarPage }) =>
createWaybarPage() createWaybarPage()
); );
} }
if (location.pathname === "/") { if (location.pathname === import.meta.env.BASE_URL) {
import("./routes/root/root").then(({ createRootPage }) => createRootPage()); import("./routes/root/root").then(({ createRootPage }) => createRootPage());
} }

View File

@@ -3,6 +3,8 @@ const appElement = document.getElementById("app")!;
export const createRootPage = async () => { export const createRootPage = async () => {
appElement.innerHTML = ` appElement.innerHTML = `
<iframe id="waybar-iframe" src="/waybar"></iframe> <iframe id="waybar-iframe" src="${
import.meta.env.BASE_URL
}/waybar"></iframe>
`; `;
}; };

View File

@@ -282,7 +282,9 @@ export interface WaybarConfig {
} }
export const parseConfig = async () => { export const parseConfig = async () => {
const response = await fetch("/resources/config.jsonc"); const response = await fetch(
import.meta.env.BASE_URL + "/resources/config.jsonc"
);
const jsoncString = await response.text(); const jsoncString = await response.text();
return parse(jsoncString) as WaybarConfig; return parse(jsoncString) as WaybarConfig;
}; };

View File

@@ -4,9 +4,9 @@ export const clamp = (val: number, min: number, max: number) =>
Math.min(Math.max(val, min), max); Math.min(Math.max(val, min), max);
const fetchFontAwesomeStylesheet = async () => { const fetchFontAwesomeStylesheet = async () => {
const raw = await fetch("/fontawesome/css/fontawesome.min.css").then((res) => const raw = await fetch(
res.text() import.meta.env.BASE_URL + "/fontawesome/css/fontawesome.min.css"
); ).then((res) => res.text());
return raw; return raw;
}; };

View File

@@ -9,7 +9,7 @@ const appElement = document.getElementById("app")!;
export const createWaybarPage = async () => { export const createWaybarPage = async () => {
const styleElement = document.createElement("link"); const styleElement = document.createElement("link");
styleElement.rel = "stylesheet"; styleElement.rel = "stylesheet";
styleElement.href = "/resources/style.css"; styleElement.href = import.meta.env.BASE_URL + "/resources/style.css";
document.head.appendChild(styleElement); document.head.appendChild(styleElement);
const config = await parseConfig(); const config = await parseConfig();

5
vite.config.ts Normal file
View File

@@ -0,0 +1,5 @@
import { defineConfig } from "vite";
export default defineConfig({
base: "/waybar-online",
});