-
-
Notifications
You must be signed in to change notification settings - Fork 516
feat(routing): allow custom URL prefix per locale #3896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughAdds support for custom locale URL prefixes by introducing a Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (6)
docs/content/docs/02.guide/91.new-features.md (1)
8-26: New feature blurb is clear; only a tiny wording nitThe description and example accurately reflect the new
prefixoption and correctly point to the options docs. If you care about style, you might vary the repeated “want to use …” phrasing in lines 9 and 11, but that’s purely cosmetic.src/types.ts (1)
518-526: Clarifyprefixtyping and adjustcodeJSDoc for consistencyTwo small consistency points around this new field:
- The options docs describe
prefixasnull | string, while the type here isprefix?: string. In TS configs this meansprefix: nullwill be rejected even though the docs say it’s allowed. Either change the docs to juststringor widen the type here tostring | nullif you want that to be officially supported.- The
codeJSDoc above still says “Used for route prefixing…”, but with this option routing may useprefixinstead ofcodewhen defined. Consider rephrasing to something like “Unique identifier of the locale. Used for route naming and as an argument in i18n utilities; used as the default URL prefix when no customprefixis configured.”docs/content/docs/04.api/00.options.md (1)
152-169:prefixoption docs are accurate; consider clarifying allowed formatThe behavior and example are aligned with the implementation: custom segment for the URL path while route names still use the locale code.
Two possible clarifications to make this harder to misuse:
- Explicitly say the value should be a single path segment without leading/trailing
/(e.g.brazil, not/brazil/), since the module prefixes it with/internally.- Optionally note that prefixes should be unique per locale when using path-based strategies; duplicate prefixes will lead to routes sharing the same URL path even though their names differ.
src/routing.ts (1)
67-79: Locale prefix map wiring looks correct; consider guarding against duplicate prefixesThe
buildLocalePrefixeshelper and its use inlocalizeRoutescleanly introduce per-locale prefixes while preserving existing behavior whenprefixis unset/empty.You might optionally mirror the domain-duplication guard from
shouldLocalizeRoutesand log a warning or error if multiple locales share the same non-emptyprefix, since that will generate routes with identical paths but different names, which can be surprising in Vue Router. Not critical, but would make misconfigurations easier to catch.Also applies to: 91-94
test/locale-prefix.test.ts (1)
1-241: Good coverage for locale prefixes; only a minor naming nitThe test suite does a solid job exercising custom prefixes across strategies, children, aliases, trailing slashes, empty prefixes, and mixed configurations. This gives good confidence in the new behavior.
Tiny nit: the “should handle prefix with special characters” case currently uses
'br', which is not really special. Either rename the test description or switch the sample to something like'br-south'or similar to better reflect the intent.src/kit/gen.ts (1)
70-78: RouteContextgetLocalePrefixintegration looks correct; consider constraining prefix formatThe introduction of
getLocalePrefixand its use in both path and alias localization is consistent:
- Locales with a configured prefix use that value; others fall back to the locale code.
no_prefixand unprefixed trees still work becauseshouldPrefixcontrols whetherprefixedvsunprefixedis used.- Empty prefixes behave as intended via the
buildLocalePrefixeslogic.One behavior worth calling out (and possibly guarding against): if a user supplies a prefix containing
/(e.g.'/brazil'),join('/', prefix, unprefixed)will yield paths with multiple slashes like'///about'. You may want to normalize or reject such prefixes in one place (e.g. when building thelocalePrefixesmap) and/or document thatprefixmust be a bare segment without slashes.Also applies to: 105-123, 145-160, 191-203
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
docs/content/docs/02.guide/01.index.md(1 hunks)docs/content/docs/02.guide/91.new-features.md(1 hunks)docs/content/docs/04.api/00.options.md(1 hunks)src/kit/gen.ts(4 hunks)src/routing.ts(2 hunks)src/types.ts(1 hunks)test/locale-prefix.test.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/routing.ts (1)
src/types.ts (1)
LocaleObject(457-539)
test/locale-prefix.test.ts (3)
src/kit/gen.ts (1)
LocalizableRoute(17-25)src/types.ts (1)
LocaleObject(457-539)src/routing.ts (1)
localizeRoutes(84-138)
🪛 LanguageTool
docs/content/docs/02.guide/91.new-features.md
[style] ~11-~11: You have already used this phrasing in nearby sentences. Consider replacing it to add variety to your writing.
Context: ...n the locale code. For example, if you want to use /brazil/ instead of /pt-BR/ for...
(REP_WANT_TO_VB)
🔇 Additional comments (1)
docs/content/docs/02.guide/01.index.md (1)
100-125: Guide “Custom Locale Prefix” section matches implementationThe explanation, example config, and resulting routes align with the new
prefixbehavior (custom URL segment while keeping the locale code internal). No issues from a behavior or consistency standpoint.
Removed redundant comment about SEO-friendly prefixes.
🔗 Linked issue
Resolves #3761
📚 Description
This PR adds support for custom URL prefixes per locale. Instead of using the locale code as the URL prefix, users can now define a custom
prefixproperty in the locale configuration.Problem
Users want to use URL prefixes instead of locale codes. For example,
/brazil/aboutinstead of/pt-BR/about.Solution
Added a new optional
prefixproperty to theLocaleObjecttype. When defined, the module uses this custom prefix in the URL instead of the locale code.Example configuration: