First Contentful Paint (FCP) Delay Fix
In this tutorial, you'll learn about First Contentful Paint (FCP) Delay Fix. We cover key concepts, practical examples, and best practices.
The Problem
First Contentful Paint (FCP) marks the moment when the browser renders the first piece of content from the DOM. Delayed FCP means users stare at a blank screen — caused by render-blocking CSS/JS, slow server response, or missing critical rendering path optimization.
Quick Fix
Step 1: Eliminate render-blocking resources
<!-- Wrong — CSS blocks rendering -->
<link rel="stylesheet" href="styles.css">
<script src="app.js"></script>
<!-- Right — inline critical CSS, defer non-critical -->
<style>
/* Critical above-the-fold styles */
body { font-family: sans-serif; margin: 0; }
.header { display: flex; padding: 1rem; }
.hero { max-width: 1200px; margin: 0 auto; }
</style>
<link rel="preload" href="styles.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="styles.css"></noscript>
<script defer src="app.js"></script>
Step 2: Optimize server response time
// Wrong — slow TTFB delays FCP
// Server response takes 3 seconds
// Right — use CDN, caching, and server optimization
// 1. Use CDN (Cloudflare, CloudFront) for edge caching
// 2. Enable compression (Brotli or gzip)
// 3. Use HTTP/2 for multiplexing
// 4. Configure caching headers:
// Cache-Control: public, max-age=31536000, immutable
// For HTML:
// Cache-Control: no-cache // or use CDN page caching
Step 3: Minimize CSS and remove unused styles
# Wrong — sending all CSS, including unused
# styles.css is 250KB
# Right — purge unused CSS (with PurgeCSS)
npx purgecss --css styles.css --content index.html --output dist/
<!-- Before: 250KB -->
<link rel="stylesheet" href="styles.css">
<!-- After: 15KB -->
<link rel="stylesheet" href="styles-purged.min.css">
Step 4: Use font-display to prevent blank text
/* Wrong — font blocks rendering (FOIT) */
@font-face {
font-family: 'Custom';
src: url('/fonts/custom.woff2');
}
/* Right — use swap to show fallback immediately */
@font-face {
font-family: 'Custom';
src: url('/fonts/custom.woff2');
font-display: swap;
}
/* Or use optional for performance */
@font-face {
font-family: 'Custom';
src: url('/fonts/custom.woff2');
font-display: optional;
}
Step 5: Preconnect to third-party origins
<!-- Wrong — DNS lookup delays third-party resources -->
<script src="https://api.example.com/sdk.js"></script>
<!-- Right — preconnect to critical origins -->
<link rel="preconnect" href="https://api.example.com">
<!-- or use dns-prefetch as fallback -->
<link rel="dns-prefetch" href="https://api.example.com">
Prevention
- Inline critical CSS and defer the rest with
loadCSSpattern - Set cache headers and use a CDN for fast TTFB
- Purge unused CSS rules
- Use
font-display: swaporoptionalfor all custom fonts - Preconnect to third-party origins that host critical resources
Common Mistakes with fcp delay
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
These mistakes appear frequently in real-world PERF code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro