A very tiny router for non WP sites

Hi everybody!

Lately I found myself building my side projects with Frontity, even though they are not WP projects, because Frontity includes all the tech I want to use without having to wire anything (react-easy-state, emotion…).

One of the problems I run into is that the current @frontity/tiny-router package is dependent on having a source package. It might be that you don’t even have a back end, but still need to do some routing, so I just created a small package to cover my needs and I am publishing it here in case somebody else wants to use it.

The package is just a clone of @frontity/tiny-router removing the parts that are used with the source package and some features that didn’t seem relevant here (but if needed by anybody in the future they can be included).

I also cloned the Link component from @frontity/components with the needed modifications and simplifications to be used with this router.

Also, this was a quick adaptation and though I checked that it worked properly, it has no tests and it might contain bugs :sweat_smile: If you use it and run into any trouble, let me know.

Anyway, here it goes:

Info

Name: @orballo/very-tiny-router
NPM: @orballo/very-tiny-router - npm
Github: GitHub - orballo/very-tiny-router: A very tiny router (source agnostic) for Frontity

Install

npm i @orballo/very-tiny-router

Usage

Router action

const Button = () => {
  const { actions } = useConnect();

  const handleClick = () => {
    actions.router.set("/");
  };

  return <button onClick={handleClick}>Go Home</button>;
};

Link component

import Link from "@orballo/very-tiny-router/link";

const Component = () => {
  return <Link link="/">Go Home</Link>;
};

If somebody finds it useful and needs some further explanation, don’t hesitate to ping me here and I’ll gladly help.

7 Likes

Awesome work @orballo! Love the name! :sweat_smile:

Was wondering do you think we could fix-up frontity’s tiny-router as well? I feel it’s a solid case and we could bring the test cases to make sure the functionality is there :slight_smile:.

1 Like

Thanks! :smiley:

I’m not sure what you mean by fixing up tiny-router or what you have in mind. I think tiny-router is conditioned to be dependent on wp-source or at least a source package because it needs to allow users to easily start WP projects, so I think both type of routers need to exist.

Maybe a possibility is to use one router as a base for the other one to avoid duplicating code? Not sure if it is possible though.

1 Like

I was going to ask the same :grinning_face_with_smiling_eyes:

I think having another, even tinier router, is great. But tiny-router itself should work without a source package as well.

Hi @orballo!

You are completely right. The current tiny-router does function without a source but the Link component is dependant on a source package to preload routes and such. So, you are right :smiley: untangling the current tiny-router might not be doable.

Kudos again on this very tiny router :grinning_face_with_smiling_eyes: