Description
Version
2.6.14
Reproduction link
https://github.com/tcstory/vue-ssr-issue
Steps to reproduce
- npm run build
- npm run start
- visit http://localhost:3000/page1 with javascript enabled and disabled respectively
What is expected?
with javascript disabled, the corresponding css should be inject to html
What is actually happening?
the corresponding css is missed
What problems does this cause?
Let's consider the following situations
- user visit the page
- after browser finish loading the page, javascript will be excuted and the missed css file will be inject to the page
- the page will update after the css file is loaded
In this process, the user can observe that the page is "jittering" because some css styles are missing when the page is loaded at the beginning, and the css styles are not loaded back until after the js is executed.
what causes this bug?
the following are my own analysis
During server-side rendering, vue generates a string of hash inside the component. By using this string of hash, vue knows what components the page depends on during rendering, and then injects the static resources of these components into the html.
This process usually works without any problems, unless you run into webpack's splitChunks.
Let's assume we have two pages, page1 and page2, and a btn1.vue file that they both depend on
- page1.vue depends on btn1.vue
- page2.vue depends on btn1.vue
- btn1.vue depends on btn1.css
after building, you can get the following files
- page1.js
- page2.js
- page1~page2.js (this file contains the component's js and css code)
However, if you have an additional page3 page, and this page also depends on btn1.css, in this case, after building, you will get the following files
- page1.js
- page2.js
- page3.js
- page1~page2.js (this file only contains the js code of btn1.vue)
- page1~page2 ~ page3.js (this file only contains the code of btn.css)
At this time, when you visit the page1 page, ssr can only inject "page1.js" and "page1 ~ page2.js" files for you, but it doesn't know that you also need page1~page2 ~ page3.js files.
my english is poor, here is a chinese version about the issue: tcstory/blog#13