1
- import path , { dirname } from " path" ;
2
- import { fileURLToPath } from " url" ;
3
- import fastifyStatic from " @fastify/static" ;
4
- import fastify from " fastify" ;
5
- import { h } from " preact" ;
6
- import { renderToString } from " preact-render-to-string" ;
1
+ import path , { dirname } from ' path'
2
+ import { fileURLToPath } from ' url'
3
+ import fastifyStatic from ' @fastify/static'
4
+ import fastify from ' fastify'
5
+ import { h } from ' preact'
6
+ import { renderToString } from ' preact-render-to-string'
7
7
8
- const app = fastify ( { logger : true } ) ;
8
+ const app = fastify ( { logger : true } )
9
9
10
- const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
10
+ const __dirname = dirname ( fileURLToPath ( import . meta. url ) )
11
11
12
12
app . register ( fastifyStatic , {
13
- root : path . join ( __dirname , " out/client" ) ,
14
- prefix : " /client" ,
15
- } ) ;
13
+ root : path . join ( __dirname , ' out/client' ) ,
14
+ prefix : ' /client' ,
15
+ } )
16
16
17
17
app . register ( fastifyStatic , {
18
18
decorateReply : false ,
19
- root : path . join ( __dirname , " public" ) ,
20
- prefix : "/" ,
21
- } ) ;
19
+ root : path . join ( __dirname , ' public' ) ,
20
+ prefix : '/' ,
21
+ } )
22
22
23
23
async function mapComponent ( path , req , res ) {
24
- const appFile = await import ( " ./out/_app.js" ) ;
25
- const Component = await import ( path ) ;
24
+ const appFile = await import ( ' ./out/_app.js' )
25
+ const Component = await import ( path )
26
26
let pageProps = {
27
27
props : { } ,
28
- } ;
29
- if ( " getServerSideProps" in Component ) {
28
+ }
29
+ if ( ' getServerSideProps' in Component ) {
30
30
Object . assign (
31
31
pageProps ,
32
32
await Component . getServerSideProps ( {
33
33
request : req ,
34
34
response : res ,
35
35
query : req . query ,
36
36
} )
37
- ) ;
37
+ )
38
38
}
39
- const mod = " default" in appFile ? appFile . default : appFile ;
39
+ const mod = ' default' in appFile ? appFile . default : appFile
40
40
const FinalComponent = ( ) =>
41
41
h ( mod , {
42
42
Component : Component . default ,
43
43
pageProps : pageProps . props ,
44
- } ) ;
45
- const toString = renderToString ( h ( FinalComponent ) ) ;
44
+ } )
45
+ const toString = renderToString ( h ( FinalComponent ) )
46
46
return toString
47
47
. replace (
48
48
/ < \/ h e a d \> / ,
@@ -51,29 +51,29 @@ async function mapComponent(path, req, res) {
51
51
. replace (
52
52
/ < \/ b o d y \> / ,
53
53
`<script type="module" src="/client/${ path . replace (
54
- " ./out/" ,
55
- ""
54
+ ' ./out/' ,
55
+ ''
56
56
) } "></script>
57
57
<script type="application/json" id="pageProps">${ JSON . stringify (
58
58
pageProps . props
59
59
) } </script>
60
60
</body>`
61
- ) ;
61
+ )
62
62
}
63
63
64
- app . get ( "/" , async ( req , res ) => {
65
- res . header ( " content-type" , " text/html" ) ;
66
- return mapComponent ( " ./out/index.js" , req , res ) ;
67
- } ) ;
64
+ app . get ( '/' , async ( req , res ) => {
65
+ res . header ( ' content-type' , ' text/html' )
66
+ return mapComponent ( ' ./out/index.js' , req , res )
67
+ } )
68
68
69
- app . get ( " /devices" , async ( req , res ) => {
70
- res . header ( " content-type" , " text/html" ) ;
71
- return mapComponent ( " ./out/devices.js" , req , res ) ;
72
- } ) ;
69
+ app . get ( ' /devices' , async ( req , res ) => {
70
+ res . header ( ' content-type' , ' text/html' )
71
+ return mapComponent ( ' ./out/devices.js' , req , res )
72
+ } )
73
73
74
- app . get ( " /submit-rom" , async ( req , res ) => {
75
- res . header ( " content-type" , " text/html" ) ;
76
- return mapComponent ( " ./out/submit-rom.js" , req , res ) ;
77
- } ) ;
74
+ app . get ( ' /submit-rom' , async ( req , res ) => {
75
+ res . header ( ' content-type' , ' text/html' )
76
+ return mapComponent ( ' ./out/submit-rom.js' , req , res )
77
+ } )
78
78
79
- await app . listen ( { host : " 0.0.0.0" , port : 3000 } ) ;
79
+ await app . listen ( { host : ' 0.0.0.0' , port : 3000 } )
0 commit comments