Skip to content

Commit

Permalink
Merge pull request #2098 from inertiajs/deferred-props-with-encryptio…
Browse files Browse the repository at this point in the history
…n-fix

Set the history state before swapping component
  • Loading branch information
joetannenbaum authored Dec 3, 2024
2 parents 80f762d + 86773e9 commit 2d03ebc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
8 changes: 6 additions & 2 deletions packages/core/src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class History {
}
}

public pushState(page: Page): void {
public pushState(page: Page, cb: (() => void) | null = null): void {
if (isServer || this.preserveUrl) {
return
}
Expand All @@ -47,6 +47,8 @@ class History {
'',
page.url,
)

cb && cb()
})
})
}
Expand Down Expand Up @@ -93,7 +95,7 @@ class History {
return pageData instanceof ArrayBuffer ? decryptHistory(pageData) : Promise.resolve(pageData)
}

public replaceState(page: Page): void {
public replaceState(page: Page, cb: (() => void) | null = null): void {
currentPage.merge(page)

if (isServer || this.preserveUrl) {
Expand All @@ -112,6 +114,8 @@ class History {
'',
page.url,
)

cb && cb()
})
})
}
Expand Down
42 changes: 22 additions & 20 deletions packages/core/src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,35 @@ class CurrentPage {
const location = typeof window !== 'undefined' ? window.location : new URL(page.url)
replace = replace || isSameUrlWithoutHash(hrefToUrl(page.url), location)

replace ? history.replaceState(page) : history.pushState(page)
return new Promise((resolve) => {
replace ? history.replaceState(page, () => resolve(null)) : history.pushState(page, () => resolve(null))
}).then(() => {
const isNewComponent = !this.isTheSame(page)

const isNewComponent = !this.isTheSame(page)
this.page = page
this.cleared = false

this.page = page
this.cleared = false

if (isNewComponent) {
this.fireEventsFor('newComponent')
}
if (isNewComponent) {
this.fireEventsFor('newComponent')
}

if (this.isFirstPageLoad) {
this.fireEventsFor('firstLoad')
}
if (this.isFirstPageLoad) {
this.fireEventsFor('firstLoad')
}

this.isFirstPageLoad = false
this.isFirstPageLoad = false

return this.swap({ component, page, preserveState }).then(() => {
if (!preserveScroll) {
Scroll.reset(page)
}
return this.swap({ component, page, preserveState }).then(() => {
if (!preserveScroll) {
Scroll.reset(page)
}

eventHandler.fireInternalEvent('loadDeferredProps')
eventHandler.fireInternalEvent('loadDeferredProps')

if (!replace) {
fireNavigateEvent(page)
}
if (!replace) {
fireNavigateEvent(page)
}
})
})
})
}
Expand Down

0 comments on commit 2d03ebc

Please sign in to comment.