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.

v2.1 Missing media queries with SSR

See original GitHub issue

In 2.1.0 (compared to 2.0.0), some of my media queries have broken in production. Something simple like below doesn’t trigger and I get no error. It looks like it’s just missing from the CSS.

 @media (min-width: ${breakpoints.sm}) {
    width: ${imageSizeLg};
    height: ${imageSizeLg};
  }

I’m using Gatsby, so production meaning SSR. This is the code integrating styled-components

What I find strange is that there are still media query statements in the generated CSS, even ones that are using the exact same query.

Any idea what might have caused this?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:21 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
kittencommented, Jun 22, 2017

I was able to reproduce this:

.djurMT {background-color: hotpink;width: 100px;height: 100px;border-radius: 3px;-webkit-flex: none;-ms-flex: none;flex: none;overflow: hidden;-webkit-transform: translate3d(0, 0, 0);-ms-transform: translate3d(0, 0, 0);transform: translate3d(0, 0, 0);}.djurMT@media (max-width: 62.5em) {.djurMT {
    width: ;
    height: ;
  ;}}

The relevant part being:

.djurMT@media

But I was only able to reproduce it using preprocess turned on in the babel plugin. So at least @Cinamonas reproduction is only a configuration mistake. Do note that preprocess is experimental and just a test leading up to v3, if you’ve purposefully turned it on 😉

I’m also sure that this triggers @0x80 bug, due to an edge-case in the preprocessing:

[["@media (min-width: ", breakpoints.sm, ") {"], [" {width: ", imageSizeLg, ";height: ", imageSizeLg, ";}}"]]

The preprocessing maps over the first level of arrays (the second being for the interpolations) and prepends each of them with the classname. When we use a media query, the first set of rules is not supposed to have a classname prepended to it (duh), so it fails. If you do want to continue using the experimental preprocessing, just put a dummy rule in front of the media query.

Btw before being done here, coming back to the first reproduction I mentioned:

const mediaQuery = (...args) => css`
  @media (max-width: 62.5em) {
    ${css(...args)}
  }
`;

mediaQuery`display: block`

This will never work with preprocessing, as we can’t preprocess dynamic input. The correct usage with it would be:

const mediaQuery = x => css`
  @media (max-width: 62.5em) {
    ${x}
  }
`;

mediaQuery(css`display: block`)

I hope this explains the issue 😉

1reaction
geelencommented, Jun 23, 2017

Ooh sounds like a result! I’ll leave this to @philpl hopefully we can cut a new release of the parser and we’ll be golden.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NextJs SSR not recognizing CSS Media queries for mobile
I just integrated Nextjs SSR into my React project. Before this, my css media queries for mobile-responsiveness were working perfectly.
Read more >
POC Next SSR media queries - React.js Examples
An experiment and evaluation of different strategies to handle media queries when doing SSR. Jan 01, 2022 6 min read.
Read more >
Blog Js Media Queries | janosh.dev
JS media queries with React hooks ... This post assumes you're using React (16.8 or later). One thing I like about styled-components is...
Read more >
Media queries in React for responsive design - Material UI - MUI
This is a CSS media query hook for React. It listens for matches to a CSS media query. It allows the rendering of...
Read more >
Keeping Server-Side Rendering Cool With React Hydration
What is Server-Side Rendering (SSR)?; What is Client-Side Rendering ... 1. Missing Data on Server-Side. Something helpful to keep in mind is ...
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