Skip to content

Commit

Permalink
fix: double render in studio mode
Browse files Browse the repository at this point in the history
  • Loading branch information
acaldas committed Nov 6, 2024
1 parent a0841b3 commit f2f78e4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
21 changes: 21 additions & 0 deletions src/components/app-loader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { lazy, Suspense } from 'react';
import '../i18n';
import '../index.css';
import { DocumentEditorDebugTools } from '../utils/document-editor-debug-tools';
import serviceWorkerManager from '../utils/registerServiceWorker';

if (import.meta.env.MODE === 'development') {
window.documentEditorDebugTools = new DocumentEditorDebugTools();
} else {
serviceWorkerManager.registerServiceWorker(false);
}

const App = lazy(() => import('./app'));

const AppLoader = (
<Suspense>
<App />
</Suspense>
);

export default AppLoader;
4 changes: 3 additions & 1 deletion src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const Preloader = () => {
return null;
};

export default (
const App = () => (
<React.StrictMode>
<Suspense fallback={<>{/* TODO loading */}</>}>
<Provider store={atomStore}>
Expand All @@ -49,3 +49,5 @@ export default (
</Suspense>
</React.StrictMode>
);

export default App;
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createRoot } from 'react-dom/client';

async function renderApp(element: HTMLElement) {
const AppLoader = await import('./components/app-loader');
createRoot(element).render(AppLoader.default);
}

const AppElement = document.getElementById('app');
if (!AppElement) {
throw new Error('#app element not found!');
}

renderApp(AppElement).catch(console.error);
20 changes: 2 additions & 18 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,6 @@
* });
* ```
*/
import { createRoot } from 'react-dom/client';
import App from './components/app';
import './i18n';
import './index.css';
import { DocumentEditorDebugTools } from './utils/document-editor-debug-tools';
import serviceWorkerManager from './utils/registerServiceWorker';

const AppElement = document.getElementById('app');
if (!AppElement) {
throw new Error('#app element not found!');
}

if (import.meta.env.MODE === 'development') {
window.documentEditorDebugTools = new DocumentEditorDebugTools();
} else {
serviceWorkerManager.registerServiceWorker(false);
}

createRoot(AppElement).render(App);
// splits app code into separate chunk to avoid circular dependencies
import('./index');

0 comments on commit f2f78e4

Please sign in to comment.