Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PWA: First implementation of offline mode #2704

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Offline-pwa update
  • Loading branch information
BhasherBEL committed Jan 11, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 0c609c68c5f898e35f6cc17a5904bfdab6af61ab
2 changes: 1 addition & 1 deletion client/model.go
Original file line number Diff line number Diff line change
@@ -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 {
2 changes: 1 addition & 1 deletion internal/http/response/builder.go
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion internal/reader/sanitizer/sanitizer.go
Original file line number Diff line number Diff line change
@@ -237,7 +237,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":
8 changes: 4 additions & 4 deletions internal/template/templates/views/entry.html
Original file line number Diff line number Diff line change
@@ -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">
@@ -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>
@@ -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>
@@ -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>
25 changes: 22 additions & 3 deletions internal/ui/static/js/service_worker.js
Original file line number Diff line number Diff line change
@@ -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
@@ -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.