Skip to content

Commit eea516f

Browse files
rosaclaude
andcommitted
Enable offline caching for all web users
Previously, offline caching was conditionally enabled only for Hotwire Native apps. This removes that restriction and enables offline support for all users, including PWAs and regular browsers. This Uses the new `fetchOptions` support in `TurboOffline` handlers to pass `cache: "no-cache"` for document fetches (cards and misc rules only, we don't need this for assets for example), to work around a quite annoying Safari PWA bug (see #1014) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 4e3f89f commit eea516f

File tree

3 files changed

+33
-44
lines changed

3 files changed

+33
-44
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ GIT
1313

1414
GIT
1515
remote: https://github.com/hotwired/turbo-rails.git
16-
revision: 45a2a40be0a677808a3a269311f1b5abaa3b30ba
16+
revision: 9bcc244043b16ef0202f8fe93aaeb246d841f9eb
1717
branch: offline-cache
1818
specs:
1919
turbo-rails (2.0.21)

Gemfile.saas.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ GIT
6060

6161
GIT
6262
remote: https://github.com/hotwired/turbo-rails.git
63-
revision: 45a2a40be0a677808a3a269311f1b5abaa3b30ba
63+
revision: 9bcc244043b16ef0202f8fe93aaeb246d841f9eb
6464
branch: offline-cache
6565
specs:
6666
turbo-rails (2.0.21)

app/views/pwa/service_worker.js.erb

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,44 @@
11
importScripts("<%= javascript_url("turbo-offline-umd.min") %>")
22

3-
<% if hotwire_native_app? %>
4-
TurboOffline.addRule({
5-
match: /\/assets\/.+[-\.][0-9a-f]+\.(js|css|svg|png|jpg|webp|woff2?|ico)$/,
6-
handler: TurboOffline.handlers.cacheFirst({
7-
cacheName: "assets",
8-
maxAge: 60 * 60 * 24 * 7
9-
})
3+
TurboOffline.addRule({
4+
match: /\/assets\/.+[-\.][0-9a-f]+\.(js|css|svg|png|jpg|webp|woff2?|ico)$/,
5+
handler: TurboOffline.handlers.cacheFirst({
6+
cacheName: "assets",
7+
maxAge: 60 * 60 * 24 * 7
108
})
9+
})
1110

12-
TurboOffline.addRule({
13-
match: /\/(boards|cards|users)\//,
14-
except: /\/(edit|pin|watch|new)$/,
15-
handler: TurboOffline.handlers.networkFirst({
16-
cacheName: "cards",
17-
maxAge: 60 * 60 * 24 * 3,
18-
networkTimeout: 2
19-
})
11+
TurboOffline.addRule({
12+
match: /\/(boards|cards|users)\//,
13+
except: /\/(edit|pin|watch|new)$/,
14+
handler: TurboOffline.handlers.networkFirst({
15+
cacheName: "cards",
16+
maxAge: 60 * 60 * 24 * 3,
17+
networkTimeout: 2,
18+
fetchOptions: { cache: "no-cache" }
2019
})
20+
})
2121

22-
TurboOffline.addRule({
23-
match: /\/rails\/active_storage\//,
24-
handler: TurboOffline.handlers.networkFirst({
25-
cacheName: "storage",
26-
maxAge: 60 * 60 * 24 * 7,
27-
networkTimeout: 2
28-
})
22+
TurboOffline.addRule({
23+
match: /\/rails\/active_storage\//,
24+
handler: TurboOffline.handlers.networkFirst({
25+
cacheName: "storage",
26+
maxAge: 60 * 60 * 24 * 7,
27+
networkTimeout: 2
2928
})
29+
})
3030

31-
// Everything else
32-
TurboOffline.addRule({
33-
handler: TurboOffline.handlers.networkFirst({
34-
cacheName: "misc",
35-
maxAge: 60 * 60 * 24,
36-
networkTimeout: 3
37-
})
31+
// Everything else
32+
TurboOffline.addRule({
33+
handler: TurboOffline.handlers.networkFirst({
34+
cacheName: "misc",
35+
maxAge: 60 * 60 * 24,
36+
networkTimeout: 3,
37+
fetchOptions: { cache: "no-cache" }
3838
})
39+
})
3940

40-
TurboOffline.start()
41-
<% else %>
42-
self.addEventListener("fetch", (event) => {
43-
if (event.request.method !== "GET") return
44-
45-
if (event.request.destination === "document") {
46-
event.respondWith(
47-
fetch(event.request, { cache: "no-cache" })
48-
.catch(() => caches.match(event.request))
49-
)
50-
}
51-
})
52-
<% end %>
41+
TurboOffline.start()
5342

5443
self.addEventListener("activate", (event) => {
5544
event.waitUntil(self.clients.claim())

0 commit comments

Comments
 (0)