优化Vue应用性能(减小体积)

huangapple go评论50阅读模式
英文:

Increase performance(decrease size) of Vue app

问题

以下是您要翻译的内容:

I was faced with the performance of the web application, I didn't know how to optimize it. My single-page websites with good internet load in 2-3 seconds, if I put in 2G and 3G, it loads in about +20 seconds. I have installed several libraries locally Quasar, and WaveUI (Before I also used NaiveUI, it took about 1M bytes, I decided to remove it. How can I increase the loading and size of the website?

What I have done and tried to do:

  • use CDN, but it not working.

  • I use 'defineAsyncComponent for "Lazy loading of components".

  • Converted from CSS/SCSS to sass.

I even don't use any heavy scripts, I only fetch and display the data from js file as a list, that's all.

my App.vue

<template>
  <w-app class="main-app">
    <q-layout>
      <q-header reveal className="sticky-top">
        <Navigation/>
      </q-header>
      <MainContent/>
    </q-layout>
  </w-app>
</template>

<script>
import {defineAsyncComponent} from 'vue'
const MainContent = defineAsyncComponent(() =>
    import('@/components/MainContent.vue')
)
const Navigation = defineAsyncComponent(() =>
    import('@/components/NavigationComponent.vue')
)
export default {
  components: {MainContent, Navigation}
}

</script>

MainContent.vue

<template>
  <w-flex justify-center class="pa3">
    <SideElements></SideElements>
    <div class="grow fill-width text-center">
      <section id="first">
        <FirstSection></FirstSection>
      </section>
      <section>
        <AboutMe id="about"></AboutMe>
      </section>
      <section>
        <ExperienceSection id="experience"/>
      </section>
      <section>
        <FeaturedProject id="projects">

        </FeaturedProject>
      </section>
    </div>
  </w-flex>
</template>

<script>
import {defineAsyncComponent} from 'vue'
const FirstSection = defineAsyncComponent(() =>
    import('@/components/FirstSection.vue')
)
const AboutMe = defineAsyncComponent(() =>
    import('@/components/AboutMe.vue')
)
const ExperienceSection = defineAsyncComponent(() =>
    import('@/components/ExperienceSection.vue')
)
const SideElements = defineAsyncComponent(() =>
    import('@/components/SideElements.vue')
)
const FeaturedProject = defineAsyncComponent(() =>
    import('@/components/FeaturedProject.vue')
)

export default {
  components: {
    FeaturedProject,
    ExperienceSection,
    AboutMe,
    FirstSection,
    SideElements
  }
}
</script

main.js

import { createApp } from 'vue'
import App from './App.vue'
//Quasar
import { Quasar } from 'quasar'

// Import icon libraries
import '@quasar/extras/material-icons/material-icons.css'

// Import Quasar css
import 'quasar/src/css/index.sass'

//WaveUI
import WaveUI from 'wave-ui'
import 'wave-ui/dist/wave-ui.css'

const app = createApp(App)
app.use(Quasar, {
    plugins: {}, // import Quasar plugins and add here
})
app use(WaveUI, { /* Some Wave UI options */ })

app.mount('#app')

and how I call the data from js files.

<script>
import {mdiFolderOutline, mdiGithub, mdiOpenInNew} from '@mdi/js';
import {projects} from "@/assets/projects.js";

export default {
  name: "FeaturedProject",
  data: () => ({
    folder: mdiFolderOutline,
    github: mdiGithub,
    open: mdiOpenInNew,
    projects: projects
  })
}
</script>
英文:

I was faced with the performance of the web application, I didn't know how to optimize it. My single-page websites with good internet load in 2-3 seconds, if I put in 2G and 3G, it loads in about +20seconds. I have installed several libraries locally Quasar, and WaveUI(Before I also used NaiveUI, it took about 1M bytes, I decided to remove it. How can I increase the loading and size of the website?

优化Vue应用性能(减小体积)

What I have done and tried to do:

  • use CDN, but it not working.

  • I use 'defineAsyncComponent for "Lazy loading of components".

  • Converted from CSS/SCSS to sass.

I even don't use any heavy scripts, I only fetch and display the data from js file as a list, that's all.

my App.vue

&lt;template&gt;
  &lt;w-app class=&quot;main-app&quot;&gt;
    &lt;q-layout&gt;
      &lt;q-header reveal className=&quot;sticky-top&quot;&gt;
        &lt;Navigation/&gt;
      &lt;/q-header&gt;
      &lt;MainContent/&gt;
    &lt;/q-layout&gt;
  &lt;/w-app&gt;
&lt;/template&gt;

&lt;script&gt;
import {defineAsyncComponent} from &#39;vue&#39;
const MainContent = defineAsyncComponent(() =&gt;
    import(&#39;@/components/MainContent.vue&#39;)
)
const Navigation = defineAsyncComponent(() =&gt;
    import(&#39;@/components/NavigationComponent.vue&#39;)
)
export default {
  components: {MainContent, Navigation}
}

&lt;/script&gt;

MainContent.vue

&lt;template&gt;
  &lt;w-flex justify-center class=&quot;pa3&quot;&gt;
    &lt;SideElements&gt;&lt;/SideElements&gt;
    &lt;div class=&quot;grow fill-width text-center&quot;&gt;
      &lt;section id=&quot;first&quot;&gt;
        &lt;FirstSection&gt;&lt;/FirstSection&gt;
      &lt;/section&gt;
      &lt;section&gt;
        &lt;AboutMe id=&quot;about&quot;&gt;&lt;/AboutMe&gt;
      &lt;/section&gt;
      &lt;section&gt;
        &lt;ExperienceSection id=&quot;experience&quot;/&gt;
      &lt;/section&gt;
      &lt;section&gt;
        &lt;FeaturedProject id=&quot;projects&quot;&gt;

        &lt;/FeaturedProject&gt;
      &lt;/section&gt;
    &lt;/div&gt;
  &lt;/w-flex&gt;
&lt;/template&gt;

&lt;script&gt;
import {defineAsyncComponent} from &#39;vue&#39;
const FirstSection = defineAsyncComponent(() =&gt;
    import(&#39;@/components/FirstSection.vue&#39;)
)
const AboutMe = defineAsyncComponent(() =&gt;
    import(&#39;@/components/AboutMe.vue&#39;)
)
const ExperienceSection = defineAsyncComponent(() =&gt;
    import(&#39;@/components/ExperienceSection.vue&#39;)
)
const SideElements = defineAsyncComponent(() =&gt;
    import(&#39;@/components/SideElements.vue&#39;)
)
const FeaturedProject = defineAsyncComponent(() =&gt;
    import(&#39;@/components/FeaturedProject.vue&#39;)
)

export default {
  components: {
    FeaturedProject,
    ExperienceSection,
    AboutMe,
    FirstSection,
    SideElements
  }
}
&lt;/script

main.js

import { createApp } from &#39;vue&#39;
import App from &#39;./App.vue&#39;
//Quasar
import { Quasar } from &#39;quasar&#39;

// Import icon libraries
import &#39;@quasar/extras/material-icons/material-icons.css&#39;

// Import Quasar css
import &#39;quasar/src/css/index.sass&#39;

//WaveUI
import WaveUI from &#39;wave-ui&#39;
import &#39;wave-ui/dist/wave-ui.css&#39;

const app = createApp(App)
app.use(Quasar, {
    plugins: {}, // import Quasar plugins and add here
})
app.use(WaveUI, { /* Some Wave UI options */ })

app.mount(&#39;#app&#39;)

and how I call the data from js files.

&lt;script&gt;
import {mdiFolderOutline, mdiGithub, mdiOpenInNew} from &#39;@mdi/js&#39;;
import {projects} from &quot;@/assets/projects.js&quot;;

export default {
  name: &quot;FeaturedProject&quot;,
  data: () =&gt; ({
    folder: mdiFolderOutline,
    github: mdiGithub,
    open: mdiOpenInNew,
    projects: projects
  })
}
&lt;/script&gt;

答案1

得分: 1

To optimize the initial load strategy of a Vue application, there are several techniques that can be used:

  • 代码分割(Code splitting):将代码分割成小块可以加快加载速度并提高缓存效果。可以使用像webpack这样内置支持代码分割的工具来实现。

  • 惰性加载(Lazy loading):只在特定时刻加载需要的组件可以加快初始加载时间。Vue提供了通过动态导入实现的内置支持。

  • 服务器端渲染(SSR,Server-Side Rendering):在服务器上预渲染页面可以显著提高初始加载时间,因为服务器会向客户端发送完全渲染的HTML,减少客户端所需的工作量。

  • 缓存和压缩(Caching and compression):启用资源的缓存和压缩也可以加快初始加载时间。像Service Workers这样的工具可以帮助进行缓存,而gzip压缩可以减小发送到网络的资源的大小。

英文:

To optimize the initial load strategy of a Vue application, there are several techniques that can be used:

Code splitting: Splitting your code into smaller chunks allows for faster loads and better caching. This can be achieved using tools like webpack, which has built-in support for code splitting.

Lazy loading: Only loading the components that are needed at a particular moment can speed up the initial load time. Vue provides built-in support for lazy loading through dynamic imports.

SSR (Server-Side Rendering): Pre-rendering pages on the server can significantly improve the initial load time, as the server sends back fully rendered HTML to the client, reducing the amount of work required by the client.

Caching and compression: Enabling caching and compression of assets can also speed up the initial load time. Tools like service workers can help with caching while gzip compression can reduce the size of assets being sent over the network.

答案2

得分: 1

  • 在服务器中启用HTTP/3。它在处理多个请求(比如获取JS文件)时性能更好。
  • 启用gzip/brotli。可以在服务器/CDN上执行,也可以使用webpack(或其他捆绑工具)并配置production构建时使用压缩插件。
  • 拆分JS代码块。从我看到的情况,你有很多未使用的代码。应该使用类似webpack的捆绑工具,并使用treeshaking仅获取每个页面所需的代码。
  • 优化图像和字体。使用jpgwoff2以减小文件大小。
  • 根据需要在HTML <meta> 标签中使用preconnectprefetch

你还可以通过在Chrome Dev Tools中使用Lighthouse找到更多潜在的优化方法。

英文:

Performance optimisations you can do (not only frontend-wise):

  • Enable HTTP/3 in server. It has better performance handling multiple requests (like fetching JS files)
  • Enable gzip/brotli. You can either do it in the server/CDN or by using webpack (or any other bundler) and configuring production build with a compression plugin.
  • Split JS chunks. From what I see you have a lot of unused code. You should use a bundler like webpack and use treeshaking to only fetch code you need in each page.
  • Optimise images & fonts. Use jpg and woff2 for smaller sizes.
  • Use preconnect & prefetch in HTML &lt;meta&gt; tags as needed.

You can also find more potential optimisations by using Lighthouse in Chrome Dev Tools.

答案3

得分: 0

本地主机的速度直接取决于您计算机的性能。如果您的计算机旧且运行着 PHP 服务器、VSCode 等应用,您的页面刷新时间较长可能是正常的。

英文:

The speed of localhost directly depends on the power of your computer.
If you have an old computer with a running php server, vscode, .... it may be normal that your page takes a long time to refresh.

huangapple
  • 本文由 发表于 2023年4月4日 18:12:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75928145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定