GitXplorerGitXplorer
t

svelte-routing

public
14 stars
0 forks
0 issues

Commits

List of commits on branch master.
Verified
3a747e109bb67f3743a05ba3bd4c6dc34527b62f

docs: flesh out idea descriptions

ttivac committed 7 years ago
Unverified
18f0a866d83e598d4032e674553e06525c24d94f

More logging!

ttivac committed 7 years ago
Unverified
989a5d8b87fa96357d716f0dafae2c0c28dc6bc2

Adding some logging to verify behavior

ttivac committed 7 years ago
Unverified
3b8029ccf096bfc29985b618c38fc56416d15020

Fix a nasty :bug:

ttivac committed 7 years ago
Unverified
97fc25ff9f6407337aa92e2d7912d896962efe42

Just double-checking

ttivac committed 7 years ago
Unverified
011c84fd4290d7a8bb640dfaeef98f377bb6d129

Unnecessary, always has a child

ttivac committed 7 years ago

README

The README file for this repository.

svelte-routing

This is an attempt to wire together svelte and page in a way that allows for smartly nested components much like you'd get with something like abstract-state-router.

Approach Details

  1. A route that matches everything runs first and creates an array on the context object
page("*", ({ context, next }) => {
  context.components = [];
  
  return next();
});
  1. Routes after the first wildcard push a { component, <data> } object onto that array
page("/route", ({ context, next }) => {
    context.components.push({
        component : /* ... */
        data      : /* ... */
    });
    
    return next();
});
  1. There's a final wildcard route that turns the array into nested objects and sets that into the svelte app
components.reduce((prev, { component, data = {} }) => {
    prev.page = {
        child : component,
        // Setting page to null is important to avoid stale state
        // bugs when moving up & down the tree
        props : Object.assign(data, { page : null }),
    };

    return prev.page.props;
}, props);

app.set(props);
  1. Components that nest children then use <svelte:component this={page.child} {...page.props}> to pass along the nested component/object mixes to their children.

Nested Components via Wildcard Routes

By using nested routes with trailing wildcards you can get persistent nested UI components, with all the svelte lifecycle hooks working like you'd expect.

page("/route/*", ...);

// The component for the previous route still exists if you
// navigate from /route to /route/subroute, it isn't recreated
page("/route/subroute", ...);

You can see all of this happening within /src/routes.js.