Skip to content

Commit

Permalink
fix: avoid stripping query params from entries
Browse files Browse the repository at this point in the history
Fixes #92
  • Loading branch information
harlan-zw committed Jul 16, 2023
1 parent 2f3009a commit f8e1fee
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/runtime/sitemap/entries/normalise.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { hasProtocol, joinURL, parseURL } from 'ufo'
import { hasProtocol, joinURL } from 'ufo'
import { defu } from 'defu'
import { fixSlashes } from 'site-config-stack'
import type {
BuildSitemapIndexInput,
BuildSitemapInput,
ResolvedSitemapEntry,
SitemapEntryInput,
SitemapEntry,
SitemapEntryInput,
SitemapRenderCtx,
} from '../../types'
import { createFilter } from '../../util/urlFilter'
Expand Down Expand Up @@ -54,7 +55,7 @@ export async function normaliseSitemapData(data: SitemapEntryInput[], options: B
delete e.url
}
// we want a uniform loc so we can dedupe using it, remove slashes and only get the path
e.loc = parseURL(e.loc).pathname
e.loc = fixSlashes(false, e.loc)
e = defu(e, defaultEntryData)
return e
})
Expand Down
32 changes: 32 additions & 0 deletions test/queryRoutes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from 'vitest'
import { createResolver } from '@nuxt/kit'
import { $fetch, setup } from '@nuxt/test-utils'

const { resolve } = createResolver(import.meta.url)

await setup({
rootDir: resolve('../.playground'),
build: true,
server: true,
nuxtConfig: {
sitemap: {
urls: [
'/',
'/query-no-slash?foo=bar',
'/query-slash/?foo=bar',
'/query-slash-hash/?foo=bar#hash',
],
autoLastmod: false,
sitemaps: false,
},
},
})
describe('query routes', () => {
it('basic', async () => {
const sitemap = await $fetch('/sitemap.xml')

expect(sitemap).toContain('<loc>https://nuxtseo.com/query-no-slash?foo=bar</loc>')
expect(sitemap).toContain('<loc>https://nuxtseo.com/query-slash?foo=bar</loc>')
expect(sitemap).toContain('<loc>https://nuxtseo.com/query-slash-hash?foo=bar#hash</loc>')
}, 60000)
})
41 changes: 41 additions & 0 deletions test/unit/normalise.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, it } from 'vitest'
import { fixSlashes } from 'site-config-stack'
import { normaliseSitemapData } from '../../src/runtime/sitemap/entries'
import type { BuildSitemapInput } from '../../src/runtime/types'

const normaliseOptions: BuildSitemapInput = {
// @ts-expect-error test hack
moduleConfig: {},
// @ts-expect-error test hack
buildTimeMeta: {},
getRouteRulesForPath: () => ({}),
canonicalUrlResolver: (path: string) => fixSlashes(true, path),
nitroUrlResolver: (path: string) => path,
relativeBaseUrlResolver: (path: string) => path,
pages: [],
urls: [],
}
describe('normalise', () => {
it('query', async () => {
const normalisedWithoutSlash = await normaliseSitemapData([
{ loc: '/query?foo=bar' },
], normaliseOptions)
expect(normalisedWithoutSlash).toMatchInlineSnapshot(`
[
{
"loc": "/query/?foo=bar",
},
]
`)
const normalisedWithSlash = await normaliseSitemapData([
{ loc: '/query/?foo=bar' },
], normaliseOptions)
expect(normalisedWithSlash).toMatchInlineSnapshot(`
[
{
"loc": "/query/?foo=bar",
},
]
`)
})
})

0 comments on commit f8e1fee

Please sign in to comment.