Security News
How Threat Actors are Abusing GitHub’s File Upload Feature to Host Malware
GitHub is susceptible to a CDN flaw that allows attackers to host malware on any public repository.
gatsby-plugin-automatic-redirects
Advanced tools
Automatically tracks page path changes and creates redirects via Gatsby's createRedirect action from the previous paths to the new paths
Readme
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.
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.
gatsby-config.js
:
plugins: [
`gatsby-plugin-automatic-redirects`
]
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,
},
})
})
}
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:
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 rootredirectsJsonFile
- full file path where to store redirection data. Default: redirects.json
in project rootcreateRedirectData
- function that receives pageId
, fromPath
, and toPath
and returns(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.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:
pages
- object with page IDs as keys and paths as valuesredirects
- array with redirect data objectsFor 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:
/a -> /b
exists, and redirect /b -> /a
is added, then remove redirect /a -> /b
/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
.
FAQs
Automatically tracks page path changes and creates redirects via Gatsby's createRedirect action from the previous paths to the new paths
The npm package gatsby-plugin-automatic-redirects receives a total of 23 weekly downloads. As such, gatsby-plugin-automatic-redirects popularity was classified as not popular.
We found that gatsby-plugin-automatic-redirects demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub is susceptible to a CDN flaw that allows attackers to host malware on any public repository.
Security News
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
Research
Security News
The Socket Research team found this npm package includes code for collecting sensitive developer information, including your operating system username, Git username, and Git email.