Skip to content

Commit

Permalink
Offline-pwa update
Browse files Browse the repository at this point in the history
  • Loading branch information
BhasherBEL committed Mar 7, 2025
1 parent fbc3294 commit d88b8a1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ type User struct {
DefaultHomePage string `json:"default_home_page"`
CategoriesSortingOrder string `json:"categories_sorting_order"`
MarkReadOnView bool `json:"mark_read_on_view"`
CacheForOffline bool `json:"cache_for_offline"`
MediaPlaybackRate float64 `json:"media_playback_rate"`
BlockFilterEntryRules string `json:"block_filter_entry_rules"`
KeepFilterEntryRules string `json:"keep_filter_entry_rules"`
ExternalFontHosts string `json:"external_font_hosts"`
CacheForOffline bool `json:"cache_for_offline"`
}

func (u User) String() string {
Expand Down
2 changes: 1 addition & 1 deletion internal/http/response/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (b *Builder) Write() {
func (b *Builder) writeHeaders() {
b.headers["X-Content-Type-Options"] = "nosniff"
b.headers["X-Frame-Options"] = "DENY"
b.headers["Referrer-Policy"] = "no-referrer"
b.headers["Referrer-Policy"] = "strict-origin"

for key, value := range b.headers {
b.w.Header().Set(key, value)
Expand Down
2 changes: 1 addition & 1 deletion internal/reader/sanitizer/sanitizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func sanitizeAttributes(baseURL, tagName string, attributes []html.Attribute) ([
func getExtraAttributes(tagName string) ([]string, []string) {
switch tagName {
case "a":
return []string{"rel", "target", "referrerpolicy"}, []string{`rel="noopener noreferrer"`, `target="_blank"`, `referrerpolicy="no-referrer"`}
return []string{"rel", "target", "referrerpolicy"}, []string{`rel="noopener noreferrer"`, `target="_blank"`, `referrerpolicy="strict-origin"`}
case "video", "audio":
return []string{"controls"}, []string{"controls"}
case "iframe":
Expand Down
8 changes: 4 additions & 4 deletions internal/template/templates/views/entry.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<section class="entry" data-id="{{ .entry.ID }}" aria-labelledby="page-header-title">
<header class="entry-header">
<h1 id="page-header-title" dir="auto">
<a href="{{ .entry.URL | safeURL }}" target="_blank" rel="noopener noreferrer" referrerpolicy="no-referrer">{{ .entry.Title }}</a>
<a href="{{ .entry.URL | safeURL }}" target="_blank" rel="noopener noreferrer" referrerpolicy="strict-origin">{{ .entry.Title }}</a>
</h1>
{{ if .user }}
<div class="entry-actions">
Expand Down Expand Up @@ -79,7 +79,7 @@ <h1 id="page-header-title" dir="auto">
class="page-link"
target="_blank"
rel="noopener noreferrer"
referrerpolicy="no-referrer"
referrerpolicy="strict-origin"
data-original-link="{{ .user.MarkReadOnView }}">{{ icon "external-link" }}<span class="icon-label">{{ t "entry.external_link.label" }}</span></a>
</li>
<li>
Expand All @@ -98,7 +98,7 @@ <h1 id="page-header-title" dir="auto">
title="{{ t "entry.comments.title" }}"
target="_blank"
rel="noopener noreferrer"
referrerpolicy="no-referrer"
referrerpolicy="strict-origin"
data-comments-link="true"
>{{ icon "comment" }}<span class="icon-label">{{ t "entry.comments.label" }}</span></a>
</li>
Expand Down Expand Up @@ -232,7 +232,7 @@ <h1 id="page-header-title" dir="auto">
{{ end }}

<div class="entry-enclosure-download">
<a href="{{ .URL | safeURL }}" title="{{ t "action.download" }}{{ if gt .Size 0 }} - {{ formatFileSize .Size }}{{ end }}" target="_blank" rel="noopener noreferrer" referrerpolicy="no-referrer">{{ .URL | safeURL }}</a>
<a href="{{ .URL | safeURL }}" title="{{ t "action.download" }}{{ if gt .Size 0 }} - {{ formatFileSize .Size }}{{ end }}" target="_blank" rel="noopener noreferrer" referrerpolicy="strict-origin">{{ .URL | safeURL }}</a>
<small>{{ if gt .Size 0 }} - <strong>{{ formatFileSize .Size }}</strong>{{ end }}</small>
</div>
</div>
Expand Down
25 changes: 22 additions & 3 deletions internal/ui/static/js/service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@
const OFFLINE_VERSION = 2;
const CACHE_NAME = "offline";

const cachedPages = [
"/unread",
"/starred",
"/stylesheets",
"/app",
"/service-worker",
"/manifest.json",
"/feed/icon",
"/icon",
];

self.addEventListener("install", (event) => {
event.waitUntil(
(async () => {
const cache = await caches.open(CACHE_NAME);

if (USE_CACHE) {
await cache.addAll(["/", "/unread", OFFLINE_URL]);
await cache.addAll(["/", "/unread", "/starred", OFFLINE_URL]);
} else {
// Setting {cache: 'reload'} in the new request will ensure that the
// response isn't fulfilled from the HTTP cache; i.e., it will be from
Expand Down Expand Up @@ -49,12 +60,20 @@ async function cacheFirstWithRefresh(request) {
return networkResponse;
});

return (await cache.match(request)) || (await fetchResponsePromise);
try {
return (await cache.match(request)) || (await fetchResponsePromise);
} catch (error) {
const cache = await caches.open(CACHE_NAME);
return await cache.match(OFFLINE_URL);
}
}

self.addEventListener("fetch", (event) => {
if (USE_CACHE) {
return event.respondWith(cacheFirstWithRefresh(event.request));
const url = new URL(event.request.url);
if (cachedPages.some((page) => url.pathname.startsWith(page))) {
return event.respondWith(cacheFirstWithRefresh(event.request));
}
}

// We proxify requests through fetch() only if we are offline because it's slower.
Expand Down

0 comments on commit d88b8a1

Please sign in to comment.