Run script after build and write to disk

Hello,

I’m wondering if the following is possible using Netlify:

Each time a deploy is triggered, Netlify builds my site. My site has a package.json file, and within it, a build task, which builds the contents to the /build directory, which Netlify serves from.

Is it possible during that build task, to run a script (in my codebase) which makes a network request and saves the contents to disk, maybe somewhere in the /build dir? The reason I want to do this is on my site: https://algoapp.xyz, I make a page load HTTP request to a Netlify Function which returns some JSON to my front-end. This data only changes once a day, but I’m hitting my Function every single time someone visits the site. My idea was to trigger a deploy once a day, have a script fetch the needed data, store it in, for example, data.json, and have my front-end just require() it in locally, vs making the HTTP request to my function.

tl;dr - can i run an async build task and have it write some data to disk which could be required()'d from my JS code?

Yes, for sure. As long as everything happens inside the build command execution (and doesn’t take forever), you should be able to pull data from the network and write it to the build folder.

1 Like

I would like to do a similar thing like the original post, and I understand how to save a json file during the build process, BUT, I also want to save a json file during the execution of a function if the cached json file does not exist,
is it possible to create a serverless function that saves a file to disk in the build directory, I realize the file will be gone when a new build is deployed, that is why I will be creating those files during the build again, but I want to be able to cache new information without triggering a new build, based on the logic of the function.

in theory I could use the ‘fs’ node module, but I am not sure there is a restriction on that

Hi @leoschuler,

You could definitely do this. Just like @stefanmaric said, as long as your build script is the one doing it and it doesn’t take forever, it would work.

regarding saving files outside the build scope:
doing some additional reading in the forum, this does not seem to be a valid solution because serverless function architecture was meant to load and unload after use (or after 15 minutes of not being called again ). after the function is unloaded any files saved would be destroyed and not be there when the function/ environment is loaded again.

this post seem to have the solution I am looking for: Functions: node-cron fetch from external api and write results to file - #4 by gregcarv

1 Like