Skip to content

Fix/nested router not routing #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions Router.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { getContext, setContext, onDestroy } from 'svelte';
import navaid from 'navaid';
import writableSet from './writable-set';
import { writable } from 'svelte/store';
import { readable, writable } from 'svelte/store';

export let base = '/';
export let middleware = null;
Expand All @@ -25,23 +25,31 @@
let router;

const context = getContext('navaid');
const contextActive = context && context.active || writable({ path: '' });
const contextLibrary = context && context.library || writable(null);
const contextBase = (context && context.base) || readable("/");
const contextActive = (context && context.active) || readable({ path: "" });
const contextLibrary = (context && context.library) || readable(null);

setContext('navaid', { routes, active, params, base: baseStore, library: libraryStore });
setContext('navigate', navigate);

$: {
const path = $contextActive.path;
if (path && path.slice(-1) === '*') {
base = path.slice(0, -1);
baseStore.set(base);
}
}
$: baseStore.set(base);
$: libraryStore.set(library != null ? library : $contextLibrary);
$: setNestedBase($contextActive ? $contextActive.path : '');
$: updateRoutes(base, $routes, $libraryStore || navaid);

function setNestedBase(activePath = "") {
const pathIsWildcard = /\*$/.test(activePath);
const routerUsesDefaultBase = base === "/";

if (pathIsWildcard && routerUsesDefaultBase) {
base = [
$contextBase.replace(/\/$/, ""),
activePath.replace(/^\//, "").replace(/\*$/, "")
].join("/");
baseStore.set(base);
}
}

function updateRoutes(base, routes, navaid) {
if (router) {
router.unlisten();
Expand All @@ -60,10 +68,12 @@
router.on(route.path, routeParams => {
$active = route;
$params = routeParams;

if (typeof middleware === 'function') {
middleware(route.path, routeParams);
}
});

if (route.path.slice(-2) === '/*') {
// Allow /url/* to match /url as well
router.on(route.path.slice(0, -2), routeParams => {
Expand All @@ -87,4 +97,4 @@
});
</script>

<slot></slot>
<slot />
47 changes: 41 additions & 6 deletions demo/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
<Router library={useHash ? navaidHash : null} bind:navigate>
<h1>Hello World!</h1>

<Link href="/">Home</Link> | <Link href="foo/sub1">Foo</Link> | <button on:click={() => navigate('/bar?abc=def')}>Bar</button> | <Link href="/blog/">Blog</Link>
<Link href="/">Home</Link>
|
<Link href="foo/sub1">Foo</Link>
|
<button on:click={() => navigate('/bar?abc=def')}>Bar</button>
|
<Link href="/blog/">Blog</Link>

<Route path="/">
<h2>Home</h2>
Expand All @@ -24,26 +30,55 @@

<Router>

<Link href="sub1">Sub 1</Link> | <Link href="/sub2">Sub 2</Link>
<Link href="sub1">Sub 1</Link>
|
<Link href="/sub2">Sub 2</Link>

<Route path="/sub1">
<h3>Sub Foo 1</h3>
</Route>

<Route path="/sub2">
<Route path="/sub2/*">
<h3>Sub Foo 2</h3>
</Route>

<Route path="/">
<h3>Sub Foo 2 index</h3>
</Route>

<Router>
<p>
<Link href="bar1">Sub Foo Bar 1</Link>
|
<Link href="/bar2">Sub Foo Bar 2</Link>
|
<Link href="/not-found">404</Link>
</p>

<Route path="/">
<div>Sub Foo Bar index</div>
</Route>

<Route path="bar1">
<div>Sub Foo Bar 1</div>
</Route>

<Route path="/bar2">
<div>Sub Foo Bar 2</div>
</Route>

<Route>Not found</Route>
</Router>
</Route>
</Router>
</Route>

<Route path="/bar">
<h2>Bar</h2>
</Route>

<Route path="/blog/*" component={Blog}/>
<Route path="/blog/*" component={Blog} />

<Route>
<h2>Page Not Found</h2>
</Route>
</Router>
</Router>
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
},
"license": "MIT",
"dependencies": {
"navaid": "^1.0.5"
"navaid": "^1.1.1"
},
"devDependencies": {
"npm-run-all": "^4.1.5",
"rollup": "^1.20.1",
"rollup-plugin-commonjs": "^10.0.2",
"rollup-plugin-livereload": "^1.0.1",
"rollup": "^2.17.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-livereload": "^1.3.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-svelte": "^5.1.0",
"sirv-cli": "^0.4.4",
"svelte": "^3.9.1"
"rollup-plugin-svelte": "^5.2.3",
"sirv-cli": "^1.0.1",
"svelte": "^3.23.2"
},
"keywords": [
"svelte"
Expand Down