Skip to content

Commit 9c9f0ad

Browse files
committed
feat(qwik-router): add exclude option to rewriteRoutes
Skip generating rewritten route entries for matching path patterns, so a section can opt out of being rewritten under each config. Patterns use the same glob syntax as the SSG `include`/`exclude`, matched per config before the rewrite node is created.
1 parent 68d4fac commit 9c9f0ad

5 files changed

Lines changed: 41 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@qwik.dev/router': minor
3+
---
4+
5+
feat(router): add an `exclude` option to `rewriteRoutes` to skip generating localized routes for matching path patterns.

packages/docs/src/routes/docs/(qwikrouter)/routing/index.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,22 @@ export default defineConfig(async () => {
428428
});
429429
```
430430

431+
By default a config rewrites every matching route. Add `exclude` to skip routes that should not be rewritten under that config. The patterns use the same glob syntax as the SSG `include`/`exclude` and are matched per config:
432+
433+
```tsx
434+
rewriteRoutes: [
435+
{
436+
prefix: 'it',
437+
paths: {
438+
'docs': 'documentazione',
439+
'products': 'prodotti',
440+
},
441+
// `/blog` stays English-only: no `/it` mirror is generated for it.
442+
exclude: ['/blog/*'],
443+
},
444+
],
445+
```
446+
431447
## Advanced routing
432448

433449
Qwik Router also supports:

packages/qwik-router/src/buildtime/build-pages-rewrited.unit.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,14 @@ testSameRoutes(
333333
assert.equal(r.pathname, '/produkt/');
334334
}
335335
);
336+
337+
const testExclude = testAppSuite('Rewrite routes with exclude', {
338+
rewriteRoutes: [{ prefix: 'it', paths: {}, exclude: ['/docs/*'] }],
339+
});
340+
341+
testExclude('exclude skips localized mirrors for matched routes', ({ ctx }) => {
342+
const it = ctx.routeTrie.children.get('it');
343+
assert.ok(it, 'prefix mirror "it" should exist for non-excluded routes');
344+
assert.ok(it!.children.get('about-us'), 'non-excluded route is mirrored under "it"');
345+
assert.is(it!.children.get('docs'), undefined, '"/docs/*" is excluded from the "it" mirror');
346+
});

packages/qwik-router/src/buildtime/build.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { addError, addWarning } from '../utils/format';
22
import { createFileId, getPathnameFromDirPath } from '../utils/fs';
33
import { ensureSlash } from '../utils/pathname';
4+
import { createRouteTester } from '../ssg/routes';
45
import { resolveMenu } from './markdown/menu';
56
import { resolveLayout, resolveRoute } from './routing/resolve-source-file';
67
import { routeSortCompare } from './routing/sort-routes';
@@ -323,8 +324,15 @@ function applyRewriteRoutes(root: BuildTrieNode, rewriteConfigs: RewriteRouteOpt
323324
for (const config of rewriteConfigs) {
324325
const translations = config.paths || {};
325326
const translatable = new Set(Object.keys(translations).map((k) => k.toLowerCase()));
327+
const isExcluded = config.exclude?.length
328+
? createRouteTester('/', config.exclude, undefined)
329+
: undefined;
326330

327331
for (const { steps } of routables) {
332+
const originalKeyPath = steps.map((s) => s.key).join('/');
333+
if (isExcluded?.('/' + originalKeyPath)) {
334+
continue;
335+
}
328336
// Check if any segment is translatable or if there's a prefix
329337
const hasTranslatable = steps.some((s) => translatable.has(s.key));
330338
if (!hasTranslatable && !config.prefix) {
@@ -335,9 +343,6 @@ function applyRewriteRoutes(root: BuildTrieNode, rewriteConfigs: RewriteRouteOpt
335343
continue;
336344
}
337345

338-
// Build the original key path
339-
const originalKeyPath = steps.map((s) => s.key).join('/');
340-
341346
// Build translated steps
342347
const translatedSteps: TriePathStep[] = [];
343348

packages/qwik-router/src/buildtime/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export interface ParsedMenuItem {
135135
export interface RewriteRouteOption {
136136
prefix?: string;
137137
paths: Record<string, string>;
138+
exclude?: string[];
138139
}
139140

140141
/** @public */

0 commit comments

Comments
 (0)