Optimize caching strategy with HTTP headers and enhanced service worker configuration and solve first render white screen issue (#1482)#1797
Merged
martinbedouret merged 6 commits intomasterfrom May 26, 2025
Conversation
- Update service worker configuration to properly handle navigation fallback - Add proper cache control for static assets - Improve client-side routing handling
martinbedouret
approved these changes
May 26, 2025
This was referenced May 26, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements a comprehensive multi-layered caching strategy by adding HTTP caching and enhancing service worker configuration for optimal performance in our Azure-deployed React SPA.
Changes
Added a new
locationblock indefault.confto match static assets in/public(e.g./images,/symbols,/ogv), including subfolders.Applies to file types:
.svg,.png,.jpg,.jpeg,.gif,.webp,.ico,.js,.css,.woff,.woff2.Sets a single
Cache-Controlheader:Cache-Control: public, max-age=31536000, immutableRemoved the
expiresdirective to avoid duplicate cache headers.Suppressed access and 404 logs for these static assets to reduce log noise.
🌟 Why
Improved performance on repeat visits: assets are cached for up to 1 year.
Better CDN and browser caching: allows intermediaries to reuse responses efficiently.
Works seamlessly with Create React App’s service worker setup:
.hash.ext), so long-term caching is safe.Service Worker Enhancements (
sw-precache-config.js)cacheFirststrategy for/static/URLs with 200 file limitnavigateFallback: '/index.html'for proper React Router functionalityBenefits
Technical Details
Service Worker:
cacheFirststrategy for static assets with API route protection via whitelistSPA Routing:
navigateFallbackensures direct navigation to React routes works offlineCache Coordination: Both layers respect the same file patterns and hash-based invalidation
Asset Updates: When assets change, new hashes generate different filenames, bypassing all cache layers naturally
SPA Navigation: Test direct URL access (e.g.,
/dashboard) works both online and offlineAPI Protection: Verify API calls aren't cached as HTML responses
✅ How to Test Locally
Open the app in your browser at: http://localhost:8080
In DevTools → Network tab, inspect a request to any asset in
/images,/symbols, etc.Confirm the response includes the header:
Cache-Control: public, max-age=31536000, immutableValidate service worker behavior:
npm run build && serve -s build)