Skip to content

Commit 7db4fc1

Browse files
committed
fix(qwik-router): prevent duplicated assetsDir in static paths
1 parent 6e63cec commit 7db4fc1

4 files changed

Lines changed: 31 additions & 7 deletions

File tree

.changeset/puny-mice-live.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@qwik.dev/router": patch
3+
---
4+
5+
vite build.assetsDir is respected

e2e/vite-e2e/tests/static-paths-config.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ async function searchServerFiles(variantDir: string, needle: string): Promise<bo
3030
const serverDir = join(variantDir, 'server');
3131
const files = await readdir(serverDir);
3232
for (const file of files) {
33-
if (!file.endsWith('.js')) continue;
33+
if (!file.endsWith('.js')) {
34+
continue;
35+
}
3436
const content = await readFile(join(serverDir, file), 'utf-8');
35-
if (content.includes(needle)) return true;
37+
if (content.includes(needle)) {
38+
return true;
39+
}
3640
}
3741
return false;
3842
}

packages/qwik-router/src/adapters/shared/vite/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { basename, dirname, join, resolve } from 'node:path';
55
import type { Plugin, UserConfig } from 'vite';
66
import type { BuiltRoute } from '../../../buildtime/types';
77
import { ssgWorkerImportPlugin } from '../../../buildtime/vite/ssg-worker-imports';
8+
import { ensureSlash } from '../../../utils/pathname';
89
import { postBuild } from './post-build';
910

1011
/**
@@ -225,10 +226,19 @@ export function viteAdapter(opts: ViteAdapterPluginOptions) {
225226
}
226227
}
227228

229+
const normalizedBasePathname = ensureSlash(basePathname);
230+
const normalizedAssetsSegment = assetsDir ? `/${assetsDir}/` : null;
231+
const postBuildPathName =
232+
normalizedAssetsSegment && normalizedBasePathname.endsWith(normalizedAssetsSegment)
233+
? normalizedBasePathname
234+
: assetsDir
235+
? join(basePathname, assetsDir)
236+
: basePathname;
237+
228238
await postBuild(
229239
clientPublicOutDir,
230240
serverOutDir,
231-
assetsDir ? join(basePathname, assetsDir) : basePathname,
241+
postBuildPathName,
232242
staticPaths,
233243
!!opts.cleanStaticGenerated
234244
);

packages/qwik-router/src/adapters/shared/vite/post-build.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,26 @@ export async function postBuild(
7575
}
7676

7777
function normalizeStaticPath(pathname: string, pathNameBase: string) {
78-
const normalized = ensureSlash(pathname);
79-
const segment = pathNameBase.split('/').filter(Boolean).pop();
78+
const normalized = ensureLeadingSlash(ensureSlash(pathname));
79+
const normalizedPathNameBase = ensureLeadingSlash(ensureSlash(pathNameBase));
80+
const segment = normalizedPathNameBase.split('/').filter(Boolean).pop();
8081

8182
if (!segment) {
8283
return normalized;
8384
}
8485

85-
const doubledSegmentPrefix = `${pathNameBase}${segment}/`;
86+
const doubledSegmentPrefix = `${normalizedPathNameBase}${segment}/`;
8687
if (normalized.startsWith(doubledSegmentPrefix)) {
87-
return pathNameBase + normalized.slice(doubledSegmentPrefix.length);
88+
return normalizedPathNameBase + normalized.slice(doubledSegmentPrefix.length);
8889
}
8990

9091
return normalized;
9192
}
9293

94+
function ensureLeadingSlash(pathname: string) {
95+
return pathname.startsWith('/') ? pathname : `/${pathname}`;
96+
}
97+
9398
function createNotFoundPathsCode(basePathname: string, notFounds: string[][]) {
9499
/** Sort in order of longest path, so that the most specific paths match first */
95100
notFounds.sort((a, b) => {

0 commit comments

Comments
 (0)