Skip to content

Commit ee144a8

Browse files
Kyle McLarenclaude
andcommitted
Fix anchor links for React-rendered API method headers
The MethodHeader React component renders h2 headings with anchor links, but these appear after React island hydration. The initCodeBlocks script that sets up anchor link icons was running before hydration completed. Added a MutationObserver to detect when new .sl-anchor-link elements are added to the DOM and re-run the initialization to set up the dual-icon structure (link icon + checkmark for copy feedback). Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 6bac8fe commit ee144a8

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/components/Head.astro

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,35 @@ const breadcrumbSchema = {
183183
clearTimeout(resizeTimer);
184184
resizeTimer = setTimeout(initCodeBlocks, 100);
185185
});
186+
187+
// Watch for React islands hydrating and adding new anchor links
188+
// This handles MethodHeader components that render after initial page load
189+
const anchorObserver = new MutationObserver((mutations) => {
190+
for (const mutation of mutations) {
191+
for (const node of mutation.addedNodes) {
192+
if (node.nodeType === Node.ELEMENT_NODE) {
193+
// Check if the added node is or contains an anchor link
194+
const anchors = node.classList?.contains('sl-anchor-link')
195+
? [node]
196+
: node.querySelectorAll?.('.sl-anchor-link') || [];
197+
if (anchors.length > 0) {
198+
// Re-run init to set up the new anchor links
199+
initCodeBlocks();
200+
return; // Only need to run once per mutation batch
201+
}
202+
}
203+
}
204+
}
205+
});
206+
207+
// Start observing once DOM is ready
208+
if (document.readyState === 'loading') {
209+
document.addEventListener('DOMContentLoaded', () => {
210+
anchorObserver.observe(document.body, { childList: true, subtree: true });
211+
});
212+
} else {
213+
anchorObserver.observe(document.body, { childList: true, subtree: true });
214+
}
186215
</script>
187216

188217
<!-- Sidebar group navigation: click heading to expand and navigate to page -->

0 commit comments

Comments
 (0)