1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-06 17:41:00 +00:00

PWA: Warning and cache visible entries

This commit is contained in:
Brieuc Dubois 2024-06-22 12:58:42 +02:00
parent 925ea2c082
commit c50edd735a
4 changed files with 41 additions and 3 deletions

View file

@ -140,6 +140,11 @@ func LastForceRefresh(r *http.Request) int64 {
return timestamp return timestamp
} }
// Determine if the request is from a service worker.
func IsServiceWorker(r *http.Request) bool {
return r.Header.Get("Client-Type") == "service-worker"
}
// ClientIP returns the client IP address stored in the context. // ClientIP returns the client IP address stored in the context.
func ClientIP(r *http.Request) string { func ClientIP(r *http.Request) string {
return getContextStringValue(r, ClientIPContextKey) return getContextStringValue(r, ClientIPContextKey)

View file

@ -132,6 +132,7 @@
{{ if .flashErrorMessage }} {{ if .flashErrorMessage }}
<div role="alert" class="flash-error-message alert alert-error">{{ .flashErrorMessage }}</div> <div role="alert" class="flash-error-message alert alert-error">{{ .flashErrorMessage }}</div>
{{ end }} {{ end }}
<div id="offline-flag" role="alert" aria-live="assertive" aria-atomic="true" class="flash-message alert alert-warning hidden">{{ t "page.offline.warning" }}</div>
{{template "page_header" .}} {{template "page_header" .}}

View file

@ -66,7 +66,7 @@ func (h *handler) showUnreadEntryPage(w http.ResponseWriter, r *http.Request) {
prevEntryRoute = route.Path(h.router, "unreadEntry", "entryID", prevEntry.ID) prevEntryRoute = route.Path(h.router, "unreadEntry", "entryID", prevEntry.ID)
} }
if entry.ShouldMarkAsReadOnView(user) { if entry.ShouldMarkAsReadOnView(user) && !request.IsServiceWorker(r) {
entry.Status = model.EntryStatusRead entry.Status = model.EntryStatusRead
} }

View file

@ -3,8 +3,6 @@
const OFFLINE_VERSION = 2; const OFFLINE_VERSION = 2;
const CACHE_NAME = "offline"; const CACHE_NAME = "offline";
console.log(USE_CACHE);
self.addEventListener("install", (event) => { self.addEventListener("install", (event) => {
event.waitUntil( event.waitUntil(
(async () => { (async () => {
@ -64,3 +62,37 @@ self.addEventListener("fetch", (event) => {
); );
} }
}); });
self.addEventListener("load", async (event) => {
if (
navigator.onLine === true &&
event.target.location.pathname === "/unread" &&
USE_CACHE
) {
const cache = await caches.open(CACHE_NAME);
for (let article of document.getElementsByTagName("article")) {
const as = article.getElementsByTagName("a");
if (as.length > 0) {
const a = as[0];
const href = a.href;
cache
.add(
new Request(href, {
headers: new Headers({
"Client-Type": "service-worker",
}),
}),
)
.then(() => {
article;
});
}
}
}
});
self.addEventListener("DOMContentLoaded", function () {
const offlineFlag = document.getElementById("offline-flag");
offlineFlag.classList.toggle("hidden", navigator.onLine);
});