-
Notifications
You must be signed in to change notification settings - Fork 772
Universal pre-rendered styles are not correct #373
Comments
This is a known issue. Issue:Current SSR must handcraft the flexbox CSS for the initial rendered page. Reason:The flex-layout directives use the browser viewport size to determine the active breakpoint and generate the inline styles. Current SSR does not have access to remote viewport size. @vikerman - We should discuss possbile solutions to this. |
Isn't it possible the render classic/static responsive CSS ? I mean something like this, according to the template above : @media all {
display: flex;
flex-wrap: wrap;
}
@media all and (max-width: 599px) {
flex-direction: column;
flex-wrap: nowrap;
} Why is the real viewport size needed ? The browser knows how to take care of that by itself. |
@cyrilletuzi - I see two (2) issues (based on snapshots of your sample):
|
Could it be related to how default values are managed in Flex Layout ? Because when I first tried this package I was quite surprised. First, I tried to do this : as the default CSS behavior (vertical display) was fine for mobile, I tried this : <div fxLayout.gt-xs="row"></div> I expected only desktop screens to be impacted, but the result of this code is to generate It's the same for all other flex layout properties (it's why I need to do |
It is possible to predict screen information by using user-agent. However, it requires another implementation in platform-server, because it just gives "Fake user agent" in platform-server. |
@ardatan - matchMedia + getComputedStyle on SSR will not be using the viewport size of the remote client so the generated, injected inline styles will mostly likely be wrong for mobile. |
@ThomasBurleson Sorry, you are right. We still need some information about screen sizes from somewhere like user-agent. Unfortunately, it needs a change in platform-server, does it? |
I think the platform server needs to seed the remote client with micro-script to report the viewport size (and perhaps other information). |
Anything new on this thread? |
Since #353 has been merged, I assume that responsive utilities (media-query) should now work with server side rendering. But below doesn't work with latest version of flex-layout:
|
Happy to see |
Issue (1) identified by @ThomasBurleson here may be fixed by #466. The other issue seems to require a more creative solution |
@cyrilletuzi #466 has been merged, can you check to see if your desktop view is the same both Universal and browser-side? That would greatly narrow the scope of this issue (ie we could change the name to Also, as a side note, |
Default styles (meaning the ones corresponding to a directive without To be clear, here is the updated example : <div fxLayout="row wrap" fxLayout.xs="column">
<div class="test" fxFlex="calc(100% / 3)" fxFlex.xs="100%" *ngFor="let item of list" [innerHTML]="item"></div>
</div> So as this example uses desktop values as default, SSR in desktop is OK, meaning styles are :
But on mobile, SSR is not OK as styles are the same as in desktop SSR. By the way, I spotted another issue : prefixed styles (with |
@cyrilletuzi Can you open a separate issue for the vendor prefixes? (though as far as I know this is intentional -- vendor prefixes should be preferred if available, no?) As for the media-query issue, I see two possible solutions:
The two options could be selected by user preference at bootstrap as follows: In my opinion, TB's comment here is not viable, since (a) we don't know if the user supports javascript at all, and (b) it's a waste of network resources and loading time, which is what SSR, among other things, is trying to minimize. @vikerman Do you see an alternative solution? For option 2, we could finally implement the Also cc @gdi2290 @MarkPieszak @alxhub @Toxicable Thoughts? |
I don't know the exact reasons why the library is designed to be pure js, (unlike the similar flex-layout library in [email protected], which uses css media queries). But, it seems using css media queries is simpler approach and more importantly, it doesn't introduce any issue for SSR, as there is no need to know media size on the server. |
@alirezamirian The Flex Layout library is about a lot more than setting css styles based on preset breakpoints. It also allows for custom breakpoint creation, MediaQuery detection, interpolated value processing, etc. Almost all of these require a pure-JS approach, or at least the viewport size at runtime. If you’re not satisfied with this, you are more than welcome to pull the CSS style sheet from AngularJS Material and insert it in your application instead. |
These things are awesome and quite useful. However, a pure js approach has inherent issues with SSR, as the media size are not available in the backend and at best it can be estimated via client agent http header, which by the way, is not so reliable. because lots of platforms provide ability to change browser size, and if so, it's impossible to know the actual media size in backend via client agent headers, and this will prevent smooth transition from server-side rendered content. I think a css based approach can be used for directives (like AngularJS Material) while at the same time, other features like MediaQuery detection can be provided in pure JS, with the reasonable caveat of not being able to use them in server platform. By the way, the css based solution can be in form of scss mixins and functions which enables parameterizing breakpoints. I'm thinking of creating a module that does the exact same thing that AngularJS Material layout directive does (which is basically adding required classes), with the exact same syntax of |
@CaerusKaru thanks for trying to kick-start a conversation about how best to solve this. It doesn't seem like this issue has much engagement though from other contributors. Is there any sense on what priority this issue has internally? My project is getting close to complete, and this is one of the last roadblocks. I REALLY don't want to pull out flex-layout as it's been super helpful. Thanks in advance! |
@rkajbaf Unfortunately I have no idea what the real priority is. @ThomasBurleson is the main contributor and has a lot of other responsibilities in addition to maintaining this project. I remember reading somewhere that Flex Layout was going to be integrated with the Angular CDK at some point, so maybe they're prioritizing that right now. Either way, this is an issue that I'm personally looking into, possibly blending parts of the ideas @alirezamirian suggested with some of my own, but I have no ETA for you. |
Thanks @CaerusKaru, let me know if there is anything I can do to help! |
I created a css-only version of BTW, using this css based |
* Add `StyleService` class to manage application and retrieval of styles from elements in a platform-agnostic manner * Add virtual stylesheet to store server styles, which applies default styles when breakpoint overrides are not present * While not in the browser (ssr), intercept all style calls and reroute them to the virtual stylesheet. * For server-side rendering, add a new type of MediaQueryList similar to the MockMediaQueryList to support manual activation/deactivation of breakpoints * Add jasmine testing mode for SSR * Add FlexLayoutServerModule to invoke SSR styling Fixes #373. > See [Design Doc](https://docs.google.com/document/d/1fg04ihw42dJJHGd6fugdiBe39iJot8aErhiE7CjwfmQ/edit#)
* Add `StyleService` class to manage application and retrieval of styles from elements in a platform-agnostic manner * Add virtual stylesheet to store server styles, which applies default styles when breakpoint overrides are not present * While not in the browser (ssr), intercept all style calls and reroute them to the virtual stylesheet. * For server-side rendering, add a new type of MediaQueryList similar to the MockMediaQueryList to support manual activation/deactivation of breakpoints * Add jasmine testing mode for SSR * Add FlexLayoutServerModule to invoke SSR styling Fixes #373. > See [Design Doc](https://docs.google.com/document/d/1fg04ihw42dJJHGd6fugdiBe39iJot8aErhiE7CjwfmQ/edit#)
* Add `StyleService` class to manage application and retrieval of styles from elements in a platform-agnostic manner * Add virtual stylesheet to store server styles, which applies default styles when breakpoint overrides are not present * While not in the browser (ssr), intercept all style calls and reroute them to the virtual stylesheet. * For server-side rendering, add a new type of MediaQueryList similar to the MockMediaQueryList to support manual activation/deactivation of breakpoints * Add jasmine testing mode for SSR * Add FlexLayoutServerModule to invoke SSR styling Fixes #373. > See [Design Doc](https://docs.google.com/document/d/1fg04ihw42dJJHGd6fugdiBe39iJot8aErhiE7CjwfmQ/edit#)
* Add `StyleService` class to manage application and retrieval of styles from elements in a platform-agnostic manner * Add virtual stylesheet to store server styles, which applies default styles when breakpoint overrides are not present * While not in the browser (ssr), intercept all style calls and reroute them to the virtual stylesheet. * For server-side rendering, add a new type of MediaQueryList similar to the MockMediaQueryList to support manual activation/deactivation of breakpoints * Add jasmine testing mode for SSR * Add FlexLayoutServerModule to invoke SSR styling * Remove unnecessary Renderer references and replace them with DOM APIs * Add whitespace debugging mode for server styles Fixes #373. > See [Design Doc](https://docs.google.com/document/d/1fg04ihw42dJJHGd6fugdiBe39iJot8aErhiE7CjwfmQ/edit#)
* Add `StyleService` class to manage application and retrieval of styles from elements in a platform-agnostic manner * Add virtual stylesheet to store server styles, which applies default styles when breakpoint overrides are not present * While not in the browser (ssr), intercept all style calls and reroute them to the virtual stylesheet. * For server-side rendering, add a new type of MediaQueryList similar to the MockMediaQueryList to support manual activation/deactivation of breakpoints * Add jasmine testing mode for SSR * Add FlexLayoutServerModule to invoke SSR styling * Remove unnecessary Renderer references and replace them with DOM APIs * Add whitespace debugging mode for server styles Fixes #373. > See [Design Doc](https://docs.google.com/document/d/1fg04ihw42dJJHGd6fugdiBe39iJot8aErhiE7CjwfmQ/edit#)
* Add `StyleService` class to manage application and retrieval of styles from elements in a platform-agnostic manner * Add virtual stylesheet to store server styles, which applies default styles when breakpoint overrides are not present * While not in the browser (ssr), intercept all style calls and reroute them to the virtual stylesheet. * For server-side rendering, add a new type of MediaQueryList similar to the MockMediaQueryList to support manual activation/deactivation of breakpoints * Add jasmine testing mode for SSR * Add FlexLayoutServerModule to invoke SSR styling * Remove unnecessary Renderer references and replace them with DOM APIs * Add whitespace debugging mode for server styles Fixes #373. > See [Design Doc](https://docs.google.com/document/d/1fg04ihw42dJJHGd6fugdiBe39iJot8aErhiE7CjwfmQ/edit#)
* Add `StyleService` class to manage application and retrieval of styles from elements in a platform-agnostic manner * Add virtual stylesheet to store server styles, which applies default styles when breakpoint overrides are not present * While not in the browser (ssr), intercept all style calls and reroute them to the virtual stylesheet. * For server-side rendering, add a new type of MediaQueryList similar to the MockMediaQueryList to support manual activation/deactivation of breakpoints * Add jasmine testing mode for SSR * Add FlexLayoutServerModule to invoke SSR styling * Remove unnecessary Renderer references and replace them with DOM APIs * Add whitespace debugging mode for server styles Fixes #373. > See [Design Doc](https://docs.google.com/document/d/1fg04ihw42dJJHGd6fugdiBe39iJot8aErhiE7CjwfmQ/edit#)
* Add `StyleService` class to manage application and retrieval of styles from elements in a platform-agnostic manner * Add virtual stylesheet to store server styles, which applies default styles when breakpoint overrides are not present * While not in the browser (ssr), intercept all style calls and reroute them to the virtual stylesheet. * For server-side rendering, add a new type of MediaQueryList similar to the MockMediaQueryList to support manual activation/deactivation of breakpoints * Add jasmine testing mode for SSR * Add FlexLayoutServerModule to invoke SSR styling * Remove unnecessary Renderer references and replace them with DOM APIs * Add whitespace debugging mode for server styles Fixes #373. > See [Design Doc](https://docs.google.com/document/d/1fg04ihw42dJJHGd6fugdiBe39iJot8aErhiE7CjwfmQ/edit#)
* Add `StyleService` class to manage application and retrieval of styles from elements in a platform-agnostic manner * Add virtual stylesheet to store server styles, which applies default styles when breakpoint overrides are not present * While not in the browser (ssr), intercept all style calls and reroute them to the virtual stylesheet. * For server-side rendering, add a new type of MediaQueryList similar to the MockMediaQueryList to support manual activation/deactivation of breakpoints * Add jasmine testing mode for SSR * Add FlexLayoutServerModule to invoke SSR styling * Remove unnecessary Renderer references and replace them with DOM APIs * Add whitespace debugging mode for server styles Fixes #373. > See [Design Doc](https://docs.google.com/document/d/1fg04ihw42dJJHGd6fugdiBe39iJot8aErhiE7CjwfmQ/edit#)
* Add `StyleService` class to manage application and retrieval of styles from elements in a platform-agnostic manner * Add virtual stylesheet to store server styles, which applies default styles when breakpoint overrides are not present * While not in the browser (ssr), intercept all style calls and reroute them to the virtual stylesheet. * For server-side rendering, add a new type of MediaQueryList similar to the MockMediaQueryList to support manual activation/deactivation of breakpoints * Add jasmine testing mode for SSR * Add FlexLayoutServerModule to invoke SSR styling * Remove unnecessary Renderer references and replace them with DOM APIs * Add whitespace debugging mode for server styles Fixes #373. > See [Design Doc](https://docs.google.com/document/d/1fg04ihw42dJJHGd6fugdiBe39iJot8aErhiE7CjwfmQ/edit#)
Hi there. These days I faced the same problem and tried to come up with a solution using stylesheets instead. Here you can see the results: https://github.com/greg-md/ng-flex-css-layout I was trying to use the same logic of generating styles as it is right now in flex-layout sources. So, you can give it a try. I just saw that there is already a work in progress into that direction, which is good. |
* Add `StyleService` class to manage application and retrieval of styles from elements in a platform-agnostic manner * Add virtual stylesheet to store server styles, which applies default styles when breakpoint overrides are not present * While not in the browser (ssr), intercept all style calls and reroute them to the virtual stylesheet. * For server-side rendering, add a new type of MediaQueryList similar to the MockMediaQueryList to support manual activation/deactivation of breakpoints * Add jasmine testing mode for SSR * Add FlexLayoutServerModule to invoke SSR styling * Remove unnecessary Renderer references and replace them with DOM APIs * Add whitespace debugging mode for server styles Fixes #373. > See [Design Doc](https://docs.google.com/document/d/1fg04ihw42dJJHGd6fugdiBe39iJot8aErhiE7CjwfmQ/edit#)
* Add `StyleService` class to manage application and retrieval of styles from elements in a platform-agnostic manner * Add virtual stylesheet to store server styles, which applies default styles when breakpoint overrides are not present * While not in the browser (ssr), intercept all style calls and reroute them to the virtual stylesheet. * For server-side rendering, add a new type of MediaQueryList similar to the MockMediaQueryList to support manual activation/deactivation of breakpoints * Add jasmine testing mode for SSR * Add FlexLayoutServerModule to invoke SSR styling * Remove unnecessary Renderer references and replace them with DOM APIs * Add whitespace debugging mode for server styles Fixes #373. > See [Design Doc](https://docs.google.com/document/d/1fg04ihw42dJJHGd6fugdiBe39iJot8aErhiE7CjwfmQ/edit#)
The guide for using SSR (for those of you using it in the nightly builds) can be found here |
@CaerusKaru thanks for the awesome work. I tired instructions mentioned in SSR guide but receiving below error:
I have |
@naveedahmed1 please link a repo so I can take a look |
Also, I just realized Option 3 is likely broken for now. I’ll submit a fix for that ASAP before the next release |
Thanks for the quick reply. Sharing a repo would be little difficult since its based on .net core angular cli template. Let me share the setup. I have As suggested in guide Do you see any issue with this setup? |
Where does |
In all modules where I am using any of the Material component, so used in both sever and client side modules. |
Please open a separate issue for this, and I don't understand your reasoning for not being able to provide a sample repo. Without one, we cannot adequately investigate the issue since this works in our internal testing and example apps. |
Some feedback, I was finally able to successfully run
|
@naveedahmed1 Please provide an example repo to document your issues with Option 1. Options 2 and 3 will be fixed by #624, and I've updated the SSR guide to reflect the import ordering requirement. This may be patched in a future release, but for now it's a hard requirement. |
Here are the steps to reproduce the issue:
Visit |
@CaerusKaru - Could you please tell me do you have any updates about issue related to SSR and lazy modules? I mean issue from @naveedahmed1 with reproduction repo above? |
@atmman9001 Thanks for opening a separate issue for that. I have a good idea of why it's happening and should have a fix soon (it'll be fixed regardless in v6 by #642) for 5.x. I'm going to lock this issue since this has been resolved for the most part. Any other issues with SSR should be filed separately. |
With last nightly build, there are no more blocking errors with Flex Layout in Universal, but the pre-rendered styles are not correct.
Repro :
git clone https://github.com/cyrilletuzi/nguniversal-expressengine-httpinterceptor.git
git checkout -b flexlayoutissue
npm install
npm start
The only flex relative code is in
src/app/app.component
(the other files are just Universal boilerplate, andinnerHTML
is just to quickly test different heights) :Javascript-Enabled:
Mobile Viewport
Desktop Viewport
Javscript-Disabled (prerender version only):
Mobile Viewport
Desktop Viewport
The text was updated successfully, but these errors were encountered: