-
Notifications
You must be signed in to change notification settings - Fork 0
should prevent issues when loading website from cache #171
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
InfinityBowman
merged 3 commits into
main
from
169-safari-unload-page-user-loses-access-to-projects-in-view
Dec 26, 2025
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /** | ||
| * Back-Forward Cache (bfcache) Handler | ||
| * | ||
| * Detects when a page is restored from the browser's back-forward cache | ||
| * and triggers state refresh to ensure auth session and project data are current. | ||
| */ | ||
|
|
||
| import { useBetterAuth } from '@api/better-auth-store.js'; | ||
| import projectStore from '@/stores/projectStore.js'; | ||
|
|
||
| /** | ||
| * Initialize bfcache restoration handler | ||
| * Should be called once on app initialization | ||
| */ | ||
| export function initBfcacheHandler() { | ||
| if (typeof window === 'undefined') return; | ||
|
|
||
| // Get auth instance once (it's a singleton) | ||
| const auth = useBetterAuth(); | ||
|
|
||
| const handlePageshow = async event => { | ||
| // event.persisted === true means the page was restored from bfcache | ||
| if (!event.persisted) return; | ||
|
|
||
| console.log('[bfcache] Page restored from back-forward cache, refreshing state...'); | ||
|
|
||
| // Wait for auth to finish loading if it's currently loading | ||
| // This ensures we have the current user before validating project cache | ||
| if (auth.authLoading()) { | ||
| // Wait for auth to complete (with timeout) | ||
| await new Promise((resolve, reject) => { | ||
| const timeout = setTimeout(() => { | ||
| reject(new Error('Auth loading timeout')); | ||
| }, 5000); | ||
|
|
||
| const checkAuth = () => { | ||
| if (!auth.authLoading()) { | ||
| clearTimeout(timeout); | ||
| resolve(); | ||
| } else { | ||
| setTimeout(checkAuth, 100); | ||
| } | ||
| }; | ||
|
|
||
| checkAuth(); | ||
| }).catch(err => { | ||
| console.warn('[bfcache] Auth loading timeout:', err); | ||
| }); | ||
| } | ||
|
|
||
| // Force refresh the auth session to ensure it's current | ||
| try { | ||
| await auth.forceRefreshSession(); | ||
| } catch (err) { | ||
| console.warn('[bfcache] Failed to refresh auth session:', err); | ||
| } | ||
|
|
||
| // Wait a bit for session to update | ||
| await new Promise(resolve => setTimeout(resolve, 100)); | ||
|
|
||
| // Validate and refresh project list if user is authenticated | ||
| const currentUser = auth.user(); | ||
| if (currentUser?.id) { | ||
| // Validate project list cache against current user | ||
| projectStore.validateProjectListCache(currentUser.id); | ||
|
|
||
| // Refresh project list to ensure it's current | ||
| try { | ||
| await projectStore.refreshProjectList(currentUser.id); | ||
| } catch (err) { | ||
| console.warn('[bfcache] Failed to refresh project list:', err); | ||
| } | ||
| } else { | ||
| // User is not authenticated, clear project list | ||
| projectStore.clearProjectList(); | ||
| } | ||
| }; | ||
|
|
||
| window.addEventListener('pageshow', handlePageshow); | ||
|
|
||
| // Return cleanup function (though typically not needed for singleton) | ||
| return () => { | ||
| window.removeEventListener('pageshow', handlePageshow); | ||
| }; | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.