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' ) ;
91import { Hono } from 'hono' ;
102import { HTTPException } from 'hono/http-exception' ;
11- import jsesc from 'jsesc' ;
123import ReactDOMServer from 'react-dom/server' ;
134import { StaticRouter } from 'react-router-dom/server' ;
145import { 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' ;
207import { 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
2511const 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
7722app . 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