简体   繁体   English

防止单文件组件的无样式内容的 flash

[英]Prevent flash of unstyled content for Single-File Components

I want to replace my html navigation我想更换我的 html 导航

<div id="header-wrapper">
  <div id="header-logo-title">
    <nav>
      <ul id='mainNav'>
         <li>Home</li>
      </ul>
    </nav>
  </div>
</div>

with a VUE component like this:使用这样的 VUE 组件:

<navigation>
    <nav-item>Home</nav-item>
</navigation>

I read here that one should extract the css, so that it won't be injected by JS which could cause flash of unstyled content :在这里读到应该提取 css,这样它就不会被 JS 注入,这可能导致flash 的无样式内容

When using Single-File Components, the CSS inside components are injected dynamically as tags via JavaScript.使用单文件组件时,组件内部的 CSS 通过 JavaScript 作为标签动态注入。 This has a small runtime cost, and if you are using server-side rendering it will cause a “flash of unstyled content”.这具有很小的运行时成本,如果您使用服务器端渲染,它将导致“无样式内容的闪烁”。 Extracting the CSS across all components into the same file will avoid these issues, and also result in better CSS minification and caching.将所有组件中的 CSS 提取到同一个文件中将避免这些问题,并且还会导致更好的 CSS 缩小和缓存。

So, I extraced my component CSS into a file, but I still experience flash of unstyled content on load.因此,我将我的组件 CSS 提取到一个文件中,但我仍然会在加载时遇到无样式内容的 flash

Does the flash of unstyled content happen when <navigation>..</navigaton> is replaced by当 < navigation <navigation>..</navigaton>替换为

<div id="header-wrapper">
  <div id="header-logo-title">
    <nav>
      <ul id='mainNav'>
         ....
      </ul>
    </nav>
  </div>
</div>

through VUE?通过 VUE? If so, how can one prevent this?如果是这样,如何防止这种情况发生?

One can avoid this by using server-side rendering of VUE .可以通过使用VUE 的服务器端渲染来避免这种情况。 This will result in sending the client the rendered HTML这将导致向客户端发送呈现的 HTML

<div id="header-wrapper">
  <div id="header-logo-title">
    <nav>
      <ul id='mainNav'>
         <li>Home</li>
      </ul>
    </nav>
  </div>
</div>

instead of代替

<navigation>
    <nav-item>Home</nav-item>
</navigation>

before JavaScript runs.在 JavaScript 运行之前。

An important note from this article : 这篇文章的一个重要说明:

With SSR, your app does not load or run any faster, indeed it may run slightly slower as the server has the added task of rendering the app.使用 SSR,您的应用程序不会加载或运行得更快,实际上它可能会运行得稍微慢一些,因为服务器增加了渲染应用程序的任务。 But the page content is shown sooner therefore the user can see engage with page sooner.但是页面内容显示得更快,因此用户可以更快地看到与页面的互动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM