Skip to content

Commit c50edd7

Browse files
committed
PWA: Warning and cache visible entries
1 parent 925ea2c commit c50edd7

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

internal/http/request/context.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ func LastForceRefresh(r *http.Request) int64 {
140140
return timestamp
141141
}
142142

143+
// Determine if the request is from a service worker.
144+
func IsServiceWorker(r *http.Request) bool {
145+
return r.Header.Get("Client-Type") == "service-worker"
146+
}
147+
143148
// ClientIP returns the client IP address stored in the context.
144149
func ClientIP(r *http.Request) string {
145150
return getContextStringValue(r, ClientIPContextKey)

internal/template/templates/common/layout.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
{{ if .flashErrorMessage }}
133133
<div role="alert" class="flash-error-message alert alert-error">{{ .flashErrorMessage }}</div>
134134
{{ end }}
135+
<div id="offline-flag" role="alert" aria-live="assertive" aria-atomic="true" class="flash-message alert alert-warning hidden">{{ t "page.offline.warning" }}</div>
135136

136137
{{template "page_header" .}}
137138

internal/ui/entry_unread.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (h *handler) showUnreadEntryPage(w http.ResponseWriter, r *http.Request) {
6666
prevEntryRoute = route.Path(h.router, "unreadEntry", "entryID", prevEntry.ID)
6767
}
6868

69-
if entry.ShouldMarkAsReadOnView(user) {
69+
if entry.ShouldMarkAsReadOnView(user) && !request.IsServiceWorker(r) {
7070
entry.Status = model.EntryStatusRead
7171
}
7272

internal/ui/static/js/service_worker.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
const OFFLINE_VERSION = 2;
44
const CACHE_NAME = "offline";
55

6-
console.log(USE_CACHE);
7-
86
self.addEventListener("install", (event) => {
97
event.waitUntil(
108
(async () => {
@@ -64,3 +62,37 @@ self.addEventListener("fetch", (event) => {
6462
);
6563
}
6664
});
65+
66+
self.addEventListener("load", async (event) => {
67+
if (
68+
navigator.onLine === true &&
69+
event.target.location.pathname === "/unread" &&
70+
USE_CACHE
71+
) {
72+
const cache = await caches.open(CACHE_NAME);
73+
74+
for (let article of document.getElementsByTagName("article")) {
75+
const as = article.getElementsByTagName("a");
76+
if (as.length > 0) {
77+
const a = as[0];
78+
const href = a.href;
79+
cache
80+
.add(
81+
new Request(href, {
82+
headers: new Headers({
83+
"Client-Type": "service-worker",
84+
}),
85+
}),
86+
)
87+
.then(() => {
88+
article;
89+
});
90+
}
91+
}
92+
}
93+
});
94+
95+
self.addEventListener("DOMContentLoaded", function () {
96+
const offlineFlag = document.getElementById("offline-flag");
97+
offlineFlag.classList.toggle("hidden", navigator.onLine);
98+
});

0 commit comments

Comments
 (0)