如何为 Vue 应用程序提供 robots.txt
How to serve robots.txt for a Vue app
我使用 vue-loader 使用 webpack 构建我的 Vue 应用程序。我的应用程序由 Firebase 提供服务。
出于 SEO 目的,我需要在我的应用程序 (GET /robots.txt
) 的根目录提供文件 robots.txt。
如何配置 webpack/vue-loader 来提供此文件?
这是我当前的 webpack 基本配置 ./config/index.js
// see http://vuejs-templates.github.io/webpack for documentation.
var path = require('path')
module.exports = {
build: {
env: require('./prod.env'),
index: path.resolve(__dirname, '../dist/index.html'),
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/',
productionSourceMap: true,
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
},
dev: {
env: require('./dev.env'),
port: 8080,
autoOpenBrowser: true,
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {},
// CSS Sourcemaps off by default because relative paths are "buggy"
// with this option, according to the CSS-Loader README
// (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected,
// just be aware of this issue when enabling this option.
cssSourceMap: false
}
}
如果我的假设正确,您正在使用来自 webpack 模板的 npm 运行 build 命令构建您的应用程序,创建一个部署到 Firebase 的 /dist 文件夹。如果是这种情况,您只需将 robots.txt 文件添加到索引旁边的 dist 文件夹即可。应该可以。
但是,如果更好的 SEO 是您的目标,则根据您的应用程序的复杂性,预呈现页面或使用服务器端呈现可能会更好。
MEVN案例
MEVN 代表 MongoDB、Express、Vue.js 和 Node.js。
如果您有 Vue.js 作为前端,Node.js 作为后端和 RESTful API 服务器,您可以将 robots.txt 放入Vue.js /static/
文件夹。这是Vue.js project structure供您参考。
然后您可以通过以下方式简单地配置 Express 路由来为 robots.txt 文件提供服务:
app.use('/robots.txt', express.static(path.join(__dirname, 'dist/static/robots.txt')));
(注意:每次构建 Vue.js 项目时都会新生成 dist 文件夹:npm run build
。这种方法可以使您不必将 robots.txt 文件添加到 dist每次构建后的文件夹。)
VueJS v3 构建命令将 /public 中的任何内容复制到您的最终 dist/。因此,将 public/ 文件夹用于您希望在最终分发中使用的任何其他文件。
我使用 vue-loader 使用 webpack 构建我的 Vue 应用程序。我的应用程序由 Firebase 提供服务。
出于 SEO 目的,我需要在我的应用程序 (GET /robots.txt
) 的根目录提供文件 robots.txt。
如何配置 webpack/vue-loader 来提供此文件?
这是我当前的 webpack 基本配置 ./config/index.js
// see http://vuejs-templates.github.io/webpack for documentation.
var path = require('path')
module.exports = {
build: {
env: require('./prod.env'),
index: path.resolve(__dirname, '../dist/index.html'),
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/',
productionSourceMap: true,
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
},
dev: {
env: require('./dev.env'),
port: 8080,
autoOpenBrowser: true,
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {},
// CSS Sourcemaps off by default because relative paths are "buggy"
// with this option, according to the CSS-Loader README
// (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected,
// just be aware of this issue when enabling this option.
cssSourceMap: false
}
}
如果我的假设正确,您正在使用来自 webpack 模板的 npm 运行 build 命令构建您的应用程序,创建一个部署到 Firebase 的 /dist 文件夹。如果是这种情况,您只需将 robots.txt 文件添加到索引旁边的 dist 文件夹即可。应该可以。
但是,如果更好的 SEO 是您的目标,则根据您的应用程序的复杂性,预呈现页面或使用服务器端呈现可能会更好。
MEVN案例
MEVN 代表 MongoDB、Express、Vue.js 和 Node.js。
如果您有 Vue.js 作为前端,Node.js 作为后端和 RESTful API 服务器,您可以将 robots.txt 放入Vue.js /static/
文件夹。这是Vue.js project structure供您参考。
然后您可以通过以下方式简单地配置 Express 路由来为 robots.txt 文件提供服务:
app.use('/robots.txt', express.static(path.join(__dirname, 'dist/static/robots.txt')));
(注意:每次构建 Vue.js 项目时都会新生成 dist 文件夹:npm run build
。这种方法可以使您不必将 robots.txt 文件添加到 dist每次构建后的文件夹。)
VueJS v3 构建命令将 /public 中的任何内容复制到您的最终 dist/。因此,将 public/ 文件夹用于您希望在最终分发中使用的任何其他文件。