Skip to content

Commit

Permalink
fix: more logical sorting of urls
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Oct 5, 2023
1 parent d53d166 commit 611006b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
9 changes: 3 additions & 6 deletions src/runtime/sitemap/entries/normalise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,10 @@ export async function normaliseSitemapData(data: SitemapEntryInput[], options: B

function normaliseEntries(entries: SitemapEntry[]) {
return mergeOnKey(entries.map(normaliseEntry), 'loc')
// sort based on logical string sorting of the loc
// sort based on logical string sorting of the loc, we need to properly account for numbers here
// so that urls: /route/1, /route/2 is displayed instead of /route/1, /route/10
.sort((a, b) => {
if (a.loc > b.loc)
return 1
if (a.loc < b.loc)
return -1
return 0
return a.loc.localeCompare(b.loc, undefined, { numeric: true })
})
.sort((a, b) => {
// we need to sort based on the path segments as well
Expand Down
34 changes: 33 additions & 1 deletion test/unit/normalise.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import { fixSlashes } from 'site-config-stack'
import { normaliseSitemapData } from '../../src/runtime/sitemap/entries'
import {normaliseEntries, normaliseSitemapData} from '../../src/runtime/sitemap/entries'
import type { BuildSitemapInput } from '../../src/runtime/types'

const normaliseOptions: BuildSitemapInput = {
Expand Down Expand Up @@ -38,4 +38,36 @@ describe('normalise', () => {
]
`)
})
it('sorting', async () => {
const data = await normaliseSitemapData([
{ loc: '/a' },
{ loc: '/b' },
{ loc: '/c' },
{ loc: '/1' },
{ loc: '/2' },
{ loc: '/10' },
], normaliseOptions)
expect(data).toMatchInlineSnapshot(`
[
{
"loc": "/1/",
},
{
"loc": "/2/",
},
{
"loc": "/10/",
},
{
"loc": "/a/",
},
{
"loc": "/b/",
},
{
"loc": "/c/",
},
]
`)
})
})

0 comments on commit 611006b

Please sign in to comment.