英文:
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?
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>
答案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仅获取每个页面所需的代码。 - 优化图像和字体。使用
jpg
和woff2
以减小文件大小。 - 根据需要在HTML
<meta>
标签中使用preconnect
和prefetch
。
你还可以通过在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 usingwebpack
(or any other bundler) and configuringproduction
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
andwoff2
for smaller sizes. - Use
preconnect
&prefetch
in HTML<meta>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论