gbowne1 / gbowne1/codestream

Fix: Service Worker Fails to Fetch Uncached Resources

Open
#2 3 comments 0 reactions 1 assignee Claimed by @abhisheksingh1204 View on GitHub
bug good first issue help wanted
Dominant language
JavaScript
Stars
9
Forks
24
PR merge metrics
No merged PRs in 30d

Description

The current service worker's fetch event handler has a flawed implementation of the cache-first strategy. The network request is nested within the .catch() block of the caches.match() promise. This means the application will only attempt to fetch a resource from the network if there's an error during the cache lookup, not if the resource simply isn't present in the cache.

## Impact

- Stale Content: Users may not see new or updated content (e.g., new streams, images) because the app is unable to fetch them from the network.

- Unreliable Caching: The current logic breaks the intended cache-first, then network-update strategy, making the PWA's offline
functionality inconsistent.

- Failed Requests: When the user is online, new assets that aren't yet cached will fail to load, resulting in broken images or missing
CSS/JS.

## Proposed Solution

The fetch call should be executed when a caches.match() returns undefined (a cache miss). The corrected logic should first
check the cache. If a match is found, serve it. If not, proceed to a network request, and then cache the new response for future
use.

Code to be Updated: public/service-worker.js

## Expected Behavior After Fix:

- When a user is online, all resources will be loaded, whether from the cache or the network.

- Un-cached resources will be correctly fetched and then stored in the cache for future offline access.

- The service worker will properly handle both cache hits and misses, improving the app's performance and reliability.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.