Angular通用样式:服务器到客户端转换时出现FOUC问题

4
我有一个问题,我的通用应用程序在服务端应用程序被交换为客户端应用程序时,页面中的路由组件没有样式。这导致页面加载正确,然后短暂地显示未经过样式处理的内容 (FOUC),看起来很糟糕,然后才能自行解决。
我的网站头部和底部组件的样式一直很好,但是加载在<router-outlet>元素内部的组件没有正确的样式。
我使用Preboot管理服务器端到客户端的转换,并且没有做任何超出标准配置的事情。我尝试使用@ngx-universal/state-transfer@ngx-cache库进行实验,但我认为它们不是我需要的。
我使用延迟加载路由,但是我已经尝试去掉这些路由,错误仍然存在。我也尝试在我的路由配置中设置{initialNavigation: 'enabled'}
我使用webpack构建我的服务器端应用程序,使用Angular CLI构建客户端应用程序,大多基于此项目,并且我使用AOT编译器。非常感谢您提供的任何想法!

你是怎么解决这个问题的? - Michael
你修好了吗? - Rakesh Chand
1个回答

1
因此,我最终采用的解决方案是创建一个StyleStateTransferService,它利用@ngx-universal/state-transfer库。

在服务器端应用程序中,我从head中获取angular样式并将其添加到状态传输服务中。在客户端应用程序中,我从传输的状态中获取样式并将其添加到head中。一旦Angular完成引导,我就会遍历并删除我添加的样式,以免有重复的样式存在。

我不知道这是否是最好的解决方案,也许有人有更好的解决方案,我最初希望preboot中缺少一些配置,但似乎不是这样。

服务:

@Injectable()
export class StyleStateTransferService {
  document: any;
  window: Window;
  renderer: Renderer2;
  ngStyleId = 'ng_styles';

  constructor(private stateTransferService: StateTransferService,
              @Inject(DOCUMENT) document: any,
              private winRef: WindowRef,
              private rendererFactory: RendererFactory2) {
    this.window = winRef.nativeWindow;
    this.document = document;
    this.renderer = rendererFactory.createRenderer(this.document, null);
  }

  addStylesToState() {
    const styles: string[] = this.document.head.children
      // elements have a weird structure on the server
      // this filters to style tags with the ng-transition attribute that have content
      .filter(el =>
        el.name === 'style' && el.attribs['ng-transition'] && el.firstChild && el.firstChild.data)
      // extract the css content of the style tags
      .map(el => el.firstChild.data.replace(/\n/g, ' '));

    this.stateTransferService.set(this.ngStyleId, styles);
    this.stateTransferService.inject();
  }

  injectStylesFromState() {
    const styles = _.get(this.window, `${DEFAULT_STATE_ID}.${this.ngStyleId}`, []);
    styles.forEach(content => {
      const styleEl = this.renderer.createElement('style');
      // set this attribute so we can remove them later
      this.renderer.setAttribute(styleEl, 'ng-state-transfer', null);
      this.renderer.setProperty(styleEl, 'innerHTML', content);
      this.renderer.appendChild(this.document.head, styleEl);
    });
  }

  cleanupInjectedStyles() {
    Array.from(<HTMLElement[]>this.document.head.children)
      .filter(htmlEl => htmlEl.tagName === 'STYLE' && htmlEl.hasAttribute('ng-state-transfer'))
      .forEach(styleEl => this.renderer.removeChild(this.document.head, styleEl));
}

在服务器和浏览器模块中使用它(清理操作发生在应用组件中,但似乎不值得展示):

export class AppServerModule {
  constructor(private appRef: ApplicationRef,
              private styleStateTransferService: StyleStateTransferService) {
    // waits until the app has fully initialised before adding the styles to the state
    this.appRef.isStable
      .filter(isStable => isStable)
      .first()
      .subscribe(() => {
        this.styleStateTransferService.addStylesToState();
      });
  }
}

export class AppBrowserModule {
  constructor(private styleStateTransferService: StyleStateTransferService) {
    this.styleStateTransferService.injectStylesFromState();
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接