feature: support serving from subpath (#33)

Co-authored-by: Karthik T <karthikt.holmes+github@gmail.com>
This commit is contained in:
Henry Dollman
2025-02-04 21:22:40 -05:00
parent ce171cf375
commit 58085bf300
17 changed files with 118 additions and 54 deletions

View File

@@ -1,16 +1,36 @@
import { createRouter } from "@nanostores/router"
export const $router = createRouter(
{
home: "/",
server: "/system/:name",
settings: "/settings/:name?",
forgot_password: "/forgot-password",
},
{ links: false }
)
const routes = {
home: "/",
system: `/system/:name`,
settings: `/settings/:name?`,
forgot_password: `/forgot-password`,
} as const
/** Navigate to url using router */
/**
* The base path of the application.
* This is used to prepend the base path to all routes.
*/
export const basePath = window.BASE_PATH || ""
/**
* Prepends the base path to the given path.
* @param path The path to prepend the base path to.
* @returns The path with the base path prepended.
*/
export const prependBasePath = (path: string) => (basePath + path).replaceAll("//", "/")
// prepend base path to routes
for (const route in routes) {
// @ts-ignore need as const above to get nanostores to parse types properly
routes[route] = prependBasePath(routes[route])
}
export const $router = createRouter(routes, { links: false })
/** Navigate to url using router
* Base path is automatically prepended if serving from subpath
*/
export const navigate = (urlString: string) => {
$router.open(urlString)
}