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

[1.x] Fix <Render /> component to respect "preserveState" #1943

Merged
merged 4 commits into from
Sep 16, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ For changes prior to v1.0.0, see the [legacy releases](https://legacy.inertiajs.
- Fix form helper `transform` return type in React adapter ([#1896](https://github.com/inertiajs/inertia/pull/1896))
- Use updater function in `setData` in `useForm` hook in React adapter ([#1859](https://github.com/inertiajs/inertia/pull/1859))
- Skip intercepting non-left button clicks on links ([#1908](https://github.com/inertiajs/inertia/pull/1908), [#1910](https://github.com/inertiajs/inertia/pull/1910))
- [Svelte] Fix `<Render />` component to respect `preserveState` option ([#1943](https://github.com/inertiajs/inertia/pull/1943))
- [Svelte] Fix 'received an unexpected slot "default"' warning ([#1941](https://github.com/inertiajs/inertia/pull/1941))

## [v1.2.0](https://github.com/inertiajs/inertia/compare/v1.1.0...v1.2.0)
Expand Down
15 changes: 6 additions & 9 deletions packages/svelte/src/lib/components/Render.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,17 @@
export let component: ComponentType
export let props: PageProps = {}
export let children: RenderProps[] = []

let prevComponent: ComponentType
let key: number
$: if (prevComponent !== component) {
key = Date.now()
prevComponent = component
}
</script>

{#if $store.component}
{#key key}
<!--
Add the `key` only to the last (page) component in the tree.
This ensures that the page component re-renders when `preserveState` is disabled,
while the layout components are persisted across page changes. -->
{#key children?.length === 0 ? $store.key : null}
{#if children.length > 0}
<svelte:component this={component} {...props}>
{#each children as child, index (component && component.length === index ? $store.key : null)}
{#each children as child}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since children always contains a single element, there's no need to use a key in the #each block in this case.

<svelte:self {...child} />
{/each}
</svelte:component>
Expand Down
Loading