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

gatsby-plugin-dynamic-routes

v1.0.2

Published

Creating dynamic routes based on your environment and/or renaming existing routes

Downloads

2,383

Readme

Version Downloads Total

gatsby-plugin-dynamic-routes

Use one file to declare your routes, provides to chose dynamic route paths based on your BUILD_ENV or ROUTE_ENV to your custom routing env. Also it's possible to only renaming routes on pages/, or use everything together.

Install

$ npm i gatsby-plugin-dynamic-routes

or

$ yarn add gatsby-plugin-dynamic-routes

How to use

Add the plugin to your gatsby-config.js.

module.exports = {
  plugins: [
    `gatsby-plugin-dynamic-routes`
  ]
}

Create your's Routes.js inside your src/ folder

project/
├── src/
  └── Routes.js

Routes.js

module.exports = {
  home: {
    path: `/casa`,
    component: `src/pages/Home.js`
  }
}

Use in client-side, include globals comment

component/Example.js

import { Link } from "gatsby"

function Example() {
  return <Link href="/casa" />
}

Dynamic routes

Routes.js

module.exports = {
  development: {
    two: {
      path: `/2`,
      component: `src/pages/Two.js`
    }
  },
  staging: {
    two: {
      path: `/dois`,
      component: `src/pages/Two.js`
    }
  },
  home: {
    path: `/casa`,
    component: `src/pages/Home.js`
  }
}

Run with your BUILD_ENV environment

BUILD_ENV=staging yarn start

If you are using the plugin Dynamic Environment Variables, what will happen is that your environments inside your .env and .env.staging will be loaded, and your routes inside staging key will go to root of the object that is exported of Routes and will be available in yours global environment variables.

component/Example.js

import { Link } from "gatsby"
/* globals ROUTES */

function Example() {
  return <Link href={ROUTES.two.path} />
}

Note that you need to put eslint globals comment in each file that will use ROUTES global variable.

You need to create an empty .eslintrc in root of your folder to remove this comments.

If you are using eslint in your project, just update this key to your config:

{
  "rules": {
    "no-undef": "off"
  }
}

You can pass more than path or component keys, these keys will be available in your component later

Variations

If you want to made variations of the same environment, but whit different routes, you can use ROUTE_ENV variable to chose your dynamic routes

example

ROUTE_ENV=organic BUILD_ENV=production yarn build

Options

routeFilePath

This options allows you to specify what's the path to your file with your Routes object

Example:

module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-dynamic-routes`,
      options: {
        routeFilePath: `config/Routes.js`
      }
    }
  ]
}
project/
├── config/
  └── Routes.js

If you want to put in root of your project, simply put the name of your file

Ignoring gatsby default page creator

By default, gatsby generates one route to each file inside pages/ folder, to disable this feature, put in you gatsby-config.js:

{
  resolve: `gatsby-plugin-page-creator`,
  options: {
    path: `${__dirname}/src/pages`,
    ignore: {
      patterns: [`**/*`],
    },
  },
},

Recommended plugins

Check out the Dynamic Environment Variables plugin that provides you to load different files based on your env variables