Skip to content

fix(hydration): load Lit hydration support before Page client code - #522

Open
daKmoR wants to merge 2 commits into
mainfrom
fix/hydrate-support-before-page-scripts
Open

daKmoR wants to merge 2 commits into
mainfrom
fix/hydrate-support-before-page-scripts

Conversation

@daKmoR

@daKmoR daKmoR commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Summary

A js client block that imports a server-rendered Lit element (directly or through a dependency) evaluated LitElement before Lit hydration support. The element then upgraded without hydration support and rendered a second copy of its template into its declarative shadow root.

Rocket joined the Page client code and the hydration code into one module script. Static imports evaluate in source order, and the Hydration Loader loaded hydrate support only when the first hydrate:* component resolved:

 <head>
+  <script type="module">import "@rocket/js/hydration/hydrationLoader.js";</script>
   <script type="module">
     import 'some-package';            // client code: evaluates LitElement
     import {HydrationLoader} from '@rocket/js/hydration/hydrationLoader.js';
     …
   </script>
 hydrationLoader.js
+  import '@lit-labs/ssr-client/lit-element-hydrate-support.js'   // first import
   setup()
-    await import('@lit-labs/ssr-client/lit-element-hydrate-support.js')
     applyLitElementHydrateSupport()   // kept as fallback, e.g. for layout scripts
  • createComponentHydration exposes the loader as parseComponents.hydrationSupportSpecifier, so the dev server, static build and Netlify live assets each use their own resolvable path.
  • Pages with server or hydrate:* Registered Components get the extra module script. Client code and hydration code keep their order.
  • @lit-labs/ssr-client is now a declared dependency.
  • RocketBuild.build() registers the Markdown import hook once per process. A second registration rejected Markdown files the first hook had compiled, so a second Markdown build in the same process failed.

Evidence

New end-to-end test src/cli/RocketBuild.test.js › 50:

build a Markdown Page
  components: hydration-probe → hydrate:onVisible
  js client:  import 'hydration-probe'   // package that defines the element
open it in headless Chrome
expect shadowRoot.querySelectorAll('[data-probe]').length === 1
  • Before: ✖ 50 … AssertionError: Expected the server-rendered template to hydrate, not render twice — 2 !== 1. Removing either half of the fix (the extra script or the static import) also fails.
    After: ✔ 50: hydrates server-rendered Lit components that a Page client script imports first

Unit tests cover the script order (layout-helper.test.js 20, 21), which Loading Strategies get the script (loaded-page-module.test.js 10) and the Netlify specifier. Lint, Prettier and tsc pass, and so do the browser tests (12/12).

Merge Danger

Door: two-way

Blast Radius: hydrated Pages

Pages with server-rendered Registered Components load one extra small module script before their client code. Pages that loaded hydration support themselves as a workaround keep working and can drop that import.

🤖 Generated with Claude Code

The Hydration Loader imported @lit-labs/ssr-client's hydrate support
lazily, when the first hydrate:* component resolved. A js client block
that imported a server-rendered Lit element, directly or through a
dependency, evaluated LitElement first. The element then upgraded
without hydration support and rendered a second copy of its template
into its declarative shadow root.

- The Hydration Loader imports hydrate support statically as its first
  import. setup() keeps patching an already evaluated LitElement as a
  fallback.
- createComponentHydration exposes the loader as
  hydrationSupportSpecifier. Pages with server or hydrate:* Registered
  Components get a module script that imports it ahead of their client
  code script.
- Declare @lit-labs/ssr-client as a dependency.
- An end-to-end test builds a Markdown Page whose client script
  imports a hydrated element and counts template copies in Chrome.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b48a15b

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

This PR includes changesets to release 1 package
Name Type
@rocket/js 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

On Linux, every browser launch unpacks @sparticuz/chromium to the same
temporary path. The hydration order test ran in its own test process,
launched Chromium in parallel with RocketBuild.test.js and failed in CI
with spawn ETXTBSY.

The test moves into RocketBuild.test.js, where browser launches run one
after another. It had been moved out because every RocketBuild.build()
registered another Markdown import hook, and the second hook rejected
Markdown files the first had compiled. build() now registers the hook
once per process.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant