From bab8056272e669ca28529c168ab739e96b4a467c Mon Sep 17 00:00:00 2001 From: Jon Sadka Date: Sun, 7 Apr 2024 12:22:21 -0700 Subject: [PATCH] bugfix: do not overwrite styletron instances We cannot guarantee that `onRenderBody` gets called after `wrapRootElement` in series. Because of this, we need to namespace the styletron instance so it does not get blow away if `wrapRootElement` for the next page is called before `onRenderBody` has been called. --- packages/gatsby-plugin-styletron/src/gatsby-ssr.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/gatsby-plugin-styletron/src/gatsby-ssr.js b/packages/gatsby-plugin-styletron/src/gatsby-ssr.js index 5d08efb934f12..4b01384276233 100644 --- a/packages/gatsby-plugin-styletron/src/gatsby-ssr.js +++ b/packages/gatsby-plugin-styletron/src/gatsby-ssr.js @@ -2,15 +2,17 @@ import * as React from "react" import { Server as Styletron } from "styletron-engine-atomic" import { Provider } from "styletron-react" -let instance +const instances = {} export function wrapRootElement({ element }, options) { - instance = new Styletron({ prefix: options.prefix }) + const instance = new Styletron({ prefix: options.prefix }) + instances[element.props.url] = instance return {element} } -export function onRenderBody({ setHeadComponents }) { +export function onRenderBody({ pathname, setHeadComponents }) { + const instance = instances[pathname] if (!instance) { return }