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

feat: cache sw assets with service worker #234

Merged
merged 8 commits into from
May 14, 2024

Conversation

SgtPooki
Copy link
Member

@SgtPooki SgtPooki commented Apr 29, 2024

Title

feat: cache sw assets with service worker

Description

Fixes #222

This PR enables caching of ipfs-sw-* assets with the service worker. This will allow the service worker to cache the assets and serve them from the cache when the user is offline.
It will also allow users to load assets more quickly, and since all assets have a postfixed contenthash in their filename, we shouldn't have to worry about stale assets being served.

Also FYI: requests for ipfs-sw-sw.js (the root service worker that is registered) will not hit this cache, because service workers can't see requests for themselves.

Notes & open questions

This enables offline browsing of sites. Before this change, root assets would not be accessible if user is offline:

Offline with changes in main

2024-04-29.at.10.51.45.-.Brown.Rhinoceros.mp4

Offline with changes from this PR

2024-04-29.at.10.47.01.-.Lavender.Wombat.mp4

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

@SgtPooki SgtPooki linked an issue Apr 29, 2024 that may be closed by this pull request
@SgtPooki SgtPooki requested review from lidel and a team April 29, 2024 17:17
@SgtPooki SgtPooki self-assigned this Apr 29, 2024
@SgtPooki SgtPooki added this to the Beta milestone Apr 29, 2024
Copy link
Member Author

@SgtPooki SgtPooki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self review

src/sw.ts Outdated
const isIndexHtmlRequest = url.pathname === '/' && !isSubdomainRequest(event)

// if request is for https://cdn.jsdelivr.net/* we should consider it a sw asset
const isJsDelivrRequest = /^https:\/\/cdn.jsdelivr.net\/.+$/.test(event.request.url)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tangentially related: what do you think about publishing the CSS as part of this app instead of relying on a third-party origin (jsdeliver).

With both Cloudflare and the SW Cache, should be performant enough.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea i started to do that previously and i think i squashed it because of other changes. I could do it as a part of this pr

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that i'm at my laptop, I think i started to do that with the dynamic imports, but it only made the bundle larger.. now that index.ts and app.tsx is smaller, and we have a router, I could include tachyons and ipfs-css on only helper-ui, redirect-page, and config, and it will reduce burden for redirect-interstitial (ipfs first-hit without sw registered)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed by 20a9c6d (#234)

note that because the bundle is larger now (-rw-r--r-- 1 sgtpooki 13M Apr 30 08:08 ipfs-sw-138-66da405c07a0317cdbbf.css), there is FOUC on routing determination.

I don't think we should include it in the bundle.. but also, I think we should because then we're not dependent upon a third party.. ¯\_(ツ)_/¯

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lidel could use eyes on this one here.

@SgtPooki SgtPooki merged commit 20a8f32 into main May 14, 2024
16 checks passed
@SgtPooki SgtPooki deleted the 222-feat-cache-sw-gateway-assets branch May 14, 2024 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: cache sw-gateway assets
2 participants