Skip to content

Commit

Permalink
Merge pull request #458 from georgebutter/master
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvishnusankar committed Mar 8, 2023
2 parents 6911a3f + b24143f commit 2e59550
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,40 @@ describe('UrlSetBuilder', () => {
])
})

test('createUrlSet: With async exclusion', async () => {
const sleep = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms))
const builder = new UrlSetBuilder(
{
...sampleConfig,
exclude: async () => {
await sleep(10)
return ['/', '/page-0', '/page-2']
},
},
sampleManifest
)

await expect(builder.createUrlSet()).resolves.toStrictEqual([
{
changefreq: 'daily',
lastmod: expect.any(String),
priority: 0.7,
loc: 'https://example.com/page-1',
alternateRefs: [],
trailingSlash: false,
},
{
changefreq: 'daily',
lastmod: expect.any(String),
priority: 0.7,
loc: 'https://example.com/page-3',
alternateRefs: [],
trailingSlash: false,
},
])
})

test('createUrlSet: Without trailing slash', async () => {
const builder = new UrlSetBuilder(
{
Expand Down
9 changes: 7 additions & 2 deletions packages/next-sitemap/src/builders/url-set-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,13 @@ export class UrlSetBuilder {
}

// Remove the urls based on this.config?.exclude array
if (this.config?.exclude && this.config?.exclude.length > 0) {
urlSet = removeIfMatchPattern(urlSet, this.config?.exclude)
if (this.config?.exclude) {
if (typeof this.config.exclude === 'function') {
const asyncExcludes = await this.config.exclude()
urlSet = removeIfMatchPattern(urlSet, asyncExcludes)
} else {
urlSet = removeIfMatchPattern(urlSet, this.config?.exclude)
}
}

urlSet = [...new Set(urlSet)]
Expand Down
2 changes: 1 addition & 1 deletion packages/next-sitemap/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export interface IConfig {
* Apart from this option next-sitemap also offers a custom transform option which could be used to exclude urls that match specific patterns
* @example ['/page-0', '/page-*', '/private/*']
*/
exclude?: string[]
exclude?: string[] | (() => Promise<string[]>)

alternateRefs?: Array<IAlternateRef>

Expand Down

0 comments on commit 2e59550

Please sign in to comment.