-
Notifications
You must be signed in to change notification settings - Fork 86
[#669] Declarative partial update use case #958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ce174f1
[#669] Declarative partial update use case
jamesnw b61d192
Apply suggestions from code review
jamesnw d276471
Merge branch 'main' of https://github.com/GoogleChrome/modern-web-gui…
jamesnw 1bf8b17
Add empty web feature array
jamesnw 98b937d
Merge branch 'main' of https://github.com/GoogleChrome/modern-web-gui…
jamesnw b5225e8
Move out of order streaming to higher level guide
jamesnw ea24af3
Update guides/performance/out-of-order-html-streaming/guide.md
jamesnw 4cb4659
Merge branch 'main' of https://github.com/GoogleChrome/modern-web-gui…
jamesnw 7bc545f
Preflight
jamesnw ce62123
Apply batched suggestions from code review
jamesnw 0c71564
Merge branch 'main' of https://github.com/GoogleChrome/modern-web-gui…
jamesnw 998af45
Update tmp web feature ids
jamesnw b7d3cd8
Lint
jamesnw 450774d
Merge branch 'main' of https://github.com/GoogleChrome/modern-web-gui…
jamesnw 870b9d3
Update web features, add textStream
jamesnw b732aec
Merge branch 'main' into 669-declarative-partial-update
tunetheweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
guides/performance/out-of-order-html-streaming/guide.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| --- | ||
| name: out-of-order-html-streaming | ||
| description: Declaratively update a page with content streamed later into the same HTML document or via imperative JavaScript APIs. | ||
| web-feature-ids: | ||
| - tmp-declarative-partial-updates | ||
|
jamesnw marked this conversation as resolved.
Outdated
|
||
| --- | ||
|
|
||
| # Out-of-Order (OOO) HTML Streaming | ||
|
|
||
| Out-of-Order (OOO) HTML Streaming (also known as Declarative Partial Updates) is an alternative to the traditional "top-to-bottom" linear processing of HTML. It allows developers to define placeholders earlier in a document and "patch" them with content that arrives later in the same network stream, or via new imperative JavaScript streaming APIs. | ||
|
|
||
| IMPORTANT: This feature is currently in Early Preview. It requires Chrome 148 or higher with the `#enable-experimental-web-platform-features` flag enabled. | ||
|
|
||
|
jamesnw marked this conversation as resolved.
Outdated
|
||
| ## Quick Overview | ||
|
|
||
| The feature consists of two primary mechanisms: | ||
|
|
||
| 1. **Declarative Patching (HTML-based)**: Uses new processing instructions (`<?marker>`, `<?start>`, `<?end>`) and `<template for>` to swap content in the DOM as it streams in from the server. | ||
| 2. **Imperative Streaming (JS-based)**: A suite of new methods (e.g., `streamHTML()`, `setHTMLUnsafe()`) that allow piping readable streams directly into DOM elements with native performance and optional script execution. | ||
|
jamesnw marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Best Practices | ||
|
|
||
| * **Prioritize Above-the-Fold Content**: Stream critical UI (header, hero, primary navigation, primary content) first. Use placeholders for "expensive" or lower priority components like comments, ads, or related content. | ||
| * **Implement Native Loading States**: Use the `<?start>` and `<?end>` markers to wrap fallback content. The browser automatically replaces everything between these markers when the corresponding `<template for>` arrives. | ||
| * **Minimize Cumulative Layout Shift (CLS)**: Always provide stable dimensions for placeholders. If the final content size is unknown, use skeleton screens or CSS `aspect-ratio` to reserve space and prevent jarring shifts. | ||
| * **Accessibility & Focus Management**: Be mindful that out-of-order updates can confuse screen reader users. Use `aria-live` regions for critical updates and ensure that focus isn't lost if a placeholder containing the active element is replaced. | ||
|
|
||
|
|
||
|
jamesnw marked this conversation as resolved.
Outdated
|
||
| ## Implementation Details | ||
|
|
||
| ### 1. Declarative HTML Markers | ||
| The browser looks for specific processing instructions to identify where content should be patched. | ||
|
|
||
| ```html | ||
| <!-- A simple insertion point --> | ||
| <div id="sidebar"> | ||
| <?marker name="dynamic-comments"> | ||
| </div> | ||
|
|
||
| <!-- A replacement range with loading state --> | ||
| <main> | ||
| <?start name="main-content"> | ||
| <div class="skeleton">Loading content...</div> | ||
| <?end> | ||
| </main> | ||
|
|
||
| ... (other content) ... | ||
|
|
||
| <!-- Later in the same stream, provide the content --> | ||
| <template for="dynamic-comments"> | ||
| <div class="comments">...</div> | ||
| </template> | ||
|
|
||
| <template for="main-content"> | ||
| <article> | ||
| <h1>The Real Content</h1> | ||
| <p>This replaces the skeleton div automatically.</p> | ||
| </article> | ||
| </template> | ||
| ``` | ||
|
|
||
| Once a `<template>` replaces an insertion point, that insertion point no longer exists, and can not be reused. However, a `<template>` may contain insertion points that can be used later in the stream. | ||
|
|
||
| ```html | ||
| <?start name="replaced-content"> | ||
| <div class="skeleton"> | ||
| <?end> | ||
| <!-- Later in the same stream, provide initial content --> | ||
| <template for="replaced-content"> | ||
| <?start name="replaced-content"> | ||
| <div>Quickly computed result</div> | ||
| <?end> | ||
| </template> | ||
| <!-- Later in the same stream, provide full content --> | ||
| <template for="replaced-content"> | ||
| <?start name="replaced-content"> | ||
| <div>Full result</div> | ||
| <?end> | ||
| </template> | ||
| ``` | ||
|
|
||
| ### 2. Imperative JS Streaming | ||
| You can now pipe fetch responses directly into the DOM without manual chunk handling. | ||
|
|
||
| ```javascript | ||
| const main = document.querySelector('main'); | ||
| const response = await fetch('/api/partial-update'); | ||
|
|
||
| // Pipe the stream directly into the element | ||
| await response.body | ||
| .pipeThrough(new TextDecoderStream()) | ||
| .pipeTo(main.streamHTMLUnsafe({ runScripts: true })); | ||
| ``` | ||
|
|
||
| ## Use Case Reference Matrix | ||
|
|
||
| * **Island Architecture**: Hydrate independent interactive components on top of static HTML without a full framework runtime. | ||
| * **Deferred "Expensive" Blocks**: Deliver the shell of a page immediately and stream database-heavy components as they become available. | ||
| * **Single Page Application (SPA) Updates**: Stream large UI transitions in SPAs using `streamHTML()` instead of waiting for a full JSON payload and manual DOM construction. | ||
|
|
||
| ## Anti-Patterns & Warnings (DO NOT DO THIS) | ||
|
|
||
| * **Do not use OOO streaming for everything.** Over-fragmenting a page can increase the complexity of the stream and potentially delay the "First Meaningful Paint" if the browser spends too much time patching small nodes. | ||
|
jamesnw marked this conversation as resolved.
Outdated
|
||
| * **Do not ignore Trusted Types.** When using `*Unsafe` methods in environments with Trusted Types enforced, ensure you are passing a sanitized policy object if required. | ||
| * **Do not assume script execution order.** Scripts inside a `<template for>` will execute as soon as they are patched into the document. Do not rely on them executing in a specific sequence relative to other deferred templates. | ||
| * **Do not use without a polyfill strategy.** Ensure you have a fallback or use polyfills (`html-setters-polyfill` and `template-for-polyfill`) to support your required browsers. | ||
|
jamesnw marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Implementation Status | ||
|
|
||
| * **Current Status**: Experimental / Early Preview. | ||
| * **Required Version**: Chromium 148+. | ||
| * **Activation**: Requires enabling the `chrome://flags/#enable-experimental-web-platform-features` or `edge://flags/#enable-experimental-web-platform-features`. | ||
| * **Specification**: [Declarative Partial Updates (Explainer)](https://github.com/WICG/declarative-partial-updates). | ||
|
jamesnw marked this conversation as resolved.
Outdated
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.