Skip to content

Caching

Whenever executing a build via the npx projen build command (or any nx command), NX (the underlying build system) will cache your outputs so that subsequent builds which do not have any changes can be skipped and use cached results. It does this by generating a hash, based on an input set which typically comprises of:

  • All the source files of your package and its dependencies
  • Relevant global config
  • Versions of external dependencies
  • Runtime values passed in by the user
  • CLI flags

After NX computes the hash for a task, it then checks if it ran this exact computation before. First, it checks locally, and then if it is missing, and if a remote cache is configured, it checks remotely.

If Nx finds the computation, it retrieves it and replays it. Nx places the right files in the right folders and prints the terminal output. From the user’s point of view, the command runs the same, just a lot faster.

By default, all cached results are stored within either .nx/cache or node_modules/.cache/nx. The PDK uses sane defaults for which files to cache i.e: build directory, cdk.out, etc. There may be situations where certain input files or output files need to be adjusted. To do this, add the following to your projenrc file:

NxProject.ensure(<construct>).setTarget(
      "build", // target to configure cache for
      {
        inputs: [
          { "fileset": "{workspaceRoot}/<some-file>" }, // generate a new hash if <some-file> changes
          { "runtime": "node -v" }, // generate a new hash if the node runtime changes
        ],
        outputs: [
          "{projectRoot}/<some-other-file>", // store <some-other-file> in cache results
        ],
      },
      true
    );

Last update: 2024-12-09