Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: react Deferred component error on partial visits #2223

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions packages/react/src/Deferred.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ReactElement, useEffect, useState } from 'react'
import { ReactElement, useEffect, useMemo, useState } from 'react'
import { router } from '.'
import usePage from './usePage'

interface DeferredProps {
Expand All @@ -14,7 +15,22 @@ const Deferred = ({ children, data, fallback }: DeferredProps) => {

const [loaded, setLoaded] = useState(false)
const pageProps = usePage().props
const keys = Array.isArray(data) ? data : [data]
const keys = useMemo(() => (Array.isArray(data) ? data : [data]), [data])

useEffect(() => {
const removeListener = router.on('start', (e) => {
if (
(e.detail.visit.only.length === 0 && e.detail.visit.except.length === 0) ||
e.detail.visit.only.find((key) => keys.includes(key))
) {
setLoaded(false)
}
})

return () => {
removeListener()
}
}, [])

useEffect(() => {
setLoaded(keys.every((key) => pageProps[key] !== undefined))
Expand Down
33 changes: 33 additions & 0 deletions packages/react/test-app/Pages/DeferredProps/WithPartialReload.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Deferred, router, usePage } from '@inertiajs/react'

const WithPartialReload = ({ withOnly, withExcept }) => {
const handleTriggerPartialReload = () => {
router.reload({
only: withOnly,
except: withExcept,
})
}

return (
<div>
<Deferred data="users" fallback={<span>Loading...</span>}>
<DeferredUsers />
</Deferred>
<button onClick={handleTriggerPartialReload}>Trigger a partial reload</button>
</div>
)
}

const DeferredUsers = () => {
const props = usePage().props

return (
<div>
{props.users.map((user) => (
<span key={user.id}>{user.name}</span>
))}
</div>
)
}

export default WithPartialReload
46 changes: 46 additions & 0 deletions tests/app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,52 @@ app.get('/deferred-props/page-1', (req, res) => {
)
})

app.get('/deferred-props/with-partial-reload/:mode', (req, res) => {
if (!req.headers['x-inertia-partial-data']) {
return inertia.render(req, res, {
component: 'DeferredProps/WithPartialReload',
deferredProps: {
default: ['users'],
},
props: {
withOnly: (() => {
if (req.params.mode === 'only') {
return ['users']
}

if (req.params.mode === 'only-other') {
return ['other']
}

return []
})(),
withExcept: (() => {
if (req.params.mode === 'except') {
return ['users']
}

if (req.params.mode === 'except-other') {
return ['other']
}

return []
})(),
},
})
}

setTimeout(
() =>
inertia.render(req, res, {
component: 'DeferredProps/WithPartialReload',
props: {
users: req.headers['x-inertia-partial-data']?.includes('users') ? [{ id: 1, name: 'John Doe' }] : undefined,
},
}),
500,
)
})

app.get('/deferred-props/page-2', (req, res) => {
if (!req.headers['x-inertia-partial-data']) {
return inertia.render(req, res, {
Expand Down
51 changes: 51 additions & 0 deletions tests/deferred-props.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,54 @@ test('props will re-defer if a link is clicked to go to the same page again', as
await expect(page.getByText('foo value')).toBeVisible()
await expect(page.getByText('bar value')).toBeVisible()
})

const shoulReload = ['only']

shoulReload.forEach((type) => {
test(`it will handle partial reloads properly when deferred is being reloaded (${type})`, async ({ page }) => {
test.skip(process.env.PACKAGE !== 'react', 'React only test')

await page.goto(`/deferred-props/with-partial-reload/${type}`)

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

await page.waitForResponse(page.url())

await expect(page.getByText('Loading...')).not.toBeVisible()
await expect(page.getByText('John Doe')).toBeVisible()

const responsePromise = page.waitForResponse(page.url())

await page.getByRole('button', { exact: true, name: 'Trigger a partial reload' }).click()
await expect(page.getByText('Loading...')).toBeVisible()

await responsePromise

await expect(page.getByText('John Doe')).toBeVisible()
})
})

const noReload = ['except', 'only-other', 'none', 'except-other']

noReload.forEach((type) => {
test(`it will handle partial reloads properly when deferred is not reloaded (${type})`, async ({ page }) => {
test.skip(process.env.PACKAGE !== 'react', 'React only test')

await page.goto(`/deferred-props/with-partial-reload/${type}`)

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

await page.waitForResponse(page.url())

await expect(page.getByText('Loading...')).not.toBeVisible()
await expect(page.getByText('John Doe')).toBeVisible()

const responsePromise = page.waitForResponse(page.url())
await page.getByRole('button', { exact: true, name: 'Trigger a partial reload' }).click()
await expect(page.getByText('Loading...')).not.toBeVisible()

await responsePromise

await expect(page.getByText('John Doe')).toBeVisible()
})
})
Loading