Coder Social home page Coder Social logo

kidkarolis / space-router Goto Github PK

View Code? Open in Web Editor NEW
38.0 3.0 4.0 5.11 MB

Framework agnostic router for single page apps

Home Page: https://kidkarolis.github.io/space-router/

License: ISC License

JavaScript 100.00%
browser router hash pushstate callback react

space-router's Introduction

space router logo, a skeleton floating in space

Space Router

Framework agnostic router for single page apps


Space Router packs all the features you need to keep your app in sync with the url. It's distinct from many other routers in that there is only a single callback. This callback can be used to re-render your applocation, update a store and perform other actions on each url change. Space Router is stateless, it doesn't store the current route leaving state completely up to you to handle.

In summary, Space Router:

  • listens to url changes using popstate or hashchange event
  • extracts url parameters and parses query strings
  • supports nested routes and arbitrary route metadata
  • fits into a wide range of application architectures and frameworks
  • has no dependencies and weighs less than 2kb

Install

npm i space-router

API

See the API Docs for more details.

space-router's People

Contributors

kidkarolis avatar kwanman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

space-router's Issues

Add a .match() or similar helper for SSR

Right now I had to do this to extract the route on the server:

router.match = (url) => {
    const result = {}
    router.start((route, data) => {
      result.route = route
      result.data = data
    })
    router.push(url)
    return result
  }

Should just be a simple call, like const { route, data } = router.match(url) or similar.

Child route can't have same route as parent unless its top-level

I'm trying to implement something like this:

['', App, [
  ['/foos', FoosList, [
    ['/foos/:foo', FooContainer, [
      ['/foos/:foo', FooOverview],
      ['/foos/:foo/bar', FooBar],
      ['/foos/:foo/baz', FooBaz]
    ]
  ]
]]

Where:

  • FooContainer renders the 'shell' of the view (maybe a tab/header bar at the top)
  • FooOverview, Foobar and FooBaz are sub-views of FooContainer
  • FooOverview is the 'default' sub-view that gets rendered when you go to /foos/:foo

This doesn't work right now because when router.data() just returns the first route that matches the pattern , which is FooContainer. Which means FooOverview never gets rendered. FooBar and FooBaz work, however, because the pattern is different.

Not sure what the right fix is here. The easiest solution is literally just to .reverse() the route array after flattening it, but it feels kinda messy because there are items in the array that basically never get rendered.

Consider not matching '*'

So that relative links that aren't routable aren't intercepted. But sometimes it's useful if you know you can render 404 page. So consider doing it by returning false in onTransition which would indicate to opt out of handling this route and letting the browser do so. This is kind of an alternative to #6

Passing multiple props based on route; not just `children`

From what I understand, space-router passes child-route components as the children prop to the parent route component. It would be useful if we had the option of passing multiple props based on route configuration. One for rendering the main page, say, and one for rendering some controls in the upper right corner.

I'm imagining a syntax like this, though I suspect it wouldn't be compatible with the current API:

const routes = ['', Qfn, [
  ['/p/:propertyId/integrations/:friendlyId', {
    mainPage: QfnOverview,
    controls: QfnOverviewControls
  }],
  // other stuff
]]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.