Skip to content

Commit d982a0a

Browse files
DavertMikclaude
andcommitted
refactor(sitemap): simplify config to file= and strip query by default
Replace outputName/output with a single `file` option relative to the output directory (default output/sitemap.xml). stripQuery is now true by default. CLI args (-p sitemap:file=...:stripQuery=false) are parsed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 13dfeac commit d982a0a

3 files changed

Lines changed: 30 additions & 24 deletions

File tree

‎docs/plugins/sitemap.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ The sitemap is written once all tests finish.
3030

3131
Possible config options:
3232

33-
* `outputName`: file name for the sitemap. Default: `sitemap.xml`.
34-
* `output`: directory where the sitemap is stored, relative to the project root. Default: the `output` directory.
35-
* `stripQuery`: remove query strings so `/posts?page=1` and `/posts?page=2` become one `/posts` entry. Default: false.
33+
* `file`: path to the sitemap file, relative to the output directory. Default: `sitemap.xml`, saved as `output/sitemap.xml`.
34+
* `stripQuery`: remove query strings so `/posts?page=1` and `/posts?page=2` become one `/posts` entry. Set to `false` to keep them. Default: true.
3635

3736
CLI examples:
3837

3938
npx codeceptjs run -p sitemap
40-
npx codeceptjs run -p sitemap:outputName=visited.xml
39+
npx codeceptjs run -p sitemap:file=visited.xml
40+
npx codeceptjs run -p sitemap:stripQuery=false
4141

4242
Works with `run-workers`: URLs from all workers are merged into a single sitemap.
4343

‎lib/plugin/sitemap.js‎

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import event from '../event.js'
99
import recorder from '../recorder.js'
1010
import store from '../store.js'
1111
import output from '../output.js'
12+
import { parsePluginArgs } from '../utils/pluginParser.js'
1213

1314
const defaultConfig = {
14-
outputName: 'sitemap.xml',
15-
output: null,
16-
stripQuery: false,
15+
file: 'sitemap.xml',
16+
stripQuery: true,
1717
}
1818

1919
const SITEMAP_NS = 'http://www.sitemaps.org/schemas/sitemap/0.9'
@@ -41,31 +41,27 @@ const PARTIAL_PREFIX = '.sitemap.'
4141
*
4242
* Possible config options:
4343
*
44-
* * `outputName`: file name for the sitemap. Default: `sitemap.xml`.
45-
* * `output`: directory where the sitemap is stored, relative to the project root. Default: the `output` directory.
46-
* * `stripQuery`: remove query strings so `/posts?page=1` and `/posts?page=2` become one `/posts` entry. Default: false.
44+
* * `file`: path to the sitemap file, relative to the output directory. Default: `sitemap.xml`, saved as `output/sitemap.xml`.
45+
* * `stripQuery`: remove query strings so `/posts?page=1` and `/posts?page=2` become one `/posts` entry. Set to `false` to keep them. Default: true.
4746
*
4847
* CLI examples:
4948
*
5049
* ```
5150
* npx codeceptjs run -p sitemap
52-
* npx codeceptjs run -p sitemap:outputName=visited.xml
51+
* npx codeceptjs run -p sitemap:file=visited.xml
52+
* npx codeceptjs run -p sitemap:stripQuery=false
5353
* ```
5454
*
5555
* Works with `run-workers`: URLs from all workers are merged into a single sitemap.
5656
*
5757
* @param {*} config
5858
*/
5959
export default function (config = {}) {
60-
config = Object.assign({}, defaultConfig, config)
61-
if (config.stripQuery === 'true') config.stripQuery = true
60+
config = Object.assign({}, defaultConfig, config, parsePluginArgs(config._args))
6261

6362
const urls = new Set()
6463

65-
const outputDir = () => {
66-
if (config.output) return path.resolve(store.codeceptDir || process.cwd(), config.output)
67-
return store.outputDir || process.cwd()
68-
}
64+
const outputDir = () => store.outputDir || process.cwd()
6965

7066
const addUrl = url => {
7167
if (!url) return
@@ -84,9 +80,8 @@ export default function (config = {}) {
8480
const writeSitemap = list => {
8581
if (!list.length) return
8682

87-
const dir = outputDir()
88-
mkdirp.sync(dir)
89-
const file = path.join(dir, config.outputName)
83+
const file = path.resolve(outputDir(), config.file)
84+
mkdirp.sync(path.dirname(file))
9085
fs.writeFileSync(file, buildXml(list.sort()))
9186
output.plugin('sitemap', `Sitemap with ${list.length} pages saved to ${file}`)
9287
}

‎test/unit/plugin/sitemap_test.js‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('sitemap plugin', () => {
5858
})
5959

6060
it('writes unique visited pages to sitemap.xml', async () => {
61-
sitemap({})
61+
sitemap({ stripQuery: false })
6262
await startTest()
6363
await visit('http://localhost:8000/')
6464
await visit('http://localhost:8000/form/field#top')
@@ -159,18 +159,29 @@ describe('sitemap plugin', () => {
159159
expect(xml).not.to.include('/frame')
160160
})
161161

162-
it('strips query strings when configured', async () => {
163-
sitemap({ stripQuery: true, outputName: 'pages.xml' })
162+
it('strips query strings by default and saves to custom file', async () => {
163+
sitemap({ file: 'reports/pages.xml' })
164164
await startTest()
165165
await visit('http://localhost:8000/posts?page=1')
166166
await visit('http://localhost:8000/posts?page=2')
167167
event.dispatcher.emit(event.all.result, {})
168168

169-
const xml = fs.readFileSync(path.join(outputDir, 'pages.xml'), 'utf8')
169+
const xml = fs.readFileSync(path.join(outputDir, 'reports', 'pages.xml'), 'utf8')
170170
expect(xml.match(/<loc>/g).length).to.equal(1)
171171
expect(xml).to.include('<loc>http://localhost:8000/posts</loc>')
172172
})
173173

174+
it('keeps query strings when stripQuery=false is passed from CLI', async () => {
175+
sitemap({ _args: ['stripQuery=false'] })
176+
await startTest()
177+
await visit('http://localhost:8000/posts?page=1')
178+
await visit('http://localhost:8000/posts?page=2')
179+
event.dispatcher.emit(event.all.result, {})
180+
181+
const xml = fs.readFileSync(path.join(outputDir, 'sitemap.xml'), 'utf8')
182+
expect(xml.match(/<loc>/g).length).to.equal(2)
183+
})
184+
174185
it('merges worker partials on workers.result', async () => {
175186
sitemap({})
176187
fs.writeFileSync(path.join(outputDir, '.sitemap.1.1.json'), JSON.stringify(['http://localhost:8000/a']))

0 commit comments

Comments
 (0)