Angular 8 站点地图和 robots.txt

Gae*_*n56 4 seo angular

我使用 angular 8 创建了一个 angular 应用程序。我正在努力在 Google 上引用我的网站。我在项目的路径中添加了一个 sitemap.xml 和 robots.txt 但是当我尝试通过这样做访问浏览器中的文件时:

https://blablawebsite.fr/sitemap.xml

路由模块正在取路由,找不到页面。如何确保路由模块没有选择 sitemap.xml 和 robots.txt?

Dan*_*cki 8

使用Angular UniversalwithCloud Functions for Firebase主持,我把sitemap.xmlwith robots.txtinside src。然后,在angular.json内部"assets"增加它像这样:

...
"assets": [
  "APP_NAME/src/robots.txt",
  "APP_NAME/src/sitemap.xml",
],
...
Run Code Online (Sandbox Code Playgroud)

效果很好:)


ano*_*der 6

您需要首先考虑将项目转换为 Angular Universal。Google 和其他搜索引擎机器人无法也不会浏览您的应用程序,因为所有这些都是事后发生的。

从这里开始: https: //angular.io/guide/universal

很多人都误解了所有这些是如何工作的,并且在意识到网站和 SPA 之间的区别之前就深入了解了他们的项目。没什么大不了的,您仍然可以让您的 Angular 应用程序通过服务器端渲染进行排名


SLL*_*dre 6

与我对已接受答案的理解相反, tt没有必要仅仅为了添加 sitemap.xml 或 robots.txt 就向您的项目添加角度通用。

正如@David在评论中更简洁地说的那样,您需要做的就是

  1. 在 /src 中添加 put 文件到您的项目中(在 favicon.ico 旁边)
  2. 将它们添加到 angular.json 中的projects>architect>build>options> assets中,如下所示:
 "assets": [
                  "src/favicon.ico",
                  "src/assets",
                  "src/robots.txt",
                  "src/sitemap.xml"
                ],
Run Code Online (Sandbox Code Playgroud)

这里有更详细的解释。

添加。评论:

  • 确保您不会意外地仅在 angular.json 中捕获“test”的资产,它们在浏览文件时会更早地出现
  • 如果 Chrome Dev Tools 中 Lighthouse 报告的 SEO 部分下方不再出现关于您的 robots.txt 文件无效的警告,您可以验证 Google 现在是否识别您的 robots.txt
  • 然而,即使使用robots.txt,除了Google 之外的搜索引擎也无法很好地处理没有服务器端渲染(例如通过Angular Universal)的Angular SPA。甚至谷歌在这方面的能力也存在争议。这家伙解释的很好。
  • 我的答案适用于 Angular 10 和 11,其他版本我不知道


Nen*_*vic 6

robots.txt虽然其他答案提供了添加单个文件和文件的解决方案sitemap.xml,但通常您会希望根据要部署的实例拥有这些文件的不同版本。

例如,在生产中,您可能希望允许搜索引擎对您的页面进行爬网和索引,但不允许在临时或开发实例上。


如果你想根据环境有不同版本的文件,你可以这样做:

步骤1

创建一个名为 、 的文件夹robots,并在其中创建 3 个名为developmentstaging和 的子文件夹production(或您想要的任何环境)。然后,在每个子文件夹中创建特定于环境的robots.txt文件sitemap.xml

第2步

在文件中,为每个环境单独angular.json指定:assets

{
  "architect": {
    "build": {
      "configurations": {
        "configurations": {
          "production": {
            "assets": [
              "src/favicon.ico",
              "src/assets",
              "src/manifest.webmanifest",
              {
                "glob": "robots.txt",
                "input": "src/robots/production/",
                "output": "./"
              },
              {
                "glob": "sitemap.xml",
                "input": "src/robots/production/",
                "output": "./"
              }
            ],
            ...
          },
          "staging": {
            "assets": [
              "src/favicon.ico",
              "src/assets",
              "src/manifest.webmanifest",
              {
                "glob": "robots.txt",
                "input": "src/robots/staging/",
                "output": "./"
              },
              {
                "glob": "sitemap.xml",
                "input": "src/robots/staging/",
                "output": "./"
              }
            ],
            ...
          },
          "development": {
            "assets": [
              "src/favicon.ico",
              "src/assets",
              "src/manifest.webmanifest",
              {
                "glob": "robots.txt",
                "input": "src/robots/development/",
                "output": "./"
              },
              {
                "glob": "sitemap.xml",
                "input": "src/robots/development/",
                "output": "./"
              }
            ],
            ...
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)