Skip to content

Instantly share code, notes, and snippets.

@coreyward
Created January 29, 2021 15:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coreyward/a79919c2bdf4b3f86b50b6777db8a466 to your computer and use it in GitHub Desktop.
Save coreyward/a79919c2bdf4b3f86b50b6777db8a466 to your computer and use it in GitHub Desktop.
Generating a Netlify Redirects file from redirect-directives created in Sanity.io
exports.onPostBuild = ({ graphql }) =>
graphql(`
{
redirects: allSanityRedirect {
nodes {
matchPath
target
statusCode
}
}
}
`).then(({ data }) => {
const redirectData = data.redirects.nodes
.map(
({ matchPath, target, statusCode }) =>
`${matchPath} ${target} ${statusCode}`
)
.join("\n")
return fs.appendFile(
path.resolve(__dirname, "public/_redirects"),
redirectData
)
})
export default {
title: "Redirect",
name: "redirect",
type: "document",
fields: [
{
title: "Internal Name",
name: "internalName",
type: "string",
validation: Rule => Rule.required(),
},
{
title: "Match Rule",
description:
"The URL path to match. This is the “from” portion of the redirect. A “splat” (*) can be used to match any additional characters at the end of the line (e.g. `/blog/*`).",
name: "rule",
type: "string",
validation: Rule =>
Rule.required().regex(/^\//, { name: "Must start with a `/`" }),
},
{
title: "Redirect Target",
description:
"The destination for the redirect. If a “splat” (*) was used in the match rule, it can be expanded in the target using `:splat` (e.g. `/blog/:splat`).",
name: "target",
type: "string",
validation: Rule => Rule.required().regex(/^(\/|https?:\/\/)/),
},
{
title: "Redirect Type",
description:
"Both temporary and permanent redirects can be internal or external links. Rewrites can only be internal (i.e. to another page that is built in Sanity).",
name: "statusCode",
type: "string",
options: {
list: [
{ title: "Temporary (302)", value: "302" },
{ title: "Permanent (301)", value: "301" },
{ title: "Rewrite (200)", value: "200" },
],
},
validation: Rule => Rule.required(),
},
],
preview: {
select: {
title: "internalName",
rule: "rule",
target: "target",
statusCode: "statusCode",
},
prepare: ({ title, rule, target, statusCode }) => ({
title,
subtitle: `${rule} ${target} ${statusCode}`,
}),
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment