-
Notifications
You must be signed in to change notification settings - Fork 0
perf: lazy-load cache-manager to reduce initial bundle size #256
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /** | ||
| * Prefetch Module - Idle-time module preloading | ||
| * | ||
| * Uses requestIdleCallback to preload heavy modules during browser idle time. | ||
| * This ensures the modules are cached and ready when actually needed, | ||
| * without blocking initial page load. | ||
| */ | ||
|
|
||
| import { logger } from "./logger-client"; | ||
|
|
||
| // Track which modules have been prefetched | ||
| const prefetchedModules = new Set<string>(); | ||
|
|
||
| /** | ||
| * Prefetch a module during browser idle time | ||
| * @param moduleLoader - Dynamic import function | ||
| * @param name - Module name for logging | ||
| */ | ||
| function prefetchModule(moduleLoader: () => Promise<unknown>, name: string): void { | ||
| if (prefetchedModules.has(name)) return; | ||
| prefetchedModules.add(name); | ||
|
|
||
| const callback = () => { | ||
| moduleLoader() | ||
| .then(() => logger.debug(`Prefetched module: ${name}`)) | ||
| .catch(() => logger.debug(`Failed to prefetch: ${name}`)); | ||
| }; | ||
|
|
||
| // Use requestIdleCallback if available, otherwise setTimeout | ||
| if ("requestIdleCallback" in window) { | ||
| requestIdleCallback(callback, { timeout: 5000 }); | ||
|
Comment on lines
+30
to
+31
|
||
| } else { | ||
| setTimeout(callback, 2000); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Start prefetching heavy modules after initial page load | ||
| * Called once after app mounts and becomes interactive | ||
| */ | ||
| export function startIdlePrefetch(): void { | ||
| // Wait a bit for initial render to complete | ||
| setTimeout(() => { | ||
| // Prefetch cache-manager (contains music-metadata ~100KB) | ||
| // Needed for: upload, library delete | ||
| prefetchModule( | ||
| () => import("@/lib/pwa/cache-manager"), | ||
| "cache-manager" | ||
| ); | ||
| }, 3000); // 3 seconds after load | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The startIdlePrefetch function is called unconditionally in a useEffect with an empty dependency array, which means it will be called every time the AuthProvider mounts. However, the prefetchModule function already has a guard (prefetchedModules Set) that prevents duplicate prefetching. This guard won't work correctly if the module is ever unmounted and remounted because startIdlePrefetch() creates a new setTimeout each time. Consider either: 1) ensuring startIdlePrefetch itself checks if prefetching is already in progress/completed, or 2) documenting that the guard in prefetchModule is intentional and sufficient.