nuxt项目部署,npm run build 和npm run generate的区别

每日鸡汤:每个你想要学习的瞬间都是未来的你向自己求救

非服务端渲染的项目,比如普通的vite + vue项目,我们在部署生产环境的时候,只需要两步

  1.  运行 npm run build  然后得到了一个 dist 文件夹
  2. 将这个dist文件夹部署到一个静态服务器上

一般来说这样就可以访问这个项目了

但是服务端渲染的项目有所不同,因为会需要在服务端运行一些代码,所以一个普通的静态服务器满足不了我们需求,我们需要在服务器端运行自己的服务端代码,所以 nuxt 项目是使用 npm run preview 这个命令运行打包出来的文件。

在部署过程中还有一个问题刚开始不懂 ,就是build 和generate 命令有啥不同!来看一下官网文档的描述

The build command creates a .output directory with all your application, server and dependencies ready for production.

build命令创建了一个.output目录,其中包含所有可用于生产的应用程序、服务器和依赖项。

The generate command pre-renders every route of your application and stores the result in plain HTML files that you can deploy on any static hosting services. The command triggers the nuxi build command with the prerender argument set to true

generate命令预呈现应用程序的每个路由,并将结果存储在可以部署在任何静态托管服务上的纯HTML文件中。该命令触发nuxi build命令,prerender参数设置为true

也就是说generate命令在运行了 build 命令的同时,并且开启了预渲染的功能,在打包过程中会为每个路由生成一个静态的html文件 。

  • prerender:boolean - Prerenders routes at build time and includes them in your build as static assets

在构建时预渲染路由,并将它们作为静态资产包含在构建中

由于 generate 会生成静态文件,所以更加有利于seo! 所以你的网站如果对seo【搜索引擎优化】有需求的话那么最好是用这个命令。 但是转念一想,你之所以使用 nuxt 框架不就是为了服务端渲染,不就是为了为了seo优化?

所以我建议你别纠结了,对nuxt项目就直接使用 generate 命令打包部署就好了,至少我做过的nuxt项目都是用的generate

猜你喜欢

转载自blog.csdn.net/qq_17335549/article/details/131873611