用于使用 Meteor 进行 SEO 的动态 URL 变量

Dynamic URL variable for SEO with Meteor

我正在开发一个使用 ms-seo package 的 Meteor 应用程序。我想知道是否有办法让 URL 对 SEO 更友好?

Router.route('/item/:_id', {
  name: 'item.detail',
  controller: 'ItemsController',
  action: 'detail',
  where: 'client',
  onAfterAction: function() {
    var data = this.data();
    if (data) {
    SEO.set({
      title: data.title + ' - ' + data.company + ' (' + data.city + ')',
      meta: {
        'description': data.descriptionHTML
      }
    });
  }
});

虽然这很完美,但它产生的 URL 是 /item/5RTxofPPn3LwifP24,我想将 data.title 推到 url,所以我可以得到 /item/i-am-a-lower-case-dash-replaced-unique-title/

是否有相应的软件包?

您需要创建一个 slug。所以你的 collection 会有这样的字段:

  • _id
  • 标题
  • 鼻涕虫
  • 内容

然后,为了制作您的 slug,您可以使用 https://atmospherejs.com/yasaricli/slugify 之类的东西将您的标题转换为 slug。基本上它所做的是将名为 "Unique Shopping Cart Item" 的标题转换为 "unique-shopping-cart-item."

然后在您的路由器中将 slug 作为参数传入。

Router.route('/blog/:slug',{
    name:'blogPosts',
    waitOn: function() { return Meteor.subscribe('collection'); },
    data: function(){
        var slug = this.params.slug;
        return Collection.findOne({slug:slug});
        // this is saying search the collection's slug for the passed in parameter which we're also calling "slug"
    }
});

您可以尝试 slugifydata.title 推为漂亮的 url。