diff --git a/adex/runtime/client.js b/adex/runtime/client.js index f43b2ab..d2bb424 100644 --- a/adex/runtime/client.js +++ b/adex/runtime/client.js @@ -23,13 +23,14 @@ const withComponents = routes.map(d => { function ComponentWrapper({ url = '' }) { return h( LocationProvider, + //@ts-expect-error no types for non-jsx function { url: url }, h( ErrorBoundary, {}, h( Router, - { url: url }, + {}, withComponents.map(d => h(Route, { path: d.routePath, component: d.component }) ) diff --git a/adex/runtime/handler.js b/adex/runtime/handler.js index 6f566e0..02db02c 100644 --- a/adex/runtime/handler.js +++ b/adex/runtime/handler.js @@ -1,7 +1,6 @@ import { CONSTANTS, emitToHooked } from 'adex/hook' import { prepareRequest, prepareResponse } from 'adex/http' import { toStatic } from 'adex/ssr' -import { LocationProvider, ErrorBoundary, Router } from 'adex/router' import { renderToString } from 'adex/utils/isomorphic' import { h } from 'preact' @@ -21,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() @@ -60,7 +60,9 @@ export async function handler(req, res) { // @ts-expect-error global.location = new URL(req.url, 'http://localhost') - const rendered = await renderToString(h(App, { url: req.url })) + const rendered = await renderToString( + h(App, { url: [baseURL, search].filter(Boolean).join('?') }) + ) const htmlString = HTMLTemplate({ metas, @@ -148,3 +150,7 @@ const stringify = (title, metas, links) => { ${stringifyTag('link', links)} ` } + +function normalizeRequestUrl(url) { + return url.replace(/\/(index\.html)$/, '/') +} diff --git a/adex/src/fonts.js b/adex/src/fonts.js index 0b3a9b4..b105c12 100644 --- a/adex/src/fonts.js +++ b/adex/src/fonts.js @@ -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, } diff --git a/adex/src/vite.js b/adex/src/vite.js index 4094a80..17ad98d 100644 --- a/adex/src/vite.js +++ b/adex/src/vite.js @@ -61,102 +61,39 @@ export function adex({ 'virtual:adex:handler', readFileSync(join(__dirname, '../runtime/handler.js'), 'utf8') ), - createVirtualModule( - 'virtual:adex:server', - `import { createServer } from '${adapterMap[adapter]}' - import { dirname, join } from 'node:path' - import { fileURLToPath } from 'node:url' - import { existsSync, readFileSync } from 'node:fs' - import { env } from 'adex/env' - - import 'virtual:adex:font.css' - import 'virtual:adex:global.css' - - const __dirname = dirname(fileURLToPath(import.meta.url)) - - const PORT = parseInt(env.get('PORT', '3000'), 10) - const HOST = env.get('HOST', 'localhost') - - const paths = { - assets: join(__dirname, './assets'), - islands: join(__dirname, './islands'), - client: join(__dirname, '../client'), - } - - function getServerManifest() { - const manifestPath = join(__dirname, 'manifest.json') - if (existsSync(manifestPath)) { - const manifestFile = readFileSync(manifestPath, 'utf8') - return parseManifest(manifestFile) - } - return {} - } - - function getClientManifest() { - const manifestPath = join(__dirname, '../client/manifest.json') - if (existsSync(manifestPath)) { - const manifestFile = readFileSync(manifestPath, 'utf8') - return parseManifest(manifestFile) - } - return {} - } - - function parseManifest(manifestString) { - try { - const manifestJSON = JSON.parse(manifestString) - return manifestJSON - } catch (err) { - return {} - } - } - - const server = createServer({ - port: PORT, - host: HOST, - adex:{ - manifests:{server:getServerManifest(),client:getClientManifest()}, - paths, - } - }) - - if ('run' in server) { - server.run() - } - - export default server.fetch - ` - ), addFontsPlugin(fonts), adexDevServer({ islands }), adexBuildPrep({ islands }), + adexClientBuilder({ islands }), + adexIslandsBuilder(), - !ssr && adexClientBuilder({ config: __clientConfig }), - - // SSR Specific plugins - ssr && adexServerBuilder(), - ssr && !islands && adexClientSSRBuilder({ config: __clientConfig }), - ssr && islands && adexIslandsBuilder({ config: __clientConfig }), - + // SSR/Render Server Specific plugins + ssr && adexServerBuilder({ fonts, adapter, islands }), ...adexGuards(), ] } /** - * @param {object} options - * @param {import('vite').UserConfig} options.config * @returns {import("vite").Plugin} */ -function adexClientBuilder({ config }) { +function adexClientBuilder({ islands = false } = {}) { return { name: 'adex-client-builder', config(cfg) { const out = cfg.build.outDir ?? 'dist' return { + appType: 'custom', build: { + write: !islands, + manifest: 'manifest.json', outDir: join(out, 'client'), rollupOptions: { input: 'virtual:adex:client', }, + output: { + entryFileNames: '[name]-[hash].js', + format: 'esm', + }, }, } }, @@ -169,9 +106,10 @@ function adexClientBuilder({ config }) { } const links = [ + // @ts-expect-error invalid types by vite? figure this out ...(bundle[clientEntryPath]?.viteMetadata?.importedCss ?? new Set()), ].map(d => { - return `` + return `` }) this.emitFile({ @@ -182,7 +120,7 @@ function adexClientBuilder({ config }) { ${links.join('\n')}
- +