bug: v5 streamed SSR host is released too early when a script inserts a node while it is parsed

オープン
#7,049 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
55/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript
領域
backend, web-dev

調査の方向性

Start by reading packages/core/src/runtime/parse-gate.ts, especially isParsed() and flush(), and compare the three streaming cases in the linked reproduction. Trace how document mutations can release hosts while parsing is paused. Done means all three pages retain the expected light DOM and shadow root without duplicate rendering or a declarative shadow root warning; add or run focused coverage for those cases.

索引モデルが issue の本文から書いたものです。

説明

Stencil Version

5.0.0-rc.12

Current Behavior

The parse gate for streamed server markup (#7005) releases a host too early when a script inserts a node into the document while the parser is still inside that host.

The gate treats a host as parsed once the host or one of its ancestors has a next sibling. A node that a script appends to an ancestor, e.g. document.body.appendChild(container) for a portal, a toast container or a script tag, is such a sibling, although the parser has not finished the host.

Depending on where the parser is at that moment:

  • Before the <template shadowrootmode> of the host: Stencil attaches its own shadow root, and the parser can no longer attach the declarative one. Chromium logs A second declarative shadow root cannot be created on a host, the <template> stays in the light DOM of the host, and the component renders on the client instead of hydrating. This is the outcome #6690 describes.
  • Inside the <template>: Stencil hydrates the part of the shadow root that has arrived and renders the rest. The parser then appends the rest of the server markup to the same nodes, so the content is there twice.

The parser stops inside a host whenever a chunk of the response ends there, so this needs no unusual markup, only a script that runs in that pause.

Expected Behavior

A host is held until the parser has finished it, whatever scripts do to the document meanwhile. In the reproduction all three pages should end like control:

light DOM children of the host: span
shadow root: <div><p>Server label</p><slot></slot></div>
System Info
      System: node 22.21.0
    Platform: darwin (27.0.0)
   CPU Model: Apple M2 Pro (12 cpus)
     Stencil: 5.0.0-rc.12 🔈
  TypeScript: 6.0.3
    Rolldown: 1.3.3
      Terser: 5.51.4

Browsers: Chromium and WebKit (Playwright 1.63)
Steps to Reproduce
  1. Clone https://github.com/henrijoss/stencil-v5-repros and run npm install
  2. npm install @stencil/[email protected] (the repository is pinned to rc.10, which behaves the same)
  3. npm start --workspace=ssr-streaming and open http://localhost:3336
  4. Open control, insert-before-template and insert-inside-template

The project has a loader-bundle and an ssr output target. The server renders <repro-ssr label="Server label"><span>Slotted</span></repro-ssr> with renderToString(), starts the response, waits until the component is defined, sends the markup up to the middle of the host, and sends the rest once the script of the page has run. Each page prints the light DOM children and the shadow root of the host:

control, nothing is inserted:

light DOM children of the host: span
shadow root: <div><p>Server label</p><slot></slot></div>

insert-before-template, the page appends a <div> to <body> after the opening tag of the host has arrived:

light DOM children of the host: template, span
shadow root: <div><p>Server label</p><slot></slot></div>

plus A second declarative shadow root cannot be created on a host. in the console of Chromium.

insert-inside-template, the same after <template shadowrootmode="open"><div c-id="…"> has arrived:

light DOM children of the host: span
shadow root: <div><p>Server label</p><slot></slot><p c-id="3.1.1.0"><!--t.3.2.2.0-->Server label</p><slot c-id="3.3.1.1"></slot></div>
Code Reproduction URL

https://github.com/henrijoss/stencil-v5-repros/tree/main/ssr-streaming

Additional Information

The check is isParsed() in packages/core/src/runtime/parse-gate.ts:

const isParsed = (elm) => {
  if (win.document.readyState !== 'loading') return true;
  for (let n = elm; n; n = parentOf(n)) if (n.nextSibling) return true;
  return false;
};

A next sibling only proves that the parser has moved on when the parser inserted it. flush() runs for every mutation of the document, so the first mutation after such an insertion releases every host that is still open below that ancestor.

I have not tried a fix. A signal that scripts of the page can't produce by accident would close it, e.g. a marker the server writes as the last node of a host, or of its template.

This is not a regression from Stencil 4, which has no parse gate and always fails for markup that is streamed after the components are defined (#6690). It is a gap in the fix for that.

I found no existing issue for this.

主要言語
TypeScript
スター
13.2k
フォーク
870
平均マージ
2日 54分
マージ済み PR(30日)
50

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

stenciljs/core のほかの issue

stenciljs/core の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。