All Projects → tivac → svelte-routing

tivac / svelte-routing

Licence: MIT license
Svelte routing experiments

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].