Skip to content

Commit 9d042af

Browse files
committed
fix(dev): group routes by kind and label method-less handlers
1 parent 6f6e530 commit 9d042af

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎packages/nuxt-cli/src/dev/tui/route-overlay.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class RouteOverlay extends ScreenOverlay {
116116
#formatRoute(route: DevRoute,width: number,columns: number): string{
117117
constkind=route.kind==='page'
118118
? styleText('cyan','page ')
119-
: styleText('magenta',(route.method?.toUpperCase()??'all').padEnd(6))
119+
: styleText('magenta',(route.method?.toUpperCase()??'any').padEnd(6))
120120
constpath=route.route.padEnd(width)
121121
constfile=route.file ? relative(this.#cwd,route.file) : ''
122122
// The tail of a path identifies it; the head is the part every row shares.
@@ -131,7 +131,7 @@ export class RouteOverlay extends ScreenOverlay {
131131
returnthis.#routes
132132
.filter(route=>this.#filter ==='all'||route.kind===this.#filter)
133133
.filter(route=>!query||`${route.route}${route.file??''}`.toLowerCase().includes(query))
134-
.sort((a,b)=>a.route.localeCompare(b.route))
134+
.sort((a,b)=>(a.kind===b.kind ? 0 : a.kind==='page' ? -1 : 1)||a.route.localeCompare(b.route))
135135
}
136136

137137
#setFilter(filter: RouteFilter): boolean{

‎packages/nuxt-cli/test/unit/dev-tui.spec.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,27 @@ describe('route overlay', () => {
15541554
}
15551555
})
15561556

1557+
it('groups pages before server routes, each sorted by path',()=>{
1558+
const{ overlay, lastFrame }=create()
1559+
overlay.setRoutes({routes: [
1560+
{kind: 'server',route: '/api/zebra',method: 'get',file: '/project/server/api/zebra.ts'},
1561+
{kind: 'page',route: '/zoo',file: '/project/app/pages/zoo.vue'},
1562+
{kind: 'server',route: '/api/apple',method: 'get',file: '/project/server/api/apple.ts'},
1563+
{kind: 'page',route: '/about',file: '/project/app/pages/about.vue'},
1564+
]})
1565+
overlay.open()
1566+
constframe=lastFrame()
1567+
constorder=['/about','/zoo','/api/apple','/api/zebra'].map(route=>frame.indexOf(route))
1568+
expect(order).toEqual([...order].sort((a,b)=>a-b))
1569+
})
1570+
1571+
it('labels a method-less server route as any rather than all',()=>{
1572+
const{ overlay, lastFrame }=create()
1573+
overlay.setRoutes({routes: [{kind: 'server',route: '/api/catch',file: '/project/server/api/catch.ts'}]})
1574+
overlay.open()
1575+
expect(lastFrame()).toContain('any')
1576+
})
1577+
15571578
it('sorts routes by path and filters by kind',()=>{
15581579
const{ overlay, lastFrame }=create()
15591580
overlay.setRoutes({ routes })

0 commit comments

Comments
 (0)