Created
April 9, 2021 19:27
-
-
Save runeb/2b7d465a0922e6f7fe9e8c4339b1c3a2 to your computer and use it in GitHub Desktop.
Listening for Sanity dataset changes and causing Eleventy rebuild
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
require("dotenv").config(); | |
const fs = require("fs"); | |
const { groqStore, groq } = require("@sanity/groq-store"); | |
const store = groqStore({ | |
projectId: process.env.SANITY_PROJECT_ID, | |
dataset: process.env.SANITY_DATASET, | |
listen: true, | |
overlayDrafts: true, | |
token: process.env.SANITY_TOKEN || "", | |
}); | |
const sub = store.subscribe( | |
groq`*[_type == $type][]`, | |
{ type: "post" }, | |
(err, result) => { | |
if (err) { | |
console.error(err); | |
return; | |
} | |
fs.writeFileSync("./views/_data/posts.json", JSON.stringify(result)); | |
} | |
); | |
process.stdin.resume(); | |
process.on("SIGINT", function () { | |
sub.unsubscribe(); | |
store.close(); | |
process.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment