Skip to content

Commit

Permalink
Force clear remembered state on page reload
Browse files Browse the repository at this point in the history
  • Loading branch information
reinink committed Nov 24, 2022
1 parent f1e360c commit 769f643
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/inertia/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class Router {
protected resolveComponent!: PageResolver
protected swapComponent!: PageHandler
protected visitOptions!: VisitOptions
protected navigationType?: string
protected activeVisit?: ActiveVisit
protected visitId: VisitId = null

Expand All @@ -33,6 +34,9 @@ export class Router {
this.swapComponent = swapComponent
this.visitOptions = visitOptions

this.setNavigationType()
this.clearRememberedStateOnReload()

if (this.isBackForwardVisit()) {
this.handleBackForwardVisit(this.page)
} else if (this.isLocationVisit()) {
Expand All @@ -44,6 +48,18 @@ export class Router {
this.setupEventListeners()
}

protected setNavigationType(): void {
this.navigationType = (window.performance && window.performance.getEntriesByType('navigation').length > 0)
? (window.performance.getEntriesByType('navigation')[0] as PerformanceNavigationTiming).type
: 'navigate'
}

protected clearRememberedStateOnReload(): void {
if (this.navigationType === 'reload' && window.history.state?.rememberedState) {
delete window.history.state.rememberedState
}
}

protected handleInitialPageVisit(page: Page): void {
this.page.url += window.location.hash
this.setPage(page, { preserveState: true }).then(() => fireNavigateEvent(page))
Expand Down Expand Up @@ -109,10 +125,7 @@ export class Router {
}

protected isBackForwardVisit(): boolean {
return window.history.state
&& window.performance
&& window.performance.getEntriesByType('navigation').length > 0
&& (window.performance.getEntriesByType('navigation')[0] as PerformanceNavigationTiming).type === 'back_forward'
return window.history.state && this.navigationType === 'back_forward'
}

protected handleBackForwardVisit(page: Page): void {
Expand Down

0 comments on commit 769f643

Please sign in to comment.