Skip to content

Commit 2eda74e

Browse files
authored
fix: resolve hydration mismatch for keyword interpolation in code blocks (#15473)
<!-- Use this checklist to make sure your PR is ready for merge. You may delete any sections you don't need. --> ## DESCRIBE YOUR PR Fixing an issue where a hydration error renders just a string without keyword processing in a code snippet. Added a hook to defer keyword interpolation until after component mount to prevent server/client HTML mismatch. This way both server and client render identical initial HTML, with keywords being upgraded client-side after hydration. ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE --> - [ ] Other deadline: <!-- ENTER DATE HERE --> - [ ] None: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it. Thanks in advance for your help! ## PRE-MERGE CHECKLIST *Make sure you've checked the following before merging your changes:* - [ ] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs) ## LEGAL BOILERPLATE <!-- Sentry employees and contractors can delete or ignore this section. --> Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms. ## EXTRA RESOURCES - [Sentry Docs contributor guide](https://docs.sentry.io/contributing/)
1 parent bf63d97 commit 2eda74e

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/components/codeBlock/index.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ export function CodeBlock({filename, language, children}: CodeBlockProps) {
5757
// Show the copy button after js has loaded
5858
// otherwise the copy button will not work
5959
const [showCopyButton, setShowCopyButton] = useState(false);
60+
// Track if component is mounted to prevent hydration mismatch with keyword interpolation
61+
const [isMounted, setIsMounted] = useState(false);
6062
const {emit} = usePlausibleEvent();
6163

6264
useEffect(() => {
6365
setShowCopyButton(true);
66+
setIsMounted(true);
6467
// prevent .no-copy elements from being copied during selection Right click copy or / Cmd+C
6568
const noCopyElements = codeRef.current?.querySelectorAll<HTMLSpanElement>('.no-copy');
6669
const handleSelectionChange = () => {
@@ -102,6 +105,15 @@ export function CodeBlock({filename, language, children}: CodeBlockProps) {
102105
}
103106
}
104107

108+
// Process children to add highlighting
109+
const highlightedChildren = makeHighlightBlocks(children, language);
110+
111+
// Only apply keyword interpolation after component mounts to prevent hydration mismatch
112+
// Server and client both render raw text initially, then client upgrades after mount
113+
const processedChildren = isMounted
114+
? makeKeywordsClickable(highlightedChildren)
115+
: highlightedChildren;
116+
105117
return (
106118
<div className={styles['code-block']}>
107119
<div className={styles['code-actions']}>
@@ -119,9 +131,7 @@ export function CodeBlock({filename, language, children}: CodeBlockProps) {
119131
>
120132
Copied
121133
</div>
122-
<div ref={codeRef}>
123-
{makeKeywordsClickable(makeHighlightBlocks(children, language))}
124-
</div>
134+
<div ref={codeRef}>{processedChildren}</div>
125135
</div>
126136
);
127137
}

0 commit comments

Comments
 (0)