0
0
Flaskframework~8 mins

Custom error pages (404, 500) in Flask - Performance & Optimization

Choose your learning style9 modes available
Performance: Custom error pages (404, 500)
MEDIUM IMPACT
Custom error pages affect the user experience during error conditions by controlling the content shown and can impact perceived load speed and visual stability.
Displaying a 404 error page when a user visits a non-existent route
Flask
from flask import Flask, render_template
app = Flask(__name__)

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404
Uses a pre-designed HTML template that can be cached and styled for consistent layout, reducing layout shifts.
📈 Performance GainReduces CLS by serving stable layout; template caching improves response time.
Displaying a 404 error page when a user visits a non-existent route
Flask
from flask import Flask
app = Flask(__name__)

@app.errorhandler(404)
def page_not_found(e):
    return '<html><body><h1>404 Not Found</h1><p>The page does not exist.</p></body></html>', 404
Returns a minimal HTML string without caching or styling, causing inconsistent layout and possible flicker.
📉 Performance CostTriggers layout shift on error page load; no caching increases server response time.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Minimal inline HTML error pageLow (few nodes)Multiple on load due to layout shiftsLow but unstable[X] Bad
Styled cached template error pageModerate (templated nodes)Single stable reflowModerate but stable[OK] Good
Rendering Pipeline
When an error occurs, Flask routes to the error handler which returns HTML. The browser parses the HTML, calculates styles, performs layout, paints pixels, and composites layers. Custom error pages with stable layout and caching reduce layout shifts and speed up paint.
Style Calculation
Layout
Paint
Composite
⚠️ BottleneckLayout due to unstable or minimal HTML causing layout shifts
Core Web Vital Affected
CLS
Custom error pages affect the user experience during error conditions by controlling the content shown and can impact perceived load speed and visual stability.
Optimization Tips
1Always use cached HTML templates for custom error pages to reduce server response time.
2Design error pages with fixed layout and styling to prevent layout shifts and improve CLS.
3Avoid returning minimal inline HTML strings without styling or caching for error responses.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance benefit of using a cached template for custom error pages in Flask?
AAdds more DOM nodes causing slower paint
BReduces layout shifts and improves visual stability
CIncreases server CPU usage
DBlocks rendering until JavaScript loads
DevTools: Performance
How to check: Open DevTools, go to Performance tab, reload the error page, and record. Look for layout shifts and long tasks during error page load.
What to look for: Check for Layout Shift events and long scripting or rendering tasks that delay paint or cause visual instability.