Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions adex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
"type": "module",
"exports": {
"./package.json": "./package.json",
"./router": {
"types": "./src/router.d.ts",
"import": "./src/router.js"
},
"./utils/isomorphic": {
"types": "./src/utils/isomorphic.d.ts",
"import": "./src/utils/isomorphic.js"
},
"./ssr": {
"types": "./src/ssr.d.ts",
"import": "./src/ssr.js"
Expand Down Expand Up @@ -61,6 +69,7 @@
"hoofd": "^1.7.1",
"mri": "^1.2.0",
"node-stream-zip": "^1.15.0",
"preact-iso": "^2.9.0",
"preact-render-to-string": "^6.5.5",
"regexparam": "^3.0.0",
"sirv": "^2.0.4",
Expand Down
59 changes: 45 additions & 14 deletions adex/runtime/client.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
import { hydrate as preactHydrate, h } from 'preact'
import { h } from 'preact'
import {
LocationProvider,
Router,
Route,
lazy,
hydrate as preactHydrate,
ErrorBoundary,
} from 'adex/router'

import 'virtual:adex:global.css'

const pageRoutes = import.meta.glob('/src/pages/**/*.{tsx,jsx,js}')
// @ts-expect-error injected by vite
import { routes } from '~routes'

async function hydrate() {
const entryPage = document.getElementById('app').dataset.entryPage
const routeParams = document.getElementById('app').dataset.routeParams
const componentModule = await pageRoutes[entryPage]()
const Component =
'default' in componentModule ? componentModule.default : componentModule
preactHydrate(
h(Component, {
routeParams: routeParams ? JSON.parse(atob(routeParams)) : {},
}),
document.getElementById('app')
const withComponents = routes.map(d => {
return {
...d,
component: lazy(d.module),
}
})

function ComponentWrapper({ url = '' }) {
return h(
LocationProvider,
//@ts-expect-error no types for non-jsx function
{ url: url },
h(
ErrorBoundary,
{},
h(
Router,
{},
withComponents.map(d =>
h(Route, { path: d.routePath, component: d.component })
)
)
)
)
}

hydrate()
export const App = ({ url = '' }) => {
return h(ComponentWrapper, { url })
}

async function hydrate() {
preactHydrate(h(ComponentWrapper, {}), document.getElementById('app'))
}
if (typeof window !== 'undefined') {
hydrate()
}
32 changes: 20 additions & 12 deletions adex/runtime/handler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { CONSTANTS, emitToHooked } from 'adex/hook'
import { prepareRequest, prepareResponse } from 'adex/http'
import { renderToString, toStatic } from 'adex/ssr'
import { toStatic } from 'adex/ssr'
import { renderToString } from 'adex/utils/isomorphic'
import { h } from 'preact'

// @ts-expect-error injected by vite
import { App } from 'virtual:adex:client'

// @ts-expect-error injected by vite
import { routes as apiRoutes } from '~apiRoutes'
// @ts-expect-error injected by vite
Expand All @@ -16,7 +20,8 @@ export async function handler(req, res) {
prepareRequest(req)
prepareResponse(res)

const [baseURL] = req.url.split('?')
const [url, search] = req.url.split('?')
const baseURL = normalizeRequestUrl(url)

const { metas, links, title, lang } = toStatic()

Expand Down Expand Up @@ -50,10 +55,15 @@ export async function handler(req, res) {
})

if (matchedInPages) {
const module = await matchedInPages.module()
const render = 'default' in module ? module.default : module
const routeParams = getRouteParams(baseURL, matchedInPages)

// @ts-expect-error
global.location = new URL(req.url, 'http://localhost')

const rendered = await renderToString(
h(App, { url: [baseURL, search].filter(Boolean).join('?') })
)

const htmlString = HTMLTemplate({
metas,
links,
Expand All @@ -63,7 +73,7 @@ export async function handler(req, res) {
routeParams: Buffer.from(JSON.stringify(routeParams), 'utf8').toString(
'base64'
),
body: renderToString(h(render, { routeParams })),
body: rendered.html,
})
const modifiableContext = {
req: req,
Expand Down Expand Up @@ -105,13 +115,7 @@ function HTMLTemplate({
${headString}
</head>
<body>
<div
id="app"
data-entry-page="${entryPage}"
data-route-params="${routeParams}"
>
${body}
</div>
<div id="app">${body}</div>
</body>
</html>
`
Expand Down Expand Up @@ -146,3 +150,7 @@ const stringify = (title, metas, links) => {
${stringifyTag('link', links)}
`
}

function normalizeRequestUrl(url) {
return url.replace(/\/(index\.html)$/, '/')
}
2 changes: 1 addition & 1 deletion adex/runtime/pages.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pathToRegex } from 'adex/ssr'
import { pathToRegex } from 'adex/utils/isomorphic'

const pages = import.meta.glob('#{__PLUGIN_PAGES_ROOT}')

Expand Down
2 changes: 1 addition & 1 deletion adex/src/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function fonts({ providers = [], families = [] } = {}) {
},
async transform(code, id) {
const resolvedData = await this.resolve('virtual:adex:client')
if (id === resolvedData.id) {
if (resolvedData?.id == id) {
return {
code: `import "${fontVirtualId}";\n` + code,
}
Expand Down
8 changes: 8 additions & 0 deletions adex/src/router.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export {
hydrate,
Router,
Route,
lazy,
LocationProvider,
ErrorBoundary,
} from 'preact-iso'
8 changes: 8 additions & 0 deletions adex/src/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export {
hydrate,
Router,
ErrorBoundary,
Route,
lazy,
LocationProvider,
} from 'preact-iso'
4 changes: 1 addition & 3 deletions adex/src/ssr.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export { toStatic } from 'hoofd/preact'
export { renderToString } from 'preact-render-to-string'
export { parse as pathToRegex } from 'regexparam'
export { use as useMiddleware } from '@barelyhuman/tiny-use'
export { default as sirv } from 'sirv'
export { default as mri } from 'mri'
export { default as mri } from 'mri'
3 changes: 1 addition & 2 deletions adex/src/ssr.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export { renderToString } from 'preact-render-to-string'
export { prerender as renderToString } from 'preact-iso'
export { default as sirv } from 'sirv'
export { default as mri } from 'mri'
export { parse as pathToRegex } from 'regexparam'
export { toStatic } from 'hoofd/preact'
export { use as useMiddleware } from '@barelyhuman/tiny-use'
2 changes: 2 additions & 0 deletions adex/src/utils/isomorphic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { parse as pathToRegex } from 'regexparam'
export { prerender as renderToString } from 'preact-iso'
1 change: 1 addition & 0 deletions adex/src/vite.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface AdexOptions {
fonts?: FontOptions
islands?: boolean
adapter?: Adapters
ssr?: boolean
__clientConfig?: UserConfig
}

Expand Down
Loading
Loading