Skip to content

Instantly share code, notes, and snippets.

@SimeonGriggs
Created April 25, 2022 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimeonGriggs/5176e49fe5cf20275a14770f13318aaa to your computer and use it in GitHub Desktop.
Save SimeonGriggs/5176e49fe5cf20275a14770f13318aaa to your computer and use it in GitHub Desktop.
A script to be run `prebuild` to dynamically generate a new vercel.json file with redirects written in Sanity
const sanityClient = require('@sanity/client')
const fs = require('fs')
const client = sanityClient({
apiVersion: '2021-04-01',
dataset: `XXX`,
projectId: `XXX`,
useCdn: true,
})
const query = `*[_type == "siteRedirect"][]{from, to}`
client.fetch(query).then((res) => {
const siteRedirects = res.map(({ from, to }) => ({
source: from,
destination: to,
}))
const config = {
trailingSlash: false,
cleanUrls: true,
redirects: siteRedirects,
}
fs.writeFile(`./vercel.json`, JSON.stringify(config), (err) => {
if (err) throw err
console.log(`vercel.json written`)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment