Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 37 ยท Build Settings and Environments

Build Caching, Plugins and Faster Builds

Cut build time on Vercel and Netlify: how dependency and framework caches work, what invalidates them, Netlify build plugins, and how to measure and trim slow builds.

Build minutes are a limited resource and slow builds slow your whole team. Most of the time in a typical build is spent downloading dependencies and rebuilding things that did not change. Both platforms keep a cache between builds to avoid that, and you can help it work.

This lesson explains what is cached, what clears it, how Netlify build plugins hook into the process, and a simple method for finding the slow part.

What gets cached

  • Dependencies. The installed packages (for example node_modules, or the package manager's download cache) are restored so installs take seconds instead of minutes.
  • Framework build caches. Tools such as Next.js (.next/cache), Astro, Gatsby and Nx can reuse work from the previous build. Both platforms know the standard cache folders of popular frameworks and keep them for you.
  • Your own folders (Netlify). Netlify lets plugins and scripts save and restore additional folders between builds.

The cache is per project and generally keyed on things like the lockfile and the branch, so a changed lockfile triggers a fresh install. If something looks stale, clear it: on Vercel, redeploy with the option to skip the build cache; on Netlify, use Clear cache and deploy site from the Deploys menu.

Node.js Cache key model (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Habits that keep the cache effective

  1. Commit the lockfile. Without it the key changes unpredictably and installs are never reusable.
  2. Avoid changing the lockfile unless you change dependencies. A tool that rewrites it on every build defeats the cache.
  3. Do not write build output into the source tree in ways that confuse caching (for example, generating files that the framework then watches).
  4. Use incremental builds where your framework supports them, and keep the framework cache folder configured as documented.
  5. Pin Node. A new Node version is a new cache key and a possible native-module rebuild.

Netlify build plugins

Netlify has an extension system called Build Plugins. A plugin hooks into build events such as onPreBuild, onBuild, onPostBuild and onSuccess. You add plugins from the plugin directory in the dashboard or by listing them in netlify.toml:

TOML
[[plugins]]
  package = "@netlify/plugin-lighthouse"

[[plugins]]
  package = "netlify-plugin-cache"
  [plugins.inputs]
    paths = [".cache", "tmp-images"]

Package names and options change, so confirm them in the plugin's own README. A local plugin can also live in your repository, with a manifest.yml and an index.js that exports functions named after the events:

JavaScript
// plugins/log-size/index.js (read-only example)
export const onPostBuild = async ({ utils }) => {
  const files = await utils.run.command("du -sh dist");
  console.log("Build output size recorded");
};

Vercel has no direct equivalent of build plugins; instead you use scripts in package.json, framework configuration and Integrations from its marketplace. If you need custom logic, put it in your build command or a postbuild npm script.

A custom hook sequence

Node.js Build lifecycle with hooks (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Find the slow part

Optimize by measuring. In the build log, note timestamps for install, build and upload. Typical culprits:

  • Slow install: large dependency trees. Remove unused packages; prefer lighter alternatives.
  • Slow build: heavy image processing, type-checking and linting inside the build, or building thousands of pages. Move linting and tests to CI checks on pull requests, and use incremental or on-demand rendering for large sites.
  • Slow upload: many tiny files or huge assets. Compress and optimize images.
Node.js Where did the time go? (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Skip work that does not change the output. In a monorepo, use ignore commands; for content sites, avoid rebuilding everything for a one-page edit by using on-demand revalidation.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What usually invalidates the dependency cache?

    The cache key depends on dependency inputs such as the lockfile and the runtime version.
  2. 2. How do you force a build without the cache on Netlify?

    That option discards the stored cache and runs a fresh build.
  3. 3. Which Netlify plugin event runs after the build command completes?

    onPostBuild runs after your build command and before the deploy is finalized.
  4. 4. Which is a good way to reduce build time for a large site?

    Moving checks to CI and building incrementally keeps deploys fast.

Summary

  • Dependencies and framework build folders are cached between builds, keyed on inputs such as the lockfile and Node version.
  • Commit the lockfile, pin Node and avoid unnecessary lockfile churn to keep cache hits.
  • Netlify Build Plugins hook into events like onPreBuild and onPostBuild; Vercel relies on scripts, config and integrations.
  • Measure phases in the log before optimizing, and move checks to CI.
  • Clear the cache when something stale or inconsistent appears.

Next: Serverless Functions: The Basics