preface
- 【 Music Blog 】Online!
- Happy deployment to the server, and then full of joy to open the home page, the results found a little slow, this is very unpleasant
- Let’s deal with the vUE first screen rendering
Five aspects:
1. SPA single page application
- The first screen is slow to open because the first load requires the user to download the SPA framework and the application code before rendering the page.
- Is not conducive to SEO
- According to these shortcomings, we will lead to the next two topics for discussion, SEO and SSR, first talk about SEO.
SEO search engines
- Search engine optimization. SEO is the process of adjusting a website to improve its ranking for certain keywords in search engines by understanding how search engines work (how pages are crawled, indexed, and displayed in order of search results based on specific keywords, etc.).
- Vue SSR said.
If your application initially displays a loading chrysanthemum graph and then fetts the content via Ajax, the fetching tool does not wait for the asynchronism to complete before fetching the page content.
- Search engines don’t support Ajax for retrieving data, let alone SEO
- SEO is very important for some websites, such as Quora, StackOverflow, Zhihu and Douban, which are mainly about content output. How can SPA be used normally without affecting SEO? Lu xun yue:
- There is always technology to solve technical problems
- This is where SSR comes in
3. SSR server rendering
- In ordinary SPA, general framework and web page code is sent to the browser, and then in the browser to generate and manipulate the DOM (which is why the first SPA website under the same bandwidth than traditional back-end generate HTML sent to the browser to the main reason for the slower), but can also be packaged SPA applications on the server, HTML pages rendered on the server and sent to the browser are not yet interactive, so they need to be “blended” into an interactive application on the browser in conjunction with the SPA framework. Therefore, as long as the reasonable use of SSR technology, not only can solve the problem of slow first screen to a certain extent, but also can obtain better SEO.
- The advantages of SSR
-
Faster response time, no need to wait for all the JS to download, the browser can display a more complete page. I have experienced this deeply. My personal blog only used vue.js at the beginning without server rendering. Besides, the server was not in mainland China, so it was almost 4 or 5 seconds after the first address input to see the complete page, sometimes even longer.
-
Better SSR, we can directly render SEO key information into HTML in the background, and ensure that search engine crawlers can crawl to the key data.
- The disadvantage of SSR
- The renderer used in SSR naturally takes more CPU and memory resources than a server that simply needs to serve static files
- Common SSR framework
- The React of Next
- Vue. Js Nuxt
- Therefore, in order to do SEO well, it is recommended to use server SSR rendering (web pages are generated through server rendering and output to the client), and Nuxt (its essence is VUE SSR framework encapsulated by Node, so it runs on the server and is friendly to the first screen rendering).
4. Precompile the prerender-spa-plugin
- Nuxt will use its set of constraints, that is to say, your project will migrate to Nuxt, is it only SSR to solve SEO problems? You can also do pre-render
- Also, is the webpack precompilation, you can load the home page first, and then he slowly to load js files, so that there is no need to wait for the home page to load
- Server-side rendering to solve the problem, not only the HTML page to the browser, more importantly, after processing dynamic logic and JS code, will render the complete HTML to the browser, rendering process in the server.
- Pre-render, is the use of build tools in webpack to generate static HTML, directly to the browser, the rendering process is local.
- There are two types of pre-rendered plugins that cannot be used: lots of routing and dynamic content
- Let’s take a look at prerender-spa-plugin for pre-compilation
- Installation:
-
cnpm install prerender-spa-plugin --saveCopy the code
- The vue – cli3.0 way
- Increase in the vue – config. Js
-
const PrerenderSPAPlugin = require('prerender-spa-plugin'); const Renderer = PrerenderSPAPlugin.PuppeteerRenderer; const path = require('path'); module.exports = { configureWebpack: config => { if(process.env.NODE_ENV ! = ='production') return; return{ plugins: [new PrerenderSPAPlugin({// the path of the PrerenderSPAPlugin can be the same as that of the webpakc package. // The path of the PrerenderSPAPlugin can only be one level. StaticDir: path.join(__dirname,'dist'), // Corresponding to its own routing file. For example, if a has parameters, write /a/param1. routes: ['/'.'/product'.'/about'Renderer: new renderer ({inject: {foo:) renderer: new renderer ({inject: {foo:)'bar' }, headless: false, // Document.dispatchEvent (new Event()'render-event'), the event names of the two should correspond. renderAfterDocumentEvent:'render-event'})}),]}; }}Copy the code
- Add it to main.js
-
new Vue({ router, store, render: h => h(App), mounted () { document.dispatchEvent(new Event('render-event'))}}).$mount('#app')Copy the code
- Router. js set mode to “history”
Note: Routing mode on official documentation must be
history
. If you do not set history mode, you can run and generate files, and the contents of each index.html file will be the same. So you have to use
history
model - Run NPM run build to see if the generated dist directory has a folder for each route name. Then find a directory in index.html and open it with the IDE to see if the file has the content it should have. If so, the setup is successful
- After packaging, the picture is as follows:
-
-
-
It was nice to realize that I had been using Gzip to compress files
- The vue – cli2.0 way
- Modify the WebPack configuration, which is relatively easy to complete
-
const PrerenderSPAPlugin = require('prerender-spa-plugin') const Renderer = PrerenderSPAPlugin.PuppeteerRenderermodule.exports = { plugins: [ //... new PrerenderSPAPlugin({ staticDir: path.join(__dirname, 'dist'), outputDir: path.join(__dirname, 'prerendered'), indexPath: path.join(__dirname, 'dist'.'index.html'), routes: ['/'.'/about'.'/some/deep/nested/route'], postProcess(renderedRoute) { renderedRoute.route = renderedRoute.originalPath renderedRoute.html = renderedRoute.html.split(/>[\s]+</gmi).join('> <') if (renderedRoute.route.endsWith('.html')) { renderedRoute.outputPath = path.join(__dirname, 'dist', renderedRoute.route) } return renderedRoute }, minify: { collapseBooleanAttributes: true, collapseWhitespace: true, decodeEntities: true, keepClosingSlash: true, sortAttributes: true }, renderer: new Renderer({ inject: { foo: 'bar' }, maxConcurrentRoutes: 4 }) }) ] }Copy the code
- It is not too cool, the speed is much faster, basically 1 second can display the home page, interested partners quickly use in the project!!
The difference between the two
- Server SSR rendering to solve the problem, not only the HTML page to the browser, more importantly, after processing dynamic logic and JS code, will render the complete HTML to the browser, rendering process in the server.
- Pre-render, is the use of build tools in webpack to generate static HTML, directly to the browser, the rendering process is local. Bringing the server side up to build time to compile HTML reduces the pressure on the server side
- Commonality: loading speed is the same; Less invasive;
- Different: server render on server; It is suitable for complex and large websites with frequent interaction with the server, such as e-commerce pre-rendering on the client; This works for simple static pages
- Very strange point is: open F12, found the refresh is very slow, turn off F12, refresh is very fast, I do not know what is serious, know students share in the comment section below!
The final summary
To seo, use SSR server rendering, that is, the server generates static pages to the client, please combine Nuxt interaction; Or instead of rendering on the server, use webpack’s prerender-spa-plugin to precompile static HTML directly to the browser, rendering locally. Optimize home page rendering or Vue sharding using Webpack’s Code Splitting load compression on demand
The original address
Juejin. Cn/post / 684490…
reference
Vue – cli3.0 use prerender – spa – plugin plug-in pre-rendered: blog.csdn.net/lv5751394/a…
Simple pre-render vuE-CLI2.0 using prerender-spa-plugin: juejin.cn/post/684490…