May 07, 2021 Vue CLI
If you use the Vue CLI to work with static resources and work with the back-end framework as part of your deployment, all you need is to make sure that the build files generated by the Vue CLI are in the right place and that the back-end framework is published.
If you deploy a front-end app independently of the back end -- that is, the back end exposes a front-end accessible API -- then the front end is actually a purely static app. Then you can deploy the content built in the dist directory to any static file server, but make sure that the publicPath is correct.
The dist directory needs to start an HTTP server to access (unless you have configured publicPath as a relative value), so opening dist/index.html directly with the file:// protocol does not work. The easiest way to build a production environment in a local preview is to use a node .js a static file server, such as serve:
npm install -g serve
# -s 参数的意思是将其架设在 Single-Page Application 模式下
# 这个模式会处理即将提到的路由问题
serve -s dist
If you use Vue Router in history mode, you can't match a simple static file server. For example, if you use Vue Router to define a route for /todos/42/, the development server has configured the appropriate localhost:3000/todos/42 response, but a simple static server built for the production environment will return 404.
To solve this problem, you need to configure the production environment server to roll back any requests that do not match the static file to the index .html. Vue Router's documentation provides guidelines for common server configurations.
If the front-end static content is deployed on a domain name that is different from the back-end API, you need to configure CORS appropriately.
If you use a PWA plug-in, the app must be set up on HTTPS for the Service Worker to be properly registered.
According to the description in the GitLab Pages documentation, all configurations are in the .gitlab-ci.yml file in the root. The following example is a good place to get started:
# .gitlab-ci.yml 文件应放在你仓库的根目录下
pages: # 必须定义一个名为 pages 的 job
image: node:latest
stage: deploy
script:
- npm ci
- npm run build
- mv public public-vue # GitLab Pages 的钩子设置在 public 文件夹
- mv dist public # 重命名 dist 文件夹 (npm run build 之后的输出位置)
artifacts:
paths:
- public # artifact path 一定要在 /public , 这样 GitLab Pages 才能获取
only:
- master
Typically, your static page will be hosted on https://yourUserName.gitlab.io/yourProjectName, so you can create an initial vue.config .js file to update the value BASE_URL to match:
// vue.config.js 位于仓库的根目录下
// 确保用 GitLab 项目的名称替换了 `YourProjectName`
module.exports = {
publicPath: process.env.NODE_ENV === 'production'
? '/yourProjectName/'
: '/'
}
Read the documentation at GitLab Pages domains to learn more about page deployment URLs. Note that you can also use a custom domain name.
Submit the .gitlab-ci.yml and vue.config files before pushing .js repository. GitLab CI's pipeline will be triggered: When successful, go to Settings and Pages to see links to websites.
You can also view vue-cli-plugin-netlify-lambda.
If you use Vue Router's history mode, you need to create a file in the /public directory _redirects file:
# 单页应用的 Netlify 设置
/* /index.html 200
For more information, check out the Netlify redirect documentation.
Render offers a free static site hosting service with fully managed SSL, global CDNs, and GitHub continuous automatic deployments.
Home and dry! At the end of the build, your app will be online on your Render URL.
If you use Vue Router's history mode, you'll need to add the following override rules to your site's Redirects/Rewrites settings:
For more information, check out Render's Redirect and Overwrite and Custom Domain Name documentation.
Create a new Firebase project Firebase console. Refer to the documentation.
Make sure that firebase-tools is installed globally:
npm install -g firebase-tools
At the root of the project, initialize firebase with the following command:
firebase init
Firebase will ask some questions about the initialization project.
// firebase.json
{
"hosting": {
"public": "dist"
}
}
// firebase.json
{
"hosting": {
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
Perform the npm run build to build the project.
In the Firebase Hosting deployment project, perform the following commands:
firebase deploy --only hosting
If additional Firebase CLI features are required for use in the deployed project, perform firebase deploy to remove the --only parameter.
You can now visit your project at https://your-PROJECT-ID.com.firebaseapp.
Please refer to the Firebase documentation for more details.
ZEIT Now is a website and serverless API cloud platform where you can deploy your Vue project using your personal domain name (or free .now.sh URL).
To install its command-line interface using npm, run the following command:
npm install -g now
Run the following commands at the project root to deploy your app:
now
You can also use their GitHub or GitLab integration services.
Home and dry!
Your site will start deploying and you'll get a link that https://vue.now-examples.now.sh/ a file.
Out of the box, the request is automatically overwrite to the index .html (except for custom static files) with the appropriate cache request header. You can rewrite these rules.
The | Welcome to contribute.
Details: https://gist.github.com/hone/24b06869b4c1eca701f9
To deploy with Surge, the steps are simple.
First, build the project by running the npm run build. If you don't have Surge's command-line tools installed yet, you can do so by running commands:
npm install --global surge
Then cd goes to the item's dist/folder, then runs the surge and follows the on-screen prompts. I f you're using Surge for the first time, it will require an email and password. Confirm the project folder and enter your preferred domain to view the items being deployed, as shown below.
project: /Users/user/Documents/myawesomeproject/dist/
domain: myawesomeproject.surge.sh
upload: [====================] 100% eta: 0.0s (31 files, 494256 bytes)
CDN: [====================] 100%
IP: **.**.***.***
Success! - Published to myawesomeproject.surge.sh
By accessing myawesomeproject.surge.sh to ensure that your project has been successfully published with Surge, more settings details about custom domain names, etc. can be viewed at Surge's help page.
Use Nginx to deploy your app in a Docker container.