Skip to content

Commit e9504f2

Browse files
authored
Allows sitemap output on root of public folder (#31130)
1 parent 5634ef9 commit e9504f2

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

packages/gatsby-plugin-sitemap/src/__tests__/__snapshots__/gatsby-ssr.js.snap

+22
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,26 @@ exports[`gatsby-plugin-sitemap SSR API should create a Link if createLinkInHead
4444
}
4545
`;
4646

47+
exports[`gatsby-plugin-sitemap SSR API should create a Link in root when output is "/" 1`] = `
48+
[MockFunction] {
49+
"calls": Array [
50+
Array [
51+
Array [
52+
<link
53+
href="/sitemap-index.xml"
54+
rel="sitemap"
55+
type="application/xml"
56+
/>,
57+
],
58+
],
59+
],
60+
"results": Array [
61+
Object {
62+
"type": "return",
63+
"value": undefined,
64+
},
65+
],
66+
}
67+
`;
68+
4769
exports[`gatsby-plugin-sitemap SSR API should not create Link if createLinkInHead is false 1`] = `[MockFunction]`;

packages/gatsby-plugin-sitemap/src/__tests__/gatsby-ssr.js

+17
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ describe(`gatsby-plugin-sitemap SSR API`, () => {
2828
expect(setHeadComponents).toMatchSnapshot()
2929
expect(setHeadComponents).toHaveBeenCalledTimes(1)
3030
})
31+
it(`should create a Link in root when output is "/"`, async () => {
32+
const pluginOptions = {
33+
createLinkInHead: true,
34+
output: `/`,
35+
}
36+
const setHeadComponents = jest.fn()
37+
38+
await onRenderBody(
39+
{
40+
setHeadComponents,
41+
},
42+
pluginOptions
43+
)
44+
45+
expect(setHeadComponents).toMatchSnapshot()
46+
expect(setHeadComponents).toHaveBeenCalledTimes(1)
47+
})
3148
it(`should not create Link if createLinkInHead is false`, async () => {
3249
const pluginOptions = {
3350
createLinkInHead: false,

packages/gatsby-plugin-sitemap/src/gatsby-ssr.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from "react"
22
import { withPrefix as fallbackWithPrefix, withAssetPrefix } from "gatsby"
3-
import { withoutTrailingSlash } from "./internals"
3+
import { posix } from "path"
44

55
// TODO: Remove for v3 - Fix janky path/asset prefixing
66
const withPrefix = withAssetPrefix || fallbackWithPrefix
@@ -17,7 +17,7 @@ exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
1717
key={`gatsby-plugin-sitemap`}
1818
rel="sitemap"
1919
type="application/xml"
20-
href={withPrefix(withoutTrailingSlash(output) + `/sitemap-index.xml`)}
20+
href={withPrefix(posix.join(output, `/sitemap-index.xml`))}
2121
/>,
2222
])
2323
}

0 commit comments

Comments
 (0)