-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
clientEntry.tsx
74 lines (64 loc) · 1.91 KB
/
clientEntry.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* 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, {startTransition} from 'react';
import ReactDOM, {type ErrorInfo} from 'react-dom/client';
import {BrowserRouter} from 'react-router-dom';
import {HelmetProvider} from 'react-helmet-async';
import ExecutionEnvironment from './exports/ExecutionEnvironment';
import App from './App';
import preload from './preload';
import docusaurus from './docusaurus';
declare global {
interface NodeModule {
hot?: {accept: () => void};
}
}
const hydrate = Boolean(process.env.HYDRATE_CLIENT_ENTRY);
// Client-side render (e.g: running in browser) to become single-page
// application (SPA).
if (ExecutionEnvironment.canUseDOM) {
window.docusaurus = docusaurus;
const container = document.getElementById('__docusaurus')!;
const app = (
<HelmetProvider>
<BrowserRouter>
<App />
</BrowserRouter>
</HelmetProvider>
);
const onRecoverableError = (error: unknown, errorInfo: ErrorInfo): void => {
console.error(
'Docusaurus React Root onRecoverableError:',
error,
errorInfo,
);
};
const renderApp = () => {
if (window.docusaurusRoot) {
window.docusaurusRoot.render(app);
return;
}
if (hydrate) {
window.docusaurusRoot = ReactDOM.hydrateRoot(container, app, {
onRecoverableError,
});
} else {
const root = ReactDOM.createRoot(container, {onRecoverableError});
root.render(app);
window.docusaurusRoot = root;
}
};
preload(window.location.pathname).then(() => {
startTransition(renderApp);
});
// Webpack Hot Module Replacement API
if (module.hot) {
// Self-accepting method/ trick
// (https://github.com/webpack/webpack-dev-server/issues/100#issuecomment-290911036)
module.hot.accept();
}
}