0.0.3 • Published 3 years ago

svelte-regex-router v0.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

Note

A more detailed README and github repo will be coming soon.

Svelte Regex Router

svelte-regex-router is a simple, lightweight library for you to easily handle routes in your Svelte application.

Personally I had tried a couple options but none really worked, so I made my own.

The way this package works is that it uses document.location.pathname internally, which should be supported by all major browsers (yes, looking at you Internet Explorer), and takes in a Map, transforms the regexs for it, and lets you easily handle each path in it.

Example Usage

App.svelte

<script>
import Router from 'svelte-regex-router';

//Won't be written for sake of brevity
import HelloWorld from './HelloWorld.svelte';

import View404 from './View404.svelte';

const routes = new Map()
  .set('/', () => HelloWorld)
  .set('(.*)', () => View404);
</script>

<Router {routes}>

View404.svelte

<script>
export let params;
const [ path ] = params;
</script>

<h1>404</h1>
<p>Sorry, can't find the path at <b>{path}</b>. Maybe you went on the wrong train stop?</p>