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: preload and cancel images with a service worker #16893

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

midzelis
Copy link
Contributor

@midzelis midzelis commented Mar 15, 2025

Description

This helps slow connections the most. When scrolling, if a thumbnail that is slow to load goes offscreen, it is removed from the DOM, but the keeps the connection open. With the 6 connection per-domain limit, this can increase latency of subsequent requests, slowing down the load of on-screen images.

Similar thing happens when hitting next/previous on photo viewer. If you hit next before img loads, it will keep loading - if on a slow connection, and the image is large, this can easily take up al 6 requests.

This PR uses a service worker that will cancel any network requests created by tags (or any other request) - as the thumbs or photos go out of view. Additionally, it supports preload of next/previous assets in the photo viewer. Lastly, it includes the sveltekit service-worker example, which preloads the entire svelte app in the background when the service worker is activated.

This requires a secure context (http://localhost or a https://example.com). If its not present, you don't lose any function, just don't get these benefits it provides.

Checklist:

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if applicable
  • I have no unrelated changes in the PR.
  • I have confirmed that any new dependencies are strictly necessary.
  • I have written tests for new code (if applicable)
  • I have followed naming conventions/patterns in the surrounding code
  • All code in src/services/ uses repositories implementations for database calls, filesystem operations, etc.
  • All code in src/repositories/ is pretty basic/simple and does not have any immich specific logic (that belongs in src/services/)

@midzelis midzelis requested a review from bo0tzz as a code owner March 15, 2025 19:34
@midzelis midzelis changed the title feat: Service Worker to preload/cancel images and other resources feat(web): Service worker to preload/cancel images and other resources (requires https) Mar 15, 2025
Copy link
Contributor

Deploying preview environment to https://pr-16893.preview.internal.immich.cloud/


sw.addEventListener('fetch', (event) => {
// ignore POST requests etc
if (event.request.method !== 'GET') return;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (event.request.method !== 'GET') return;
if (event.request.method !== 'GET') {
return;
}

Copy link
Member

Choose a reason for hiding this comment

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

That's caught by a linting rule as well. Generally all failing actions should be addressed.

@jrasm91 jrasm91 changed the title feat(web): Service worker to preload/cancel images and other resources (requires https) feat: preload and cancel images with a service worker Mar 18, 2025
@jrasm91 jrasm91 marked this pull request as draft March 18, 2025 20:44
@jrasm91
Copy link
Contributor

jrasm91 commented Mar 18, 2025

Can you reduce the scope of this PR to only include preloading and canceling images?

@midzelis midzelis marked this pull request as ready for review March 19, 2025 03:24
@alextran1502 alextran1502 self-assigned this Mar 19, 2025
@alextran1502
Copy link
Contributor

Hi Min, I just ran some tests on this. It works very well overall!

There have been two occasions when I triggered some large buckets that were not rendering at all. Even if I scroll past it and scroll back to retrigger the render pipeline, it still shows nothing.

@midzelis
Copy link
Contributor Author

Hi Min, I just ran some tests on this. It works very well overall!

There have been two occasions when I triggered some large buckets that were not rendering at all. Even if I scroll past it and scroll back to retrigger the render pipeline, it still shows nothing.

I think what you saw a few days ago might have been related to some regressions from the asset-grid/asset-store performance PR. I just merged main. If you don't mind, could you retry with the latest merge, and see if you can still reproduce? I haven't been able to repro.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

4 participants