Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-plugin-sitemap): remove splice for default filter #37916

Merged
merged 5 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/gatsby-plugin-sitemap/src/__tests__/internals.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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