Optimize Builds with Netlify Build Plugins
Intermediatev1.0.0
Extend Netlify builds with plugins — cache optimization, sitemap generation, Lighthouse auditing, and custom build steps using the Netlify Build Plugin API.
Content
Overview
Netlify Build Plugins hook into the build lifecycle to extend functionality. Use community plugins for common tasks (caching, sitemaps, auditing) or build custom plugins for project-specific needs.
Why This Matters
- -Faster builds — cache dependencies and build outputs between deploys
- -Quality gates — run Lighthouse audits on every deployment
- -Automation — generate sitemaps, submit to search engines, notify teams
- -Customization — hook into any build lifecycle event
How It Works
Step 1: Install Community Plugins
Step 2: Build a Custom Plugin
Step 3: Cache Build Outputs
Build Lifecycle Events
| Event | When It Runs | Common Use |
|---|---|---|
| `onPreBuild` | Before build command | Restore caches, setup |
| `onBuild` | During build | Custom build steps |
| `onPostBuild` | After build, before deploy | Save caches, audits |
| `onSuccess` | After successful deploy | Notifications, webhooks |
| `onError` | On build failure | Error reporting |
Best Practices
- -Cache aggressively — node_modules, .cache, generated files
- -Run Lighthouse on key pages with minimum thresholds
- -Fail builds on critical audit failures (accessibility, security)
- -Use custom plugins for project-specific automation
- -Keep plugin count reasonable (each adds build time)
Common Mistakes
- -Not caching between builds (slow builds every time)
- -Installing plugins via npm but not referencing in netlify.toml
- -Setting Lighthouse thresholds too high initially (builds always fail)
- -Not handling plugin errors gracefully (crashes the build)
- -Caching paths that should be regenerated on every build
FAQ
Discussion
Loading comments...