All Projects → alexhoma → routex.js

alexhoma / routex.js

Licence: GPL-3.0 license
🔼 Alternative library to manage dynamic routes in Next.js

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to routex.js

Next Routes
Universal dynamic routes for Next.js
Stars: ✭ 2,354 (+6094.74%)
Mutual labels:  router, universal, routes, routing
STCRouter
基于标准URL的iOS路由系统,可实现业务模块组件化,控制器之间零耦合,可实现黑白名单控制,可进行native降级到hybrid。
Stars: ✭ 19 (-50%)
Mutual labels:  router, routes, routing
gatsby-plugin-dynamic-routes
Creating dynamic routes based on your environment and/or renaming existing routes
Stars: ✭ 14 (-63.16%)
Mutual labels:  router, routes, routing
Universal Router
A simple middleware-style router for isomorphic JavaScript web apps
Stars: ✭ 1,598 (+4105.26%)
Mutual labels:  router, routes, routing
router
Bidirectional Ring router. REST oriented. Rails inspired.
Stars: ✭ 78 (+105.26%)
Mutual labels:  router, routes, routing
Router5
Flexible and powerful universal routing solution
Stars: ✭ 1,704 (+4384.21%)
Mutual labels:  router, universal, routing
Routerify
A lightweight, idiomatic, composable and modular router implementation with middleware support for the Rust HTTP library hyper.rs
Stars: ✭ 173 (+355.26%)
Mutual labels:  router, routing
Localize Router
An implementation of routes localisation for Angular
Stars: ✭ 177 (+365.79%)
Mutual labels:  router, universal
Ui Router
The de-facto solution to flexible routing with nested views in AngularJS
Stars: ✭ 13,738 (+36052.63%)
Mutual labels:  router, routing
Router
Router implementation for fasthttp
Stars: ✭ 234 (+515.79%)
Mutual labels:  router, routing
Router
⚡️ A lightning fast HTTP router
Stars: ✭ 158 (+315.79%)
Mutual labels:  router, routing
Falco
A functional-first toolkit for building brilliant ASP.NET Core applications using F#.
Stars: ✭ 214 (+463.16%)
Mutual labels:  router, routing
Swiftuirouter
Routing in SwiftUI
Stars: ✭ 242 (+536.84%)
Mutual labels:  router, routing
Flow builder
Flutter Flows made easy! A Flutter package which simplifies flows with a flexible, declarative API.
Stars: ✭ 169 (+344.74%)
Mutual labels:  router, routing
Rayo.js
Micro framework for Node.js
Stars: ✭ 170 (+347.37%)
Mutual labels:  router, routing
Minrouter
a micro middleware router for isomorphic javaScript web apps
Stars: ✭ 159 (+318.42%)
Mutual labels:  router, universal
Klein.php
A fast & flexible router
Stars: ✭ 2,622 (+6800%)
Mutual labels:  router, routing
React.ai
It recognize your speech and trained AI Bot will respond(i.e Customer Service, Personal Assistant) using Machine Learning API (DialogFlow, apiai), Speech Recognition, GraphQL, Next.js, React, redux
Stars: ✭ 38 (+0%)
Mutual labels:  universal, next
vue-error-page
[NO LONGER MAINTAINED] Provides a wrapper for router-view that allows you to show error pages without changing the URL.
Stars: ✭ 52 (+36.84%)
Mutual labels:  routes, routing
es6-router
🌐 Simple client side router built in ES6
Stars: ✭ 16 (-57.89%)
Mutual labels:  router, routing

Routex.js

Yes, another library to handle dynamic routes in Next.js


Features

  • 🌌 Universal
  • 🍃 Tree shakeable
  • 🐜 Not tiny, but pretty small
  • 🔗 Build your custom <Link /> on top of it
  • 🎉 Same routes contract as next-routes name, pattern, page
  • 🚀 Up to date path-to-regexp dependency
  • 🌍 Compatible with multi-domain routing (a.k.a routes localization)
  • 😎 Cool name!

Inspired by next-routes and next-minimal-routes.

Install

Install routex in your Next.js project:

npm i routex.js

Using yarn:

yarn add routex.js

Setup

Route definitions

Okay, so now we have installed routex. First of all we'll need to declare our application routes. So let's create a routes.js file:

module.exports = [
  {
    name: 'index',
    pattern: '/',
  },
  {
    name: 'post',
    pattern: '/post/:slug',
    page: 'post',
  },
  {
    name: 'tags',
    pattern: '/tags{-:id}?', // optional id param
    page: 'tags',
  },
];

If you need more info on how to create the route patterns check the path-to-regexp documentation: pillarjs/path-to-regexp.

Server getRequestHandler()

Once routes are declared, we want to handle it whenever a user loads any existing url in our application. So here we need to create our routex requestHandlerMiddleware in our server.js file, passing the next.js instance (nextApp) and our route definitions (routes) like this:

const express = require('express');
const next = require('next');
const nextApp = next({ dev: process.env.NODE_ENV !== 'production' });
const routes = require('./routes');
const { getRequestHandler } = require('routex.js');

const routexHandlerMiddleware = getRequestHandler(nextApp, routes);

nextApp.prepare().then(() => {
  express()
    .use(routexHandlerMiddleware);
    .listen(3000);
});

Client link()

Hooray! our server now handles dynamic routes. But now we need a way to create link components to point to that dynamic routes. So let's create a file CustomLink.js to use in our components.

import NextLink from 'next/link';
import { createRouteLinks } from 'routex.js';
import routes from './routes';

const { link } = createRouteLinks(routes);

export default function CustomLink({ children, route, params }) {
  return (
    <NextLink {...link({ route, params: { ...params } })}>
      <a>{children}</a>
    </NextLink>
  );
}

The createRouteLinks function transforms and closures all your routes and returns a new link function. This link is the one that will provide to the <NextLink /> component the as and href props. And it needs this two parameters:

  • route: a route name, the one that you have in the route definiton.
  • params: all dynamic params

And this is how you'll use your <CustomLink /> component:

import CustomLink from './CustomLink';

export default () => (
  <>
    This is an example page component:
    <CustomLink
      route="post"
      params={{
        slug: 'next-js-post',
      }}
    >
      Next.js post link
    </Link>
  </>
);

The output that will return your <CustomLink /> will be exactly the same that if you create a link using the current Next.js Link, like I'll show you in this example:

import NextLink from 'next/link';

export default () => (
  <NextLink as="/post/next-js-post" href="/post?slug=next-js-post">
    <a>Next.js post link</a>
  </NextLink>
);

Currently, there is no imperative way to change your app route using routex.js, like the next-routes' Router.pushRoute(route, params, options), because I didn't need it at all in my current applications. But I'm open to add it if someone finds it interesting. Since then, I'll try to keep this library as simple as possible.

For more information have a look into the example app directory.

Demos

Basic dynamic routing

Multi-domain routing (a.k.a localized routing)

Check this code example here: examples/with-route-localization

Motivation

Check out this blog post to know some of the reasons why I've decided to create another routing library: alexhoma.com/projects/routexjs-yet-another-router-for-nextjs

Things to do

  • Add an example with multi-domain application
  • Since routex.js doesn't need React at all, add an example with other Next.js integrations, like Preact, inferno, etc.
  • Avoid loading all route definitions in client side, only the ones we use per page

Contributions

If you want to suggest a change, feature or any question, feel free to open an issue or a pull request. But check the contributing file before you go.

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].