Skip to content

Commit

Permalink
Fix server build styles (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-gohri committed Mar 13, 2022
1 parent ad66f92 commit d2438dd
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 118 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-trainers-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'docusaurus-theme-redoc': major
---

Enable server style sheet collection by default, fixes style issues on fresh load
10 changes: 9 additions & 1 deletion packages/docusaurus-theme-redoc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function redocTheme(
): Plugin<null> {
return {
name: 'docusaurus-theme-redoc',
configureWebpack() {
configureWebpack(_config, isServer) {
return {
resolve: {
fallback: {
Expand All @@ -29,6 +29,14 @@ export default function redocTheme(
),
}),
new NodePolyfillPlugin(),
...(isServer
? [
new webpack.NormalModuleReplacementPlugin(
/theme\/Redoc\/Styles\.jsx/,
path.join(__dirname, 'theme', 'Redoc', 'ServerStyles.js'),
),
]
: []),
],
};
},
Expand Down
43 changes: 0 additions & 43 deletions packages/docusaurus-theme-redoc/src/merge.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/docusaurus-theme-redoc/src/redocData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RedocRawOptions } from 'redoc';
import { merge } from './merge';
import merge from 'lodash/merge';
import { GlobalData, RedocThemeOverrides, ThemeOptions } from './types/options';

const defaultOptions: Partial<RedocRawOptions> = {
Expand Down
10 changes: 7 additions & 3 deletions packages/docusaurus-theme-redoc/src/theme/Redoc/Redoc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { Redoc as RedocComponent } from 'redoc';
import { SpecProps } from '../../types/common';
import { useSpec } from '../../hooks/useSpec';
import { ServerStyles } from './Styles';
import './styles.css';

/*!
Expand All @@ -14,9 +15,12 @@ function Redoc(props: SpecProps): JSX.Element {
const { store } = useSpec(props);

return (
<div className="redocusaurus">
<RedocComponent store={store} />
</div>
<>
<ServerStyles store={store} />
<div className="redocusaurus">
<RedocComponent store={store} />
</div>
</>
);
}

Expand Down
20 changes: 20 additions & 0 deletions packages/docusaurus-theme-redoc/src/theme/Redoc/ServerStyles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import { AppStore, Redoc } from 'redoc';
// eslint-disable-next-line import/no-extraneous-dependencies
import { renderToString } from 'react-dom/server';
import { ServerStyleSheet } from 'styled-components';

export function ServerStyles({ store }: { store: AppStore }) {
const sheet = new ServerStyleSheet();
renderToString(sheet.collectStyles(React.createElement(Redoc, { store })));
const css = sheet.getStyleTags();

return (
<div
className="redocusaurus-styles"
dangerouslySetInnerHTML={{
__html: css,
}}
/>
);
}
7 changes: 7 additions & 0 deletions packages/docusaurus-theme-redoc/src/theme/Redoc/Styles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { AppStore } from 'redoc';

function ClientStyles(_props: { store: AppStore }) {
return null;
}

export { ClientStyles as ServerStyles };
49 changes: 0 additions & 49 deletions packages/docusaurus-theme-redoc/src/theme/ServerStyle/index.tsx

This file was deleted.

8 changes: 0 additions & 8 deletions packages/docusaurus-theme-redoc/src/types/modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ interface SpecProps {
url?: string;
}

declare module '@theme/ServerStyle' {
/**
* @see https://github.com/facebook/docusaurus/issues/3236#issuecomment-788953743
*/
const ServerStyle: (props: { from: React.ReactNode }) => JSX.Element;
export default ServerStyle;
}

declare module '@theme/Redoc' {
const Redoc: (props: SpecProps) => JSX.Element;
export default Redoc;
Expand Down
13 changes: 0 additions & 13 deletions website/src/theme/Root/index.tsx

This file was deleted.

0 comments on commit d2438dd

Please sign in to comment.