All Projects → fridays → Next Routes

fridays / Next Routes

Licence: mit
Universal dynamic routes for Next.js

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Next Routes

routex.js
🔼 Alternative library to manage dynamic routes in Next.js
Stars: ✭ 38 (-98.39%)
Mutual labels:  router, universal, routes, routing
Universal Router
A simple middleware-style router for isomorphic JavaScript web apps
Stars: ✭ 1,598 (-32.12%)
Mutual labels:  router, routing, routes, ssr
Firebase Functions Next Example
Host a Next.js SSR React app on Cloud Functions for Firebase with Firebase Hosting
Stars: ✭ 215 (-90.87%)
Mutual labels:  nextjs, universal, server-rendering, ssr
Router5
Flexible and powerful universal routing solution
Stars: ✭ 1,704 (-27.61%)
Mutual labels:  universal, router, routing
Nextjs Dynamic Routes
[Deprecated] Super simple way to create dynamic routes with Next.js
Stars: ✭ 145 (-93.84%)
Mutual labels:  nextjs, router, routing
Next.js
The React Framework
Stars: ✭ 78,384 (+3229.82%)
Mutual labels:  nextjs, universal, server-rendering
STCRouter
基于标准URL的iOS路由系统,可实现业务模块组件化,控制器之间零耦合,可实现黑白名单控制,可进行native降级到hybrid。
Stars: ✭ 19 (-99.19%)
Mutual labels:  router, routes, routing
Nuxt.js
The Intuitive Vue(2) Framework
Stars: ✭ 38,986 (+1556.16%)
Mutual labels:  universal, server-rendering, ssr
Awesome Nextjs
📔 📚 A curated list of awesome resources : books, videos, articles about using Next.js (A minimalistic framework for universal server-rendered React applications)
Stars: ✭ 6,812 (+189.38%)
Mutual labels:  nextjs, universal, routing
gatsby-plugin-dynamic-routes
Creating dynamic routes based on your environment and/or renaming existing routes
Stars: ✭ 14 (-99.41%)
Mutual labels:  router, routes, routing
router
Bidirectional Ring router. REST oriented. Rails inspired.
Stars: ✭ 78 (-96.69%)
Mutual labels:  router, routes, routing
Laravel Nuxt
A Laravel-Nuxt starter kit.
Stars: ✭ 943 (-59.94%)
Mutual labels:  universal, server-rendering, ssr
Startup Landing
Collection of free top of the line startup landing templates built using react/nextjs/gatsby. Free to download, simply edit and deploy! Updated weekly!
Stars: ✭ 176 (-92.52%)
Mutual labels:  nextjs, universal, server-rendering
Grip
The microframework for writing powerful web applications.
Stars: ✭ 137 (-94.18%)
Mutual labels:  router, routing
Angular9 Example App
Angular 13 Example App + Angular CLI + i18n + GraphQL
Stars: ✭ 1,769 (-24.85%)
Mutual labels:  universal, ssr
Surge Rules
🦄 🎃 👻 Surge 规则集(DOMAIN-SET 和 RULE-SET),兼容 Surge for iOS 和 Surge for Mac 客户端。
Stars: ✭ 151 (-93.59%)
Mutual labels:  routing, ssr
Reactql
Universal React+GraphQL starter kit: React 16, Apollo 2, MobX, Emotion, Webpack 4, GraphQL Code Generator, React Router 4, PostCSS, SSR
Stars: ✭ 1,833 (-22.13%)
Mutual labels:  universal, ssr
Redux Tower
Saga powered routing engine for Redux app.
Stars: ✭ 155 (-93.42%)
Mutual labels:  router, routing
Minrouter
a micro middleware router for isomorphic javaScript web apps
Stars: ✭ 159 (-93.25%)
Mutual labels:  universal, router
Next Firebase Ssr
An Next.js example repo for building authenticated pages with Firebase Authentication, cookies, and getServerSideProps
Stars: ✭ 192 (-91.84%)
Mutual labels:  nextjs, ssr

Dynamic Routes for Next.js

npm version Coverage Status Build Status

Easy to use universal dynamic routes for Next.js

  • Express-style route and parameters matching
  • Request handler middleware for express & co
  • Link and Router that generate URLs by route definition

How to use

Install:

npm install next-routes --save

Create routes.js inside your project:

const routes = require('next-routes')

                                                    // Name   Page      Pattern
module.exports = routes()                           // ----   ----      -----
.add('about')                                       // about  about     /about
.add('blog', '/blog/:slug')                         // blog   blog      /blog/:slug
.add('user', '/user/:id', 'profile')                // user   profile   /user/:id
.add('/:noname/:lang(en|es)/:wow+', 'complex')      // (none) complex   /:noname/:lang(en|es)/:wow+
.add({name: 'beta', pattern: '/v3', page: 'v3'})    // beta   v3        /v3

This file is used both on the server and the client.

API:

  • routes.add([name], pattern = /name, page = name)
  • routes.add(object)

Arguments:

  • name - Route name
  • pattern - Route pattern (like express, see path-to-regexp)
  • page - Page inside ./pages to be rendered

The page component receives the matched URL parameters merged into query

export default class Blog extends React.Component {
  static async getInitialProps ({query}) {
    // query.slug
  }
  render () {
    // this.props.url.query.slug
  }
}

On the server

// server.js
const next = require('next')
const routes = require('./routes')
const app = next({dev: process.env.NODE_ENV !== 'production'})
const handler = routes.getRequestHandler(app)

// With express
const express = require('express')
app.prepare().then(() => {
  express().use(handler).listen(3000)
})

// Without express
const {createServer} = require('http')
app.prepare().then(() => {
  createServer(handler).listen(3000)
})

Optionally you can pass a custom handler, for example:

const handler = routes.getRequestHandler(app, ({req, res, route, query}) => {
  app.render(req, res, route.page, query)
})

Make sure to use server.js in your package.json scripts:

"scripts": {
  "dev": "node server.js",
  "build": "next build",
  "start": "NODE_ENV=production node server.js"
}

On the client

Import Link and Router from your routes.js file to generate URLs based on route definition:

Link example

// pages/index.js
import {Link} from '../routes'

export default () => (
  <div>
    <div>Welcome to Next.js!</div>
    <Link route='blog' params={{slug: 'hello-world'}}>
      <a>Hello world</a>
    </Link>
    or
    <Link route='/blog/hello-world'>
      <a>Hello world</a>
    </Link>
  </div>
)

API:

  • <Link route='name'>...</Link>
  • <Link route='name' params={params}> ... </Link>
  • <Link route='/path/to/match'> ... </Link>

Props:

  • route - Route name or URL to match (alias: to)
  • params - Optional parameters for named routes

It generates the URLs for href and as and renders next/link. Other props like prefetch will work as well.

Router example

// pages/blog.js
import React from 'react'
import {Router} from '../routes'

export default class Blog extends React.Component {
  handleClick () {
    // With route name and params
    Router.pushRoute('blog', {slug: 'hello-world'})
    // With route URL
    Router.pushRoute('/blog/hello-world')
  }
  render () {
    return (
      <div>
        <div>{this.props.url.query.slug}</div>
        <button onClick={this.handleClick}>Home</button>
      </div>
    )
  }
}

API:

  • Router.pushRoute(route)
  • Router.pushRoute(route, params)
  • Router.pushRoute(route, params, options)

Arguments:

  • route - Route name or URL to match
  • params - Optional parameters for named routes
  • options - Passed to Next.js

The same works with .replaceRoute() and .prefetchRoute()

It generates the URLs and calls next/router


Optionally you can provide custom Link and Router objects, for example:

const routes = module.exports = require('next-routes')({
  Link: require('./my/link')
  Router: require('./my/router')
})

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