Created
April 25, 2022 10:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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