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

fix: reduce the number of API requests when changing route #14666

Merged
merged 2 commits into from
Dec 16, 2024

Conversation

martabal
Copy link
Member

Reduce the number of API requests when changing route by storing the already stored value to a store. (maybe there's a better way with svelte 5?)

Example with navigating from /photos to /utilities

Before After
image No request

@alextran1502
Copy link
Contributor

We probably should use the rune syntax for these since store syntax will be deprecated in the future iirc

@danieldietzler
Copy link
Member

Yup, this should be some global state :)

@martabal martabal force-pushed the fix/reduce-api-request-on-route-changed branch from bd4a3b9 to c042f71 Compare December 12, 2024 15:05
@martabal
Copy link
Member Author

@alextran1502 I don't think stores will be deprecated

@danieldietzler What's the svelte way for global state (classes, functions...)? I didn't find any example in Immich codebase.

@danieldietzler
Copy link
Member

#14662 could be a good inspiration?

@michelheusschen
Copy link
Contributor

Stores are indeed not deprecated. In this situation you can just replace writable() with $state() and make sure the file ends with .svelte or .svelte.ts

We do have to be careful about persisting state globally, because a user could log in with a different account. Data can also become stale after a certain period.

This PR also fixes #14509

@martabal martabal force-pushed the fix/reduce-api-request-on-route-changed branch from c042f71 to fa5ec6c Compare December 12, 2024 16:52
@alextran1502
Copy link
Contributor

The userInteraction global state should be cleared when logging out, correct?

@martabal martabal force-pushed the fix/reduce-api-request-on-route-changed branch from fa5ec6c to e86198a Compare December 14, 2024 19:57
@martabal
Copy link
Member Author

The userInteraction global state should be cleared when logging out, correct?

Indeed!

@martabal martabal force-pushed the fix/reduce-api-request-on-route-changed branch from e86198a to aabe2ac Compare December 14, 2024 20:09
Copy link
Member

@danieldietzler danieldietzler left a comment

Choose a reason for hiding this comment

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

Code looks good to me

@@ -4,13 +4,19 @@
import { getAllAlbums, type AlbumResponseDto } from '@immich/sdk';
import { handleError } from '$lib/utils/handle-error';
import { t } from 'svelte-i18n';
import { userInteraction } from '$lib/stores/user.svelte';

let albums: AlbumResponseDto[] = $state([]);
Copy link
Member

Choose a reason for hiding this comment

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

You could completely drop this variable and replace it with the global userInteraction.recentAlbums state

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean dropping let albums: AlbumResponseDto[] = $state([]); in favor of using only userInteraction.recentAlbums? If so, you have to wrap the each section in a {#if userInteraction.recentAlbums} because userInteraction.recentAlbums can be undefined. What do you prefer?

Copy link
Member

Choose a reason for hiding this comment

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

Can it be undefined? You'd still set it in onMount if it's undefined, and onMount finishes before it actually mounts, right?

Copy link
Member

Choose a reason for hiding this comment

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

Or is my svelte knowledge lacking here? 😅

Copy link
Member Author

@martabal martabal Dec 15, 2024

Choose a reason for hiding this comment

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

Yes, technically it can't be undefined, but Svelte isn't smart enough to figure that out. It only uses the type definition

image

interface UserInteractions {
  recentAlbums?: AlbumResponseDto[];
  versions?: ServerVersionHistoryResponseDto[];
  aboutInfo?: ServerAboutResponseDto;
  serverInfo?: ServerStorageResponseDto;
}

Copy link
Member

Choose a reason for hiding this comment

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

Ah gotcha. Then I'd prefer keeping that extra variable as well

@martabal martabal force-pushed the fix/reduce-api-request-on-route-changed branch from b15f4a5 to cc27ea5 Compare December 15, 2024 19:56
@martabal martabal force-pushed the fix/reduce-api-request-on-route-changed branch from cc27ea5 to 7cf07fe Compare December 15, 2024 19:57
@alextran1502 alextran1502 merged commit 8945a5d into main Dec 16, 2024
36 checks passed
@alextran1502 alextran1502 deleted the fix/reduce-api-request-on-route-changed branch December 16, 2024 14:45
yosit pushed a commit to yosit/immich that referenced this pull request Dec 20, 2024
…p#14666)

* fix: reduce the number of API requests when changing route

* fix: reset `userInteraction` after sign out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants