Netlify Deploy Previews Not Working? An Expert Troubleshooting Guide
Netlify Deploy Previews are an indispensable tool in modern web development workflows, offering a unique, shareable URL for every pull request (PR) or commit. They allow teams to review changes in a live environment before merging to production, catching bugs, design inconsistencies, and content errors early. However, when these crucial previews fail to generate or display correctly, it can halt development, frustrate teams, and delay deployments. This article serves as an expert, in-depth guide to diagnosing and resolving issues when your Netlify Deploy Previews aren't working as expected, ensuring your development pipeline remains smooth and efficient.
Understanding the common pitfalls and systematic troubleshooting steps is key to quickly identifying the root cause. From build command misconfigurations to environment variable discrepancies, we'll cover the most frequent culprits and provide actionable solutions.
Understanding How Netlify Deploy Previews Work (and Where They Can Break)
At its core, a Netlify Deploy Preview is triggered by a new commit to a branch or a new pull request in your connected Git repository (GitHub, GitLab, Bitbucket). Netlify then fetches your code, installs dependencies, runs your specified build command, and deploys the resulting static assets to a unique URL. This process mirrors a production build but targets a temporary, isolated environment.
Points of failure can occur at any stage:
- Git Integration: If Netlify isn't correctly connected or doesn't detect the PR/commit.
- Build Process: If the build command fails, dependencies are missing, or there are syntax errors in your code.
- Deployment: If the specified publish directory is incorrect or Netlify can't serve the assets.
- Runtime: If the deployed preview relies on external services or environment variables that aren't configured correctly in the preview environment.
Step-by-Step Troubleshooting Guide for Netlify Deploy Previews
1. Check Netlify Build Logs for Errors
This is your first and most critical step. Netlify's build logs provide a detailed narrative of the entire deployment process. They will pinpoint exactly where the build failed or if it completed successfully but still resulted in a non-functional preview.
- Navigate to Your Site: In the Netlify UI, select the site experiencing issues.
- Go to Deploys: Click on the "Deploys" tab.
- Find the Preview Deploy: Look for the deploy associated with your pull request or branch. It will often have "Deploy Preview" or the branch name next to it. Failed deploys are usually marked in red.
- Inspect the Logs: Click on the failed deploy. The logs will expand, showing the build steps. Look for error messages (often highlighted in red or preceded by
error:orfailed:).
Common Log Insights:
Command failed with exit code 1: Indicates a general build failure. Scroll up to find the specific command that failed (e.g.,npm run build, a custom script).Module not found: Missing dependency.- Syntax errors: Often points to issues in your JavaScript, TypeScript, or CSS files.
"Page not found": The build completed, but Netlify couldn't find anindex.htmlin the specified publish directory.
2. Verify Netlify Configuration (`netlify.toml` and UI Settings)
Your Netlify configuration dictates how your site is built and deployed. Discrepancies here are a frequent cause of preview failures.
netlify.tomlFile: This file, located at the root of your repository, specifies crucial build settings.build.command: Ensure this command correctly builds your project (e.g.,npm run build,gatsby build,hugo).build.publish: This must point to the directory where your build output resides (e.g.,dist,public,build). If this is incorrect, Netlify won't find your site.build.functions: If you're using Netlify Functions, verify this path is correct.base: If your project is in a subdirectory of your repository, ensure thebasedirectory is set correctly.
- Netlify UI Build Settings:
- Go to Site Settings > Build & deploy > Continuous Deployment.
- Check Build settings: Verify the "Build command" and "Publish directory" match your project's requirements and your
netlify.toml. UI settings overridenetlify.tomlif both are present. - Environment Variables: Ensure all necessary environment variables (API keys, CMS tokens, etc.) are set here, especially for the "Deploy Previews" context. Variables set locally might not be available on Netlify unless explicitly added.
3. Inspect Your Git Repository & Pull Request Status
Sometimes, the issue isn't with Netlify's build but with how your Git repository is configured or the state of your PR.
- Pull Request Status: Is the PR still open? Has it been marked as a "draft"? Draft PRs might not always trigger previews depending on your settings.
- Branch Protection Rules: If your repository has branch protection rules that require specific CI/CD checks to pass, a failing check might prevent a deploy preview from being considered "ready" or even cancel its build.
- Commit Status: Check the commit status in your Git provider (GitHub, GitLab). Did any associated CI/CD pipeline fail before Netlify even started its build?
- Netlify App Installation: Ensure the Netlify app is correctly installed and has the necessary permissions in your Git provider to access your repository and trigger builds. Reinstalling the app can sometimes resolve intermittent connection issues.
4. Local Build Verification
Can you build your project successfully on your local machine? This is a crucial diagnostic step.
- Run Local Build: Open your terminal in your project's root directory and run the same build command Netlify uses (e.g.,
npm run build,yarn build). - Check Output: Does the build complete without errors? Does it generate the expected output directory (e.g.,
dist,public)? - Serve Locally: Try serving the locally built output (e.g., using
serve -s buildornpx serve build) to ensure the site functions as expected.
If your local build fails, the problem lies within your code or project setup, not Netlify. If it succeeds, but Netlify's fails, look for environment differences (Node.js version, environment variables, OS differences).
5. Clear Netlify Cache & Retrigger Deploy
Sometimes, stale cache can lead to unpredictable build failures. Clearing the cache forces Netlify to fetch all dependencies and rebuild from scratch.
- Manual Retrigger: In the Deploys tab, click on the failed deploy, then select "Retry deploy" -> "Clear cache and deploy site".
- New Commit: Pushing a new, empty commit (
git commit --allow-empty -m "Trigger Netlify build"and thengit push) to your branch can also trigger a fresh build.
6. External Service Integrations & Environment Variables
If your site relies on external APIs, CMSs, or other services, ensure they are accessible and configured for the preview environment.
- Environment Variables: Double-check that all necessary API keys, tokens, and URLs for third-party services are correctly set as environment variables in Netlify (Site Settings > Build & deploy > Environment). Remember that preview contexts might require different values than production.
- CORS Issues: If your preview loads but fails to fetch data, check your browser's console for Cross-Origin Resource Sharing (CORS) errors. Your API might not be configured to allow requests from the
deploy-preview-XXX--your-site-name.netlify.appdomain. - Firewall/IP Whitelisting: If your external service has IP whitelisting, ensure Netlify's build IPs (which can vary) are allowed, or consider using a proxy if direct access is restricted.
Common Mistakes Leading to Failed Deploy Previews
- Incorrect Publish Directory: This is arguably the most common issue. Your build output (e.g.,
index.html, CSS, JS) must be in the directory specified bybuild.publishinnetlify.tomlor in your UI settings. - Missing or Incorrect Build Command: The command needs to be exact and runnable in Net