Skip to content

Commit 102ccb5

Browse files
committed
Clear old Gatsby cache storage and service workers to prevent stale content loading
1 parent 9cf653f commit 102ccb5

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

src/layouts/Layout.astro

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ const structuredData = {
5656
<head>
5757
<meta charset="UTF-8" />
5858
<meta name="viewport" content="width=device-width" />
59+
60+
<!-- Prevent aggressive caching -->
61+
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
62+
<meta http-equiv="Pragma" content="no-cache" />
63+
<meta http-equiv="Expires" content="0" />
64+
5965
<link rel="icon" type="image/jpeg" href="/favicon.jpg" />
6066
<link rel="canonical" href={canonicalURL} />
6167
<meta name="generator" content={Astro.generator} />
@@ -138,6 +144,35 @@ const structuredData = {
138144

139145
<ClientRouter />
140146

147+
<!-- Clear old Gatsby cache storage to prevent loading stale content -->
148+
<script is:inline>
149+
(async function() {
150+
// Unregister any old service workers (from Gatsby)
151+
if ('serviceWorker' in navigator) {
152+
const registrations = await navigator.serviceWorker.getRegistrations();
153+
for (const registration of registrations) {
154+
await registration.unregister();
155+
}
156+
}
157+
158+
// Clear cache storage (removes gatsby-plugin-offline and other old caches)
159+
if ('caches' in window) {
160+
const cacheNames = await caches.keys();
161+
await Promise.all(
162+
cacheNames.map(cacheName => caches.delete(cacheName))
163+
);
164+
}
165+
166+
// Clear old Gatsby data from localStorage
167+
const gatsbyKeys = Object.keys(localStorage).filter(key =>
168+
key.startsWith('gatsby-') ||
169+
key.includes('workbox') ||
170+
key.includes('sw-')
171+
);
172+
gatsbyKeys.forEach(key => localStorage.removeItem(key));
173+
})().catch(console.error);
174+
</script>
175+
141176
<!-- Minimal inline script to prevent FOUC - sets theme immediately -->
142177
<script is:inline>
143178
(function () {

0 commit comments

Comments
 (0)