angular-fontawesome icon indicating copy to clipboard operation
angular-fontawesome copied to clipboard

Icon size server side rendering universal

Open dottodot opened this issue 6 years ago • 11 comments

When using universal for ssr the page loads the icons full size until the browser takes over, the reason for this is the inline styles are missing from the the universal app.

I'm not entirely sure why this is but it should be possible to include them as other modules such as material do.

dottodot avatar May 22 '18 19:05 dottodot

It has the same root cause as https://github.com/FortAwesome/angular-fontawesome/issues/18 - FA inserts styles using custom JS, so Angular server side rendering is not aware about them. Until it is fixed you can include style.css from @fortawesome/fontawesome-svg-core package in your global styles to workaround the issue.

devoto13 avatar May 23 '18 00:05 devoto13

I've tried copying all styles from @fortawesome/fontawesome-svg-core in a .scss file that is served inline in my main component, like this:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.scss']
})
export class AppComponent {

and in app.scss: @import 'font-awesome-styles';

All styles are pre-rendered and served inline, but the initial icons are stil being rendered in full size...

Did anyone could solve this problem?

Thanks!

FelipeMicali avatar Jul 18 '18 18:07 FelipeMicali

@FelipeMicali When you include styles this way they are subject to component's view encapsulation and therefore will work only for elements defined in the template of AppComponent. Please include styles globally as described in this story.

devoto13 avatar Jul 18 '18 20:07 devoto13

@devoto13 yes, I've noticed the component's view encapsulation behavior and to solve it I've included all those styles inline in my maind Angular index.html. But I'm going to take a look at the link you provided and implement this method to make my application more functional.

Thanks!

FelipeMicali avatar Jul 26 '18 01:07 FelipeMicali

I've included the styles in angular.json build.options.styles but I'm still getting massive icons when using Angular Universal until the server => client transfer occurs.

 "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/browser",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": [
              "node_modules/@fortawesome/fontawesome-svg-core/styles.css",
              "src/styles.scss"
            ],
          },

in app.module.ts I've tried turning config.autoAddCss to true or false without any result.

import { config } from '@fortawesome/fontawesome-svg-core';
config.autoAddCss = false;

santekotturi avatar Oct 20 '18 00:10 santekotturi

@skotturi This is strange. Do you have a repro I can take a look at?

Also as a diagnostic step: can you open Network tab in your browser's Dev Tools and make sure that the server rendered page includes the necessary styles by searching for .fa-layers in the returned content for example?

devoto13 avatar Oct 20 '18 13:10 devoto13

thx @devoto13 adding the style.css as workaround seems to work for my website

        "styles": [
          "node_modules/@fortawesome/fontawesome-svg-core/styles.css",
          "src/styles.scss"
        ]

peterpeterparker avatar Oct 24 '18 07:10 peterpeterparker

does anyone have a solution for the problem other than the workaround?

helgetan avatar Oct 23 '19 12:10 helgetan

Adding the key-value => "extractCss": true in build.options will fix this issue.

Mohendran avatar Feb 10 '20 18:02 Mohendran

Instead of importing the whole fa-icons into the global css, we added just the following rules to the global (s)css.

fa-icon svg {
  display: inline-block;
  font-size: inherit;
  height: 1em;
}
fa-icon .fa-2x{
  height: 2em;
}

This fixes the basic sizing issues for our use case. The rules will be overwritten once the complete style is loaded from js, and shouldn't have too much impact. But your mileage might vary.

Beware, this is a workaround and might stop working in the future.

MarcoGlauser avatar Jul 17 '20 13:07 MarcoGlauser

Update to MarcoGlauser's workaround, I had to do this:

.svg-inline--fa {
  vertical-align: -0.125em;
}

fa-icon svg {
  display: inline-block;
  font-size: inherit;
  height: 1em;
}

fa-icon .fa-2x{
  font-size: 2em;
}

abuMiguel avatar Jul 10 '22 02:07 abuMiguel