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

Disable hydrating server styles component #223

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-phones-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'docusaurus-theme-redoc': patch
---

Fix flash of unstyled content
1 change: 1 addition & 0 deletions packages/docusaurus-theme-redoc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"copyfiles": "^2.4.1",
"lodash": "^4.17.21",
"mobx": "^6.5.0",
"react-hydration-on-demand": "^2.1.0",
"redoc": "2.0.0-rc.72",
"styled-components": "^5.3.5"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import '../../global';
import useBaseUrl from '@docusaurus/useBaseUrl';
import withHydrationOnDemand from 'react-hydration-on-demand';
import { AppStore, Redoc, RedocRawOptions } from 'redoc';
// eslint-disable-next-line import/no-extraneous-dependencies
import { renderToString } from 'react-dom/server';
Expand Down Expand Up @@ -53,7 +54,7 @@ const prefixCssSelectors = function (rules: string, className: string) {
const LIGHT_MODE_PREFIX = "html:not([data-theme='dark'])";
const DARK_MODE_PREFIX = "html([data-theme='dark'])";

export function ServerStyles({
function ServerStylesComponent({
specProps,
lightThemeOptions,
darkThemeOptions,
Expand Down Expand Up @@ -102,3 +103,5 @@ export function ServerStyles({
</div>
);
}

export const ServerStyles = withHydrationOnDemand()(ServerStylesComponent);
5 changes: 4 additions & 1 deletion packages/docusaurus-theme-redoc/src/theme/Redoc/Styles.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import React from 'react';
import withHydrationOnDemand from 'react-hydration-on-demand';
import '../../global';
import type { RedocRawOptions } from 'redoc';

/**
* Don't hydrate/replace server styles
* @see https://github.com/facebook/react/issues/10923#issuecomment-338715787
*/
export function ServerStyles(_props: {
function ServerStylesComponent(_props: {
specProps: SpecProps;
lightThemeOptions: RedocRawOptions;
darkThemeOptions: RedocRawOptions;
}) {
return <div className="redocusaurus-styles"></div>;
}

export const ServerStyles = withHydrationOnDemand()(ServerStylesComponent);
1 change: 1 addition & 0 deletions packages/docusaurus-theme-redoc/src/types/compat.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'react-hydration-on-demand';
13 changes: 11 additions & 2 deletions packages/redocusaurus/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
1. Install redocusaurus:

```sh
npm i --save redocusaurus
npm i --save redocusaurus babel-plugin-styled-components@^2
# OR
yarn add redocusaurus
yarn add redocusaurus babel-plugin-styled-components@^2
```

1. Add it as a preset to your docusaurus config along with [@docusaurus/preset-classic](https://docusaurus.io/docs/using-plugins#docusauruspreset-classic) and pass options:
Expand Down Expand Up @@ -82,6 +82,15 @@
};
```

2. Add `babel-plugin-styled-components` to the plugins array of your `babel.config.js`:

```babel.config.js
module.exports = {
presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
plugins: ['babel-plugin-styled-components']
};
```

The API Doc will be available at the route specified (`/api/` in the example above). To customize it see [full plugin options](https://redocusaurus.vercel.app/docs/getting-started/plugin-options).

### Options
Expand Down
1 change: 1 addition & 0 deletions website/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
plugins: ['babel-plugin-styled-components']
};
11 changes: 10 additions & 1 deletion website/docs/getting-started/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ author_url: https://rohit.page
```sh
npm i --save redocusaurus
# OR
yarn add redocusaurus
yarn add redocusaurus babel-plugin-styled-components@^2
```

1. Add it as a preset to your docusaurus config along with [@docusaurus/preset-classic](https://docusaurus.io/docs/using-plugins#docusauruspreset-classic) and pass options:
Expand Down Expand Up @@ -83,6 +83,15 @@ yarn add redocusaurus
};
```

2. Add `babel-plugin-styled-components` to the plugins array of your `babel.config.js`:

```babel.config.js
module.exports = {
presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
plugins: ['babel-plugin-styled-components']
};
```

The API Doc will be available at the path specific by `route`. To skip adding a
route altogether just don't set the `route` property. You will still be
able to reference schema elements manually using [Schema Imports](/docs/guides/schema-imports) or create Custom React Pages using the data and theme components.
Expand Down
3 changes: 2 additions & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
},
"devDependencies": {
"@percy/cli": "^1.3.0",
"@types/react": "^17.0.45"
"@types/react": "^17.0.45",
"babel-plugin-styled-components": "^2.0.7"
},
"browserslist": {
"production": [
Expand Down
Loading