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

feat(v2): add a way to exclude components from build-time prerendering #2323

Merged
merged 11 commits into from
Mar 29, 2020
17 changes: 17 additions & 0 deletions packages/docusaurus/src/client/exports/BrowserOnly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';

function BrowserOnly({children}) {
return (
ExecutionEnvironment.canUseDOM && children != null && <>{children()}</>
);
}

export default BrowserOnly;
12 changes: 12 additions & 0 deletions website/docs/docusaurus-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ function Home() {
}
```

### `<BrowserOnly/>`

This component excludes its `children` during the prerendering stage of the build process. This is useful for hiding code that is only meant to run in the browsers (e.g. where the `window`/`document` objects are being accessed).

```jsx {1,7}
lex111 marked this conversation as resolved.
Show resolved Hide resolved
import BrowserOnly from '@docusaurus/BrowserOnly';

<BrowserOnly>
{/* Something that should be excluded during build process prerendering. */}
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to give an example to show that children should be a render function and not normal children.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, done ✔️

</BrowserOnly>;
```

## Hooks

### `useDocusaurusContext`
Expand Down