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

Input resets when page fully loads #8266

Closed
nkitsaini opened this issue Feb 6, 2023 · 9 comments
Closed

Input resets when page fully loads #8266

nkitsaini opened this issue Feb 6, 2023 · 9 comments
Labels

Comments

@nkitsaini
Copy link

Describe the bug

If you load a page which contains an input element with value binded to a variable, the input will reset all the user edits when page load finishes.

# Timeline
- Page starts loading
- `input` becomes visible on screen
- user edits the `input` to some value
- Page finishes loading. Edits are gone.

I'm not sure if this falls exactly into svelte-kit and will this be considered a bug or not. But it sure is little annoying sometimes.

Reproduction

Repo: https://github.com/nkitsaini/svelte-bug-repro
Video: https://github.com/nkitsaini/svelte-bug-repro/raw/main/repro.mkv
Snippet:

<script lang="ts">
	let a = "234"
</script>

<input bind:value={a}>

Logs

-- No Logs --

System Info

System:
    OS: Linux 6.1 Arch Linux
    CPU: (16) x64 AMD Ryzen 7 5825U with Radeon Graphics
    Memory: 30.83 GB / 38.02 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 19.6.0 - /usr/bin/node
    npm: 8.19.2 - /usr/bin/npm
  Browsers:
    Chromium: 109.0.5414.119
    Firefox: 109.0.1
  npmPackages:
    @sveltejs/adapter-auto: ^1.0.0 => 1.0.2
    @sveltejs/kit: ^1.0.0 => 1.3.10
    svelte: ^3.54.0 => 3.55.1
    vite: ^4.0.0 => 4.1.1

Severity

annoyance

Additional Information

No response

@dummdidumm
Copy link
Member

This sounds like a timing issue - hydration happens too late, so it clears out the value the user has already typed in. This is not strictly related to SvelteKit, more a general limitation of the current Svelte hydration.

@dummdidumm dummdidumm added the bug label Feb 6, 2023
@Rich-Harris
Copy link
Member

Rich-Harris commented Feb 6, 2023

A decent workaround is to disable the input until hydration has completed:

<script lang="ts">
+	import { browser } from '$app/environment';
	let a = "234"
</script>

-<input bind:value={a}>
+<input disabled={!browser} bind:value={a}>

Ultimately it would be better if the binding was initialized with the input value — not sure if that would cause other problems, but I'd like it if we could do that in a future version of Svelte.

@nkitsaini
Copy link
Author

nkitsaini commented Feb 8, 2023

Yeah, the suggested workaround is good enough for my use-case. It is interesting that browser is false until hydration.

@Rich-Harris
Copy link
Member

Transferring to sveltejs/svelte since there's nothing we can do here

@Rich-Harris Rich-Harris transferred this issue from sveltejs/kit Feb 8, 2023
@adiguba
Copy link
Contributor

adiguba commented Mar 6, 2023

Svelte could perhaps check the value of the binding just after hydration ?

Something like this approximately :

	input = claim_element(nodes, "INPUT", {});
	
	// When input is different from the context value
	if (input.value !== /*a*/ ctx[0]) {
		// We use the binding handler to update it's value
		/*input_input_handler*/ ctx[1].call(input).
	}

@rmunn
Copy link
Contributor

rmunn commented Jul 10, 2023

Probably a duplicate of #1755, which tells you how long this problem has been around.

@Gerschtli
Copy link

@Rich-Harris Ultimately it would be better if the binding was initialized with the input value — not sure if that would cause other problems, but I'd like it if we could do that in a future version of Svelte.

Did you have a look into this idea already? I think it would be a good thing to implement because this issue is indeed quite annoying and disabling the input until JS loads also seems like a unnecessary delay.

Would be great to have at least the option to enable that via svelte without some hacky workarounds. What do you think? Would love to support you there on that topic, despite svelte code base being new for me :)

@enyo
Copy link

enyo commented Nov 17, 2023

A decent workaround is to disable the input until hydration has completed

@Rich-Harris Now that we have nice forms that work without JavaScript via use:enhance it's a pity that this is the solution, since it renders SSR forms kinda useless. Hopefully this is something that can be resolved in the future by hydrating with the correct values.

@Rich-Harris
Copy link
Member

closing as dupe of #1755

@Rich-Harris Rich-Harris closed this as not planned Won't fix, can't repro, duplicate, stale Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants