如何为Vue应用程序提供robots.txt

Ove*_*ivr 7 webpack vue.js vue-loader vuejs2

我使用vue-loader使用webpack构建我的Vue应用程序.我的应用程序由Firebase提供.

出于搜索引擎优化的目的,我需要在我的应用程序(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
  }
}
Run Code Online (Sandbox Code Playgroud)

Bra*_*ram 15

VueJS v3 build 命令将 /public 中的任何内容复制到您的最终 dist/ 中。因此,将 public/ 文件夹用于您在最终发行版中需要的任何其他文件。


Imr*_*e_G 6

如果我假设正确,那么您正在使用webpack模板中的npm run build命令构建应用程序,从而创建部署到Firebase的/ dist文件夹.如果是这种情况,您只需将robots.txt文件添加到索引旁边的dist文件夹即可.这应该工作.

但是,如果您的目标是更好的SEO,那么根据应用程序的复杂性,预渲染页面或使用服务器端渲染可能会更好.

  • VueJS v3 构建命令将 /public 中的所有内容复制到您的最终 dist/ 中。所以实际的答案是:我们的 public/ 文件夹。 (2认同)