diff --git a/README.md b/README.md index 1c2860b..dd40688 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ > + Always-on path and hash routing. Simultaneous and independent routing modes. > + The router that invented multi hash routing. ++ **Electron support**: Works with Electron (all routing modes) + **Reactivity-based**: All data is reactive, reducing the need for events and imperative programming. + **Always-on path and hash routing**: Add routers that use the URL's path name or the URL's hash value in the same application. Both routing modes are possible simultaneously. @@ -107,6 +108,21 @@ init(); init({ implicitMode: 'hash' }); ``` +#### Electron Variant + +In Electron, we must immediately navigate to the homepage (or your preferred initial route) right after initializing if you use path routing: + +```typescript +import { init, location } from "@wjfe/n-savant"; + +init(); +location.goTo('/'); +``` + +For applications that also run in the browser, condition the navigation to Electron only. See the [Electron page](https://wjfe-n-savant.hashnode.space/wjfe-n-savant/introduction/electron-support) online for more details. + +> **⚠️ Important:** Hash routing doesn't require this extra step. + ### Define the Routes ``s are added inside ``s. ``s can be nested inside other ``s. ``s can render diff --git a/src/lib/core/RouterEngine.svelte.ts b/src/lib/core/RouterEngine.svelte.ts index 462253f..c53d95a 100644 --- a/src/lib/core/RouterEngine.svelte.ts +++ b/src/lib/core/RouterEngine.svelte.ts @@ -137,7 +137,7 @@ export class RouterEngine { return this.#routePatterns; } - #testPath = $derived.by(() => noTrailingSlash(this.#hashId ? (location.hashPaths[this.#hashId] || '/') : this.url.pathname)); + #testPath = $derived.by(() => noTrailingSlash(this.#hashId ? (location.hashPaths[this.#hashId] || '/') : this.path)); #routeStatusData = $derived.by(() => { const routeStatus = {} as Record; @@ -243,6 +243,16 @@ export class RouterEngine { get url() { return location.url; } + /** + * Gets the environment's current path. + * + * This is a sanitized version of `location.url.pathname` that strips out drive letters for the case of Electron in + * Windows. It is highly recommended to always use this path whenever possible. + */ + get path() { + const hasDriveLetter = this.url.protocol.startsWith('file:') && this.url.pathname[2] === ':'; + return hasDriveLetter ? this.url.pathname.substring(3) : this.url.pathname; + } /** * Gets the browser's current state. *