npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

malinajs-router

v0.1.9

Published

Declarative router for Malina.js web applications

Downloads

14

Readme

malinajs-router

Declarative router for Malina.js web applications.

Contents

Usage

There is only one Route component you should to import into your project. Example of the simple routes stricture:

//App.xht
<script>
  import {Route} from 'malinajs-router';
  import Contacts from './Contacts.xht';
</script>

<a href="/">Home</a> 
<a href="/about">About</a>
<a href="/portfolio">Portfolio</a>
<a href="/contacts">Contacts</a>

<Route>
  <Route path="/">This is main page</Route>
  <Route path="/about">This is about page</Route>
  <Route path="/contacts"><Contacts/></Route>
  <Route path="/portfolio/*">

    <a href="/portfolio/photos">Photography</a>
    <a href="/portfolio/websites">Websites</a>

    <Route path="/">Please choose a section</Route>
    <Route path="/photos">My photo gallery...</Route>
    <Route path="/websites">My websites gallery...</Route>

  </Route>
<Route>

Links

Use common a links with needed href attribute in your app. They will be handeled by router. You don't need to change hrefs when you use different navigation methods.

<a href="/">Main page</a>
<a href="/foo">Foo page</a>

Also you can use hash-based notation, but usualy it is not necessary.

<a href="/#/">Main page</a>
<a href="/#/foo">Foo page</a>

Nested routes

Routes may be two types exact and non-exact. Non-exact routes must have an path property which ends with /*.

  • exact-route will be shown only when its path property exactly match given URL.
  • non-exact-route will be shown when its path property coresponding URL from start. Place nested routes only inside non-exact routes.

Examples:

<!-- shows only when URL is /page1, but not /page1/subpage -->
<Route path="/page1">Page1</Route> 

<!-- shows when URL is /page2 or /page2/subpage -->
<Route path="/page2/*">Page2</Route> 

<!-- place nested routes inside non-exact routes only -->
<Route path="/page3/*">
  <Route path="/subpage"> 
</Route>

<!-- this will cause an error -->
<Route path="/page3">
  <Route path="/subpage"> 
</Route>

Redirects

An redirect property will rdirects user to another page.

<!-- will redirect user to the /anotherpage only 
     when given URL is /page1, but not /page1/subpage -->
<Route path="/page1" redirect="/anotherpage" />

<!-- will redirect user to the /anotherpage when 
     given URL is /page1 or /page1/subpage -->
<Route path="/page1/*" redirect="/anotherpage" />

Fallbacks

When you need to show something to user when no any matched routes were found, use fallback property. It is important, that routes should be inside of nonexact Route component.

Fallbacks are bubbling, so when there no route matched under any nonexact subroute and it not have a fallback on its level, will be shown fallback from upper levels(firs occured). For example:

<Route>
  <Route path="/">Root</Route>
  <Route path="/foo/*">
     <Route path="/">Foo root</Route> 
     <Route fallback>404. Page not in foo</Route>
  </Route>
  <Route path="/bar/*">
     <Route path="/">Bar root</Route>
  </Route>
  <Route fallback>404. Page not found</Route>
</Route>

In this example, any URL except /,/foo or /bar will cause fallback showing. If URL is like /foo/something - will be shown fallback Page not in foo, but for URL like /bar/something will be shown common fallback from upper level, because there no fallbacks on its level.

Force reload

Sometimes you need that content inside Route slot would be reloaded each time the URL changes. For example, when Route's path has parameter components inside slot never be rerendered when paramaeter changes. Add the force property to reload Route content each time something changes un the URL.

<Route>
  <Route path="/foo/:param">
    <input *{$element.value = ''} />
  </Route>
</Route>

URL metadata

Each Route component provide additional metadata from URL as slot variables...

<Route>
  {#slot url,query,params}
    Query for this route is: {query.toJson()}
  {/slot}
</Route>

... or as a store from component's context.

<!-- App.xht-->
<Route>
  <Sub/>
</Route>

<!-- Sub.xht-->
<script>
  let url;
  let query;
  let params;

  $context.route.subscribe( meta => {
    url = meta.url;
    query = meta.query;
    params = meta.params;
  });
</script>

Query for this route is: {query.toJson()}

At the moment you can get folowing metadata:

  • url - current URL (only path without query)
  • query - query string parsed in object
  • params - parameters values from URL

Query

Query string is a part of URL where you can path some data. It is situated right after ? sign in URL. It is parsed in object.

For example, ?hello=world&&foo will be parsed in object {hello:"world",foo: true}.

Parameters

You can get slugs from URL as parameters, just use :name notation to define parameter place in the path property of the Route component.

<!-- Example URL - /books/stanislav_lem/solaris-->
<Route path="/book/:author">
  {#slot params}
    <!-- Params here: {author: "stanislav_lem"} -->
    Author: {params.author}
  {/slot}
  <Route path="/:title">
    {#slot params}
      <!-- Params here: {author: "stanislav_lem", title: "solaris"} -->
      Book title: {params.title}
    {/slot}
  </Route>
</Route>

Router API

Use router import to perform some actions and settings for routing.

import {router} from 'malinajs-router';

Programmatic navigation

You can perform user to navigate to any path using goto method.

router.goto('/some/path');

Navigation method

The malinajs-router supports two types of navigation - History API and hash-based routing. B

  • History API - in this case you'll get common URL path for each page like /,/foo,/foo/bar. Page not reladed when user folows links on a page. But you must do some additional settings on your production server(nginx,apache,derver) to serve all requests to index.html file to avoid 404 error, when user starts navigating not from root URL. This method used by default, except when app is loaded inside frame.

  • Hash-based - in some cases you need to use this type of routing. In this case URL path will be situated in fragment part of URL only, right after # sign. For example, #/, #/foo,#/foo/bar. It is good choice when you can't setup your server to support Histroy API. If your app is loadied inside frame, this method will be active by default, in other cases you may to turn on hash-based method by calling router.method in your app's root file, usually App.xht.

import {router} from 'malinajs-router';
router.method('hash');