fix(helper): self-removing bootstrap script avoids React hydration mismatch#121
Merged
Conversation
…n mismatch The injected bootstrap <script> is prepended right after <!DOCTYPE>, which the HTML5 parser relocates into <head>. Next.js app-router (and any other framework that hydrates from the <html> root) sees an extra child not present in the React tree and reports a hydration mismatch. Fix: have the bootstrap script remove its own <script> tag from the DOM at the end of the IIFE. `document.currentScript` is set during synchronous script execution, so removal runs before hydration. Side effects (console interception, error listeners, and the requestIdleCallback-deferred asset loads for client-log.js and feedback/script.js) survive removal because they're attached to window/console, not the script element.
New clippy lint in latest stable flags `sort_by(|a,b| a.x.cmp(&b.x))` on a Copy key — `sort_by_key(|a| a.x)` is equivalent and idiomatic. Unrelated to the hydration fix but blocks CI.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
<script>injected byveld_injectlands between<!DOCTYPE>and<html>. HTML5 parser relocates it into<head>. Next.js app-router hydrates from the<html>root and reports a hydration mismatch because the script is not in the React tree.<script>tag viadocument.currentScript.parentNode.removeChild(...)before returning. Runs synchronously during script execution, before React hydration walks the live DOM.window/console; the dynamically-loadedclient-log.jsandfeedback/script.jsare scheduled viarequestIdleCallbackand usedocument.head.appendChild, independent of the bootstrap tag's presence.Why this is safe
document.currentScriptis non-null during synchronous inline script execution.__veld_cldedup guard sits onwindow, not the DOM, so re-injection on the same page is still a no-op.if(s&&s.parentNode).Workaround removed
Previously users had to use
next/scriptwithstrategy="beforeInteractive"to dodge the mismatch. No longer needed.Test plan
cargo test -p veld-helper caddy::tests::test_bootstrap— 13 passed (includes newtest_bootstrap_script_self_removescovering all 3 feature combos)cargo build -p veld-helperclean