Skip to content

Commit 131cbbd

Browse files
committed
Feat: Fix race condition for lit components in Astro 6
1 parent d8d5932 commit 131cbbd

5 files changed

Lines changed: 64 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11

2+
## 5.2.0
3+
4+
### Changes
5+
- Inline hydration support as a blocking script instead of using Astro's `before-hydration` or `page` injection. While debugging SSR hydration issues we discovered two separate bugs:
6+
1. Astro 6 doesn't emit the `before-hydration` chunk for the client build environment, causing a 404. We [contributed a fix](https://github.com/withastro/astro/pull/15904) upstream which has been merged.
7+
2. Even with the Astro fix, `before-hydration` loads as a module script which can race with other module scripts that import lit-element. If lit-element evaluates first, `globalThis.litElementHydrateSupport` isn't set yet and the hydration patches are never applied, causing intermittent duplicate rendering.
8+
9+
This release bundles the hydration support (~13KB) and inlines it via `head-inline` as a classic blocking script, guaranteeing it executes before any module scripts.
10+
211
## 5.1.3
312

413
### Bugs

dist/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ function index_default() {
3232
"head-inline",
3333
readFileSync(new URL("../client-shim.min.js", import.meta.url), { encoding: "utf-8" })
3434
);
35-
injectScript("before-hydration", `import '@semantic-ui/astro-lit/hydration-support.js';`);
35+
injectScript(
36+
"head-inline",
37+
readFileSync(new URL("../hydration-support.min.js", import.meta.url), { encoding: "utf-8" })
38+
);
3639
addRenderer({
3740
name: "@semantic-ui/astro-lit",
3841
serverEntrypoint: "@semantic-ui/astro-lit/server.js",

hydration-support.min.js

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@semantic-ui/astro-lit",
3-
"version": "5.1.3",
3+
"version": "5.2.0",
44
"description": "Use Lit components with Astro",
55
"type": "module",
66
"types": "./dist/index.d.ts",
@@ -29,6 +29,7 @@
2929
"client-shim.js",
3030
"client-shim.min.js",
3131
"hydration-support.js",
32+
"hydration-support.min.js",
3233
"server.js",
3334
"server.d.ts",
3435
"server-shim.js"
@@ -37,7 +38,7 @@
3738
"build": "npm run build:js && npm run build:types && npm run build:minify",
3839
"build:js": "esbuild src/index.ts src/client.ts --format=esm --bundle --platform=node --outdir=dist",
3940
"build:types": "tsc src/client.ts --declaration --emitDeclarationOnly --skipLibCheck --outDir ./dist",
40-
"build:minify": "esbuild client-shim.js --minify --outfile=client-shim.min.js",
41+
"build:minify": "esbuild client-shim.js --minify --outfile=client-shim.min.js && esbuild hydration-support.js --bundle --format=iife --minify --outfile=hydration-support.min.js",
4142
"dev": "npm run build -- --watch",
4243
"test": "node test/server.test.js && node test/sass.test.js"
4344
},

src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ export default function (): AstroIntegration {
3636
'head-inline',
3737
readFileSync(new URL('../client-shim.min.js', import.meta.url), { encoding: 'utf-8' })
3838
);
39-
// Inject the hydration code, before a component is hydrated.
40-
injectScript('before-hydration', `import '@semantic-ui/astro-lit/hydration-support.js';`);
39+
// Inject hydration support as a blocking script to guarantee it runs
40+
// before any module scripts can load lit-element. This avoids a race
41+
// condition where lit-element evaluates before the hydration patches
42+
// are registered on globalThis.litElementHydrateSupport.
43+
injectScript(
44+
'head-inline',
45+
readFileSync(new URL('../hydration-support.min.js', import.meta.url), { encoding: 'utf-8' })
46+
);
4147
// Add the lit renderer so that Astro can understand lit components.
4248
addRenderer({
4349
name: '@semantic-ui/astro-lit',

0 commit comments

Comments
 (0)