- Understand the difference between local and distributed caching
- Learn how to add NxCloud and enable distributed on an existing Nx workspace
-
Earlier in the workshop, we discussed about Nx's local caching capabilities. Let's enable distributed caching.
nx connect-to-nx-cloud
- Inspect the changes added in
nx.json
- especially the access token. We'll explain that more in a bit!
-
Very important: Make sure, at this stage, you commit and push your changes:
# make sure you're on master git checkout master git add . && git commit -m "add nx cloud" git push origin master
-
Run a build:
nx run-many --target=build --all
🕑 Watch the process in the terminal - it might take a few seconds...
-
Try to build all projects again:
nx run-many --target=build --all
⚡ It should finish much quicker this time - because it just pulled from the local cache!
-
Let's try something different now - in a different folder on your machine, let's try and do a fresh of your repository:
# go into a new folder cd .. # clone your repo again git clone [email protected]:<your-username>/<your-repo>.git test-distributed-caching cd test-distributed-caching # install dependencies yarn
-
In your new instance, let's try and build again:
nx run-many --target=build --all
⚡ It should be almost instant...
-
But how? You have no local cache: we just did a fresh pull of the repository.
Check your terminal output - you should see this message:
That means that instead of rebuilding locally again, we just pulled from the distributed cache.
-
Let's try a different command - in the same folder you are in, try to run:
nx run-many --target=lint --all
🕑 It should start the linting work, and take a few seconds...
-
Now let's go back to our main workshop repository and run:
nx run-many --target=lint --all
⚡ It should pull again from the NxCloud cache...This is even works across laptops! CI will use it as well!