11import { CONSTANTS , emitToHooked } from 'adex/hook'
22import { prepareRequest , prepareResponse } from 'adex/http'
3- import { renderToString , toStatic } from 'adex/ssr'
3+ import { toStatic } from 'adex/ssr'
4+ import { renderToString } from 'adex/utils/isomorphic'
45import { h } from 'preact'
56
7+ // @ts -expect-error injected by vite
8+ import { App } from 'virtual:adex:client'
9+
610// @ts -expect-error injected by vite
711import { routes as apiRoutes } from '~apiRoutes'
812// @ts -expect-error injected by vite
@@ -16,7 +20,8 @@ export async function handler(req, res) {
1620 prepareRequest ( req )
1721 prepareResponse ( res )
1822
19- const [ baseURL ] = req . url . split ( '?' )
23+ const [ url , search ] = req . url . split ( '?' )
24+ const baseURL = normalizeRequestUrl ( url )
2025
2126 const { metas, links, title, lang } = toStatic ( )
2227
@@ -50,10 +55,15 @@ export async function handler(req, res) {
5055 } )
5156
5257 if ( matchedInPages ) {
53- const module = await matchedInPages . module ( )
54- const render = 'default' in module ? module . default : module
5558 const routeParams = getRouteParams ( baseURL , matchedInPages )
5659
60+ // @ts -expect-error
61+ global . location = new URL ( req . url , 'http://localhost' )
62+
63+ const rendered = await renderToString (
64+ h ( App , { url : [ baseURL , search ] . filter ( Boolean ) . join ( '?' ) } )
65+ )
66+
5767 const htmlString = HTMLTemplate ( {
5868 metas,
5969 links,
@@ -63,7 +73,7 @@ export async function handler(req, res) {
6373 routeParams : Buffer . from ( JSON . stringify ( routeParams ) , 'utf8' ) . toString (
6474 'base64'
6575 ) ,
66- body : renderToString ( h ( render , { routeParams } ) ) ,
76+ body : rendered . html ,
6777 } )
6878 const modifiableContext = {
6979 req : req ,
@@ -105,13 +115,7 @@ function HTMLTemplate({
105115 ${ headString }
106116 </ head >
107117 < body >
108- < div
109- id ="app "
110- data-entry-page ="${ entryPage } "
111- data-route-params ="${ routeParams } "
112- >
113- ${ body }
114- </ div >
118+ < div id ="app "> ${ body } </ div >
115119 </ body >
116120 </ html >
117121 `
@@ -146,3 +150,7 @@ const stringify = (title, metas, links) => {
146150 ${ stringifyTag ( 'link' , links ) }
147151 `
148152}
153+
154+ function normalizeRequestUrl ( url ) {
155+ return url . replace ( / \/ ( i n d e x \. h t m l ) $ / , '/' )
156+ }
0 commit comments