Skip to content

Commit 28e9723

Browse files
committed
fix(routing): prefix roots should point to root
1 parent 9963f0a commit 28e9723

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

packages/qwik-router/src/buildtime/runtime-generation/generate-routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ function serializeBuildTrie(
198198
}
199199

200200
// _G rewrite target
201-
if (node._G) {
201+
if (node._G != null) {
202202
lines.push(`${nextIndent}_G: ${JSON.stringify(node._G)},`);
203203
}
204204

packages/qwik-router/src/runtime/src/routing.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function resolveLoaders(
164164
node: RouteData,
165165
gatheredLayouts: ModuleLoader[]
166166
): ModuleLoader[] | undefined {
167-
if (node._G) {
167+
if (node._G != null) {
168168
// Rewrite: re-walk from root using _G's keys
169169
const keys = (node._G as string).split('/').filter((p) => p.length > 0);
170170
const target = walkTrieKeys(root, keys);
@@ -176,7 +176,7 @@ function resolveLoaders(
176176

177177
// If the target node doesn't have _I directly, check group children
178178
// (e.g., root index inside (common) group, or routes inside pathless groups)
179-
if (!targetNode._I && !targetNode._G) {
179+
if (!targetNode._I && targetNode._G == null) {
180180
const indexResult = findIndexNode(targetNode);
181181
if (indexResult) {
182182
for (const g of indexResult.groups) {
@@ -350,7 +350,7 @@ function tryWildcardMatch(
350350
* node and the groups entered to reach it.
351351
*/
352352
function findIndexNode(node: RouteData): { target: RouteData; groups: RouteData[] } | undefined {
353-
if (node._I || node._G) {
353+
if (node._I || node._G != null) {
354354
return { target: node, groups: [] };
355355
}
356356
if (node._M) {
@@ -502,7 +502,7 @@ function matchRouteTree(
502502
// Check if _I is in a group child (e.g. (common)/index.tsx is the root "/" route)
503503
// This must come before _A checks so that an index route takes priority over
504504
// a rest wildcard with empty value.
505-
if (!node._I && !node._G) {
505+
if (!node._I && node._G == null) {
506506
const indexResult = findIndexNode(node);
507507
if (indexResult) {
508508
collectNodeMeta(
@@ -518,7 +518,7 @@ function matchRouteTree(
518518
}
519519

520520
// Check for _A (rest wildcard with empty value) on the node itself
521-
if (!node._I && !node._G && node._A) {
521+
if (!node._I && node._G == null && node._A) {
522522
const next = node._A as RouteData;
523523
const paramName = next._P!;
524524
params[paramName as string] = '';
@@ -533,7 +533,7 @@ function matchRouteTree(
533533
}
534534

535535
// Also check _M groups for _A with empty value
536-
if (!node._I && !node._G) {
536+
if (!node._I && node._G == null) {
537537
const restInfo = findRestNode(node);
538538
if (restInfo) {
539539
const next = restInfo.next;

packages/qwik-router/src/runtime/src/routing.unit.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,32 @@ test('loadRoute — _G with params preserves captured params', async () => {
425425
assert.deepEqual(result.$params$, { slug: 'my-post' });
426426
});
427427

428+
test('loadRoute — _G empty string rewrite resolves prefix root to index', async () => {
429+
const rootLayout = makeLoader();
430+
const pageLoader = makeLoader();
431+
const routes: RouteData = {
432+
_L: rootLayout,
433+
_M: [
434+
{
435+
_I: pageLoader,
436+
},
437+
],
438+
en: {
439+
_G: '',
440+
},
441+
de: {
442+
_G: '',
443+
},
444+
};
445+
const resultEn = await loadRoute(routes, false, '/en');
446+
assert.isFalse(resultEn.$notFound$);
447+
assert.equal(resultEn.$mods$.length, 2); // rootLayout + pageLoader
448+
449+
const resultDe = await loadRoute(routes, false, '/de');
450+
assert.isFalse(resultDe.$notFound$);
451+
assert.equal(resultDe.$mods$.length, 2);
452+
});
453+
428454
test('loadRoute — _G target not found returns 404', async () => {
429455
const routes: RouteData = {
430456
es: {

0 commit comments

Comments
 (0)