Skip to content

Commit 4172cb9

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

4 files changed

Lines changed: 30 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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,19 @@ export function viteAdapter(opts: ViteAdapterPluginOptions) {
225225
}
226226
}
227227

228+
const normalizedBasePathname = ensureSlash(basePathname);
229+
const normalizedAssetsSegment = assetsDir ? `/${assetsDir}/` : null;
230+
const postBuildPathName =
231+
normalizedAssetsSegment && normalizedBasePathname.endsWith(normalizedAssetsSegment)
232+
? normalizedBasePathname
233+
: assetsDir
234+
? join(basePathname, assetsDir)
235+
: basePathname;
236+
228237
await postBuild(
229238
clientPublicOutDir,
230239
serverOutDir,
231-
assetsDir ? join(basePathname, assetsDir) : basePathname,
240+
postBuildPathName,
232241
staticPaths,
233242
!!opts.cleanStaticGenerated
234243
);

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)