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): allow paths with trailing slash in exclude option #20625

Merged
merged 2 commits into from Jan 15, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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