Skip to content

Commit

Permalink
fix for page loading from history initially instead of server
Browse files Browse the repository at this point in the history
  • Loading branch information
joetannenbaum committed Oct 11, 2024
1 parent 729a4d6 commit a756a1b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/core/src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class History {
public preserveUrl = false
protected current: Partial<Page> = {}
protected queue: (() => Promise<void>)[] = []
// We need initialState for `restore`
protected initialState: Partial<Page> | null = null

public remember(data: unknown, key: string): void {
this.replaceState({
Expand All @@ -24,7 +26,7 @@ class History {

public restore(key: string): unknown {
if (!isServer) {
return this.getState<{ [key: string]: any }>(this.rememberedState, {})?.[key]
return this.initialState?.[this.rememberedState]?.[key]
}
}

Expand Down Expand Up @@ -77,7 +79,11 @@ class History {
throw new Error('Unable to decrypt history')
}

this.current = data ?? {}
if (this.initialState === null) {
this.initialState = data ?? undefined
} else {
this.current = data ?? {}
}

return data
})
Expand Down
26 changes: 26 additions & 0 deletions tests/deferred-props.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,29 @@ test('can load deferred props', async ({ page }) => {
await expect(page.getByText('qux value')).toBeVisible()
await expect(page.getByText('both baz value and qux value')).toBeVisible()
})

test('we are not caching deferred props after reload', async ({ page }) => {
await page.goto('/deferred-props/page-1')

await expect(page.getByText('Loading foo...')).toBeVisible()
await expect(page.getByText('Loading bar...')).toBeVisible()

await page.waitForResponse(page.url())

await expect(page.getByText('Loading foo...')).not.toBeVisible()
await expect(page.getByText('Loading bar...')).not.toBeVisible()
await expect(page.getByText('foo value')).toBeVisible()
await expect(page.getByText('bar value')).toBeVisible()

await page.reload()

await expect(page.getByText('Loading foo...')).toBeVisible()
await expect(page.getByText('Loading bar...')).toBeVisible()

await page.waitForResponse(page.url())

await expect(page.getByText('Loading foo...')).not.toBeVisible()
await expect(page.getByText('Loading bar...')).not.toBeVisible()
await expect(page.getByText('foo value')).toBeVisible()
await expect(page.getByText('bar value')).toBeVisible()
})

0 comments on commit a756a1b

Please sign in to comment.