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

Live Code Editor be Server Side Rendered #5747

Closed
ntucker opened this issue Oct 20, 2021 · 9 comments · Fixed by #5754
Closed

Live Code Editor be Server Side Rendered #5747

ntucker opened this issue Oct 20, 2021 · 9 comments · Fixed by #5754
Labels
proposal This issue is a proposal, usually non-trivial change

Comments

@ntucker
Copy link
Contributor

ntucker commented Oct 20, 2021

💥 Proposal

It would be nice for the Editor part of live editor to be server side rendered, that way it can be previewed before the editor loads as well as improving search engine optimization.

A preliminary investigation makes me unsure of why this even is, since there's an explicit part that keys the LiveEditor component based on whether it is server side or not.

Have you read the Contributing Guidelines on issues?

yes

@ntucker ntucker added status: needs triage This issue has not been triaged by maintainers proposal This issue is a proposal, usually non-trivial change labels Oct 20, 2021
@slorber slorber removed the status: needs triage This issue has not been triaged by maintainers label Oct 20, 2021
@slorber
Copy link
Collaborator

slorber commented Oct 20, 2021

Hmmm yes we should try to minimize layout shifts and render code in SSR.

Not sure it's possible for the preview, but at least it should be for the editor.

https://docusaurus.io/docs/next/markdown-features/code-blocks#interactive-code-editor

With JS:

image

Without JS:

image


This line looks suspicious, maybe we should get rid of it:

image

@Josh-Cena
Copy link
Collaborator

Code blocks need to be rendered client-side whatsoever because of Prism being client-side. #4318 removed SSR because double-evaluation can lead to side effects.

@ntucker
Copy link
Contributor Author

ntucker commented Oct 20, 2021

Code blocks that aren't live are evaluated during SSR - so why can't the SSR render produce the same HTML?

@Josh-Cena
Copy link
Collaborator

Code blocks that aren't live are evaluated during SSR

In a perfect world we could. I think we can render a normal code block (maybe another "dummy element" for the output) during SSR and only really "evaluate" the code block during CSR. Problem is, currently the "live code block" which looks deceptively like a code block is actually a completely different component that's part of a context provider, and rendering it inevitably means sending that context value to the renderer and producing side effects, making things a little more complicated that we may have expected

@ntucker
Copy link
Contributor Author

ntucker commented Oct 20, 2021

Can you elaborate on the side effects? I would love to understand the nature of this difficulty more.

@Josh-Cena
Copy link
Collaborator

Can you elaborate on the side effects?

You can check the comment for #4318 and the related issue for that. The code is here:

<div className={styles.playgroundContainer}>
<LiveProvider
key={isBrowser}
code={isBrowser ? children.replace(/\n$/, '') : ''}
transformCode={transformCode || ((code) => `${code};`)}
theme={prismTheme}
{...props}>
{playgroundPosition === 'top' ? (
<>
<ResultWithHeader />
<EditorWithHeader />
</>
) : (
<>
<EditorWithHeader />
<ResultWithHeader />
</>
)}
</LiveProvider>
</div>

The code-block-looking comp is actually EditorWithHeader provided by react-live, not the default one. So if you send the actual code in SSR, the context value would be updated when the SSR output arrives at the end user, and again after hydration. But I have a solution in mind with BrowserOnly and using its fallback

@ntucker
Copy link
Contributor Author

ntucker commented Oct 20, 2021

I already override my liveprovider and just removing the key and code forks worked fine so I guess I'll just use that for now.

After reading the issue thread I'm still very confused as to what issue people were having. I'm not realizing 'side-effect' means 'something else broken, rather than side-effect like global variable change. Other that than I'm still stumped.

On the plus side since pointing out the code conditional I was able to get what I wanted working!

@Josh-Cena
Copy link
Collaborator

@ntucker In the current situation, I just rendered a static code block during SSR. Does that look better than rendering nothing?

@ljharb
Copy link

ljharb commented Oct 21, 2021

Literally anything looks better than rendering nothing, no?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue is a proposal, usually non-trivial change
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants