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

feat: $props.id(), a SSR-safe ID generation #15185

Merged
merged 20 commits into from
Feb 11, 2025
Merged

Conversation

adiguba
Copy link
Contributor

@adiguba adiguba commented Feb 2, 2025

A work and progress implementation for #7517 (SSR-safe ID generation)

It introduce a new constant $$uid, unique for each instance of a component, based on this comment from dummdidumm (edit: sorry for the tag on sunday : it was not intentional) : #7517 (comment)

The id will be generated by a simple counter, prefixed by s on server side, and c on client side in order to avoid conflict.
On server side, a comment like <!--#s100--> will be generated in order to "pass" the ID for hydrating the client.

<div id="{$$uid}-title">The title</div>

<label for="{$$uid}-firstname">First Name: </label>
<input type="text" id="{$$uid}-firstname" />

<label for="{$$uid}-lastname">Last Name: </label>
<input type="text" id="{$$uid}-lastname" />

<div>My UID is {$$uid}</div>

Note :

  • I'm not sure if this syntax is acceptable, or if a runes $uid() would be more appropriate.
  • I haven't done any testing yet, but I could take care of it if the proposal is accepted.
  • Currently $$uid can be used in a svelte.js/ts file, but this causes an error at runtime. I don't known how to handle this...

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

Sorry, something went wrong.

Copy link

changeset-bot bot commented Feb 2, 2025

🦋 Changeset detected

Latest commit: adbcbd7

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

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

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

Copy link
Contributor

github-actions bot commented Feb 2, 2025

Playground

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

@Ocean-OS
Copy link
Contributor

Ocean-OS commented Feb 2, 2025

I like this, but I'm not sure if it should just be a function imported from the svelte module. The $$stuff syntax reminds me of a relic from Svelte 3 and 4. But then again, this does feel more integrated with your component than something like this...

<script>
import {createUID} from 'svelte';
let uid = createUID();
</script>

However, I also don't think it should be a rune, as not everything should be a rune; in my opinion, if it can be easily recreated while still writing less code, it doesn't need to be a rune.

@dummdidumm
Copy link
Member

This should be either a rune or regular function - not sure which one makes more sense. I lean towards function.

@adiguba
Copy link
Contributor Author

adiguba commented Feb 3, 2025

A regular function may not be possible, as it need to generate an hydration comment.

So do I adapt it into a rune ?
Something like :

$uid(); // return "s100"
$uid('title'); // return "s100-title"

@Ocean-OS
Copy link
Contributor

Ocean-OS commented Feb 3, 2025

A regular function may not be possible, as it need to generate an hydration comment.

So do I adapt it into a rune ? Something like :

$uid(); // return "s100"
$uid('title'); // return "s100-title"

I'm not sure if it should be an independent rune, or if a rune called like $server or sth should be added to group any new features that get added that relate to this sort of feature, with this feature being put under $server.uid, for example.
However, I would still want to try to keep this as a regular function if possible.

@Rich-Harris
Copy link
Member

What about $props.id() or something? That way it's clear that it's a once-per-component thing, and that it doesn't work in .svelte.js files

@adiguba
Copy link
Contributor Author

adiguba commented Feb 3, 2025

Yes an $props.id() seem a good solution to me :

<script>
    const uid = $props.id();
</script>

<label for="{uid}-firstname">First Name: </label>
<input type="text" id="{uid}-firstname" />

<label for="{uid}-lastname">Last Name: </label>
<input type="text" id="{uid}-lastname" />

I will try to implement it

@adiguba
Copy link
Contributor Author

adiguba commented Feb 3, 2025

Demo here : REPL

The following code :

<script>
    const uid = $props.id();
</script>

Will be compiled to something like that in server-side :

const uid = $.props_id($$payload);

The function $.props_id() will generate an unique id, and write a comment like <!--#xxx--> for hydration.

On client-side, this code will be compiled as :

const uid = $.props_id();

The function $.props_id() will read the server's unique id on hydration mode, or generate a new value otherwise.

@webJose
Copy link
Contributor

webJose commented Feb 3, 2025

Why start in 100? Start with 1. Less characters, less bytes to transfer.

@Rich-Harris Rich-Harris changed the title feat: $$uid, a SSR-safe ID generation feat: $props.id(), a SSR-safe ID generation Feb 5, 2025
@svelte-docs-bot
Copy link

Copy link
Member

@Rich-Harris Rich-Harris left a comment

Choose a reason for hiding this comment

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

made some small tweaks and added some docs and I think this is ready to go. Thank you @adiguba! Great feature

@adiguba
Copy link
Contributor Author

adiguba commented Feb 5, 2025

Nice !

@david-plugge
Copy link

It seems like this can only be used within the components code. Lets say i want to build a class within select.svelte.ts that implements the logic for a custom select component. Could i use$props.id() in that class IF it would be invoced on component initialization?

@adiguba
Copy link
Contributor Author

adiguba commented Feb 6, 2025

No you cannot use it on *.svelte.js
props.id() add a hydration comment on the generated code, so it need to be present on the init script of the component.

But you can just pass it as param :

// select.svelte.js

export function init(uid) {
   ...
}
<script>
   import { init } from './select.svelte.js'

   const uid = $props.id();
   init(uid);
</script>

@adiguba
Copy link
Contributor Author

adiguba commented Feb 6, 2025

I just added a small example code in the doc...


## `$props.id()`

This rune, added in version 5.20.0, generates an ID that is unique to the current component instance. When hydrating a server-rendered a component, the value will be consistent between server and client.
Copy link
Contributor

Choose a reason for hiding this comment

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

- When hydrating a server-rendered a component
+ When hydrating a server-rendered component

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I pushed the change.
But I'm not sure if it's the correct way to accept the contribution

Copy link
Contributor

Choose a reason for hiding this comment

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

All good, just noticed it. Thanks for updating it 👍

@@ -339,6 +339,15 @@ declare namespace $effect {
declare function $props(): any;

declare namespace $props {
/**
* Generates an ID that is unique to the current component instance. When hydrating a server-rendered a component,
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

@Rich-Harris Rich-Harris merged commit 85f83ec into sveltejs:main Feb 11, 2025
9 checks passed
@Rich-Harris
Copy link
Member

Just realised we never merged/released this, oops! Happening now. Thanks @adiguba

@Rican7
Copy link
Contributor

Rican7 commented Feb 15, 2025

Wow, fantastic job on this!

It's all well communicated and considered, with a nice useful scope, good docs, and it's also just a simple to understand feature. 👏🙌

@cschroeter
Copy link

@adiguba @Rich-Harris

Great feature! One minor downside for DX: it's no longer possible to assign $props() to a variable named props, as it now needs a different name:

  const props: AccordionRootProps = $props() // No longer possible  
  const id = $props.id()
  const rootProps: AccordionRootProps = $props() // Works  
  const id = $props.id()

@paoloricciuti
Copy link
Member

@adiguba @Rich-Harris

Great feature! One minor downside for DX: it's no longer possible to assign $props() to a variable named props, as it now needs a different name:

  const props: AccordionRootProps = $props() // No longer possible  
  const id = $props.id()
  const rootProps: AccordionRootProps = $props() // Works  
  const id = $props.id()

This code works at runtime...the error should just be from the language server but that's already been fixed (we just need to release the new version)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet