Skip to content

Commit

Permalink
fix(gatsby-plugin-sitemap): remove splice for default filter (#37916)
Browse files Browse the repository at this point in the history
  • Loading branch information
kathmbeck committed Apr 12, 2023
1 parent ccb0e7a commit dd47b7c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
25 changes: 25 additions & 0 deletions packages/gatsby-plugin-sitemap/src/__tests__/internals.js
Expand Up @@ -97,4 +97,29 @@ describe(`gatsby-plugin-sitemap internals tests`, () => {

expect(results).toMatchSnapshot()
})

it(`pageFilter should filter correctly on consecutive runs`, () => {
const allPages = [
{ path: `/to/keep/1` },
{ path: `/to/keep/2` },
{ path: `/404.html` },
]
const filterPages = jest.fn()

const { filteredPages } = pageFilter({
allPages,
filterPages,
excludes: [],
})
expect(filteredPages).toHaveLength(2)
expect(filteredPages).not.toContainEqual({ path: `/404.html` })

const { filteredPages: filteredPages2 } = pageFilter({
allPages,
filterPages,
excludes: [],
})
expect(filteredPages2).toHaveLength(2)
expect(filteredPages2).not.toContainEqual({ path: `/404.html` })
})
})
7 changes: 1 addition & 6 deletions packages/gatsby-plugin-sitemap/src/internals.js
Expand Up @@ -161,19 +161,14 @@ export function pageFilter({ allPages, filterPages, excludes }) {

// TODO we should optimize these loops
const filteredPages = allPages.filter(page => {
const defaultFilterMatches = defaultExcludes.some((exclude, i, arr) => {
const defaultFilterMatches = defaultExcludes.some(exclude => {
try {
const doesMatch = defaultFilterPages(page, exclude, {
minimatch,
withoutTrailingSlash,
resolvePagePath,
})

// default excludes can only be found once, so remove them from the arr once excluded
if (doesMatch) {
arr.splice(i, 1)
}

return doesMatch
} catch {
throw new Error(`${REPORTER_PREFIX} Error in default page filter`)
Expand Down

0 comments on commit dd47b7c

Please sign in to comment.