Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CodeExample block SSRd for better SEO. #26829

Merged
merged 5 commits into from
Jan 4, 2025
Merged

Conversation

salazarm
Copy link
Contributor

@salazarm salazarm commented Jan 4, 2025

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

@salazarm salazarm force-pushed the salazarm/ssr-code-example branch from 26c2f14 to 9d70d10 Compare January 4, 2025 00:24
Copy link

github-actions bot commented Jan 4, 2025

@salazarm
Copy link
Contributor Author

salazarm commented Jan 4, 2025

One unexpected benefit of this approach is that we can now make invalid code examples cause the build to fail. I tried to fix a few but then realized some examples were just completely deleted (not just moved) so I wrapped the require call in try/catch. Once we fix those bad code examples we should remove the try/catch.

Here are the bad examples (cc @neverett):

Error: Cannot find module './docs_beta_snippets/docs_beta_snippets/guides/automation/multi-asset-sensor.py'

Error: Cannot find module './docs_beta_snippets/docs_beta_snippets/guides/build/create-asset-pipelines/data-assets/quality-testing/asset-checks/single-asset-check.py'

Error: Cannot find module './docs_beta_snippets/docs_beta_snippets/guides/build/create-asset-pipelines/data-assets/quality-testing/asset-checks/multiple-asset-checks.py'

Error: Cannot find module './docs_beta_snippets/docs_beta_snippets/guides/build/create-asset-pipelines/data-assets/quality-testing/asset-checks/asset-checks-factory.py'

Error: Cannot find module './docs_beta_snippets/docs_beta_snippets/guides/build/create-asset-pipelines/data-assets/quality-testing/asset-checks/block-downstream-with-asset-checks.py'

Error: Cannot find module './docs_beta_snippets/docs_beta_snippets/guides/build/create-asset-pipelines/data-assets/quality-testing/asset-checks/asset-checks-with-schedule-and-sensor.py'

@salazarm salazarm requested a review from yuhan January 4, 2025 01:07
@salazarm salazarm merged commit 151d6a9 into master Jan 4, 2025
2 of 3 checks passed
@salazarm salazarm deleted the salazarm/ssr-code-example branch January 4, 2025 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants