简体   繁体   English

创建一个meteor.js网站地图

[英]Create a meteor.js sitemap

Sitemaps are cool. 站点地图很棒。 If I want one for my meteor project, I guess I could do one of the following: 如果我想要一个用于流星项目,我想我可以执行以下一项操作:

  1. Hard code a sitemap.xml file and put in the /public/ folder. 硬编码sitemap.xml文件,并将其放在/ public /文件夹中。 Not dynamic, but easy for google to find. 不是动态的,但Google容易找到。 Might be sufficient for apps with few sub pages. 对于子页面很少的应用程序可能就足够了。
  2. (Haven't tried this at all) Create a dynamic sitemap using the meteor Router at /sitemap.xml (?) and output this as XML. (根本没有尝试过)使用流星路由器在/sitemap.xml(?)创建动态站点地图,并将其输出为XML。 Dynamic, but won't be very easy for google to find as it's loaded dynamically (maybe with the spiderable package?) 动态的,但由于它是动态加载的(可能是带有可抓取的软件包?),因此Google很难找到它

But neither is very satisfying to me. 但是,两者都不令我满意。 I would want it to be dynamic but yet stored in an (preferably) static file that's easy to find by Google. 我希望它是动态的,但要存储在一个(最好是)静态文件中,该文件很容易被Google找到。 Or something like that. 或类似的东西。

What's the best way? 最好的方法是什么?

if there is a way you could use the client side router to dynamically generate a sitemap, It might be possible with Meteor Router 如果有一种方法可以使用客户端路由器动态生成站点地图,则可能使用Meteor Router

You need meteor router and meteor's http. 您需要流星路由器和流星的http。 Meteor router has a prequisite of meteorite , which it looks like you already have. 流星路由器具有陨石的先决条件,看起来就像您已经拥有的一样。

So the first step is to make a route for that sitemap.xml with server side routing: 因此,第一步是使用服务器端路由为该sitemap.xml路由:

Meteor.Router.add('sitemap.xml', function() {
    //get sitemap data (below)
    return generated_sitemap;
});

and a function that generates the sitemap: 以及生成站点地图的函数:

We need that router.js , which is unfortunately only run on the client. 我们需要该router.js ,不幸的是,它仅在客户端上运行。 So we need to fetch it with meteor.http. 因此,我们需要使用meteor.http获取它。 The router.js file basically contains the Meteor.Router.add bit of your router code. router.js文件基本上包含路由器代码的Meteor.Router.add位。 Adjust the url to wherever your router.js file might be stored 将URL调整为您的router.js文件可能存储的位置

routerdata = Meteor.http.get("http://localhost:3000/client/router.js").content 

We then need to parse the router data out of there (make sure you use the var so scoping doesn't ruin the actual router) 然后,我们需要从那里解析路由器数据(确保您使用var,以便范围不会破坏实际的路由器)

Server side js 服务器端js

Meteor.Router.add('/sitemap.xml', function() {
    routerdata = Meteor.http.get("http://localhost:3000/client/router.js").content 
    var Meteor = {};
    Meteor.Router = {add:function(input) {return input}};

    //drag the data out of the routerdata, eval is quick and dirty but you could shackle it down further
    routers = eval(routerdata);

    //now generate the sitemap.xml data

    xmldata = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
    for(var url in routers) {
        xmldata+="<url>\n";
        xmldata+="<loc>"+url+"</loc>\n";
        xmldata+="<lastmod>2013-03-03</lastmod>\n";
        xmldata+="<changefreq>daily</changefreq>\n";
        xmldata+="<priority>0.8</priority>\n";
        xmldata+="</url>\n";
    }

    xmldata+="</urlset>";

    return xmldata;
});

You might need to customize it a bit further to exactly how you want it. 您可能需要对其进行自定义,以使其完全符合您的要求。 I can't say i've tried the above im not really conversant with how to optimally use sitemaps but it might get you a start 我不能说我已经尝试过上述即时消息,但我不太了解如何最佳使用站点地图,但这可能会让您有所收获

this package looks promising: 这个软件包看起来很有希望:

https://atmosphere.meteor.com/package/sitemaps https://atmosphere.meteor.com/package/sitemaps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM