Skip to content

Commit 9c6231d

Browse files
committed
PWA: Warning and cache visible entries
1 parent 0c1b43d commit 9c6231d

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

internal/http/request/context.go

+5
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/locale/translations/en_US.json

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@
253253
"page.offline.title": "Offline Mode",
254254
"page.offline.message": "You are offline",
255255
"page.offline.refresh_page": "Try to refresh the page",
256+
"page.offline.warning": "You are currently offline. Some features may not be available.",
256257
"page.webauthn_rename.title": "Rename Passkey",
257258
"alert.no_shared_entry": "There is no shared entry.",
258259
"alert.no_bookmark": "There are no starred entries.",

internal/template/templates/common/layout.html

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
{{ if .flashErrorMessage }}
126126
<div role="alert" class="flash-error-message alert alert-error">{{ .flashErrorMessage }}</div>
127127
{{ end }}
128+
<div id="offline-flag" role="alert" aria-live="assertive" aria-atomic="true" class="flash-message alert alert-warning hidden">{{ t "page.offline.warning" }}</div>
128129

129130
{{template "page_header" .}}
130131

internal/ui/entry_unread.go

+1-1
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 user.MarkReadOnView {
69+
if user.MarkReadOnView && !request.IsServiceWorker(r) {
7070
entry.Status = model.EntryStatusRead
7171
}
7272

internal/ui/static/js/service_worker.js

+34-2
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)