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.

Breakpoint fallback broken for CSS @media queries with Universal server rendering

See original GitHub issue

Bug Report

I’m trying to migrate to Universal SSR. Flex Layout @media query breakpoints seem to be broken. Is there a workaround for now?

What is the expected behavior?

Universal SSR generates a <style> block that should mimic the browser-side JS runtime behavior, but it’s consistently not.

Example A: xs shows 1, 2, 3 in a column, sm shows 1,2 on one line and 3 on the next, and md/lg/xl shows 1, 2, 3 in a row.

<div fxLayout="row wrap" fxLayoutAlign="center start">
  <div fxFlex="100%" fxFlex.gt-xs="50%" fxFlex.gt-sm="33.33%"> 1 </div>
  <div fxFlex="100%" fxFlex.gt-xs="50%" fxFlex.gt-sm="33.33%"> 2 </div>
  <div fxFlex="100%" fxFlex.gt-xs="50%" fxFlex.gt-sm="33.33%"> 3 </div>
</div>

Example B: xs/sm shows 1, 2, 3 in a column, md/lg shows 1,2 on one line and 3 on the next, and xl shows 1, 2, 3 in a row.

<div fxLayout="row wrap" fxLayoutAlign="center start">
  <div fxFlex="100%" fxFlex.gt-sm="50%" fxFlex.gt-lg="33.33%"> 1 </div>
  <div fxFlex="100%" fxFlex.gt-sm="50%" fxFlex.gt-lg="33.33%"> 2 </div>
  <div fxFlex="100%" fxFlex.gt-sm="50%" fxFlex.gt-lg="33.33%"> 3 </div>
</div>

What is the current behavior?

Breakpoints aren’t properly calculated, showing the wrong layout on initial page render, which then jarringly corrects itself once JS has loaded.

Example A: xs shows 1, 2, 3 in a column, sm/md/lg/xl shows in a row.

Example B: all screen sizes shows 1, 2, 3 in a row.

It seems the @media queries are not generated correctly. Here’s the CSS that Example A generates for one of the divs. The gt-xs generates a media query at the top ((min-width: 600px)) that is then overridden by the sm below it ((min-width: 600px) and (max-width: 959px)):

<style class="flex-layout-ssr">
@media all { .flex-layout-355 { max-width:100%; } }
@media screen and (min-width: 600px) { .flex-layout-355 { max-width:50%; } }
@media screen and (min-width: 960px) { .flex-layout-355 { max-width:33.33%; } }
@media screen and (min-width: 1280px) { .flex-layout-355 { max-width:33.33%; } }
@media screen and (min-width: 1920px) { .flex-layout-355 { max-width:33.33%; } }
@media screen and (min-width: 1920px) and (max-width: 5000px) { .flex-layout-355 { max-width:33.33%; } }
@media screen and (max-width: 1919px) { .flex-layout-355 { max-width:33.33%; } }
@media screen and (min-width: 1280px) and (max-width: 1919px) { .flex-layout-355 { max-width:33.33%; } }
@media screen and (max-width: 1279px) { .flex-layout-355 { max-width:33.33%; } }
@media screen and (min-width: 960px) and (max-width: 1279px) { .flex-layout-355 { max-width:33.33%; } }
@media screen and (max-width: 959px) { .flex-layout-355 { max-width:33.33%; } }
@media screen and (min-width: 600px) and (max-width: 959px) { .flex-layout-355 { max-width:33.33%; } }
@media screen and (max-width: 599px) { .flex-layout-355 { max-width:100%; } }
@media screen and (min-width: 0px) and (max-width: 599px) { .flex-layout-355 { max-width:100%; } }
</style>

What are the steps to reproduce?

Serve app via Angular Universal (FlexLayoutServerModule in app.server.module.ts). Add this to a page:

<div fxLayout="row wrap" fxLayoutAlign="center start">
  <div fxFlex="100%" fxFlex.gt-xs="50%" fxFlex.gt-sm="33.33%"> 1 </div>
  <div fxFlex="100%" fxFlex.gt-xs="50%" fxFlex.gt-sm="33.33%"> 2 </div>
  <div fxFlex="100%" fxFlex.gt-xs="50%" fxFlex.gt-sm="33.33%"> 3 </div>
</div>

Disable JS in browser. Load page.

What is the use-case or motivation for changing an existing behavior?

To render page accurately. Without this, Flex Layout with Angular Universal displays layouts improperly.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

FlexLayout 7.0.0-beta.24. Angular 7.2.15. Material 7.3.7. macOS 10.14.3. TypeScript 3.2.4. Firefox 66 and Chrome 74.

Is there anything else we should know?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:25 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
intellixcommented, Nov 18, 2019

mine’s generating 100kb of styles. It starts at flex-layout-551 (for some reason) and they’re each repeated 10x.

1reaction
nicholas-daviscommented, Mar 3, 2020

Will the fix (https://github.com/angular/flex-layout/issues/1065#issuecomment-498759579) be applied to the new releases anytime soon? Or is there a quick fix temporary?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Breakpoint fallback broken for CSS @media ... - Bountysource
I'm trying to migrate to Universal SSR. Flex Layout @media query breakpoints seem to be broken. Is there a workaround for now?
Read more >
5 Reasons Why Your CSS Media Queries Are NOT ... - YouTube
... are not working ? CSS Media queries are an important part of Responsive web design but sometimes they don't work as we...
Read more >
How To Fix Cumulative Layout Shift (CLS) Issues
I advise using the Intrinsic Size (which is the actual size of the image source) and the browser will then scale these down...
Read more >
Angular Universal Pre-render : Not handling CSS media ...
The pre-rendered build works fine when I serve the dist folder locally and check in localhost. But when I host it on a...
Read more >
@angular/flex-layout | Yarn - Package Manager
Please see https://blog.angular.io/modern-css-in-angular-layouts-4a259dca9127. Angular Flex-Layout ======= angular, flex-layout, flexbox css, media query ...
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