Skip to content

Instantly share code, notes, and snippets.

@romainavalle
Last active June 10, 2022 09:59
Show Gist options
  • Save romainavalle/8dc78fad1e327bcd8fe7fbbff9ea2c56 to your computer and use it in GitHub Desktop.
Save romainavalle/8dc78fad1e327bcd8fe7fbbff9ea2c56 to your computer and use it in GitHub Desktop.
Dato write redirects module (to include in build module)
const { SiteClient } = require('datocms-client')
const client = new SiteClient(process.env.GRAPHQL_TOKEN)
const fs = require('fs')
export default function () {
client.items
.all({ 'filter[type]': 'your_redirect_modelApiKey' }, { allPages: true })
.then((records) => {
const array = records.map((el) => {
return {
old: el.fieldNameOfOldUrl,
new: el.fieldNameOfNewUrl,
}
})
let content = ''
array.forEach((element, i) => {
content += `${element.old} ${element.new}\n`
})
fs.writeFile('./static/_redirects', content, (err) => {
if (err) {
console.error(err)
return
}
})
})
}
@romainavalle
Copy link
Author

need to install "datocms-client" and "fs" in devDependencies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment