Skip to content

separate context for different pages with pathname #106

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 8 additions & 4 deletions src/gatsby-ssr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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 }} = {};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correctly typed?

On Line #14 we're checking if (helmet) and then accessing helmet.base but it's actually helmet.helmet.base now?

npm ERR! src/gatsby-ssr.tsx(16,39): error TS2339: Property 'title' does not exist on type '{ helmet?: HelmetServerState | undefined; }'.
npm ERR! src/gatsby-ssr.tsx(18,20): error TS2339: Property 'priority' does not exist on type '{ helmet?: HelmetServerState | undefined; }'.
npm ERR! src/gatsby-ssr.tsx(19,20): error TS2339: Property 'meta' does not exist on type '{ helmet?: HelmetServerState | undefined; }'.
npm ERR! src/gatsby-ssr.tsx(20,20): error TS2339: Property 'link' does not exist on type '{ helmet?: HelmetServerState | undefined; }'.
npm ERR! src/gatsby-ssr.tsx(21,20): error TS2339: Property 'style' does not exist on type '{ helmet?: HelmetServerState | undefined; }'.
npm ERR! src/gatsby-ssr.tsx(22,20): error TS2339: Property 'script' does not exist on type '{ helmet?: HelmetServerState | undefined; }'.
npm ERR! src/gatsby-ssr.tsx(23,20): error TS2339: Property 'noscript' does not exist on type '{ helmet?: HelmetServerState | undefined; }'.
npm ERR! src/gatsby-ssr.tsx(32,34): error TS2339: Property 'htmlAttributes' does not exist on type '{ helmet?: HelmetServerState | undefined; }'.
npm ERR! src/gatsby-ssr.tsx(33,34): error TS2339: Property 'bodyAttributes' does not exist on type '{ helmet?: HelmetServerState | undefined; }'.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this typing is probably correct but we should access context on onRenderBody like this:

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


export const onRenderBody: GatsbySSR['onRenderBody'] = ({
setHeadComponents,
Expand Down Expand Up @@ -35,7 +35,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>
);
}