coastdigitalgroup/coastai-skills/bfcache-optimization
Claude Code and agent skill by coastdigitalgroup/coastai-skills
- community
Checking that the SKILL.md file is still in the repository.
Optimize websites for the Back/Forward Cache (bfcache) to enable instant navigations by managing lifecycle events and eliminating eligibility blockers.
Install
npx vskill@latest install coastdigitalgroup/coastai-skills/bfcache-optimizationSKILL.md
The same SKILL.md was published earlier as coastdigitalgroup/coastai-skills/bfcache-optimization.
Bfcache Optimization
Purpose
The Bfcache Optimization skill provides a technical protocol for ensuring that web pages are eligible for the browser's Back/Forward Cache. This feature allows for instant navigations when a user moves back or forward in their history by keeping a complete snapshot of the page (including the JS heap) in memory.
Use Cases
- Improving user-perceived performance for multi-page navigations.
- Reducing Core Web Vitals (specifically LCP and INP) for returning visits.
- Modernizing legacy codebases that rely on the
unloadevent. - Handling real-time UI state (e.g., shopping carts, notifications) that must
stay fresh even when restored from cache.
When NOT to Use
- Highly Sensitive Data: Pages displaying extremely sensitive information
(e.g., bank balances that change every second) may require Cache-Control: no-store to ensure the data is never cached, even in memory.
- Short-Lived Authentication: If a page's entire purpose is a one-time
security check that must never be re-entered.
Inputs
- Eligibility Audit: Reports from Chrome DevTools "Back/forward cache"
panel identifying why a page is not being cached.
- Event Inventory: Identification of
unloadorbeforeunloadlisteners. - Caching Headers: Current
Cache-Controlconfiguration. - Restoration Logic: Identification of UI elements that need to be
updated when a user returns to the page.
Outputs
- Bfcache-Eligible Codebase: Removal of blockers like
unloadlisteners. - Lifecycle Management Logic: Implementation of
pageshowandpagehide
handlers.
- State Synchronization: Scripts that update stale UI components upon
restoration using event.persisted.
- Optimized Headers:
Cache-Controlsettings that allow bfcache while
maintaining security.
Workflow
1. Audit Eligibility
- Open Chrome DevTools, go to Application > Back/forward cache.
- Click Test back/forward cache.
- Address any listed "Not restored" reasons (e.g., "Page used unload listener").
2. Replace unload Listeners
- The Problem: The
unloadevent is a primary blocker for bfcache because
it was historically used to perform cleanup that would be broken if the page remained in memory.
- The Fix: Move logic from
unloadtopagehideorvisibilitychange. - Use
pagehidefor cleanup that should happen when the user navigates away. - Use
visibilitychangefor saving user data or state.
3. Handle Restoration with pageshow
- When a page is restored from bfcache, the standard
DOMContentLoadedor
load events do not fire.
- Use the
pageshowevent to detect restoration:
``javascript window.addEventListener('pageshow', (event) => { if (event.persisted) { // Page was restored from bfcache refreshStaleData(); } }); ``
4. Manage Active Connections
- WebSockets/SSE: Close active connections in
pagehideand re-establish
them in pageshow (if event.persisted is true).
- Timers: Pause intervals or timeouts in
pagehideto prevent background
processing, and resume in pageshow.
5. Configure Headers
- Avoid
Cache-Control: no-storeunless absolutely necessary for security,
as it prevents bfcache.
- Prefer
Cache-Control: no-cacheormax-age=0, must-revalidatewhich allow
the browser to cache the page in the bfcache while still requiring it to re-validate with the server for fresh navigations.
Decision Rules
- `pagehide` vs. `unload`: Always use
pagehide. It fires in all scenarios
where unload does but does not block bfcache.
- `visibilitychange` vs. `pagehide`: Use
visibilitychangeto save data
periodically or when the tab is hidden (e.g., user switches tabs). Use pagehide for the final "leaving the page" cleanup.
- Handling Stale Content: If the page content changes frequently, use the
pageshow event to fetch only the deltas rather than reloading the entire page.
Constraints
- Single Announcer: Ensure that
pageshowlogic doesn't trigger duplicate
analytics pings or multiple UI refreshes.
- Browser Behavior: Be aware that different browsers (Safari/Firefox vs.
Chrome) have slightly different heuristics for bfcache eligibility, but removing unload is the universal first step.
Non-Goals
- Implementing a custom client-side caching layer (e.g., IndexedDB or
localStorage).
- General HTTP caching strategy for static assets (CSS, JS, Images).
- Managing Service Worker caches (though they interact with bfcache).
Common Failure Patterns
- Sticking with `unload`: Continuing to use
window.onunload, which is the
#1 cause of bfcache exclusion.
- Stale UI after Restore: A user clicks "Back" and sees a shopping cart
with 0 items even though they just added 3 on the previous page.
- Broken Analytics: Not tracking "Back/Forward" navigations because the
analytics script only runs on the initial load event.
- Zombie Connections: Leaving WebSockets open, causing the server to keep
connections alive for pages that are "frozen" in the user's cache.
Validation Criteria
- [ ] DevTools Audit: Verify "Back/forward cache" panel shows "Successfully
restored from back/forward cache."
- [ ] Event Execution Check: Confirm that
pageshowfires and its
persisted property is true when using the Back button.
- [ ] State Restoration Test: Verify that any dynamic UI (counters, cart
totals, timestamps) is updated correctly upon return.
- [ ] Connection Audit: Verify that network connections (WebSockets) are
cleanly closed when navigating away and reopened when returning.
- [ ] Header Inspection: Ensure
Cache-Controlis not set tono-store.
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingwebsite-development/bfcache-optimization/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/coastdigitalgroup/coastai-skills/bfcache-optimization)