Running Script before build

Hi,
I have a Nextjs website with a search feature that requires running a script before build, it’s pretty simple:
node scripts/cache.js

That updates a file located in the cache folder like this:

export const posts = [{"id":"test-1","title":"Test 1","tags":["lifestyle","test","perrisimo"]},{"id":"test-2","title":"Test 2","tags":["test","landscape","lifestyle"]}]

it’s pretty simple, but right now if I want it to be populated, I need to do run the script manually and then commit.

Is there a simple way to do this?

Here’s the link to the website: website

Thanks

@alfredo.gtz92 It’s hard to make suggestions when I’m not sure how you’re starting your build currently or what you’re running.

However if you’re using npm run build to start your build, then you could edit the build script within the package.json so that it executes node scripts/cache.js && {whatever-you-were-running}.

Alternatively you can change your Build command in Netlify to, node scripts/cache.js && {whatever-you-were-running}.

1 Like

Thank you very much. Yes, I’m using npm run build. This is just what I was looking for