Last active
June 10, 2022 09:59
-
-
Save romainavalle/8dc78fad1e327bcd8fe7fbbff9ea2c56 to your computer and use it in GitHub Desktop.
Dato write redirects module (to include in build module)
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 { 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 | |
} | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
need to install "datocms-client" and "fs" in devDependencies