Skip to content

feat: ssr select value #16017

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

Merged
merged 21 commits into from
May 28, 2025
Merged

feat: ssr select value #16017

merged 21 commits into from
May 28, 2025

Conversation

paoloricciuti
Copy link
Member

Closes #7160

Bit of a disclaimer...don't know if we should actually do this but this implementation seems to work. It might be a breaking change if someone was relying on this quirky behaviour for some reason also i don't like the fact that it makes testing a bit harder (since a purely client side renderer component will not have the selected attribute) which is also why I had to update multiple tests.

Let's discuss if we want to merge this or not (also if you spot any glaring issues let me know).

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented May 28, 2025

🦋 Changeset detected

Latest commit: 7832341

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot
Copy link

Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@16017

@Rich-Harris
Copy link
Member

This makes total sense to me, I think it definitely qualifies as a bugfix

@paoloricciuti
Copy link
Member Author

Btw didn't add a test for cross component select->option relation ship but it should work. Should we add one just to be safe?

@Rich-Harris
Copy link
Member

also if you spot any glaring issues let me know

Couple of things:

  • it doesn't handle implicit values<option>foo</option> is equivalent to <option value="foo">foo</option>
  • spreads are handled incorrectly (it only recognises the first spread attribute, disregarding any subsequent spreads or value or bind:value, and it evaluates the attribute a second time which could have consequences)

Should we add one just to be safe?

Yeah, can't hurt

@paoloricciuti
Copy link
Member Author

it doesn't handle implicit values — foo is equivalent to foo

Uh weird I specifically tested this this morning and it was working...will look into that

@paoloricciuti
Copy link
Member Author

Ok i think i fixed everything @Rich-Harris ...the problem with spread was also there for option.

I think there's one other problem which I'm not sure how to fix: what happens if the content of a value-less option is not statically analysable? (i think the only case is if you have a snippet rendered inside but it's cases nonetheless)

@paoloricciuti
Copy link
Member Author

Yeah this is broken right now, let me add a failing test while i try to think of something

@paoloricciuti
Copy link
Member Author

I guess we can try to do it it at runtime tho...let me try

@paoloricciuti
Copy link
Member Author

Ok i think I fixed it...basically if there's a valueless option we do something like this

import * as $ from 'svelte/internal/server';

export default function App($$payload) {
	$$payload.out += `<select>`;
	$$payload.select_value = true;
	$$payload.out += `<option>`;

	const $$body = $.valueless_option($$payload);

	$$body(() => {
		child($$payload);
		$$payload.out += `<!---->`;
	});

	$$payload.out += `</option>`;
	$$payload.select_value = void 0;
	$$payload.out += `</select>`;
}

and the $$body function store the current payload, runs the children, remove the old payload from the payload after child, clean that from eventual comments, and compare the two.

I fully expect 10/15 simplify commit but it should work 😄

@Rich-Harris Rich-Harris merged commit 40e0e33 into main May 28, 2025
13 checks passed
@Rich-Harris Rich-Harris deleted the ssr-select-value branch May 28, 2025 22:33
@github-actions github-actions bot mentioned this pull request May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

<select> element has wrong initial value in SSR
2 participants