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-automatic-redirects

v1.0.0

Published

Automatically tracks page path changes and creates redirects via Gatsby's createRedirect action from the previous paths to the new paths

Downloads

51

Readme

gatsby-plugin-automatic-redirects

Automatically tracks page path changes during builds and creates redirects via Gatsby's createRedirect action.

This plugin is very useful when managing slugs dynamically in a CMS such as Contentful or when refactoring page paths. Redirects allow keeping page SEO scores and help users to get content from the latest URLs when clicking some old link.

IMPORTANT NOTE

This plugin only calls Gatsby's createRedirect action. It does not implement the redirects as this depends on your hosting solution.

Please also install & configure a Gatsby plugin that implements redirection such as gatsby-plugin-meta-redirect, gatsby-plugin-s3, gatsby-plugin-nginx-redirect, or others. Without such a plugin, the redirects will not work.

Usage

  1. Add the plugin to gatsby-config.js:
    plugins: [
      `gatsby-plugin-automatic-redirects`
    ]
  2. Edit your createPages API call in gatsby-node.js to retrieve page IDs and pass them in the page context, for example:
    exports.createPages = async ({ graphql, actions }) => {
      const result = await graphql(`
        query {
          allContentfulPage {
            edges {
              node {
                contentful_id
                slug
              }
            }
          }
        }
      `)
       
      const { createPage } = actions
       
      result.data.allContentfulPage.edges.forEach(({ node }) => {
        const { slug, contentful_id } = node
       
        createPage({
          path: `/${slug}`,
          component: path.resolve("./src/templates/page.js"),
          context: {
            id: contentful_id,
          },
        })
      })
    }
  3. Unless overriding the getData and saveData options, make the files defined in pagesJsonFile and redirectsJsonFile (see below) persist between builds, or the plugin will not work properly. This can be done in one of two ways:
    1. Recommended: commit + push the files to your repository after building the website, possibly as part of a CI/CD workflow
    2. Define the files as CI/CD workflow artifacts, downloading them before the build and uploading them after build

Configuration

  • pageIdPropertyName - page ID property name that is passed in the page context - see example above. Default: id
  • pagesJsonFile - full file path where to store page data. Default: pages.json in project root
  • redirectsJsonFile - full file path where to store redirection data. Default: redirects.json in project root
  • createRedirectData - function that receives pageId, fromPath, and toPath and returns
    redirect data. Use this to customize redirect options or implement logic for specific paths and pages. Default:
    (pageId, fromPath, toPath) => ({
      fromPath,
      toPath,
      isPermanent: true,
    })
  • getData - async function that receives pagesJsonFile and redirectsJsonFile as params and returns an object with pages and redirects properties. Use this option only if you would like to retrieve the plugin data from a custom source, such as an API endpoint or a database. Default: retrieve the data from the files defined in the pagesJsonFile and redirectsJsonFile options.
  • saveData - async function that receives pages, redirects, pagesJsonFile, and redirectsJsonFile as params and saves pages and redirects somewhere. Use this option only if you would like to save plugin data in a custom source such as API endpoint or a database. Default: save the data in the files defined in the pagesJsonFile and redirectsJsonFile options.

How does it work

The plugin calls await getData(pagesJsonFile, redirectsJsonFile), by default reads the files as specified by pagesJsonFile and redirectsJsonFile, and returns an object with two properties:

  1. pages - object with page IDs as keys and paths as values
  2. redirects - array with redirect data objects

For example:

{
  "pages": {
    "sjdf823fosd": "/",
    "j3n7832ksdf": "/about",
    "mujvds73nfd": "/contact"
  },
  "redirects": [
    {
      "fromPath": "/about-us",
      "toPath": "/about",
      "isPermanent": true
    }
  ]
}

When a page is created with Gatsby's createPage action, the plugin uses the page ID to compare the page's previous path in pages with the current path. If the path has changed, the plugin creates new redirect data by calling createRedirectData and adds the resulting object to redirects.

The redirects are parsed to remove circular redirections and transitions:

  1. If redirect /a -> /b exists, and redirect /b -> /a is added, then remove redirect /a -> /b
  2. If redirect /a -> /b exists, and redirect /b -> /c is added, then update the former redirect to /a -> /c

Once the build process is finished, redirects to non-existent paths are removed, and the createRedirect action is called for each object in redirects

Finally, the plugin calls await saveData(pages, redirects, pagesJsonFile, redirectsJsonFile) to save the latest pages and redirects, by default in the files as specified by pagesJsonFile and redirectsJsonFile.