Skip to content

Commit

Permalink
fix(gatsby-plugin-sitemap): allow paths with trailing slash in `exclu…
Browse files Browse the repository at this point in the history
…de` option (#20625)

* fix(gatsby-plugin-sitemap): allow paths with trailing slash in `exclude` option

Now `gatsby-plugin-sitemap` treats `/foo/bar/` and `/foo/bar` as the same,
which removes unnecessary confusion when using the `exclude` option.

* chore(gatsby-plugin-sitemap): fix inconsistencies in `README.md`
  • Loading branch information
notzenox authored and GatsbyJS Bot committed Jan 15, 2020
1 parent feebb26 commit ba99607
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-sitemap/README.md
Expand Up @@ -33,7 +33,7 @@ The options are as follows:
- `createLinkInHead` (boolean) Whether to populate the `<head>` of your site with a link to the sitemap.
- `serialize` (function) Takes the output of the data query and lets you return an array of sitemap entries.

We _ALWAYS_ exclude the following pages: `/dev-404-page/`,`/404` &`/offline-plugin-app-shell-fallback/`, this cannot be changed.
We _ALWAYS_ exclude the following pages: `/dev-404-page`,`/404` &`/offline-plugin-app-shell-fallback`, this cannot be changed.

Example:

Expand All @@ -50,7 +50,7 @@ plugins: [
// Exclude specific pages or groups of pages using glob parameters
// See: https://github.com/isaacs/minimatch
// The example below will exclude the single `path/to/page` and all routes beginning with `category`
exclude: ["/category/*", `/path/to/page`],
exclude: [`/category/*`, `/path/to/page`],
query: `
{
site {
Expand Down
10 changes: 9 additions & 1 deletion packages/gatsby-plugin-sitemap/src/__tests__/internals.js
Expand Up @@ -70,14 +70,22 @@ describe(`results using default settings`, () => {
])
})

it(`excludes pages`, async () => {
it(`excludes pages without trailing slash`, async () => {
const graphql = () => Promise.resolve(generateQueryResultsMock())
const queryRecords = await runQuery(graphql, ``, [`/page-2`], pathPrefix)
const urls = serialize(queryRecords)

verifyUrlsExistInResults(urls, [`http://dummy.url${pathPrefix}/page-1`])
})

it(`excludes pages with trailing slash`, async () => {
const graphql = () => Promise.resolve(generateQueryResultsMock())
const queryRecords = await runQuery(graphql, ``, [`/page-2/`], pathPrefix)
const urls = serialize(queryRecords)

verifyUrlsExistInResults(urls, [`http://dummy.url${pathPrefix}/page-1`])
})

it(`should fail when siteUrl is not set`, async () => {
const graphql = () =>
Promise.resolve(generateQueryResultsMock({ siteUrl: null }))
Expand Down
5 changes: 4 additions & 1 deletion packages/gatsby-plugin-sitemap/src/internals.js
Expand Up @@ -18,7 +18,10 @@ export const runQuery = (handler, query, excludes, pathPrefix) =>
r.data.allSitePage.edges = r.data.allSitePage.edges.filter(
page =>
!excludes.some(excludedRoute =>
minimatch(withoutTrailingSlash(page.node.path), excludedRoute)
minimatch(
withoutTrailingSlash(page.node.path),
withoutTrailingSlash(excludedRoute)
)
)
)

Expand Down

0 comments on commit ba99607

Please sign in to comment.