Fix CustomCode scriptsClientOnly not re-running scripts after hydration - #4848
Manish-Builder-io wants to merge 3 commits into
Conversation
|
View your CI Pipeline Execution ↗ for commit 274496e
💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗ ☁️ Nx Cloud last updated this comment at |
|
@builderio-bot Review all unresolved PR comments and latest review feedback. Be skeptical and verify each suggestion against the code and requirements before making changes. For every comment:
Process every comment and finish with a summary of fixed, rejected, and blocked items. |
|
All checks pass. Summary of this round: Fixed (1)
Replied on the thread and resolved it. Found while verifying (not raised by any reviewer) CI runs Rejected: none. Blocked: none. Verification: exact CI command passes; full package suite 48 passed; One thing I left alone deliberately: the merge from |
🦋 Changeset detectedLatest commit: 274496e The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
There was a problem hiding this comment.
Builder reviewed your changes — looks good ✅
Review Details
Incremental Code Review Summary
The latest PR head retains the same focused implementation: CustomCode now types its hydration state and allows the one-time hydrated transition to trigger the render needed to restore client-only scripts, while preserving the code-based render bailout. The latest diff introduces no new production-code or test-isolation regressions. Parallel reviewers also validated the focused hydration tests and package build/test paths; broader setup output is affected by the environment's isolated-vm install failure, and one reviewer noted unrelated pre-existing test/build issues.
The previously reported high-severity compatibility finding remains open: packages/react/test/custom-code.test.tsx still imports React 18-only react-dom/client APIs despite the package supporting React/ReactDOM >=16.8 and declaring React DOM 16 types. That existing comment is intentionally not resubmitted during this incremental review.
Risk remains standard because this changes shared React lifecycle behavior and package test/build compatibility.
🧪 Browser testing: Skipped — dev server unavailable on HEAD because dependency setup fails while building isolated-vm; will retry on the next incremental review.
| @@ -0,0 +1,5 @@ | |||
| --- | |||
There was a problem hiding this comment.
Will do a dev release first
Description
Fixes a regression where
CustomCodeblocks withscriptsClientOnly: truefail to restore SSR-stripped<script>tags after hydration.Root cause:
shouldComponentUpdateinCustomCodeComponentonly comparednextProps.code !== this.props.code. This meant thesetState({ hydrated: true })call made incomponentDidMount(which is meant to trigger a one-time re-render to re-insert the scripts that were stripped for SSR) was blocked, since onlystate.hydratedchanged and notprops.code.Fix:
Statetype ({ hydrated: boolean }) and used it as the generic parameter onReact.Component<Props, State>instead of leaving the component untyped for state.shouldComponentUpdateto also acceptnextState: Stateand returntruewhennextState.hydrated !== this.state.hydrated, in addition to the existingcodeprop comparison. This preserves the original perf optimization (skipping re-renders whencodeis unchanged) while no longer suppressing the one-time hydration re-render.Tests:
packages/react/test/custom-code.test.tsxcovering:scriptsClientOnlyis true.CustomCodestill skips unnecessary re-renders when neithercodenor hydration state change (verifying no perf regression).packages/react/test/setupTests.tsto polyfillTextEncoder/TextDecoderin the jsdom test environment, whichreact-dom/server'srenderToStringrequires but jsdom 19 doesn't provide.@builder.io/react.Link to JIRA ticket:
https://builder-io.atlassian.net/browse/SUPP-2847
To clone this PR locally use the Github CLI with command
gh pr checkout 4848You can tag me at @BuilderIO for anything you want me to fix or change
Note
Low Risk
Narrow lifecycle fix in CustomCode with regression tests; behavior change is restoring intended client-only script execution after hydration.
Overview
Fixes a regression where Custom Code blocks with
scriptsClientOnlystripped<script>tags on SSR but never put them back or executed them on the client.shouldComponentUpdateonly comparedprops.code, so the one-timesetState({ hydrated: true })incomponentDidMountdid not trigger a re-render. The change types component state as{ hydrated: boolean }and allows updates whenhydratedchanges, while still skipping re-renders when only unrelated parent props change.Adds jsdom tests for SSR script stripping, post-hydration script restore/execution, and a render-count guard. Test setup polyfills
TextEncoder/TextDecoderforrenderToString. Patch changeset for@builder.io/react.Reviewed by Cursor Bugbot for commit 274496e. Bugbot is set up for automated code reviews on this repo. Configure here.