Skip to content

Commit 90f65a7

Browse files
committed
fix: base url preview in production
1 parent a9dabfe commit 90f65a7

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

packages/island/src/node/build.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { performance } from 'perf_hooks';
3636
import pc from 'picocolors';
3737
import { pathToFileURL } from 'url';
3838
import { CLIBuildOption } from './cli';
39+
import { withBase } from '../shared/utils';
3940

4041
const debug = createDebugger('island:build');
4142
const islandInjectId = 'island:inject';
@@ -260,7 +261,10 @@ class SSGBuilder {
260261
}
261262
})
262263
.join('\n');
263-
264+
const normalizeVendorFilename = (fileName: string) =>
265+
fileName.replace(/\//g, '_') + '.js';
266+
const withBaseUrl = (url: string) =>
267+
withBase(url, this.#config.base || '/');
264268
const html = `
265269
<!DOCTYPE html>
266270
<html>
@@ -280,14 +284,18 @@ class SSGBuilder {
280284
{
281285
"imports": {
282286
${DEFAULT_EXTERNALS.map(
283-
(name) => `"${name}": "/${name.replace(/\//g, '_')}.js"`
287+
(name) =>
288+
`"${name}": "${withBaseUrl(normalizeVendorFilename(name))}"`
284289
).join(',')}
285290
}
286291
}
287292
</script>
288293
289294
${styleAssets
290-
.map((item) => `<link rel="stylesheet" href="/${item.fileName}">`)
295+
.map(
296+
(item) =>
297+
`<link rel="stylesheet" href="${withBaseUrl(item.fileName)}">`
298+
)
291299
.join('\n')}
292300
293301
</head>
@@ -304,12 +312,16 @@ class SSGBuilder {
304312
!this.#config.enableSpa && hasIsland
305313
? `<script id="island-props">${JSON.stringify(
306314
propsData
307-
)}</script><script type="module" src="/${injectIslandsPath}"></script>`
315+
)}</script><script type="module" src="${withBaseUrl(
316+
injectIslandsPath
317+
)}"></script>`
308318
: ''
309319
}
310320
${
311321
this.#config.enableSpa
312-
? `<script type="module" src="/${clientChunk.fileName}"></script>`
322+
? `<script type="module" src="${withBaseUrl(
323+
clientChunk.fileName!
324+
)}"></script>`
313325
: `<script type="module">${clientChunk.code}</script>`
314326
}
315327
</body>

packages/island/src/node/plugin-routes/RouteService.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { RUNTIME_BUNDLE_OUTDIR } from '../constants';
55
import path from 'path';
66
import { DEFAULT_EXCLUDE, DEFAULT_PAGE_EXTENSIONS } from '.';
77
import { RouteOptions } from 'shared/types';
8-
import { normalizeSlash } from '../../shared/utils';
8+
import { withBase } from '../../shared/utils';
99

1010
export interface RouteMeta {
1111
routePath: string;
@@ -59,12 +59,7 @@ export class RouteService {
5959
): string | undefined {
6060
const fileRelativePath = path.relative(root, filePath);
6161
const routePath = normalizeRoutePath(fileRelativePath);
62-
return RouteService.withBase(routePath, base);
63-
}
64-
65-
static withBase(url: string, base: string) {
66-
const normalizedBase = normalizeSlash(base);
67-
return normalizedBase ? `${normalizedBase}${url}` : url;
62+
return withBase(routePath, base);
6863
}
6964

7065
addRoute(filePath: string) {
@@ -131,7 +126,7 @@ ${this.#routeData
131126
* filePath: '/Users/xxx/xxx/index.md'
132127
* }
133128
*/
134-
return `{ path: '${RouteService.withBase(
129+
return `{ path: '${withBase(
135130
route.routePath,
136131
this.#base
137132
)}', element: React.createElement(${component}), filePath: '${

packages/island/src/node/serve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export async function serve(root: string, cliOptions: CLIServeOption) {
4545

4646
if (base) {
4747
polka({ onNoMatch })
48-
.use(base, compress, serve)
48+
.use(base, serve)
4949
.listen(port, host, (err: Error) => {
5050
if (err) throw err;
5151
console.log(`Built site served at http://${host}:${port}/${base}/\n`);

packages/island/src/shared/utils/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ export function removeTrailingSlash(url: string) {
1717
export function normalizeSlash(url: string) {
1818
return removeTrailingSlash(addLeadingSlash(url));
1919
}
20+
21+
export function withBase(url: string, base: string) {
22+
const normalizedBase = normalizeSlash(base);
23+
const normalizedUrl = normalizeSlash(url);
24+
return normalizedBase ? `${normalizedBase}${normalizedUrl}` : normalizedUrl;
25+
}

0 commit comments

Comments
 (0)