ResponsiveContext doesn't work with Gatsby SSR
See original GitHub issueI know this issue looks like a duplicate, but #2311 differs from this bug.
Steps to reproduce:
- Install a Gatsby starter of your choice (default is fine)
- Add Grommet and wrap the root file, e.g. index.js or layout.js (theme doesn’t matter)
- Add
ResponsiveContext.Consumer
- Print the current
size
to the screen - Run
npm run develop
and resize the screen. Like expected thesize
value changes as the screen size does. - Run
npm run build
, open index.html from public folder and resize the screen. Now thesize
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:
- Created 4 years ago
- Comments:12 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Do you have gatsby-plugin-styled-components installed?
@pawelsas thanks for the update!