GitXplorerGitXplorer
t

svelte-routing

public
14 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
0b697e8e4781177a3fc02541c9504d2a01753515

:lock:

ttivac committed 7 years ago
Unverified
17f7d0cb7cdf72068735ebcacca226c36c92db33

Better nested route state example

ttivac committed 7 years ago
Unverified
94415d5e35dc85aa53d7c9029b19fdee4e58d973

:pencil2: readme and minor cleanup

ttivac committed 7 years ago
Unverified
0b26932913401cff713560021efc77953ce169f6

Try out a component hierarchy via middleware

ttivac committed 7 years ago
Unverified
84961b1a99c958a7f1d5067be8d361801925d3df

Basic layout + routing

ttivac committed 7 years ago
Unverified
d05c1ecf5615c6f590cbc6fdd59aef0c2e234df4

Setting up a shop

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.