Skip to content

Commit f15e5c8

Browse files
authoredDec 24, 2024··
fix: doubly encoding of URLs (#390)
1 parent 32ca5bb commit f15e5c8

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed
 

‎src/runtime/server/sitemap/urlset/normalise.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function preNormalizeEntry(_e: SitemapUrl | string, resolvers?: NitroUrlR
6565
e.loc = e._relativeLoc
6666
}
6767
}
68-
else {
68+
else if (!isEncoded(e.loc)) {
6969
e.loc = encodeURI(e.loc)
7070
}
7171
if (e.loc === '')
@@ -75,6 +75,16 @@ export function preNormalizeEntry(_e: SitemapUrl | string, resolvers?: NitroUrlR
7575
return e as ResolvedSitemapUrl
7676
}
7777

78+
export function isEncoded(url: string) {
79+
// checks, if an url is already decoded
80+
try {
81+
return url !== decodeURIComponent(url)
82+
}
83+
catch {
84+
return false
85+
}
86+
}
87+
7888
export function normaliseEntry(_e: ResolvedSitemapUrl, defaults: Omit<SitemapUrl, 'loc'>, resolvers?: NitroUrlResolvers): ResolvedSitemapUrl {
7989
const e = defu(_e, defaults) as ResolvedSitemapUrl
8090
if (e.lastmod) {
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { createResolver } from '@nuxt/kit'
3+
import { $fetch, setup } from '@nuxt/test-utils'
4+
5+
const { resolve } = createResolver(import.meta.url)
6+
7+
await setup({
8+
rootDir: resolve('../../fixtures/basic'),
9+
nuxtConfig: {
10+
sitemap: {
11+
urls: [
12+
'/Bücher',
13+
'/Bibliothèque',
14+
],
15+
},
16+
},
17+
})
18+
describe('query routes', () => {
19+
it('should be url encoded', async () => {
20+
const sitemap = await $fetch('/sitemap.xml')
21+
22+
expect(sitemap).toContain('<loc>https://nuxtseo.com/B%C3%BCcher</loc>')
23+
expect(sitemap).toContain('<loc>https://nuxtseo.com/Biblioth%C3%A8que</loc>')
24+
expect(sitemap).not.toContain('https://nuxtseo.com/Bücher')
25+
expect(sitemap).not.toContain('https://nuxtseo.com/Bibliothèque')
26+
}, 60000)
27+
})

0 commit comments

Comments
 (0)
Please sign in to comment.