question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Site's Server rendered components not getting cleaned up in browser

See original GitHub issue

Description

With two of my site’s components, the NavBar and the CookieConsentForm. Multiple instances are present on load. It appears that the components created during the build, which I imagine are created using the component tree described in the gatsby-ssr.js are overriding OR existing with the components described in the gatsby-browser.js file.

So in the example of the NavBar, the component looks a little like this

function NavBar() {
  <Router>
    <SimpleNavBar path="/landing-page/" />
    <NavBar default path="/" />
  </Router>
}

This displays the right components at the right routes when I start my development server locally. I imagine this is because Gatsby’s building the component tree according to gatsby-browser.js file. When I run the production build and serve it, and navigate to the landing page, the majority of the styles of NavBar component are present, with some of the content of the SimpleNavBar also being present. It’s as if Gatsby has merged the components together.

A similar thing happens in my CookieConsentForm component. From a component standpoint it’s just a div container, with some text and a button inside of it. When I run my development server, it appears just as it should. When I run my production build, it displays as it should, and when I click the button (to acknowledge that I accept the cookie terms), it closes the component rendered by the gatsby-browser.js, but keeps the container from the server-rendered component.

My current workaround is to remove the CookieConsentForm and NavBar from the server-rendered components. I don’t mind doing this for the CCF, but with the NavBar only being rendered on the browser, it makes my site feel jerky.

In the below image, you can see that I’m rendering two components, the top of the two is just the container and nothing else, while the other is contains the full component. It looks like the former is the left over from the server-rendered components.

image

Does Gatsby somehow use a component equality check to match components built with SSR and the ones built in the browser, and then swaps them out? If so it could make sense as top why it’s causing the NavBar to break, because it’s wrapped in a router (and I guess routing isn’t something that the server is capable of dealing with).

Up until now I’ve kept the gatsby-browser and gatsby-ssr files in sync, but it’s making less sense to do so, considering that I don’t need to render a cookie consent form on the server, and I don’t need to perform any of the initialisation that’s required when a user starts a session.

Steps to reproduce

As for reproducing, it’s only appeared now, and not before so I don’t know what the cause of the issue is. I upgraded my packages a week or so ago, and had no issues until i created a brand new landing page today.

Environment

  System:
    OS: macOS Mojave 10.14.6
    CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 12.14.1 - ~/.nvm/versions/node/v12.14.1/bin/node
    Yarn: 1.21.1 - /usr/local/bin/yarn
    npm: 6.13.4 - ~/.nvm/versions/node/v12.14.1/bin/npm
  Languages:
    Python: 2.7.10 - /usr/bin/python
  Browsers:
    Chrome: 79.0.3945.130
    Safari: 13.0
  npmPackages:
    gatsby: ^2.19.12 => 2.19.12 
    gatsby-image: ^2.2.40 => 2.2.40 
    gatsby-plugin-catch-links: ^2.1.25 => 2.1.25 
    gatsby-plugin-google-analytics: ^2.1.35 => 2.1.35 
    gatsby-plugin-manifest: ^2.2.41 => 2.2.41 
    gatsby-plugin-offline: ^3.0.34 => 3.0.34 
    gatsby-plugin-postcss: ^2.1.20 => 2.1.20 
    gatsby-plugin-robots-txt: ^1.5.0 => 1.5.0 
    gatsby-plugin-s3: ^0.3.2 => 0.3.2 
    gatsby-plugin-sharp: ^2.4.5 => 2.4.5 
    gatsby-plugin-sitemap: ^2.2.27 => 2.2.27 
    gatsby-plugin-styled-components: ^3.1.19 => 3.1.19 
    gatsby-plugin-typescript: ^2.1.27 => 2.1.27 
    gatsby-plugin-zopfli: ^1.3.2 => 1.3.2 
    gatsby-source-filesystem: ^2.1.48 => 2.1.48 
    gatsby-source-graphql: ^2.1.25 => 2.1.25 
    gatsby-transformer-sharp: ^2.3.14 => 2.3.14 

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
piehcommented, Feb 10, 2020

Does Gatsby somehow use a component equality check to match components built with SSR and the ones built in the browser, and then swaps them out? If so it could make sense as top why it’s causing the NavBar to break, because it’s wrapped in a router (and I guess routing isn’t something that the server is capable of dealing with).

It’s not Gatsby directly that does that - it’s React when it “hydrates” elements - React expects that DOM elements in SSR html match exactly what first render on the client/browser will be - if they don’t match then it will lead to weird cases, that could explain what you are describing - check https://reactjs.org/docs/react-dom.html#hydrate for explainer on that.

I’m not sure if I ever tried using <Router> inside gatsby-browser or ssr, so behaviour might be undefined here - would be great if you could provide some reproduction sample of that

0reactions
LekoArtscommented, May 7, 2021

Closing this as stale since in the meantime Gatsby v3 and updated related packages were released. Please try with the latest versions and if you still see this problem open a new bug report (it must include a minimal reproduction).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server-side-rendered styles are missing and/or incorrect #17676
Clearing all "Site Data" from the Developer Tools console seems to fix the issue which might be a hint to something wrong with...
Read more >
Rendering on the Web
If you're unsure whether a given solution is static rendering or prerendering, try this test: disable JavaScript and load the created web pages....
Read more >
Client-side vs. server-side rendering: why it's not all black and ...
Once the request is done processing, your browser gets back the fully rendered HTML and displays it on the screen. If you then...
Read more >
How to Enable Server-Side Rendering for a React App
Step 1 — Creating the React App and Modifying the App Component. First, use npx to start up a new React app using...
Read more >
In React, how do I detect if my component is rendering from ...
How do I detect if I'm at the client or at the server? I'm looking for something like isClient() or isServer() . javascript...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found