Skip to content

Commit 0ed3eaf

Browse files
authored
feat: add baseURL support for client only setups (#28)
1 parent c081fd7 commit 0ed3eaf

File tree

7 files changed

+46
-116
lines changed

7 files changed

+46
-116
lines changed

adex/runtime/client.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,36 @@ import {
66
lazy,
77
hydrate as preactHydrate,
88
ErrorBoundary,
9+
join,
10+
prerender as ssr,
911
} from 'adex/router'
1012

1113
import 'virtual:adex:global.css'
1214

15+
const baseURL = import.meta.env.BASE_URL ?? '/'
16+
17+
const normalizeURLPath = url => (url ? join(baseURL, url) : undefined)
18+
1319
// @ts-expect-error injected by vite
1420
import { routes } from '~routes'
1521

1622
function ComponentWrapper({ url = '' }) {
1723
return h(
1824
LocationProvider,
1925
//@ts-expect-error no types for non-jsx function
20-
{ url: url },
26+
{ url: normalizeURLPath(url) },
2127
h(
2228
ErrorBoundary,
2329
{},
2430
h(
2531
Router,
2632
{},
27-
routes.map(d =>
28-
h(Route, { path: d.routePath, component: lazy(d.module) })
29-
)
33+
routes.map(d => {
34+
return h(Route, {
35+
path: normalizeURLPath(d.routePath),
36+
component: lazy(d.module),
37+
})
38+
})
3039
)
3140
)
3241
)
@@ -43,3 +52,14 @@ async function hydrate() {
4352
if (typeof window !== 'undefined') {
4453
hydrate()
4554
}
55+
56+
export const prerender = async ({ url }) => {
57+
const { html, links: discoveredLinks } = await ssr(
58+
h(ComponentWrapper, { url: url })
59+
)
60+
61+
return {
62+
html,
63+
links: new Set([...(discoveredLinks ?? [])]),
64+
}
65+
}

adex/runtime/pages.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const routes = normalizeRouteImports(pages, [
77
'#{__PLUGIN_PAGES_ROOT_REGEX_REPLACER}',
88
])
99

10-
// taken from
10+
// major bits taken from
1111
// https://github.com/cyco130/smf/blob/c4b601f48cd3b3b71bea6d76b52b9a85800813e4/smf/shared.ts#L22
1212
// as it's decently tested and aligns to what we want for our routing
1313
function compareRoutePatterns(a, b) {

adex/src/router.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ export {
77
ErrorBoundary,
88
useLocation,
99
useRoute,
10+
prerender,
1011
} from 'preact-iso'
12+
13+
export declare const join: (...args: string[]) => string

adex/src/router.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,14 @@ export {
77
LocationProvider,
88
useLocation,
99
useRoute,
10+
prerender,
1011
} from 'preact-iso'
12+
13+
export const join = (...parts) => {
14+
if (parts.some(part => part == null)) {
15+
throw new Error(
16+
'Expected join to get valid paths, but received undefined or null'
17+
)
18+
}
19+
return parts.join('/').replace(/\/{2,}/g, '/')
20+
}

adex/src/vite.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export function adex({
142142
* @returns {import("vite").Plugin}
143143
*/
144144
function adexClientBuilder({ ssr = true, islands = false } = {}) {
145+
let baseUrl = '/'
145146
return {
146147
name: 'adex-client-builder',
147148
config(cfg) {
@@ -162,6 +163,10 @@ function adexClientBuilder({ ssr = true, islands = false } = {}) {
162163
},
163164
}
164165
},
166+
configResolved(cfg) {
167+
baseUrl = cfg.base
168+
return
169+
},
165170
generateBundle(opts, bundle) {
166171
let clientEntryPath
167172
for (const key in bundle) {
@@ -178,7 +183,7 @@ function adexClientBuilder({ ssr = true, islands = false } = {}) {
178183
// @ts-expect-error invalid types by vite? figure this out
179184
...(bundle[clientEntryPath]?.viteMetadata?.importedCss ?? new Set()),
180185
].map(d => {
181-
return `<link rel="stylesheet" href="/${d}" />`
186+
return `<link rel="stylesheet" href="${join(baseUrl, d)}" />`
182187
})
183188

184189
if (!ssr) {
@@ -191,7 +196,7 @@ function adexClientBuilder({ ssr = true, islands = false } = {}) {
191196
</head>
192197
<body>
193198
<div id="app"></div>
194-
<script src="/${clientEntryPath}" type="module"></script>
199+
<script src="${join(baseUrl, clientEntryPath)}" type="module"></script>
195200
</body>
196201
</html>`,
197202
})

playground/.islands/island-counter.js

Lines changed: 0 additions & 109 deletions
This file was deleted.

playground/src/pages/index.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default function Page() {
1414
<a href="https://preactjs.com" target="_blank">
1515
<img src={'/vite.svg'} class="logo preact" alt="Preact logo" />
1616
</a>
17+
<a href="/about">About</a>
1718
</div>
1819
<h1>Vite + Preact</h1>
1920
<div class="card">

0 commit comments

Comments
 (0)