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

标签 firebase seo google-cloud-functions sitemap firebase-hosting

我正在阅读谷歌关于 SEO 的指南,我发现了这一点。

Help Google find your content

The first step to getting your site on Google is to be sure that Google can find it. The best way to do that is to submit a sitemap. A sitemap is a file on your site that tells search engines about new or changed pages on your site. Learn more about how to build and submit a sitemap.


观察:我的网络应用程序是 电子商务/博客 我有一个商店,我有产品要出售,我有一个博客部分,我在其中创建和发布有关这些产品的内容。
所以,每个产品都有一个 产品页面 ,每篇博文都有一个 博文页面 .
然后我去寻找一些 的例子。网站 map 来自像我这样具有良好 SEO 排名的网站。
我发现了这个很好的例子:
robots.txt
User-Agent: *
Disallow: ... // SOME ROUTES

Sitemap: https://www.website.com/sitemap.xml
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>
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>
产品-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>
问题
我怎样才能保持更新Sitemap如果我的网络应用程序是 单页应用 与客户端站点路由?
由于我使用 Firebase 作为我的主机,我想做的是:
选项 #1 - 在 Firebase 托管中保留 sitemap.xml
从这个问题 Upload single file to firebase hosting via CLI or other without deleting existing ones?
弗兰克·范·普菲伦 说:

Update (December 2018): Firebase Hosting now has a REST API. While this still doesn't officially allow you to deploy a single file, you can use it creatively to get what you want. See my Gist here: https://gist.github.com/puf/e00c34dd82b35c56e91adbc3a9b1c412


我可以使用他的 Gist 来更新 sitemap.xml文件并每天运行一次此脚本,或者在我需要的时候运行。这适用于我当前的项目,但不适用于动态页面更改频率较高的项目,例如新闻门户或市场。
选项 #2 - 将 sitemap.xml 保存在 Firebase 存储中
将站点地图文件保存在我的存储桶中,并通过管理脚本或云计划功能根据需要频繁更新。
在我的 firebase.json 中设置重写并在请求时指定一个函数来响应和提供存储桶中的站点地图文件。
firebase.json
"hosting": {
 // ...

 // Add the "rewrites" attribute within "hosting"
 "rewrites": [ {
   "source": "/sitemap.xml",
   "function": "serveSitemapFromStorageBucket"
 } ]
}
最后一个问题
我倾向于选项#2,我想知道它是否适用于这个特定目的,或者我是否遗漏了一些东西。

最佳答案

我最终创建了一个云功能来按需构建站点地图文件。
firebase.json

"rewrites": [
  {
    "source": "/sitemap.xml",
    "function": "buildSitemap"
  },
]
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;

关于firebase - 如何使用 Firebase 为单页应用程序实现 sitemap.xml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57870096/

相关文章:

javascript - Firebase 的云函数 : Increment Counter

javascript - Web 上的 Firebase 电话号码身份验证

firebase - 拦截 firebase-functions 响应(中间件)

api - 使用 API 网关保护云功能

apache - 使用 Apache mod 重写更改 Joomla 中的站点 URL

optimization - SEO:正常链接旁边的 nofollow 链接

node.js - 使用 Firebase 的 Node.js 网站是否应该有两个 package.json?

Firebase:如何防止在firestore中写入特定字段?

firebase - 无法对 flutter 中的对象列表进行排序

子域的 SEO 站点地图