-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
chore: 后端路由模式时,本地静态路由和后端路由合并 #4899
base: main
Are you sure you want to change the base?
Conversation
|
WalkthroughThe changes involve modifications to the Changes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Warning There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure. 🔧 eslint
apps/web-antd/src/router/access.tsOops! Something went wrong! :( ESLint: 9.14.0 Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/node_modules/@vben/eslint-config/dist/index.mjs' imported from /eslint.config.mjs Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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: 5
🧹 Outside diff range and nitpick comments (12)
apps/web-ele/src/router/access.ts (1)
37-38
: Translate comment to English for better collaboration.The Chinese comment "本地菜单和动态菜单合并" should be in English.
- // 本地菜单和动态菜单合并 + // Merge local static menus with dynamic menusapps/web-antd/src/router/access.ts (1)
35-38
: Consider performance optimizations and error handlingThe current implementation could benefit from the following architectural improvements:
- Cache the deep-cloned static menu list to avoid redundant cloning operations
- Add proper error handling for failed dynamic menu fetches
- Consider implementing a retry mechanism for dynamic menu fetches
Consider implementing these improvements:
// Cache the cloned static menus const cachedStaticMenus = cloneDeep(staticMenuList); async function fetchMenuListAsync() { try { const dynamicMenus = await getAllMenusApi(); return [...cachedStaticMenus, ...dynamicMenus]; } catch (error) { message.error($t('common.fetchMenuError')); // Fallback to static menus only return cachedStaticMenus; } }apps/web-antd/src/router/routes/static.ts (4)
5-7
: Consider adding English documentationThe file's purpose is documented in Chinese. Consider adding English documentation or making it bilingual for better international collaboration.
/** - * 该文件放非后台返回的前端静态路由 + * This file contains frontend static routes that are not returned from the backend + * 该文件放非后台返回的前端静态路由 */
45-45
: Standardize translation key formatThe meta.title values use inconsistent formats:
'page.dashboard.title'
(direct key)'page.dashboard.analytics'
(direct key)'page.dashboard.workspace'
(direct key)Consider using the
$t()
function consistently for all titles, similar to how it's used in the demo route.meta: { icon: 'lucide:layout-dashboard', order: -1, - title: 'page.dashboard.title', + title: $t('page.dashboard.title'), }, // ... and similar changes for other titlesAlso applies to: 58-58, 68-68
72-72
: Consider using deep clone for demo routesWhen spreading
demoRoute
, consider using a deep clone to prevent potential mutations if the array is used elsewhere.- ...demoRoute, + ...JSON.parse(JSON.stringify(demoRoute)),
1-73
: Consider documenting route merging strategySince this file defines static routes that will be merged with backend routes (as per PR objective), consider:
- Adding documentation about the merging strategy
- Implementing conflict resolution for duplicate routes
- Defining clear priorities between static and backend routes
This will help prevent potential routing conflicts and make the system's behavior more predictable.
apps/web-ele/src/router/routes/static.ts (2)
5-7
: Consider adding English translations for documentationWhile the Chinese comment "该文件放非后台返回的前端静态路由" clearly explains the file's purpose, consider adding English translations to improve international collaboration.
/** - * 该文件放非后台返回的前端静态路由 + * 该文件放非后台返回的前端静态路由 + * This file contains frontend static routes that are not returned by the backend */
39-71
: Standardize the translation key patternThere's an inconsistency in how translations are handled:
- Some use direct
$t()
calls (in demoRoute)- Others use translation keys (e.g., 'page.dashboard.title')
Consider standardizing to use translation keys throughout for better maintainability.
{ component: 'BasicLayout', meta: { icon: 'lucide:layout-dashboard', order: -1, - title: 'page.dashboard.title', + title: $t('page.dashboard.title'), }, // ... rest of the configuration }apps/web-naive/src/router/routes/static.ts (4)
5-7
: Consider adding English documentationThe file's purpose is documented in Chinese. Consider adding English documentation or making it bilingual for better international collaboration.
/** - * 该文件放非后台返回的前端静态路由 + * This file contains frontend static routes that are not returned from the backend + * 该文件放非后台返回的前端静态路由 */
45-45
: Ensure consistent internationalization usageThe title uses a direct string 'page.dashboard.title' instead of the $t function used elsewhere in the file.
- title: 'page.dashboard.title', + title: $t('page.dashboard.title'),
56-56
: Document the affixTab propertyThe
affixTab
property is only used in the analytics route. Consider adding a comment explaining its purpose and why it's specifically needed here.meta: { + // Indicates that this tab should be pinned in the tab bar affixTab: true, icon: 'lucide:area-chart',
1-73
: Document the route merging strategyWhile this file clearly defines static routes, it would be helpful to document how these routes will be merged with backend routes. Consider adding a comment explaining:
- The merging strategy
- How route conflicts are resolved
- The order of precedence between static and backend routes
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (6)
apps/web-antd/src/router/access.ts
(2 hunks)apps/web-antd/src/router/routes/static.ts
(1 hunks)apps/web-ele/src/router/access.ts
(2 hunks)apps/web-ele/src/router/routes/static.ts
(1 hunks)apps/web-naive/src/router/access.ts
(2 hunks)apps/web-naive/src/router/routes/static.ts
(1 hunks)
🔇 Additional comments (8)
apps/web-naive/src/router/access.ts (2)
36-36
: Consider the menu merging order
The current implementation places static menus before dynamic ones. Please verify if this order aligns with the expected menu structure and navigation hierarchy.
#!/bin/bash
# Description: Check for any existing menu order specifications or related configurations
# Look for menu-related configurations
rg -g '!*.{lock,min.js}' -i "menu.*order|menu.*priority|menu.*position"
# Check for any documentation about menu structure
fd -g "README*" -X rg -l "menu"
8-8
: Verify the static routes implementation
The imports look correct, but let's verify the structure and exports of the static routes file.
Also applies to: 15-15
✅ Verification successful
Import and static routes implementation verified
The verification confirms:
- The static routes file exists at
apps/web-naive/src/router/routes/static.ts
staticMenuList
is properly exported and used inaccess.ts
- The implementation shows correct usage with
cloneDeep
to prevent mutations when merging with dynamic menus - The static menu structure follows the expected format with component, meta, and order properties
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify the static routes file exists and exports the required entities
# Check if the static routes file exists
fd -p "routes/static\.(ts|js)" apps/web-naive/src/router
# Verify the exports
ast-grep --pattern 'export const staticMenuList = $_'
Length of output: 699
Script:
#!/bin/bash
# Let's try a different approach to verify the static routes implementation
# Check for any file containing "static" in the router directory
fd -p "static" apps/web-naive/src/router
# Search for staticMenuList declaration in the codebase
rg "staticMenuList" apps/web-naive/src/router -A 5
# Look for menu-related type definitions that might help understand the structure
rg "type.*Menu" apps/web-naive/src/router
Length of output: 1504
apps/web-ele/src/router/access.ts (2)
8-8
: LGTM! Imports are well organized.
The new imports for cloneDeep
and staticMenuList
are appropriately placed and necessary for the menu merging functionality.
Also applies to: 16-16
36-38
: Verify menu structure compatibility.
Please ensure that the structure of static and dynamic menus is compatible to prevent runtime errors.
✅ Verification successful
Let me try to find the correct paths and check the menu structures.
Let me check the static menu structure and the API response structure to verify compatibility.
Menu structures are compatible and type-safe
The verification shows that both static and dynamic menus use the same type RouteRecordStringComponent[]
:
- Static menus are explicitly typed as
RouteRecordStringComponent[]
instatic.ts
- Dynamic menus from
getAllMenusApi()
return the same typeRouteRecordStringComponent[]
- Both follow the base menu type definition from
menu-record.ts
which ensures consistent structure with properties likepath
,meta
,children
, etc.
The merge operation using [...cloneDeep(staticMenuList), ...dynamicMenus]
is safe as both arrays contain compatible objects.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Check menu structure consistency between static and dynamic menus
# Check static menu structure
echo "Checking static menu structure..."
ast-grep --pattern 'export const staticMenuList = $_' ./router/routes/static.ts
# Check dynamic menu structure from API response type definitions
echo "Checking API response structure..."
ast-grep --pattern 'interface $_Response {
$$$
}'
Length of output: 364
Script:
#!/bin/bash
# First, let's find the correct paths for static menu and API files
echo "Finding static menu file..."
fd -t f "static.ts"
echo "\nFinding menu-related files..."
fd -t f -e ts | rg -i "menu"
echo "\nChecking menu-related type definitions..."
rg -A 5 "interface.*Menu"
Length of output: 14186
Script:
#!/bin/bash
# Check static menu structure in the correct file
echo "Static menu structure:"
cat apps/web-ele/src/router/routes/static.ts
echo "\nAPI response structure:"
cat apps/web-ele/src/api/core/menu.ts
echo "\nChecking base menu type definition:"
cat packages/@core/base/typings/src/menu-record.ts
Length of output: 3083
apps/web-antd/src/router/access.ts (1)
8-8
: Verify the static routes implementation
The introduction of staticMenuList
creates a new dependency. We should verify its implementation to ensure it follows the same structure as the dynamic menus.
Also applies to: 16-16
✅ Verification successful
Let me verify the static routes implementation by searching for it in the codebase.
Let me check the implementation of the static routes to verify its structure.
Based on the code inspection, I can now provide a final response:
Static routes implementation is properly structured
The static routes implementation in static.ts
follows the correct structure:
- Uses the same
RouteRecordStringComponent
type as dynamic routes - Properly implements meta fields (icon, order, title)
- Follows nested route structure with children
- Includes all necessary fields for menu generation (component, meta, name, path)
The use of cloneDeep
from @vben/utils
is appropriate here as it prevents mutations when merging static and dynamic menus.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify the structure of static routes matches dynamic routes
# Check the structure of static routes
echo "Checking static routes structure..."
rg -A 10 "staticMenuList.*=" "./apps/web-antd/src/router/routes/static"
# Check if the static routes follow the same interface as dynamic routes
echo "Checking menu interface usage..."
ast-grep --pattern 'interface $MENU_TYPE {
$$$
}'
Length of output: 127229
Script:
#!/bin/bash
# Search for static routes implementation
echo "Searching for static routes implementation..."
rg -A 5 "staticMenuList" apps/web-antd/src/router/
# Check if there are any route-related files in the router directory
echo -e "\nChecking router directory structure..."
fd . apps/web-antd/src/router/
Length of output: 2035
Script:
#!/bin/bash
# Check the full implementation of static routes
echo "Checking static routes implementation..."
cat apps/web-antd/src/router/routes/static.ts
# Check the menu interface to verify structure compatibility
echo -e "\nChecking menu interfaces..."
rg -A 5 "interface.*Menu" apps/web-antd/src/router/
Length of output: 1716
apps/web-antd/src/router/routes/static.ts (1)
25-25
: Verify the component path convention
The component path /_core/about/index
starts with an underscore. Verify if this follows the project's conventions for internal/private components.
✅ Verification successful
Let me check the contents of these _core directories and their usage pattern to make a more informed decision.
Component path convention is consistent across the project
The /_core/about/index
path follows the established convention in the project. This pattern is consistently used across all web apps (antd, ele, naive) and is also documented in the project's route documentation. The underscore prefix is intentionally used to organize core/internal components, as evidenced by:
- Consistent
_core
directory structure in all web apps undersrc/views/
- Same component path pattern in both static and module routes
- Usage in official documentation examples
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Check for other component paths starting with underscore
fd -e ts -e tsx -e vue | xargs rg "component:.*['\"]/_.*['\"]" -l
# Check if _core directory exists and its structure
fd -t d "_core"
Length of output: 334
Script:
#!/bin/bash
# Check the structure and contents of _core directories
fd . "apps/*/src/views/_core" -t f
# Check for any documentation about _core convention
rg -i "(_core|core|internal)" "README.md" -A 2 -B 2
# Look for similar route patterns across different web apps
rg "component:.*/_core/about" -A 2 -B 2
Length of output: 3683
apps/web-ele/src/router/routes/static.ts (1)
39-73
: Well-structured route configuration
The static menu configuration follows Vue Router best practices:
- Proper use of meta properties for customization
- Clear hierarchy with BasicLayout and child routes
- Effective use of route ordering
- Modular integration of demo routes
apps/web-naive/src/router/routes/static.ts (1)
22-30
: Review path consistency and component path convention
A few concerns about the route configuration:
- The parent path is
/about
but the child path is/vben-admin/about
, which seems inconsistent - The component path
/_core/about/index
uses an underscore prefix, which typically indicates internal/private paths
Consider:
- Making the paths hierarchically consistent
- Reviewing if this component should be in a private directory
✅ Verification successful
Based on the search results, I can now generate the final response:
Path structure and component location are consistent with project conventions
The search results show that:
- The path
/vben-admin/about
is consistently used across all web app variants (antd, naive, ele) in their route configurations - The
/_core
directory is a standard location for core components, containing authentication, fallback, and about pages across the project
No changes needed as both the path structure and component location follow established project patterns.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Check for other routes using similar path patterns
rg -g '*.{ts,js,vue}' '/vben-admin/about|/_core/'
Length of output: 5408
const dynamicMenus = await getAllMenusApi(); | ||
// 本地菜单和动态菜单合并 | ||
return [...cloneDeep(staticMenuList), ...dynamicMenus]; |
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.
🛠️ Refactor suggestion
Add error handling and translate comment
- Consider adding error handling for
getAllMenusApi()
to gracefully handle API failures. - The Chinese comment should be translated to English for consistency.
Apply this diff:
- const dynamicMenus = await getAllMenusApi();
- // 本地菜单和动态菜单合并
- return [...cloneDeep(staticMenuList), ...dynamicMenus];
+ try {
+ const dynamicMenus = await getAllMenusApi();
+ // Merge local static menus with dynamic menus
+ return [...cloneDeep(staticMenuList), ...dynamicMenus];
+ } catch (error) {
+ message.error(`${$t('common.loadingMenuFailed')}`);
+ console.error('Failed to fetch dynamic menus:', error);
+ // Fallback to static menus only
+ return cloneDeep(staticMenuList);
+ }
Committable suggestion skipped: line range outside the PR's diff.
const dynamicMenus = await getAllMenusApi(); | ||
// 本地菜单和动态菜单合并 | ||
return [...cloneDeep(staticMenuList), ...dynamicMenus]; |
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.
🛠️ Refactor suggestion
Add error handling for getAllMenusApi.
The API call could fail, but there's no error handling. Consider adding try-catch to handle potential failures gracefully.
- const dynamicMenus = await getAllMenusApi();
- // 本地菜单和动态菜单合并
- return [...cloneDeep(staticMenuList), ...dynamicMenus];
+ try {
+ const dynamicMenus = await getAllMenusApi();
+ // Merge local static menus with dynamic menus
+ return [...cloneDeep(staticMenuList), ...dynamicMenus];
+ } catch (error) {
+ ElMessage.error($t('common.menuLoadError'));
+ console.error('Failed to load dynamic menus:', error);
+ // Fallback to static menus only
+ return cloneDeep(staticMenuList);
+ }
Committable suggestion skipped: line range outside the PR's diff.
const dynamicMenus = await getAllMenusApi(); | ||
// 本地菜单和动态菜单合并 | ||
return [...cloneDeep(staticMenuList), ...dynamicMenus]; |
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.
🛠️ Refactor suggestion
Consider adding validation and deduplication logic
The current implementation merges static and dynamic menus without validation. This could lead to:
- Duplicate menu items if the same routes exist in both lists
- Potential conflicts in menu hierarchy
- Performance impact from deep cloning large menu structures
Consider implementing the following improvements:
- return [...cloneDeep(staticMenuList), ...dynamicMenus];
+ const staticMenus = cloneDeep(staticMenuList);
+ // Validate menu structure
+ const validatedDynamicMenus = validateMenuStructure(dynamicMenus);
+ // Remove duplicates based on route path or menu key
+ const uniqueMenus = deduplicateMenus(staticMenus, validatedDynamicMenus);
+ return uniqueMenus;
Consider translating the Chinese comment to English for consistency:
- // 本地菜单和动态菜单合并
+ // Merge local static menus with dynamic menus
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const dynamicMenus = await getAllMenusApi(); | |
// 本地菜单和动态菜单合并 | |
return [...cloneDeep(staticMenuList), ...dynamicMenus]; | |
const dynamicMenus = await getAllMenusApi(); | |
// Merge local static menus with dynamic menus | |
return [...cloneDeep(staticMenuList), ...dynamicMenus]; |
path: '/about', | ||
children: [ | ||
{ | ||
component: '/_core/about/index', | ||
meta: { | ||
title: $t('demos.vben.about'), | ||
}, | ||
name: 'VbenAbout', | ||
path: '/vben-admin/about', |
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.
Review path inconsistency
The parent route path is /about
but the child route path is /vben-admin/about
. This inconsistency might cause routing issues.
path: '/about',
children: [
{
component: '/_core/about/index',
meta: {
title: $t('demos.vben.about'),
},
name: 'VbenAbout',
- path: '/vben-admin/about',
+ path: 'index',
},
],
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
path: '/about', | |
children: [ | |
{ | |
component: '/_core/about/index', | |
meta: { | |
title: $t('demos.vben.about'), | |
}, | |
name: 'VbenAbout', | |
path: '/vben-admin/about', | |
path: '/about', | |
children: [ | |
{ | |
component: '/_core/about/index', | |
meta: { | |
title: $t('demos.vben.about'), | |
}, | |
name: 'VbenAbout', | |
path: 'index', |
const demoRoute: RouteRecordStringComponent[] = [ | ||
{ | ||
component: 'BasicLayout', | ||
meta: { | ||
hideChildrenInMenu: true, | ||
icon: 'lucide:copyright', | ||
order: 9999, | ||
title: $t('demos.vben.about'), | ||
}, | ||
name: 'About', | ||
path: '/about', | ||
children: [ | ||
{ | ||
component: '/_core/about/index', | ||
meta: { | ||
title: $t('demos.vben.about'), | ||
}, | ||
name: 'VbenAbout', | ||
path: '/vben-admin/about', | ||
}, | ||
], | ||
}, | ||
]; |
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.
Fix inconsistent route path structure
The current path structure might cause routing issues:
- Parent path is
/about
- Child path is
/vben-admin/about
- Child path should extend the parent path pattern
Also, hideChildrenInMenu: true
might be unnecessary since there's only one child route.
Consider this fix:
const demoRoute: RouteRecordStringComponent[] = [
{
component: 'BasicLayout',
meta: {
- hideChildrenInMenu: true,
icon: 'lucide:copyright',
order: 9999,
title: $t('demos.vben.about'),
},
name: 'About',
- path: '/about',
+ path: '/vben-admin',
children: [
{
component: '/_core/about/index',
meta: {
title: $t('demos.vben.about'),
},
name: 'VbenAbout',
path: '/vben-admin/about',
},
],
},
];
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const demoRoute: RouteRecordStringComponent[] = [ | |
{ | |
component: 'BasicLayout', | |
meta: { | |
hideChildrenInMenu: true, | |
icon: 'lucide:copyright', | |
order: 9999, | |
title: $t('demos.vben.about'), | |
}, | |
name: 'About', | |
path: '/about', | |
children: [ | |
{ | |
component: '/_core/about/index', | |
meta: { | |
title: $t('demos.vben.about'), | |
}, | |
name: 'VbenAbout', | |
path: '/vben-admin/about', | |
}, | |
], | |
}, | |
]; | |
const demoRoute: RouteRecordStringComponent[] = [ | |
{ | |
component: 'BasicLayout', | |
meta: { | |
icon: 'lucide:copyright', | |
order: 9999, | |
title: $t('demos.vben.about'), | |
}, | |
name: 'About', | |
path: '/vben-admin', | |
children: [ | |
{ | |
component: '/_core/about/index', | |
meta: { | |
title: $t('demos.vben.about'), | |
}, | |
name: 'VbenAbout', | |
path: '/vben-admin/about', | |
}, | |
], | |
}, | |
]; |
Description
Type of change
Please delete options that are not relevant.
pnpm-lock.yaml
unless you introduce a new test example.Checklist
pnpm run docs:dev
command.pnpm test
.feat:
,fix:
,perf:
,docs:
, orchore:
.Summary by CodeRabbit
Release Notes
New Features
Bug Fixes
Documentation