Skip to content

feat(hydrate): cache platform closure and add opt-in window reuse - #6794

Closed
Armand-Lluka wants to merge 2 commits into
stenciljs:mainfrom
Armand-Lluka:test/hydrate-platform-reuse
Closed

Armand-Lluka wants to merge 2 commits into
stenciljs:mainfrom
Armand-Lluka:test/hydrate-platform-reuse

Conversation

@Armand-Lluka

@Armand-Lluka Armand-Lluka commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

The hydrate factory wraps the entire platform (runtime, vdom and every component class) in hydrateAppClosure() so it lexically captures the per-call window, and re-executes that closure on EVERY renderToString call. For component libraries this is the dominant fixed cost per render (framework output targets call renderToString once per component instance).

Two changes:

  1. The factory now caches the evaluated closure on the window object and reuses it whenever the same window is passed again. Transparent for fresh-window renders.

  2. New opt-in HydrateDocumentOptions.reuseWindow: the string-input path of hydrateDocument()/renderToString() reuses a process-global MockWindow, one per serializeShadowRoot mode (scoped serialization permanently ORs shadowNeedsScopedCss into component metadata inside the cached closure). Fresh head/body ELEMENTS are swapped in per render (never innerHTML='') because rootAppliedStyles is keyed on the head node. Renders are serialized through an internal promise queue since the shared window is not concurrency-safe.

Measured 2.2x per-render speedup on a 2-component test app; scales with component count (~4.3x on a 475-component library).

What is the current behavior?

GitHub Issue Number: N/A

What is the new behavior?

Closes #6794

Documentation

Does this introduce a breaking change?

  • Yes
  • No

Testing

Other information

Copilot AI lite review requested due to automatic review settings July 27, 2026 12:12
@Armand-Lluka
Armand-Lluka requested a review from a team as a code owner July 27, 2026 12:12
@Armand-Lluka
Armand-Lluka marked this pull request as draft July 27, 2026 12:13
@Armand-Lluka
Armand-Lluka force-pushed the test/hydrate-platform-reuse branch from 872bc7a to 3a28bcb Compare July 27, 2026 12:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes hydrate SSR performance by avoiding repeated evaluation of the hydrate platform closure and (optionally) reusing a process-global mock window for string-based renders.

Changes:

  • Cache the evaluated hydrateAppClosure() result on the provided window and reuse it when the same window is used again.
  • Add HydrateDocumentOptions.reuseWindow to reuse a per-process MockWindow (keyed by serializeShadowRoot) for string-input hydrateDocument() / renderToString(), serializing renders through a promise queue.
  • Update public type declarations/documentation to describe the new reuseWindow option.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/hydrate/runner/render.ts Adds reusable MockWindow caching and a serialized render queue behind reuseWindow.
src/declarations/stencil-public-compiler.ts Documents/exposes the new reuseWindow?: boolean public option.
src/compiler/output-targets/dist-hydrate-script/hydrate-factory-closure.ts Changes generated hydrate factory to cache the evaluated hydrate closure on window.
Comments suppressed due to low confidence (1)

src/hydrate/runner/render.ts:135

  • runRender only catches synchronous errors. If render(reusedWin, ...) ever rejects, the cached window won't be evicted/closed and the rejection will propagate to callers (unlike other error paths which return a HydrateResults). It would be safer to attach a .catch() to perform the same cleanup + renderCatchError and always resolve results.
          reusedWin = getReusableWindow(doc, opts);
          return render(reusedWin, opts, results).then(() => results);
        } catch (e) {

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/hydrate/runner/render.ts Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Suppressed comments (1)

src/hydrate/runner/render.ts:140

  • The reusable window cache key is recomputed in the catch block using the (potentially mutated) opts.serializeShadowRoot, which can cause the wrong entry to be deleted if serializeShadowRoot changes during render() (or if its object key order differs). Compute the key once per render attempt and reuse it for both lookup and cleanup.
        } catch (e) {
          if (reusedWin) {
            reusableWindows.delete(JSON.stringify(opts.serializeShadowRoot ?? null));
            if (reusedWin.close) {
              reusedWin.close();

Comment thread src/hydrate/runner/render.ts Resolved
@Armand-Lluka
Armand-Lluka force-pushed the test/hydrate-platform-reuse branch from 05a0488 to 3fb4315 Compare August 12, 2026 10:51
@Armand-Lluka
Armand-Lluka requested a lite review from Copilot August 13, 2026 07:39
@Armand-Lluka
Armand-Lluka marked this pull request as ready for review August 13, 2026 07:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

Suppressed comments (2)

src/compiler/output-targets/dist-hydrate-script/hydrate-factory-closure.ts:157

  • The cache guard uses a truthiness check for __stencilHydrateApp, but the code later assumes it is callable. If something (or a previous run) sets this property to a non-function truthy value, hydration will throw when invoked. Consider guarding by function type and defining the property as non-enumerable to reduce accidental collisions.
  if (!$stencilWindow.__stencilHydrateApp) {
    $stencilWindow.__stencilHydrateApp = hydrateAppClosure($stencilWindow);
  }
  $stencilWindow.__stencilHydrateApp($stencilWindow, $stencilHydrateOpts, $stencilHydrateResults, $stencilAfterHydrate, $stencilHydrateResolve);

src/hydrate/runner/reusable-window.ts:25

  • When reusing a MockWindow, this reset currently leaves MockWindow timers/event listeners and constrainTimeouts-related internal flags intact. That means a prior render can leak pending timeouts/intervals and window/document listeners into the next render, and constrainTimeouts can remain enabled/disabled across calls unexpectedly.
  const defaults = new MockWindow(false);
  resetObject(win.location, defaults.location);
  resetObject(win.navigator, defaults.navigator);
  win.localStorage.clear();
  win.sessionStorage.clear();

Copilot AI review requested due to automatic review settings August 13, 2026 14:12
@Armand-Lluka
Armand-Lluka force-pushed the test/hydrate-platform-reuse branch from 3fb4315 to 3c5d23e Compare August 13, 2026 14:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

Suppressed comments (3)

src/compiler/output-targets/dist-hydrate-script/hydrate-factory-closure.ts:157

  • __stencilHydrateApp is only checked for truthiness before invoking. If a consumer-provided window already has a truthy non-function value at __stencilHydrateApp, this will throw at runtime when called. Guarding with typeof === 'function' makes the caching logic robust to collisions.
  if (!$stencilWindow.__stencilHydrateApp) {
    $stencilWindow.__stencilHydrateApp = hydrateAppClosure($stencilWindow);
  }
  $stencilWindow.__stencilHydrateApp($stencilWindow, $stencilHydrateOpts, $stencilHydrateResults, $stencilAfterHydrate, $stencilHydrateResolve);

src/hydrate/runner/reusable-window.ts:21

  • getReusableWindow() creates a new MockWindow(false) on every reused render just to obtain default location/navigator objects. Even with html === false, MockWindow still constructs performance, customElements, console, and runs resetWindowDefaults/resetWindowDimensions (see src/mock-doc/window.ts:85-96), which can materially reduce the benefit of reuseWindow.
  const defaults = new MockWindow(false);
  resetObject(win.location, defaults.location);
  resetObject(win.navigator, defaults.navigator);

src/hydrate/runner/reusable-window.ts:23

  • When reuseWindow is enabled, destroyWindow is set to false, so MockWindow.close() is never called. That means any timers scheduled during a render remain in win.__timeouts and can fire during a later queued render, breaking the “serialized renders” concurrency guarantee and leaking work across renders (timeouts are only cleared in MockWindow.close() via resetWindow(); see src/mock-doc/window.ts:850-858).
  const document = win.document;
  const defaults = new MockWindow(false);
  resetObject(win.location, defaults.location);
  resetObject(win.navigator, defaults.navigator);
  win.localStorage.clear();

Copilot AI review requested due to automatic review settings August 15, 2026 12:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review is ineligible. To be eligible to request a review, you need a paid Copilot license, or your organization must enable Copilot code review.

@codspeed

codspeed Bot commented Aug 15, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 2 untouched benchmarks


Comparing Armand-Lluka:test/hydrate-platform-reuse (bd5970a) with main (363682e)

Open in CodSpeed

@johnjenkins

johnjenkins commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

hey @Armand-Lluka - thanks for raising - I think the concept is a good one.
I get concerns around potential memory leaks with this kind of thing on the server, so I had an agent to test some scenarios and it found some unfortunately - https://github.com/johnjenkins/stencil-hydrate-reuse-leak-repro (excuse the long winded prologue) - please can you address those first, then I'll have a complete picture before doing a full code-review.

Copilot AI review requested due to automatic review settings August 20, 2026 08:10
@Armand-Lluka
Armand-Lluka force-pushed the test/hydrate-platform-reuse branch from a816432 to c63b1e2 Compare August 20, 2026 08:10
@Armand-Lluka
Armand-Lluka force-pushed the test/hydrate-platform-reuse branch from c63b1e2 to 35be571 Compare August 20, 2026 08:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings August 20, 2026 08:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.

@Armand-Lluka
Armand-Lluka force-pushed the test/hydrate-platform-reuse branch from 7f43a8c to 06fb39a Compare August 24, 2026 09:39
Copilot AI review requested due to automatic review settings August 24, 2026 09:39
Comment thread src/hydrate/runner/reusable-window.ts Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings August 24, 2026 09:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.

@Armand-Lluka

Copy link
Copy Markdown
Contributor Author

Let me know if anything here needs adjusting 🙂 @johnjenkins

Copilot AI review requested due to automatic review settings September 3, 2026 19:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review is ineligible. To be eligible to request a review, you need a paid Copilot license, or your organization must enable Copilot code review.

@Armand-Lluka

Armand-Lluka commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @johnjenkins, just following up on this one 🙂

Since your review on:

  • Added e2e coverage for the reuse behaviour in renderToString.e2e.ts, per your request
  • Rebased on main, CI green
  • Memory-leak scenarios from your earlier repro are addressed with the numbers posted above (deltas now flat/noise-level vs. the original leak rates)

If the PR has grown past what you'd like to review in one pass, happy to split it into smaller pieces or walk through it async, whatever's easiest. Otherwise, is it ready for a re-review?

Copilot AI review requested due to automatic review settings September 15, 2026 07:57
@Armand-Lluka
Armand-Lluka force-pushed the test/hydrate-platform-reuse branch from b9ac6ab to e928aef Compare September 15, 2026 07:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved critical and moderate findings affect mode registration, browser-global freshness, and fetch abort handling.

Get a fresh assessment by requesting another Copilot review.

Review details

Suppressed comments (3)

src/compiler/output-targets/dist-hydrate-script/hydrate-factory-closure.ts:199

  • This passes the same closure-scoped $stencilAbortController to every render using the cached app. hydratedError() aborts that controller on a timeout or component failure, and an AbortController cannot be reset, so every later queued render immediately observes an aborted signal and its wrapped fetch() calls are aborted as well. Allocate a fresh controller (and corresponding fetch signal wrapper) for each invocation of the cached app.
      return hydrateApp($stencilWindow, $stencilHydrateOpts, $stencilHydrateResults, $stencilAfterHydrate, $stencilHydrateResolve, $stencilAbortController);

src/compiler/output-targets/dist-hydrate-script/hydrate-factory-closure.ts:219

  • Caching this returned closure also caches the primitive globals captured when it was first evaluated (origin, innerWidth, devicePixelRatio, etc.). The reuse path resets/applies the current window before this call, so a later render with a different url or window values can expose stale values to component code that reads those browser globals. Refresh these bindings from the current window for each invocation, or avoid caching bindings that cannot be reset in place.
    $stencilHydrateApp = $stencilWindowApps.get($stencilShadowMode);
    if (!$stencilHydrateApp) {
      $stencilHydrateApp = hydrateAppClosure($stencilWindow);
      $stencilWindowApps.set($stencilShadowMode, $stencilHydrateApp);
    }
  }
  $stencilHydrateApp($stencilWindow, $stencilHydrateOpts, $stencilHydrateResults, $stencilAfterHydrate, $stencilHydrateResolve);

src/mock-doc/window.ts:876

  • The first hydrate closure installs an own win.fetch wrapper so requests receive the render's abort signal, but this reset deletes that property. Because the cached closure is not re-evaluated, later reused renders leave window.fetch as the raw prototype method and calls through window.fetch(...) bypass the timeout/error abort wiring. Preserve or reinstall the wrapper for each render (in addition to using a per-render controller).
  for (const key of Reflect.ownKeys(win)) {
    delete (win as any)[key];
  }
  • Files reviewed: 11/11 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment on lines +194 to +198
return function($stencilWindow, $stencilHydrateOpts, $stencilHydrateResults, $stencilAfterHydrate, $stencilHydrateResolve) {
modeResolutionChain.length = 0;
if (Array.isArray($stencilHydrateOpts.modes)) {
$stencilHydrateOpts.modes.forEach((mode) => modeResolutionChain.push(mode));
}
@johnjenkins

johnjenkins commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

hey @Armand-Lluka - sorry, I'm really just trying to get v5 over the line; minimise context switching and not really considering v4 features / feature releases now. I'll be sure to look at this as soon as v5 drops

@johnjenkins

Copy link
Copy Markdown
Contributor

hey @Armand-Lluka - thanks so much again for looking at this.
With v5 release being iminent we're not accepting anymore features to v4. But I'm pleased to announce that your changes were migrated into v5 so i'd encourage you to move over and try it out (and let us know if you have any issues)

@Armand-Lluka

Copy link
Copy Markdown
Contributor Author

Glad to hear @johnjenkins, we'll for sure plan in some testing for v5 and give feedback if we discover anything 🫡

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.

3 participants