如何使用 Firebase 为单页应用程序实现 sitemap.xml 文件?

cbd*_*per 6 sitemap seo firebase firebase-hosting google-cloud-functions

我正在阅读谷歌关于搜索引擎优化的指南,我发现了这一点。

帮助 Google 找到您的内容

让您的网站出现在 Google 上的第一步是确保 Google 可以找到它。最好的方法是提交站点地图。站点地图是您网站上的一个文件,它告诉搜索引擎您网站上的新页面或更改页面。详细了解如何构建和提交站点地图。

Obs。:我的网络应用程序是一个电子商务/博客,其中我有一家商店,我有产品要销售,我有一个博客部分,我可以在其中创建和发布有关这些产品的内容。

因此,每个产品都有一个产品页面,每个博客文章都有一个blogPost 页面

然后我从像我这样的网站上寻找一些具有良好 SEO 排名的站点地图示例。

我找到了这个很好的例子:

机器人.txt

User-Agent: *
Disallow: ... // SOME ROUTES

Sitemap: https://www.website.com/sitemap.xml
Run Code Online (Sandbox Code Playgroud)

IE:显然爬虫机器人从robots.txt文件中找到了站点地图位置。

而且我还发现他们为 blogPost 和产品页面保留了单独的站点地图文件。

站点地图.xml

<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd">
  <sitemap>
    <loc>https://www.website.com/blogPosts-sitemap.xml</loc> // FOR POSTS
    <lastmod>2019-09-10T05:00:14+00:00</lastmod>
  </sitemap>
  <sitemap>
    <loc>https://www.website.com/products-sitemap.xml</loc>  // FOR PRODUCTS
    <lastmod>2019-09-10T05:00:14+00:00</lastmod>
  </sitemap>
</sitemapindex>
Run Code Online (Sandbox Code Playgroud)

blogPosts-sitemap.xml

// HUGE LIST WITH AN <url> FOR EACH BLOGPOST URL

<url>
  <loc>
    https://www.website.com/blog/some-blog-post-slug
  </loc>
  <lastmod>2019-09-03T18:11:56.873+00:00</lastmod>
  <changefreq>weekly</changefreq>
  <priority>0.8</priority>
</url>
Run Code Online (Sandbox Code Playgroud)

产品-sitemap.xml

// HUGE LIST WITH AN <url> FOR EACH PRODUCT URL

<url>
  <loc>
    https://www.website.com/gp/some-product-slug
  </loc>
  <lastmod>2019-09-08T07:00:16+00:00</lastmod>
  <changefreq>yearly</changefreq>
  <priority>0.3</priority>
</url>
Run Code Online (Sandbox Code Playgroud)

Sitemap如果我的 Web 应用程序是具有客户端站点路由的单页应用程序,我如何保持这样的更新文件?

由于我使用 Firebase 作为我的主机,我想过要做的是:

选项 #1 - 在 Firebase 托管中保留 sitemap.xml

从这个问题通过 CLI 或其他方式上传单个文件到 Firebase 托管而不删除现有文件?

Frank van Puffelen 说:

更新(2018 年 12 月):Firebase 托管现在有一个 REST API。虽然这仍然没有正式允许您部署单个文件,但您可以创造性地使用它来获得您想要的。在此处查看我的要点:https : //gist.github.com/puf/e00c34dd82b35c56e91adbc3a9b1c412

我可以使用他的 Gist 来更新sitemap.xml文件并每天或任何时候运行此脚本。这适用于我当前的项目,但不适用于动态页面更改频率较高的项目,例如新闻门户或市场。

选项 #2 - 将 sitemap.xml 保存在 Firebase 存储中

将站点地图文件保存在我的存储桶中,并根据需要通过管理脚本或云计划功能进行更新。

在 my 中设置重写firebase.json并指定一个函数来响应并在请求时从存储桶中提供站点地图文件。

firebase.json

"hosting": {
 // ...

 // Add the "rewrites" attribute within "hosting"
 "rewrites": [ {
   "source": "/sitemap.xml",
   "function": "serveSitemapFromStorageBucket"
 } ]
}
Run Code Online (Sandbox Code Playgroud)

最后的问题

我倾向于选项 #2,我想知道它是否适用于这个特定目的,或者我是否遗漏了什么。

cbd*_*per 4

我最终创建了一个云函数来按需构建站点地图文件。

firebase.json

"rewrites": [
  {
    "source": "/sitemap.xml",
    "function": "buildSitemap"
  },
]
Run Code Online (Sandbox Code Playgroud)

buildSitemap.js(这是一个云函数)

import * as admin from 'firebase-admin';

async function buildSitemap(req,res)  {

  // Use firebase-admin to gather necessary data
  // Build the sitemap file string
  // and send it back

  res.set('Content-Type', 'text/xml');
  res.status(200).send(SITEMAP_STRING);
  return;

}

export default buildSitemap;

Run Code Online (Sandbox Code Playgroud)