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

[2.x] Fix <Deferred /> in Svelte adapter #2037

Merged
merged 3 commits into from
Oct 18, 2024
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
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
15 changes: 14 additions & 1 deletion packages/svelte/src/components/Deferred.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
<script lang="ts">
import { page } from '../index'
import { onDestroy } from 'svelte'

export let data: string | string[]

const keys = Array.isArray(data) ? data : [data]
let loaded = false

const unsubscribe = page.subscribe(({ props }) => {
// Ensures the slot isn't loaded before the deferred props are available
window.queueMicrotask(() => {
loaded = keys.every((key) => typeof props[key] !== 'undefined')
})
})

onDestroy(() => {
unsubscribe()
})

if (!$$slots.fallback) {
throw new Error('`<Deferred>` requires a `<svelte:fragment slot="fallback">` slot')
}
</script>

{#if keys.every((key) => $page.props[key] !== undefined)}
{#if loaded}
<slot />
{:else}
<slot name="fallback" />
Expand Down
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