Skip to content

Commit 3764d6c

Browse files
authored
fix: default options not properly merged (#34)
1 parent 8246532 commit 3764d6c

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

specs/gatsby-ssr.specs.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,73 @@ describe("gatsby-ssr", () => {
88
});
99

1010
it("executes when includeDefaultCss is default", () => {
11-
onRenderBody({ setHeadComponents });
11+
onRenderBody({ setHeadComponents }, { plugins: [] });
1212
expect(setHeadComponents).toHaveBeenCalledTimes(1);
1313
});
1414

1515
it("executes when gistDefaultCssInclude is default", () => {
16-
onRenderBody({ setHeadComponents });
16+
onRenderBody({ setHeadComponents }, { plugins: [] });
1717
expect(setHeadComponents).toHaveBeenCalledTimes(1);
1818
});
1919

2020
it("executes when includeDefaultCss is true", () => {
21-
onRenderBody({ setHeadComponents }, { includeDefaultCss: true });
21+
onRenderBody(
22+
{ setHeadComponents },
23+
{ plugins: [], includeDefaultCss: true }
24+
);
2225
expect(setHeadComponents).toHaveBeenCalledTimes(1);
2326
});
2427

2528
it("executes when gistDefaultCssInclude is true", () => {
26-
onRenderBody({ setHeadComponents }, { gistDefaultCssInclude: true });
29+
onRenderBody(
30+
{ setHeadComponents },
31+
{ plugins: [], gistDefaultCssInclude: true }
32+
);
2733
expect(setHeadComponents).toHaveBeenCalledTimes(1);
2834
});
2935

3036
it("doesn't execute when includeDefaultCss is false", () => {
31-
onRenderBody({ setHeadComponents }, { includeDefaultCss: false });
37+
onRenderBody(
38+
{ setHeadComponents },
39+
{ plugins: [], includeDefaultCss: false }
40+
);
3241
expect(setHeadComponents).toHaveBeenCalledTimes(0);
3342
});
3443

3544
it("doesn't execute when gistDefaultCssInclude is false", () => {
36-
onRenderBody({ setHeadComponents }, { gistDefaultCssInclude: false });
45+
onRenderBody(
46+
{ setHeadComponents },
47+
{ plugins: [], gistDefaultCssInclude: false }
48+
);
3749
expect(setHeadComponents).toHaveBeenCalledTimes(0);
3850
});
3951

4052
it("executes when gistCssPreload is missing", () => {
41-
onRenderBody({ setHeadComponents });
53+
onRenderBody({ setHeadComponents }, { plugins: [] });
4254
expect(setHeadComponents).toHaveBeenCalledTimes(1);
4355
});
4456

4557
it("executes when gistCssPreload is false", () => {
46-
onRenderBody({ setHeadComponents }, { gistCssPreload: false });
58+
onRenderBody({ setHeadComponents }, { plugins: [], gistCssPreload: false });
4759
expect(setHeadComponents).toHaveBeenCalledTimes(1);
4860
});
4961

5062
it("executes when gistCssPreload is true", () => {
51-
onRenderBody({ setHeadComponents }, { gistCssPreload: true });
63+
onRenderBody({ setHeadComponents }, { plugins: [], gistCssPreload: true });
5264
expect(setHeadComponents).toHaveBeenCalledTimes(1);
5365
expect(setHeadComponents.mock.calls[0][0].length).toBe(3);
5466
});
5567

5668
it("executes when gistCssPreload is true", () => {
57-
onRenderBody({ setHeadComponents }, { gistCssPreload: true });
69+
onRenderBody({ setHeadComponents }, { plugins: [], gistCssPreload: true });
5870
expect(setHeadComponents).toHaveBeenCalledTimes(1);
5971
expect(setHeadComponents.mock.calls[0][0].length).toBe(3);
6072
});
6173

6274
it("updates the url when one is provided", () => {
6375
onRenderBody(
6476
{ setHeadComponents },
65-
{ gistCssPreload: true, gistCssUrlAddress: "https://test" }
77+
{ plugins: [], gistCssPreload: true, gistCssUrlAddress: "https://test" }
6678
);
6779
expect(setHeadComponents).toHaveBeenCalledTimes(1);
6880

src/gatsby-ssr.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ import React from "react";
1212
* @param {PluginOptions} options the options of the plugin.
1313
* @returns {*} rendered body.
1414
*/
15-
export function onRenderBody(
16-
{ setHeadComponents },
15+
export function onRenderBody({ setHeadComponents }, options = {}) {
1716
options = {
18-
gistCssPreload: false,
19-
gistCssUrlAddress:
20-
"https://github.githubassets.com/assets/gist-embed-b3b573358bfc66d89e1e95dbf8319c09.css"
21-
}
22-
) {
17+
...{
18+
gistCssPreload: false,
19+
gistCssUrlAddress:
20+
"https://github.githubassets.com/assets/gist-embed-b3b573358bfc66d89e1e95dbf8319c09.css"
21+
},
22+
...options
23+
};
24+
2325
let includeCss = true;
2426
if (options.gistDefaultCssInclude != null) {
2527
includeCss = options.gistDefaultCssInclude;
@@ -54,7 +56,7 @@ export function onRenderBody(
5456
}}
5557
></script>
5658
]
57-
: [<link href={options.gistCssUrlAddress} rel="stylesheet" />]
59+
: [<link href={options.gistCssUrlAddress} key={key} rel="stylesheet" />]
5860
);
5961
}
6062
}

0 commit comments

Comments
 (0)