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

Conversation

pedroborges
Copy link
Collaborator

@pedroborges pedroborges commented Aug 13, 2024

Issues with the page component rendering in the Svelte adapter have been reported a couple time (see: #1621, #1733). The last attempt to solve this (#1763) created another issue because it doesn't respect preserveState and reuses page components even when preserveState is set to false. This PR fixes this issue.

I have updated the Svelte playground with a reproducible to help me find a solution and also help the reviewer. We can revert these changes to the playground before merging this PR.

Before

Navigating between /foo and /bar, which use the same page component, doesn't refresh the page component, but only updates its props. The same page component is reused instead of being destroyed and a new one being mounted.

Screen Shot 2024-08-13 at 16 03 28

After

Navigating between /foo and /bar will use a fresh instance of the page component each time, see the "mounted" and "destroyed" logs.

Screen Shot 2024-08-13 at 16 13 54

This PR still doesn't fully solve the issue described in #1670. Notice on the blue arrows on the "After" screenshot that in some cases the component prop is updated shortly after the component is initialized. In my tests, this only happened when I tried to use a prop inside the <script> block. Using props in reactive expressions and lifecycle hooks such as onMount works just fine.

<script>
  import { useForm } from '@inertiajs/svelte'

  export let name

  const form = useForm({
    'name': name,
  })
</script>

After a lot of debugging, I found out that this happens because we are using a Svelte store to persist component and props state. When the store is updated their listeners can be slightly out-of-sync. This causes the internal <App /> component to pass outdated props down to the component tree.

I have a working solution and will open a separate PR to fix that because there might be a small breaking change.

@pedroborges pedroborges added the svelte Related to the svelte adapter label Aug 13, 2024
@pedroborges pedroborges changed the title Fix <Render /> component to respect "preserveState" [Svelte] Fix <Render /> component to respect "preserveState" Sep 10, 2024
@pedroborges pedroborges changed the title [Svelte] Fix <Render /> component to respect "preserveState" [2.x] Fix <Render /> component to respect "preserveState" Sep 13, 2024
@pedroborges pedroborges changed the title [2.x] Fix <Render /> component to respect "preserveState" [1.x] Fix <Render /> component to respect "preserveState" Sep 13, 2024
# Conflicts:
#	packages/svelte/src/lib/components/Render.svelte

# Conflicts:
#	packages/svelte/src/lib/components/Render.svelte
{#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.

@pedroborges pedroborges merged commit 5cd786d into master Sep 16, 2024
7 checks passed
@pedroborges pedroborges deleted the fix-svelte-render branch September 16, 2024 23:45
pedroborges added a commit to olragon/inertia that referenced this pull request Sep 24, 2024
* master: (95 commits)
  [1.x] Fix props reactivity (inertiajs#1969)
  [1.x] useForm wrongly overwrites default values ​​after successful submission (inertiajs#1935)
  Update changelog
  [1.x] Fix `resetScrollPositions` and `restoreScrollPositions` router methods (inertiajs#1980)
  [1.x] Fix [scroll-region] on html element with overflow-scroll (inertiajs#1782)
  [1.x] Fix useForm re-renders by memoizing functions in React (inertiajs#1607)
  [1.x] Fix "DataCloneError: <Object> could not be cloned" (inertiajs#1967)
  [1.x] preserveScroll should be true on initial page visit (inertiajs#1360)
  Fix type augmentation (inertiajs#1958)
  [1.x] Fix doubling hash in React StrictMode (inertiajs#1728)
  [1.x] Add SSR support for Svelte 5 (inertiajs#1970)
  [1.x] Fix <Render /> component to respect "preserveState" (inertiajs#1943)
  [1.x] Fix 'received an unexpected slot "default"' warning (inertiajs#1941)
  QA: Add @types/lodash to fix svelte-check
  QA: Update reactive if statement
  Review useForm types
  QA: Move the if server up
  QA: Revert tsconfig change
  QA: Remove plural
  QA: Remove unused props type + add extra types just in case
  ...

# Conflicts:
#	packages/react/src/index.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
svelte Related to the svelte adapter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant