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.

ResponsiveContext doesn't work with Gatsby SSR

See original GitHub issue

I know this issue looks like a duplicate, but #2311 differs from this bug.

Steps to reproduce:

  1. Install a Gatsby starter of your choice (default is fine)
  2. Add Grommet and wrap the root file, e.g. index.js or layout.js (theme doesn’t matter)
  3. Add ResponsiveContext.Consumer
  4. Print the current size to the screen
  5. Run npm run develop and resize the screen. Like expected the size value changes as the screen size does.
  6. Run npm run build, open index.html from public folder and resize the screen. Now the size value remains medium on every screen size.

layout.js

import React from "react"
import { Grommet } from "grommet"
import PropTypes from "prop-types"
import { StaticQuery, graphql } from "gatsby"
import theme from "../themes/default"

import Header from "./header"
import "./layout.css"

const Layout = ({ children }) => (
  <StaticQuery
    query={graphql`
      query SiteTitleQuery {
        site {
          siteMetadata {
            title
          }
        }
      }
    `}
    render={data => (
      <Grommet theme={theme}>
        <Header siteTitle={data.site.siteMetadata.title} />
        <div
          style={{
            margin: `0 auto`,
            maxWidth: 960,
            padding: `0px 1.0875rem 1.45rem`,
            paddingTop: 0,
          }}
        >
          <main>{children}</main>
          <footer>
            © {new Date().getFullYear()}, Built with
            {` `}
            <a href="https://www.gatsbyjs.org">Gatsby</a>
          </footer>
        </div>
      </Grommet>
    )}
  />
)

Layout.propTypes = {
  children: PropTypes.node.isRequired,
}

export default Layout

index.js

import React from "react"
import { Link } from "gatsby"
import { Heading, ResponsiveContext } from "grommet"

import Layout from "../components/layout"
import Image from "../components/image"
import SEO from "../components/seo"

const IndexPage = () => (
  <Layout>
    <SEO title="Home" keywords={[`gatsby`, `application`, `react`]} />
    <ResponsiveContext.Consumer>
      {size => (
        <>
          <Heading>Hi people</Heading>
          <Heading level="2">{`Size: ${size}`}</Heading>
          <p>Welcome to your new Gatsby site.</p>
          <div style={{ maxWidth: `300px`, marginBottom: `1.45rem` }}>
            <Image />
          </div>
          <Link to="/page-2/">Go to page 2</Link>
        </>
      )}
    </ResponsiveContext.Consumer>
  </Layout>
)

export default IndexPage

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
oorestisimecommented, Apr 23, 2019

Do you have gatsby-plugin-styled-components installed?

0reactions
ShimiSuncommented, May 9, 2019

@pawelsas thanks for the update!

Read more comments on GitHub >

github_iconTop Results From Across the Web

gatsby-ssr.js doesn't seem to be running - Stack Overflow
One works and one doesn't. After a lot of troubleshooting, seems like gatsby-ssr.js is simply not being run for some reason. I've added ......
Read more >
Troubleshooting Common Errors - Gatsby
Problems with the cache · Errors with common plugin configurations. Installing plugins for styling results in Generating SSR bundle failed · Errors in...
Read more >
The Perils of Rehydration - Josh W Comeau
This deep-dive tutorial examines how React and Gatsby can be used to pre-render content, and how we can work around the constraints to...
Read more >
Advanced Usage - styled-components
This component provides a theme to all React components underneath itself via the context API. In the render tree all styled-components will have...
Read more >
Gatsby i18n: A Hands-on Guide | Phrase
Let's face it, the modern React workflow can be a pain in the neck: We have routing, server-side rendering (SSR), static generation, ...
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