【问题标题】:How to serve robots.txt for a Vue app如何为 Vue 应用提供 robots.txt
【发布时间】:2018-08-06 19:35:30
【问题描述】:

我使用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 vue.js vuejs2 vue-loader


    【解决方案1】:

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

    【讨论】:

    • 我不明白为什么这个答案被否决了。我相信这是简单而正确的答案
    • 使用 Netlify 我最终改变了我的 package.json:"build:prod": "vue-cli-service build && mv dist/robots-production.txt dist/robots.txt", "build:staging": "vue-cli-service build --mode staging && mv dist/robots-staging.txt dist/robots.txt",
    【解决方案2】:

    如果我假设正确,您正在使用来自 webpack 模板的 npm run build 命令构建您的应用程序,创建一个部署到 Firebase 的 /dist 文件夹。如果是这种情况,您可以将 robots.txt 文件添加到索引旁边的 dist 文件夹中。应该可以的。

    但是,如果您的目标是更好的 SEO,则根据应用程序的复杂性,最好预渲染页面或使用服务器端渲染。

    【讨论】:

    • 嗯,这可以工作,但我暂时将此文件夹排除在版本控制之外。但这很好,我还可以修改我的部署脚本以将 robots.txt 复制到根目录。出于好奇,我将这个问题留了几天,如果没有任何问题,我会接受你的回答。
    • 使用 firebase deploy 您可以使用 predeploy 脚本将您的 robots.txt 移动到 dist 目录。然后,当您部署时,您将拥有 robots.txt。我将我的添加到与 firebase.json 文件相同的级别。只需在您的 firebase.json 中添加如下所示的 predeploy 行:“predeploy”:“./addrobots.sh”,然后添加执行“cp robots.txt dist”的 shell 脚本 (addrobots.sh)。现在您可以将 robots.txt 置于版本控制之下。
    • VueJS v3 build 命令将 /public 中的任何内容复制到您的最终 dist/。所以实际的答案是:我们是 public/ 文件夹。
    【解决方案3】:

    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 文件夹。)

    【讨论】:

    • 在这种情况下,变量“app”究竟是什么?
    • const express = require('express'); const app = express();,(参见 Hello world 示例:expressjs.com/en/starter/hello-world.html
    • 除了他们托管在 Firebase 上而不是 Express 上,所以这个答案是不正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 2018-10-18
    • 2019-12-06
    相关资源
    最近更新 更多