Skip to content
This repository was archived by the owner on Oct 17, 2025. It is now read-only.

Commit 3ad4923

Browse files
committed
admin ファイル分割
1 parent fe75d8c commit 3ad4923

6 files changed

Lines changed: 42 additions & 20 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import ReactDOM from 'react-dom/client';
2+
import { BrowserRouter } from 'react-router-dom';
3+
import { SWRConfig } from 'swr';
4+
5+
import { AdminApp } from '@wsh-2024/admin/src/index';
6+
import { ClientApp } from '@wsh-2024/app/src/index';
7+
8+
import { preloadImages } from './utils/preloadImages';
9+
import { registerServiceWorker } from './utils/registerServiceWorker';
10+
11+
const main = async () => {
12+
await registerServiceWorker();
13+
// await preloadImages();
14+
15+
const fn = () => {
16+
ReactDOM.createRoot(document.getElementById('root')!).render(<AdminApp />);
17+
};
18+
19+
if (document.readyState !== 'loading') {
20+
fn();
21+
} else {
22+
document.addEventListener('DOMContentLoaded', fn);
23+
}
24+
};
25+
26+
main().catch(console.error);

workspaces/client/src/index.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,14 @@ const main = async () => {
1313
// await preloadImages();
1414

1515
const fn = () => {
16-
if (window.location.pathname.startsWith('/admin')) {
17-
ReactDOM.createRoot(document.getElementById('root')!).render(<AdminApp />);
18-
} else {
19-
ReactDOM.hydrateRoot(
20-
document.getElementById('root')!,
21-
<SWRConfig value={{ revalidateIfStale: true, revalidateOnFocus: false, revalidateOnReconnect: false }}>
22-
<BrowserRouter>
23-
<ClientApp />
24-
</BrowserRouter>
25-
</SWRConfig>,
26-
);
27-
}
16+
ReactDOM.hydrateRoot(
17+
document.getElementById('root')!,
18+
<SWRConfig value={{ revalidateIfStale: true, revalidateOnFocus: false, revalidateOnReconnect: false }}>
19+
<BrowserRouter>
20+
<ClientApp />
21+
</BrowserRouter>
22+
</SWRConfig>,
23+
);
2824
};
2925

3026
if (document.readyState !== 'loading') {

workspaces/client/tsup.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default defineConfig(async (): Promise<Options[]> => {
2121
bundle: true,
2222
clean: true,
2323
entry: {
24+
admin: path.resolve(PACKAGE_DIR, './src/index.admin.tsx'),
2425
client: path.resolve(PACKAGE_DIR, './src/index.tsx'),
2526
serviceworker: path.resolve(PACKAGE_DIR, './src/serviceworker/index.ts'),
2627
},

workspaces/server/src/constants/paths.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const IMAGES_PATH = path.resolve(PACKAGE_DIR, './dist/images');
1414

1515
export const CLIENT_STATIC_PATH = path.resolve(WORKSPACE_DIR, './workspaces/client/dist');
1616

17-
export const INDEX_HTML = `<!doctype html>
17+
export const INDEX_HTML = (isAdmin: boolean) => `<!doctype html>
1818
<html lang="ja">
1919
<head>
2020
<meta charset="UTF-8" />
@@ -26,7 +26,7 @@ export const INDEX_HTML = `<!doctype html>
2626
type="image/x-icon"
2727
/>
2828
<title>WSH 2024</title>
29-
<script type="text/javascript" src="/client.global.js" defer></script>
29+
<script type="text/javascript" src="/${isAdmin ? 'admin' : 'client'}.global.js" defer></script>
3030
<style id="tag"></style>
3131
</head>
3232
<body>

workspaces/server/src/routes/admin/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { INDEX_HTML } from '../../constants/paths';
55
const app = new Hono();
66

77
app.get('/admin', async (c) => {
8-
return c.html(INDEX_HTML);
8+
return c.html(INDEX_HTML(true));
99
});
1010

1111
app.get('/admin/*', async (c) => {
12-
return c.html(INDEX_HTML);
12+
return c.html(INDEX_HTML(true));
1313
});
1414

1515
export { app as adminApp };

workspaces/server/src/routes/ssr/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ import { INDEX_HTML } from '../../constants/paths';
1111
const app = new Hono();
1212

1313
async function createHTML({ body, styleTags }: { body: string; styleTags: string }): Promise<string> {
14-
const content = INDEX_HTML.replaceAll('<div id="root"></div>', `<div id="root">${body}</div>`).replaceAll(
15-
'<style id="tag"></style>',
16-
styleTags,
17-
);
14+
const content = INDEX_HTML(false)
15+
.replaceAll('<div id="root"></div>', `<div id="root">${body}</div>`)
16+
.replaceAll('<style id="tag"></style>', styleTags);
1817

1918
return content;
2019
}

0 commit comments

Comments
 (0)