Function to fetch from API and write JSON on build

Hi, I have this function that is built like node-fetch, except it also writes the output to a json file. These are the function and file paths:

root/.functions/node-fetch/node-fetch.js
root/data.json

What I want is the function to run only when I choose to deploy the website (or autobuild and deploy every X hours), so the file is updated regularly and I don’t have any client API calls.

How can I do this? Changing the name of the function to deploy-building, though it seems to run it when I hit netlify deploy -p, doesn’t seem to create the file (which is triggered locally during development if I open the function path).

Seems like you cannot do it with functions, since Netlify doesn’t accept fs in them. However, it is possible during preBuild, using a plugin (netlify-plugin-fetch-feeds). It’s not very well documented, and it immediately threw an error. In my case, an xml2json module was not necessary, so I removed it, and it worked. There’s that odd feeling that the plugin is built for 10-item rss feeds, but if it works with 1000-record API outputs…

1 Like