Skip to content

Instantly share code, notes, and snippets.

@runeb
Created April 9, 2021 19:27
Show Gist options
  • Save runeb/2b7d465a0922e6f7fe9e8c4339b1c3a2 to your computer and use it in GitHub Desktop.
Save runeb/2b7d465a0922e6f7fe9e8c4339b1c3a2 to your computer and use it in GitHub Desktop.
Listening for Sanity dataset changes and causing Eleventy rebuild
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