Skip to content

Commit

Permalink
load deferred props on every page swap
Browse files Browse the repository at this point in the history
  • Loading branch information
joetannenbaum committed Oct 11, 2024
1 parent 88a765b commit 488d66a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
5 changes: 2 additions & 3 deletions packages/core/src/eventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { fireNavigateEvent } from './events'
import { history } from './history'
import { page as currentPage } from './page'
import { Scroll } from './scroll'
import { GlobalEvent, GlobalEventNames, GlobalEventResult } from './types'
import { GlobalEvent, GlobalEventNames, GlobalEventResult, InternalEvent } from './types'
import { hrefToUrl } from './url'

type InternalEvent = 'missingHistoryItem'
class EventHandler {
protected internalListeners: {
event: InternalEvent
Expand Down Expand Up @@ -54,7 +53,7 @@ class EventHandler {
this.fireInternalEvent('missingHistoryItem')
}

protected fireInternalEvent(event: InternalEvent): void {
public fireInternalEvent(event: InternalEvent): void {
this.internalListeners.filter((listener) => listener.event === event).forEach((listener) => listener.listener())
}

Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/page.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { eventHandler } from './eventHandler'
import { fireNavigateEvent } from './events'
import { history } from './history'
import { Scroll } from './scroll'
Expand Down Expand Up @@ -82,6 +83,8 @@ class CurrentPage {
Scroll.reset(page)
}

eventHandler.fireInternalEvent('loadDeferredProps')

if (!replace) {
fireNavigateEvent(page)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class Router {
}
})

eventHandler.onGlobalEvent('navigate', () => {
eventHandler.on('loadDeferredProps', () => {
this.loadDeferredProps()
})
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ export type GlobalEventCallback<TEventName extends GlobalEventNames> = (
...params: GlobalEventParameters<TEventName>
) => GlobalEventResult<TEventName>

export type InternalEvent = 'missingHistoryItem' | 'loadDeferredProps'

export type VisitCallbacks = {
onCancelToken: { ({ cancel }: { cancel: VoidFunction }): void }
onBefore: GlobalEventCallback<'before'>
Expand Down
1 change: 1 addition & 0 deletions packages/vue3/test-app/Pages/DeferredProps/Page1.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ defineProps<{
{{ bar }}
</Deferred>

<Link href="/deferred-props/page-1">Page 1</Link>
<Link href="/deferred-props/page-2">Page 2</Link>
</template>
26 changes: 26 additions & 0 deletions tests/deferred-props.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,29 @@ test('we are not caching deferred props after reload', async ({ page }) => {
await expect(page.getByText('foo value')).toBeVisible()
await expect(page.getByText('bar value')).toBeVisible()
})

test('props will re-defer if a link is clicked to go to the same page again', 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 clickAndWaitForResponse(page, 'Page 1', '/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()
})

0 comments on commit 488d66a

Please sign in to comment.