Skip to content

feat: add scriptLoading option - #82

Merged
chenjiahan merged 1 commit into
rstackjs:mainfrom
SoonIter:feat-script-loading
Oct 28, 2026
Merged

chenjiahan merged 1 commit into
rstackjs:mainfrom
SoonIter:feat-script-loading

Conversation

@SoonIter

Copy link
Copy Markdown
Member

The plugin currently always loads gtag.js asynchronously. Add scriptLoading: 'async' | 'defer' so sites can opt into execution after HTML parsing without rewriting the generated tags. The default remains async, matching Google's setup example.

Only the external script's loading attribute changes. The README documents the option and its ordering / DOMContentLoaded tradeoffs; inline dataLayer and gtag initialization is preserved.

Validation:

  • pnpm check
  • pnpm build (ESM, CJS, and type declarations)
  • Real Rsbuild HTML matrix: default / explicit async / defer, disabled plugin, missing IDs, and existing user tags
  • Chromium with slow and failed GA mocks: initialization queue and script / DOMContentLoaded timing (no live analytics delivery tested)

Copilot AI balanced review requested due to automatic review settings October 28, 2026 04:11
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-28T04:12:38.162567Z
We couldn’t complete this review. Comment @codex review to try again.
7d752f2 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown

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 has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

Copy link
Copy Markdown
Member Author

Adding the Rspress case that motivated this option.

On the introduction page, repeatedly clicking Chrome's reload/stop button while reading near “Why Rspress” can briefly jump the page to its final sections, then return to the original position. The URL has no hash. In the original recording below, the jump is visible at about 1.4 seconds.

Original recording

rspress-reload-scroll-flash.mp4

Controlled reproduction

Tested the production build at Rspress commit 9abba4d50, using Chrome 156.0.8078.48 (headless, 1036 × 850 viewport):

  1. Open /zh/guide/start/introduction, wait for loading and scroll restoration setup, then scroll to 320px.
  2. Disable the cache, reload, and delay stylesheet requests by 800ms.
  3. Capture compositor frames and a native trace, without reading layout metrics during loading.
Variant Captured scroll positions
Original GA async 320 → 10255 → 320
Rspress main entry blocked, GA retained 320 → 11104 → 320
GA request blocked Stayed at 320
Native scroll anchoring disabled (diagnostic control) Stayed at 320
Only GA's external script changed to defer All 21 captured frames stayed at 320

The trace shows a GA callback forcing layout before CSS was ready, followed by Chrome's native ScrollAnchor::RestoreAnchor restoring an anchor using the unstyled layout. The incorrect position was briefly painted before the original position was restored. The flash also occurred with the application entry blocked, so this reproduction does not require React Router or the table of contents to run.

This is a controlled reproduction of the same visible symptom; the original recording has no accompanying network trace. The defer result above is from that controlled run, not a completed interactive-browser A/B test. It supports offering a site-specific loading option while retaining async as the default. Inline dataLayer/gtag initialization was unchanged in the comparison.

@chenjiahan chenjiahan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@chenjiahan
chenjiahan merged commit 821f9e9 into rstackjs:main Oct 28, 2026
1 check passed
@SoonIter
SoonIter deleted the feat-script-loading branch October 28, 2026 05:42
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