Skip to content

Commit

Permalink
admin ファイル分割
Browse files Browse the repository at this point in the history
  • Loading branch information
a01sa01to committed Mar 23, 2024
1 parent fe75d8c commit 3ad4923
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 20 deletions.
26 changes: 26 additions & 0 deletions workspaces/client/src/index.admin.tsx
@@ -0,0 +1,26 @@
import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import { SWRConfig } from 'swr';

import { AdminApp } from '@wsh-2024/admin/src/index';
import { ClientApp } from '@wsh-2024/app/src/index';

import { preloadImages } from './utils/preloadImages';
import { registerServiceWorker } from './utils/registerServiceWorker';

const main = async () => {
await registerServiceWorker();
// await preloadImages();

const fn = () => {
ReactDOM.createRoot(document.getElementById('root')!).render(<AdminApp />);
};

if (document.readyState !== 'loading') {
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
};

main().catch(console.error);
20 changes: 8 additions & 12 deletions workspaces/client/src/index.tsx
Expand Up @@ -13,18 +13,14 @@ const main = async () => {
// await preloadImages();

const fn = () => {
if (window.location.pathname.startsWith('/admin')) {
ReactDOM.createRoot(document.getElementById('root')!).render(<AdminApp />);
} else {
ReactDOM.hydrateRoot(
document.getElementById('root')!,
<SWRConfig value={{ revalidateIfStale: true, revalidateOnFocus: false, revalidateOnReconnect: false }}>
<BrowserRouter>
<ClientApp />
</BrowserRouter>
</SWRConfig>,
);
}
ReactDOM.hydrateRoot(
document.getElementById('root')!,
<SWRConfig value={{ revalidateIfStale: true, revalidateOnFocus: false, revalidateOnReconnect: false }}>
<BrowserRouter>
<ClientApp />
</BrowserRouter>
</SWRConfig>,
);
};

if (document.readyState !== 'loading') {
Expand Down
1 change: 1 addition & 0 deletions workspaces/client/tsup.config.ts
Expand Up @@ -21,6 +21,7 @@ export default defineConfig(async (): Promise<Options[]> => {
bundle: true,
clean: true,
entry: {
admin: path.resolve(PACKAGE_DIR, './src/index.admin.tsx'),
client: path.resolve(PACKAGE_DIR, './src/index.tsx'),
serviceworker: path.resolve(PACKAGE_DIR, './src/serviceworker/index.ts'),
},
Expand Down
4 changes: 2 additions & 2 deletions workspaces/server/src/constants/paths.ts
Expand Up @@ -14,7 +14,7 @@ export const IMAGES_PATH = path.resolve(PACKAGE_DIR, './dist/images');

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

export const INDEX_HTML = `<!doctype html>
export const INDEX_HTML = (isAdmin: boolean) => `<!doctype html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
Expand All @@ -26,7 +26,7 @@ export const INDEX_HTML = `<!doctype html>
type="image/x-icon"
/>
<title>WSH 2024</title>
<script type="text/javascript" src="/client.global.js" defer></script>
<script type="text/javascript" src="/${isAdmin ? 'admin' : 'client'}.global.js" defer></script>
<style id="tag"></style>
</head>
<body>
Expand Down
4 changes: 2 additions & 2 deletions workspaces/server/src/routes/admin/index.ts
Expand Up @@ -5,11 +5,11 @@ import { INDEX_HTML } from '../../constants/paths';
const app = new Hono();

app.get('/admin', async (c) => {
return c.html(INDEX_HTML);
return c.html(INDEX_HTML(true));
});

app.get('/admin/*', async (c) => {
return c.html(INDEX_HTML);
return c.html(INDEX_HTML(true));
});

export { app as adminApp };
7 changes: 3 additions & 4 deletions workspaces/server/src/routes/ssr/index.tsx
Expand Up @@ -11,10 +11,9 @@ import { INDEX_HTML } from '../../constants/paths';
const app = new Hono();

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

return content;
}
Expand Down

0 comments on commit 3ad4923

Please sign in to comment.