Skip to content

Commit a05e6ab

Browse files
bugfix(svelte): fix colliding id's generated from $props.id (#13339)
* bugfix(svelte): fix colliding id's generating from $props.id by utilizing the new `uidPrefix` option exposed for both server and client side rendering. * bugfix(svelte): changeset * Add server side ID prefix, not sure how to do this on the client. * Discard changes to packages/integrations/svelte/client.svelte.js * Update .changeset/flat-cherries-rule.md Co-authored-by: Florian Lefebvre <[email protected]> * feat: context * revert peerDep bump * revert html variable seperation * feat: rename * fix lockfile --------- Co-authored-by: Florian Lefebvre <[email protected]>
1 parent 042d1de commit a05e6ab

File tree

5 files changed

+99
-64
lines changed

5 files changed

+99
-64
lines changed

.changeset/flat-cherries-rule.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/svelte': patch
3+
---
4+
5+
Fixes a case where `$props.id()` would not be unique across multiple islands
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const contexts = new WeakMap();
2+
3+
const ID_PREFIX = 's';
4+
5+
function getContext(rendererContextResult) {
6+
if (contexts.has(rendererContextResult)) {
7+
return contexts.get(rendererContextResult);
8+
}
9+
const ctx = {
10+
currentIndex: 0,
11+
get id() {
12+
return ID_PREFIX + this.currentIndex.toString();
13+
},
14+
};
15+
contexts.set(rendererContextResult, ctx);
16+
return ctx;
17+
}
18+
19+
export function incrementId(rendererContextResult) {
20+
const ctx = getContext(rendererContextResult);
21+
const id = ctx.id;
22+
ctx.currentIndex++;
23+
return id;
24+
}

packages/integrations/svelte/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"dist",
3232
"client.svelte.js",
3333
"server.js",
34-
"server.d.ts"
34+
"server.d.ts",
35+
"context.js"
3536
],
3637
"scripts": {
3738
"build": "astro-scripts build \"src/index.ts\" && astro-scripts build \"src/editor.cts\" --force-cjs --no-clean-dist && tsc",
@@ -46,7 +47,7 @@
4647
"devDependencies": {
4748
"astro": "workspace:*",
4849
"astro-scripts": "workspace:*",
49-
"svelte": "^5.21.0"
50+
"svelte": "^5.22.1"
5051
},
5152
"peerDependencies": {
5253
"astro": "^5.0.0",

packages/integrations/svelte/server.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createRawSnippet } from 'svelte';
22
import { render } from 'svelte/server';
3+
import { incrementId } from './context.js';
34

45
function check(Component) {
56
if (typeof Component !== 'function') return false;
@@ -20,8 +21,11 @@ async function renderToStaticMarkup(Component, props, slotted, metadata) {
2021

2122
let children = undefined;
2223
let $$slots = undefined;
24+
let idPrefix;
25+
if (this && this.result) {
26+
idPrefix = incrementId(this.result);
27+
}
2328
const renderProps = {};
24-
2529
for (const [key, value] of Object.entries(slotted)) {
2630
// Legacy slot support
2731
$$slots ??= {};
@@ -49,6 +53,7 @@ async function renderToStaticMarkup(Component, props, slotted, metadata) {
4953
$$slots,
5054
...renderProps,
5155
},
56+
idPrefix,
5257
});
5358
return { html: result.body };
5459
}

0 commit comments

Comments
 (0)