Skip to content

Commit

Permalink
fix: change url right before rendering new page
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Apr 5, 2021
1 parent 1ddbddb commit 97530f0
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/client/router/useClientRouter.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@ function useClientRouter({
autoSaveScrollPosition()

onLinkClick((url: string) => {
changeUrl(url)
fetchAndRender(null)
fetchAndRender(null, url)
})
onBrowserHistoryNavigation((scrollPosition) => {
fetchAndRender(scrollPosition)
})
navigateFunction = async (url: string) => {
changeUrl(url)
await fetchAndRender(null)
await fetchAndRender(null, url)
}

let resolveInitialPagePromise: () => void
Expand All @@ -56,7 +54,10 @@ function useClientRouter({

return { hydrationPromise }

async function fetchAndRender(scrollPosition?: ScrollPosition | null): Promise<undefined> {
async function fetchAndRender(
scrollPosition: ScrollPosition | null | undefined = undefined,
url: string = getUrl()
): Promise<undefined> {
const callNumber = ++callCount

if (!isFirstPageRender) {
Expand All @@ -66,8 +67,7 @@ function useClientRouter({
}
}

const urlCurrent = getUrl()
const { Page, pageProps } = await getPageByUrl(urlCurrent, navigationState.noNavigationChangeYet)
const { Page, pageProps } = await getPageByUrl(url, navigationState.noNavigationChangeYet)

if (renderPromise) {
// Always make sure that the previous render has finished,
Expand All @@ -80,12 +80,11 @@ function useClientRouter({
return
}

assert(urlCurrent === getUrl())
const isHydration = isFirstPageRender && urlCurrent === urlOriginal

changeUrl(url)
navigationState.markNavigationChange()
assert(renderPromise === undefined)
renderPromise = (async () => {
await render({ Page, pageProps, isHydration })
await render({ Page, pageProps, isHydration: isFirstPageRender && url === urlOriginal })
})()
await renderPromise
renderPromise = undefined
Expand Down Expand Up @@ -185,7 +184,6 @@ function onBrowserHistoryNavigation(callback: (scrollPosition: ScrollPosition |

function changeUrl(url: string) {
if (getUrl() === url) return
navigationState.markNavigationChange()
window.history.pushState(undefined, '', url)
}

Expand Down

0 comments on commit 97530f0

Please sign in to comment.