Make CodeExample block SSRd for better SEO. #26829
Merged
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 & Motivation
We want to make sure that the CodeExample block is included in the statically generated HTML for the site so that it's included in SEO. Currently since we use a
useEffect
we only fetch the content on the client.To fix this we need to make sure we have the content on the server. To this end I added a check to see if we're on the server and if we are then we synchronously require the content for the codeblock inline during render and use it. Otherwise, if we're on the client, we import the content dynamically and rely on Suspense to avoid unmounting the SSR'd content during the initial render.
How I Tested These Changes
yarn build
yarn serve
Looked at the source in
view-source:http://localhost:3002/
and confirmed the HTML includes the codeblocks.There's a brief flash of the light theme but confirmed on the docs-preview site that this is in an existing issue and unrelated to my approach.
Screen.Recording.2025-01-03.at.7.22.01.PM.mov