Configure Nx Cache to Ignore Changes to Specified Files

Share this video with your friends

Send Tweet
Published 5 months ago
Updated a month ago

By default, Nx considers all the files in the project as inputs to the cache, so any changes will invalidate it. But, that might not be the behavior we want. Especially if we are just doing something like updating a README.

To selectively ignore certain files, we can specify an inputs field where we are configuring the custom output. There, we can use a glob pattern to define the files you'd like to ignore.

Instructor: [0:00] Now, in this pnpm workspace with Nx, we specified the build target as the casual operation, and we also gave Nx some hints about where the potential output folders are. [0:11] Although this has been just more for the purpose of demoing how you can actually customize output because this folder is already included by default.

[0:19] Now, we can actually continue and fine-tune this even more. Let's assume, for instance, if we have here a README file. Let's run the build for the shared UI package. We run Nx build shared UI. This would build our project.

[0:40] Now, obviously, if you rerun the same command, it would be pulled out of the cache. Assume, we go and change this README in a PR and just add in our exclamation mark or fix typo.

[0:51] If we run the build again, it would not be pulled out of the cache because by default, Nx considers all the source files in here as an input to the cache, and therefore, since readme changed, it invalidates the cache.

[1:07] This might not be what we actually want to have because, for the build command, for instance, we might not want to include the README. What we can do is we can add here another property and give NX some more information.

[1:20] We can specify here some inputs property, which is also an array similar to the outputs. In here, we can actually say that we don't want to have all the Markdown files included in our cache.

[1:33] We can do this by using such a glob pattern. It is a not and from the product route, there's a wild card for all the Markdown files.

[1:43] Now, let's again run the build. This would, obviously, build the first time because we changed Nx.json. If we rerun it, it will be fetched out of the cache.

[1:52] If we now go to our readme and here remove the exclamation mark, we made a change and we rerun it, it would still be pulled out of the cache because we told Nx explicitly to ignore such Markdown files.