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 context leaking #1

Merged
merged 1 commit into from
Mar 10, 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
14 changes: 9 additions & 5 deletions gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var react_helmet_async_1 = require("react-helmet-async");
var context = {};
var onRenderBody = function (_a) {
var _b;
var setHeadComponents = _a.setHeadComponents, setHtmlAttributes = _a.setHtmlAttributes, setBodyAttributes = _a.setBodyAttributes;
var helmet = context.helmet;
var pathname = _a.pathname, setHeadComponents = _a.setHeadComponents, setHtmlAttributes = _a.setHtmlAttributes, setBodyAttributes = _a.setBodyAttributes;
var helmet = context[pathname].helmet;
if (helmet) {
var baseComponent = helmet.base.toComponent();
var titleComponent = helmet.title.toComponent();
Expand All @@ -31,15 +31,19 @@ var onRenderBody = function (_a) {
helmet.script.toComponent(),
helmet.noscript.toComponent()
];
setHeadComponents(((_b = titleComponent[0]) === null || _b === void 0 ? void 0 : _b.props.children)
setHeadComponents(
// eslint-disable-next-line
// @ts-ignore
((_b = titleComponent[0]) === null || _b === void 0 ? void 0 : _b.props.children)
? __spreadArray([baseComponent, titleComponent], components, true) : __spreadArray([baseComponent], components, true));
setHtmlAttributes(helmet.htmlAttributes.toComponent());
setBodyAttributes(helmet.bodyAttributes.toComponent());
}
};
exports.onRenderBody = onRenderBody;
var wrapRootElement = function (_a) {
var element = _a.element;
return (react_1.default.createElement(react_helmet_async_1.HelmetProvider, { context: context }, element));
var pathname = _a.pathname, element = _a.element;
context[pathname] = {};
return (react_1.default.createElement(react_helmet_async_1.HelmetProvider, { context: context[pathname] }, element));
};
exports.wrapRootElement = wrapRootElement;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"author": "Martin Rosenberg <Martin.B.Rosenberg@gmail.com> (https://martinbrosenberg.com)",
"main": "index.js",
"scripts": {
"prepublish": "yarn build",
"prepare": "yarn build",
"build": "tsc --build",
"clean": "rm gatsby-browser.d.ts gatsby-browser.js gatsby-ssr.d.ts gatsby-ssr.js",
"lint": "eslint src --ext ts,tsx --fix",
Expand All @@ -42,7 +42,7 @@
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-react": "^7.31.10",
"gatsby": "^3.8.0",
"gatsby": "^4.25.5",
"prettier": "^2.7.1",
"prettier-eslint": "^15.0.1",
"react": "^17.0.2",
Expand Down
17 changes: 11 additions & 6 deletions src/gatsby-ssr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import { GatsbySSR, RenderBodyArgs, WrapRootElementNodeArgs } from 'gatsby';
import React from 'react';
import { HelmetProvider, HelmetServerState } from 'react-helmet-async';

const context: { helmet?: HelmetServerState } = {};
const context: { [pathname: string]: { helmet?: HelmetServerState } } = {};

export const onRenderBody: GatsbySSR['onRenderBody'] = ({
pathname,
setHeadComponents,
setHtmlAttributes,
setBodyAttributes
}: RenderBodyArgs): void => {
const { helmet } = context;
const { helmet } = context[pathname];

if (helmet) {
const baseComponent = helmet.base.toComponent();
const titleComponent = helmet.title.toComponent() as unknown as any[];
const titleComponent = helmet.title.toComponent();
const components = [
helmet.priority.toComponent(),
helmet.meta.toComponent(),
Expand All @@ -35,7 +36,11 @@ export const onRenderBody: GatsbySSR['onRenderBody'] = ({
};

export const wrapRootElement: GatsbySSR['wrapRootElement'] = ({
pathname,
element
}: WrapRootElementNodeArgs): React.ReactElement => (
<HelmetProvider context={context}>{element}</HelmetProvider>
);
}: WrapRootElementNodeArgs): React.ReactElement => {
context[pathname] = {};
return (
<HelmetProvider context={context[pathname]}>{element}</HelmetProvider>
);
};
Loading