Skip to content

Commit 1a6bf85

Browse files
committed
fix(qwik-router): prevent duplicated assetsDir in static paths
1 parent 93a3629 commit 1a6bf85

5 files changed

Lines changed: 38 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/adapters-e2e/vite-env.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference types="vite/client" />
2+
3+
declare module '*.svg?url' {
4+
const src: string;
5+
export default src;
6+
}

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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { readFile, rm, unlink } from 'node:fs/promises';
66
import { basename, dirname, join, resolve } from 'node:path';
77
import { type Plugin, type UserConfig, type ViteBuilder } from 'vite';
88
import type { BuiltRoute } from '../../../buildtime/types';
9+
import { ensureSlash } from '../../../utils/pathname';
910
import { postBuild } from './post-build';
1011

1112
/**
@@ -282,10 +283,20 @@ export function viteAdapter(opts: ViteAdapterPluginOptions) {
282283
}
283284
}
284285

286+
// Avoid doubling the assetsDir when basePathname already ends with it.
287+
const normalizedBasePathname = ensureSlash(basePathname);
288+
const normalizedAssetsSegment = assetsDir ? `/${assetsDir}/` : null;
289+
const postBuildPathName =
290+
normalizedAssetsSegment && normalizedBasePathname.endsWith(normalizedAssetsSegment)
291+
? normalizedBasePathname
292+
: assetsDir
293+
? join(basePathname, assetsDir)
294+
: basePathname;
295+
285296
await postBuild(
286297
clientPublicOutDir,
287298
serverOutDir,
288-
assetsDir ? join(basePathname, assetsDir) : basePathname,
299+
postBuildPathName,
289300
staticPaths,
290301
!!opts.cleanStaticGenerated
291302
);

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 createStaticPathsCode(staticPaths: Set<string>) {
9499
// This is the body of the static paths array
95100
return JSON.stringify(Array.from(new Set<string>(staticPaths)).sort()).slice(1, -1);

0 commit comments

Comments
 (0)