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

Svelte fix when using deferred props #2026

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions packages/react/test-app/Pages/DeferredProps/Page1.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { Deferred, Link, usePage } from '@inertiajs/react'
const Foo = () => {
const { foo } = usePage().props

return foo
return foo.text
}

const Bar = () => {
const { bar } = usePage().props

return bar
return bar.text
}

export default () => {
Expand Down
12 changes: 11 additions & 1 deletion packages/svelte/src/page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { derived } from 'svelte/store'
import store from './store'

const page = derived(store, ($store) => $store.page)
let initialized = false
const page = derived(store, ($store, set) => {
// This ensures that page props and the $page store are updated
// in the same render, such as when using deferred props.
if (initialized && typeof window !== 'undefined') {
window.queueMicrotask(() => set($store.page))
return
}
set($store.page)
initialized = true
})

export default page
4 changes: 2 additions & 2 deletions packages/svelte/test-app/Pages/DeferredProps/Page1.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
<svelte:fragment slot="fallback">
<div>Loading foo...</div>
</svelte:fragment>
{foo}
{foo.text}
</Deferred>

<Deferred data="bar">
<svelte:fragment slot="fallback">
<div>Loading bar...</div>
</svelte:fragment>
{bar}
{bar.text}
</Deferred>

<a href="/deferred-props/page-2" use:inertia>Page 2</a>
8 changes: 4 additions & 4 deletions packages/vue3/test-app/Pages/DeferredProps/Page1.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import { Deferred, Link } from '@inertiajs/vue3'

defineProps<{
foo?: string
bar?: string
foo?: {text: string }
bar?: {text: string }
}>()
</script>

Expand All @@ -12,14 +12,14 @@ defineProps<{
<template #fallback>
<div>Loading foo...</div>
</template>
{{ foo }}
{{ foo?.text }}
</Deferred>

<Deferred data="bar">
<template #fallback>
<div>Loading bar...</div>
</template>
{{ bar }}
{{ bar?.text }}
</Deferred>

<Link href="/deferred-props/page-2">Page 2</Link>
Expand Down
4 changes: 2 additions & 2 deletions tests/app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ app.get('/deferred-props/page-1', (req, res) => {
inertia.render(req, res, {
component: 'DeferredProps/Page1',
props: {
foo: req.headers['x-inertia-partial-data']?.includes('foo') ? 'foo value' : undefined,
bar: req.headers['x-inertia-partial-data']?.includes('bar') ? 'bar value' : undefined,
foo: req.headers['x-inertia-partial-data']?.includes('foo') ? { text: 'foo value' } : undefined,
bar: req.headers['x-inertia-partial-data']?.includes('bar') ? { text: 'bar value' } : undefined,
},
}),
500,
Expand Down
Loading