Gatsby Meta Refresh Redirect Plugin
gatsby-plugin-meta-redirect is a Gatsby plugin designed to generate static HTML files containing a `<meta http-equiv="refresh">` tag. This enables client-side redirects on any static file host, such as S3 or Netlify, without requiring server-side redirect rules or `.htaccess` configurations. The plugin processes `createRedirect` actions called within Gatsby's Node API (e.g., in `gatsby-node.js`) and outputs an `index.html` file at the `fromPath` containing the meta refresh tag pointing to the `toPath`. Currently at version 1.1.1, the package has not seen updates in over seven years, indicating an abandoned maintenance schedule. Its primary differentiator is the universal compatibility across static hosts, but this comes with SEO and user experience trade-offs compared to server-side 301 redirects.
Common errors
-
Redirect not working or generating correctly.
cause The `gatsby-plugin-meta-redirect` plugin is not placed last in the `plugins` array in `gatsby-config.js`, allowing other plugins to override or interfere with its output.fixMove `gatsby-plugin-meta-redirect` to the very end of the `plugins` array in your `gatsby-config.js` file. -
Low SEO ranking for pages with redirects, or 'soft 404' warnings in search console.
cause The plugin uses `<meta http-equiv="refresh">` which is interpreted by search engines as a soft redirect (similar to a 302 'Found' status), not a permanent 301 'Moved Permanently'. This can impact page authority transfer and SEO.fixFor critical redirects, implement true 301 server-side redirects via your hosting provider's configuration (e.g., `_redirects` file for Netlify/Vercel, NGINX rules for custom servers) instead of relying solely on meta refresh. Update internal links and sitemaps to point to the final URLs.
Warnings
- gotcha The `gatsby-plugin-meta-redirect` plugin *must* be the last entry in the `plugins` array within your `gatsby-config.js` file. Failure to do so can result in redirects not being generated or working incorrectly, as other plugins might interfere with the build output or path generation.
- gotcha This plugin generates redirects using client-side `<meta http-equiv="refresh">` tags, not server-side 301 HTTP redirects. While functional for static hosting, meta refresh redirects are generally less favorable for Search Engine Optimization (SEO) as they are treated as temporary (302-like) redirects by search engines and may not pass full link equity. They can also lead to a poorer user experience due to a slight delay.
- deprecated The `gatsby-plugin-meta-redirect` package has not been updated in over seven years (last published in November 2018) and is considered unmaintained. This raises concerns about compatibility with newer Gatsby versions, Node.js environments, and potential security vulnerabilities that will not be addressed.
Install
-
npm install gatsby-plugin-meta-redirect -
yarn add gatsby-plugin-meta-redirect -
pnpm add gatsby-plugin-meta-redirect
Imports
- gatsby-plugin-meta-redirect
import gatsbyPluginMetaRedirect from 'gatsby-plugin-meta-redirect';
// in gatsby-config.js plugins: [ // other plugins... `gatsby-plugin-meta-redirect` // must be last ];
- createRedirect
import { createRedirect } from 'gatsby-plugin-meta-redirect';// in gatsby-node.js exports.createPages = async ({ actions }) => { const { createRedirect } = actions; createRedirect({ fromPath: '/old-url/', toPath: '/new-url/', isPermanent: true, redirectInBrowser: true }); // ... other createPage calls };
Quickstart
// gatsby-config.js
module.exports = {
plugins: [
// Ensure this plugin is the last one in your array
`gatsby-plugin-meta-redirect`
]
};
// gatsby-node.js
const path = require('path');
exports.createPages = async ({ actions, graphql }) => {
const { createRedirect, createPage } = actions;
// Example 1: Basic permanent redirect
createRedirect({
fromPath: '/legacy-page/',
toPath: '/new-destination/',
isPermanent: true,
redirectInBrowser: true
});
// Example 2: Redirect with a language preference
createRedirect({
fromPath: '/global-content/',
toPath: '/en-US/global-content/',
Language: 'en'
});
// Example 3: Programmatic redirect based on data (e.g., old blog post slugs)
const result = await graphql(`
{
allMarkdownRemark {
edges {
node {
frontmatter {
slug
oldPaths
}
}
}
}
}
`);
if (result.errors) {
throw result.errors;
}
const posts = result.data.allMarkdownRemark.edges;
posts.forEach(({ node }) => {
if (node.frontmatter.oldPaths) {
node.frontmatter.oldPaths.forEach(oldPath => {
createRedirect({
fromPath: oldPath,
toPath: node.frontmatter.slug,
isPermanent: true,
redirectInBrowser: true
});
});
}
createPage({
path: node.frontmatter.slug,
component: path.resolve('./src/templates/blog-post.js'),
context: {
slug: node.frontmatter.slug,
},
});
});
};