Skip to content

fix(live-codeblock): render static codeblock server-side#5754

Merged
slorber merged 15 commits intofacebook:mainfrom
Josh-Cena:ssr-live-codeblock
Oct 21, 2021
Merged

fix(live-codeblock): render static codeblock server-side#5754
slorber merged 15 commits intofacebook:mainfrom
Josh-Cena:ssr-live-codeblock

Conversation

@Josh-Cena
Copy link
Collaborator

@Josh-Cena Josh-Cena commented Oct 21, 2021

Motivation

Resolve #5747. I think a static code block pretty closely resembles the live code block, so we can just use that.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

With JS disabled:

Before After

image

image

This is not a fix to make live editor any more functional without JS though, so I don't think making it editable or using the actual LiveEditor comp is worthwhile. As long as we get the code block server-side rendered, it should be good.

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Oct 21, 2021
@netlify
Copy link

netlify bot commented Oct 21, 2021

✔️ [V2]

🔨 Explore the source changes: 3466069

🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-2/deploys/61717dd32158f9000846785a

😎 Browse the preview: https://deploy-preview-5754--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented Oct 21, 2021

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟢 Performance 90
🟢 Accessibility 98
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-5754--docusaurus-2.netlify.app/

@ntucker
Copy link
Contributor

ntucker commented Oct 21, 2021

This is a good improvement. I think in addition to this it may be desirable to put the wrapper that says 'live editor' and such so the layout doesn't jitter too much. If it could somehow be rendered instead of the prism editor.

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
@Josh-Cena
Copy link
Collaborator Author

This is a good improvement. I think in addition to this it may be desirable to put the wrapper that says 'live editor' and such so the layout doesn't jitter too much. If it could somehow be rendered instead of the prism editor.

Good point. That makes this fix slightly more complex, but looks good

@slorber
Copy link
Collaborator

slorber commented Oct 21, 2021

Test link: https://deploy-preview-5754--docusaurus-2.netlify.app/docs/next/markdown-features/code-blocks#interactive-code-editor

IMHO we should render the whole live layout on the server, with the playground showing a temp spinner (we don't have a spinner comp yet, but ... would be fine temporarily)

</LiveProvider>
</div>
// https://github.com/facebook/docusaurus/issues/5747
<BrowserOnly
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure exactly why we need this fallback.

Without BrowserOnly, what exactly doesn't work? What are the components throwing, not working or leading to weird side-effects?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Josh-Cena
Copy link
Collaborator Author

Josh-Cena commented Oct 21, 2021

IMHO we should render the whole live layout on the server, with the playground showing a temp spinner (we don't have a spinner comp yet, but ... would be fine temporarily)

Okay, let's make another StaticOutput :P

jk, I will just make rendering the Live comps browser only.

Edit. The code block doesn't work well with the additional output. I will figure out how to make the style more in line with the editor comp.

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
<div className={styles.playgroundPreview}>
<LivePreview />
<LiveError />
<BrowserOnly fallback={<div>Loading...</div>}>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we translate this? Feel like for a normal user this won't even be visible.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem very important. Eventually, we'll improve it later and add some kind of CSS spinner.

</Translate>
</Header>
<LiveEditor className={styles.playgroundEditor} />
<BrowserOnly
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to restore this, because LiveEditor can render on the server, and it is better to not use an alternate rendering (your solution also worked but produced more layout shifts due to different padding)

Before After

image

image

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if that would lead to the context's value being updated, but maybe because the consumer isn't rendered yet, it doesn't actually cause the re-render problem?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The double-rendering issue that was reported (#2986) was about the live preview, not the editor.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I thought rendering the editor would also trigger a re-render once the consumer (output) gets mounted. But I think I was just getting it wrong🌚

slorber and others added 2 commits October 21, 2021 16:26
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
@slorber
Copy link
Collaborator

slorber commented Oct 21, 2021

@Josh-Cena
Copy link
Collaborator Author

The live editor is not remounted in dark mode, so it stays white.

And if we remount it, we will trigger a re-render in the consumer as well 😅

@slorber
Copy link
Collaborator

slorber commented Oct 21, 2021

Good catch 🤪

Remounting just the editor does not trigger double-render in the live preview and seems to fix the theming issue

Let me know if you see another issue now

@Josh-Cena
Copy link
Collaborator Author

LGTM now. Thanks for the polishment :D

@slorber slorber merged commit 1c8b836 into facebook:main Oct 21, 2021
@Josh-Cena Josh-Cena deleted the ssr-live-codeblock branch October 21, 2021 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Signed Facebook CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Live Code Editor be Server Side Rendered

4 participants