zombieFox / nightTab

A neutral new tab page accented with a chosen colour. Customise the layout, style, background and bookmarks with nightTab.
https://zombiefox.github.io/nightTab/
GNU General Public License v3.0
1.78k stars 254 forks source link

Add service worker #399

Open sansmoraxz opened 1 year ago

sansmoraxz commented 1 year ago

Using this PR for service worker implementation as per #334. As I have scrapped #339 due to it being too ugly approach.

As suggested by @metruzanca here's modular approach for creating service workers. I have added it as an entry point for webpack builds rather than direct copy of a single js file.

Registration of service workers is done at page load from initialBackground.js rather than by the manifest.json. This way it's more portable, and doesn't break as was the case when trying to use with manifest v3 in firefox. Although firefox still disallows service workers for browser extensions, the only difference will be simply service worker registration failing and proceeding as normal.

As an added benefit this also works when the page is not run as an extension. Which means for service worker unsupported browsers (in extensions only) like firefox, users can still gain the features available to service workers by simply settin the remote url as new tab page (which works even when offline).

PS: I did remove [content-hash] from webpack build file for js as I am not sure how to use a static name to load from initialBackground.js

metruzanca commented 1 year ago

Looks good to me, though I've not used serviceWorkers for caching before so I'm unware of any pitfalls of them.

sansmoraxz commented 1 year ago

Well there's a gotcha regarding cache invalidation. With cache first strategy at least we entirely skip network fetch if it's found in the local cache. Might not be much of an issue for most cases but some ppl may start wondering why their new image isn't being displayed when they updated some image in xyz website.

Perhaps we can add functionality to clear the cache from UI.

metruzanca commented 1 year ago

Good idea. Another thought is what happens when the user tries to hard refresh (ctrl/cmd + shift + R normally overrides all caches).

Do we know if the service worker gets reset?

sansmoraxz commented 1 year ago

OK in the GitHub hosted page it seems to bypass the cache from service worker, and the page fails to load if completely offline.

But as an extension the images are loading properly but weirdly enough I don't see any network request being made.

sansmoraxz commented 1 year ago

Also do you know if chrome removed the functionality to view cache from web inspector for extensions? I can't seem to be able to access those.

metruzanca commented 1 year ago

I can try running it myself tomorrow (left work late today) and see what i can figure out.

sansmoraxz commented 1 year ago

Sorry about the delay, was busy with other work.

I added the option to clear entire cache and service workers when user clears their data. This should take care of any cache related issue that might arise.