Skip to content

Commit

Permalink
Merge pull request #136 from zugdev/fix-rerender
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 authored Oct 29, 2024
2 parents 4a88f66 + f402ee7 commit 3e0270d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
12 changes: 10 additions & 2 deletions src/home/fetch-github/fetch-and-display-previews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,19 @@ function getProposalsOnlyFilter(getProposals: boolean) {
}

// checks the cache's integrity, sorts issues, checks Directory/Proposals toggle, renders them and applies avatars
export async function displayGitHubIssues(sorting?: Sorting, options = { ordering: "normal" }) {
export async function displayGitHubIssues({
sorting,
options = { ordering: "normal" },
skipAnimation = false,
}: {
sorting?: Sorting;
options?: { ordering: string };
skipAnimation?: boolean;
} = {}) {
await checkCacheIntegrityAndSyncTasks();
const cachedTasks = taskManager.getTasks();
const sortedIssues = sortIssuesController(cachedTasks, sorting, options);
const sortedAndFiltered = sortedIssues.filter(getProposalsOnlyFilter(isProposalOnlyViewer));
renderGitHubIssues(sortedAndFiltered);
renderGitHubIssues(sortedAndFiltered, skipAnimation);
applyAvatarsToIssues();
}
8 changes: 6 additions & 2 deletions src/home/fetch-github/fetch-issues-full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ export async function postLoadUpdateIssues() {
const fetchedIssues = await fetchIssues();

if (issuesAreDifferent(cachedIssues, fetchedIssues)) {
await saveIssuesToCache(cachedIssues, fetchedIssues);
await saveIssuesToCache(cachedIssues, fetchedIssues); // this handles stale and new issues
await taskManager.syncTasks();
void displayGitHubIssues();
if (cachedIssues.length === 0) {
void displayGitHubIssues(); // if it's first time loading keep animation
} else {
void displayGitHubIssues({ skipAnimation: true }); // if there were cached issues skip animation
}
}
} catch (error) {
console.error("Error updating issues cache", error);
Expand Down
10 changes: 7 additions & 3 deletions src/home/rendering/render-github-issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { closeModal, modal, modalBodyInner, titleAnchor, titleHeader } from "./r
import { setupKeyboardNavigation } from "./setup-keyboard-navigation";
import { isProposalOnlyViewer } from "../fetch-github/fetch-and-display-previews";

export function renderGitHubIssues(tasks: GitHubIssue[]) {
export function renderGitHubIssues(tasks: GitHubIssue[], skipAnimation: boolean) {
const container = taskManager.getContainer();
if (container.classList.contains("ready")) {
container.classList.remove("ready");
Expand All @@ -22,8 +22,12 @@ export function renderGitHubIssues(tasks: GitHubIssue[]) {
if (!existingIssueIds.has(task.id.toString())) {
const issueWrapper = everyNewIssue({ gitHubIssue: task, container });
if (issueWrapper) {
setTimeout(() => issueWrapper.classList.add("active"), delay);
delay += baseDelay;
if (skipAnimation) {
issueWrapper.classList.add("active");
} else {
setTimeout(() => issueWrapper.classList.add("active"), delay);
delay += baseDelay;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/home/sorting/sorting-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class SortingManager {

// Apply the sorting based on the new state (normal or reverse)
try {
void displayGitHubIssues(option as Sorting, { ordering: newOrdering });
void displayGitHubIssues({ sorting: option as Sorting, options: { ordering: newOrdering } });
} catch (error) {
renderErrorCatch(error as ErrorEvent);
}
Expand Down
33 changes: 25 additions & 8 deletions static/progressive-web-app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const cacheName = "pwacache-v2"; // Increment this when files change
const cacheName = "pwacache-v4"; // Increment this when files change
const urlsToCache = [
"/",
"/index.html",
Expand Down Expand Up @@ -43,10 +43,15 @@ self.addEventListener("activate", (event) => {
self.clients.claim(); // Take control of all pages immediately
});

// Fetch event: Respond from cache or network
// Fetch event: Cache first approach but update cache anyways
self.addEventListener("fetch", (event) => {
const url = new URL(event.request.url);

// Ignore non-HTTP(S) requests (like 'chrome-extension://')
if (url.protocol !== "http:" && url.protocol !== "https:") {
return;
}

// If the request has query parameters, bypass the cache
if (url.search) {
event.respondWith(fetch(event.request));
Expand All @@ -55,17 +60,29 @@ self.addEventListener("fetch", (event) => {

event.respondWith(
caches.match(event.request).then((cachedResponse) => {
if (cachedResponse) {
return cachedResponse;
}
return fetch(event.request)
const fetchPromise = fetch(event.request)
.then((networkResponse) => {
// Clone the network response to avoid using the body twice
const responseClone = networkResponse.clone();

// If the network response is valid, update the cache
if (networkResponse.ok) {
caches.open(cacheName).then((cache) =>
cache.put(event.request, responseClone)
);
}
return networkResponse;
})
.catch((error) => {
console.error("[Service Worker] Network request failed:", error);
return null;
return cachedResponse || new Response("Offline content unavailable", {
status: 503,
statusText: "Service Unavailable",
});
});

// Serve from cache first, but update the cache in the background
return cachedResponse || fetchPromise;
})
);
});
});

0 comments on commit 3e0270d

Please sign in to comment.