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

Commit f53696f

Browse files
committed
index.html の I/O 処理消し
1 parent 1d4670c commit f53696f

File tree

4 files changed

+30
-92
lines changed

4 files changed

+30
-92
lines changed

workspaces/server/index.html

Lines changed: 0 additions & 21 deletions
This file was deleted.

workspaces/server/src/constants/paths.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,23 @@ 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_PATH = path.resolve(PACKAGE_DIR, './index.html');
17+
export const INDEX_HTML = `<!doctype html>
18+
<html lang="ja">
19+
<head>
20+
<meta charset="UTF-8" />
21+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
22+
<meta name="robots" content="noindex, nofollow" />
23+
<link
24+
href="/assets/favicon.ico"
25+
rel="icon"
26+
type="image/x-icon"
27+
/>
28+
<title>WSH 2024</title>
29+
<script type="text/javascript" src="/client.global.js" defer></script>
30+
<style id="tag"></style>
31+
</head>
32+
<body>
33+
<div id="root"></div>
34+
</body>
35+
</html>
36+
`;
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
import fs from 'node:fs/promises';
2-
31
import { Hono } from 'hono';
42

5-
import { INDEX_HTML_PATH } from '../../constants/paths';
3+
import { INDEX_HTML } from '../../constants/paths';
64

75
const app = new Hono();
86

97
app.get('/admin', async (c) => {
10-
const html = await fs.readFile(INDEX_HTML_PATH, 'utf-8');
11-
return c.html(html);
8+
return c.html(INDEX_HTML);
129
});
1310

1411
app.get('/admin/*', async (c) => {
15-
const html = await fs.readFile(INDEX_HTML_PATH, 'utf-8');
16-
return c.html(html);
12+
return c.html(INDEX_HTML);
1713
});
1814

1915
export { app as adminApp };

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

Lines changed: 7 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,25 @@
1-
import fs from 'node:fs/promises';
2-
3-
import dayjs from 'dayjs';
4-
import tz from 'dayjs/plugin/timezone';
5-
import utc from 'dayjs/plugin/utc';
6-
dayjs.extend(utc);
7-
dayjs.extend(tz);
8-
dayjs.tz.setDefault('Asia/Tokyo');
91
import { Hono } from 'hono';
102
import { HTTPException } from 'hono/http-exception';
11-
import jsesc from 'jsesc';
123
import ReactDOMServer from 'react-dom/server';
134
import { StaticRouter } from 'react-router-dom/server';
145
import { ServerStyleSheet } from 'styled-components';
15-
import { unstable_serialize } from 'swr';
166

17-
import { featureApiClient } from '@wsh-2024/app/src/features/feature/apiClient/featureApiClient';
18-
import { rankingApiClient } from '@wsh-2024/app/src/features/ranking/apiClient/rankingApiClient';
19-
import { releaseApiClient } from '@wsh-2024/app/src/features/release/apiClient/releaseApiClient';
207
import { ClientApp } from '@wsh-2024/app/src/index';
21-
import { getDayOfWeekStr } from '@wsh-2024/app/src/lib/date/getDayOfWeekStr';
228

23-
import { INDEX_HTML_PATH } from '../../constants/paths';
9+
import { INDEX_HTML } from '../../constants/paths';
2410

2511
const app = new Hono();
2612

27-
async function createInjectDataStr(): Promise<Record<string, unknown>> {
28-
const json: Record<string, unknown> = {};
29-
30-
{
31-
const dayOfWeek = getDayOfWeekStr(dayjs.tz());
32-
const releases = await releaseApiClient.fetch({ params: { dayOfWeek } });
33-
json[unstable_serialize(releaseApiClient.fetch$$key({ params: { dayOfWeek } }))] = releases;
34-
}
35-
36-
{
37-
const features = await featureApiClient.fetchList({ query: {} });
38-
json[unstable_serialize(featureApiClient.fetchList$$key({ query: {} }))] = features;
39-
}
40-
41-
{
42-
const ranking = await rankingApiClient.fetchList({ query: {} });
43-
json[unstable_serialize(rankingApiClient.fetchList$$key({ query: {} }))] = ranking;
44-
}
45-
46-
return json;
47-
}
48-
49-
async function createHTML({
50-
body,
51-
injectData,
52-
styleTags,
53-
}: {
54-
body: string;
55-
injectData: Record<string, unknown>;
56-
styleTags: string;
57-
}): Promise<string> {
58-
const htmlContent = await fs.readFile(INDEX_HTML_PATH, 'utf-8');
59-
60-
const content = htmlContent
61-
.replaceAll('<div id="root"></div>', `<div id="root">${body}</div>`)
62-
.replaceAll('<style id="tag"></style>', styleTags)
63-
.replaceAll(
64-
'<script id="inject-data" type="application/json"></script>',
65-
`<script id="inject-data" type="application/json">
66-
${jsesc(injectData, {
67-
isScriptContext: true,
68-
json: true,
69-
minimal: true,
70-
})}
71-
</script>`,
72-
);
13+
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+
);
7318

7419
return content;
7520
}
7621

7722
app.get('*', async (c) => {
78-
const injectData = await createInjectDataStr();
7923
const sheet = new ServerStyleSheet();
8024

8125
try {
@@ -88,7 +32,7 @@ app.get('*', async (c) => {
8832
);
8933

9034
const styleTags = sheet.getStyleTags();
91-
const html = await createHTML({ body, injectData, styleTags });
35+
const html = await createHTML({ body, styleTags });
9236

9337
return c.html(html);
9438
} catch (cause) {

0 commit comments

Comments
 (0)