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

remix-custom-routes

v1.0.1

Published

Custom routing conventions for Remix

Downloads

1,320

Readme

remix-custom-routes

A package to make custom routes easier in Remix.

Installation

npm install remix-custom-routes

Route Extensions Convention

You can choose the Remix Route Extension convention to get the following behaviour.

  • Instead of a /routes directory, routes can be defined in any directory (within /app but not at the top level).
  • Any file with the extension .route.tsx, .route.ts, .route.jsx, or .route.js will be treated as a route.
  • All other files are ignored, so you can safely put non-route files next to your routes. Components, hooks, images, etc.
  • URLs are determined by the filename, just like Remix's v2 route convention.
  • Folders do not participate in routing, so you can organize your routes however you want. As long as the file has .route in the name, Remix will find it and turn it into a route.
//remix-config.js
const { routeExtensions, flatRoutes } = require("remix-custom-routes")

module.exports = {
  ignoredRouteFiles: ["routes/**.*"], // ignore the default route files
  async routes() {
    const appDirectory = path.join(__dirname, "app")

    return routeExtensions(appDirectory)
    // or flatRoutes(appDirectory)
  },
}

Flat Route Convention

You can choose the Flat Route convention to get the following behaviour, based on Kiliman's remix-flat-routes package.

  • Routes are defined in a /routes directory.
  • URLs are determined by the filename, just like Remix's v2 route convention.
  • A folder ending in + will have its path prefixed to all child routes. This allows you to avoid repeating the same prefix for all routes in a folder.
//remix-config.js
const { flatRoutes } = require("remix-custom-routes")

module.exports = {
  ignoredRouteFiles: ["routes/**.*"], // ignore the default route files
  async routes() {
    const appDirectory = path.join(__dirname, "app")

    return flatRoutes(appDirectory)
  },
}

Custom Routes

This package exposes two functions, getRouteIds and getRouteManifest, that make it easier to create custom routes in Remix.

The flat route convention defined above is identical to the following code.

//remix-config.js
const glob = require("glob")
const {
  getRouteIds,
  getRouteManifest,
  ensureRootRouteExists,
} = require("remix-custom-routes")

module.exports = {
  async routes() {
    const appDirectory = path.join(__dirname, "app")
    ensureRootRouteExists(appDirectory)

    // array of paths to files
    const files = glob.sync("routes/*.{js,jsx,ts,tsx,md,mdx}", {
      cwd: appDirectory,
    })

    // array of tuples [routeId, filePath]
    const routeIds = getRouteIds(files, {
      indexNames: ["index", "route", "_index", "_route"],
    })

    // Remix manifest object
    return getRouteManifest(routeIds)
  },
}

Use any tool you want, such as glob, to get a list of files that should be turned into routes. If you have a custom convention, you are free to manipulate the list of files however you want. before passing it to getRouteIds.

interface GetRouteIdsOptions {
  prefix?: string // removed from the start of the route
  suffix?: string // removed from the end of the route
  indexNames?: string[] // names of files that should be treated as index files
}

function getRouteIds(
  routes: string[],
  options: GetRouteIdsOptions,
): [string, string][]

getRouteIds takes an array of file paths and returns an array of tuples. Each tuple contains the route ID and its corresponding file path.

This is the format that getRouteManifest expects.

interface RouteManifest {
  [key: string]: Route
}

function getRouteManifest(sortedRouteIds: [string, string][]): RouteManifest

getRouteManifest reads the route IDs and links each route to its parent and children while ensuring there are no conflicts. It returns a Remix manifest object that can be passed to Remix's routes() function.