站点地图不会创build,直到服务器重新启动meteor

我正在使用meteor来创build简单的博客系统。 对于站点地图文件,我正在使用这个包。

我在服务器启动函数中添加了一些初始化数据(创build一些post),并在服务器中使用下面的代码( server / sitemaps.js )为每个类别创build站点地图(例如,第一类的sitemap1.xml等):

function sitemapOutput(categoryName){ var out = [], posts = Posts.find({ category: categoryName }).fetch(); _.each(posts, function(post) { out.push({ page: post.url(), lastmod: post.insertDate, changefreq: 'weekly' }); }); return out; } Categories.find().forEach(function(Category, index) { sitemaps.add('/sitemap' + (index+1) +'.xml', function(){ return sitemapOutput(Category.name); }); }); 

我有这样的启动:( server / startup.js

 Meteor.startup(function () { // some post and category created here }); 

但是,直到服务器重新启动(我的robots.txt文件也是空的),但是当服务器重新启动sitemaps和robots.txt内容为我创build时,站点地图不存在。

我认为post插入sitemaps.js之后,但问题是什么,以及如何解决?

新的尝试:

我尝试像下面这样的新的解决scheme,但是这个代码也没有工作。 (我想为每个10000类别创build单独的站点地图文件,以防止大的站点地图和谷歌站点地图错误):

 for (var i=0;i<=Math.round(Categories.find().count()/10000);i++) { sitemaps.add('/sitemap' + i +'.xml', function(){ var out = []; Categories.find({}, {sort: {insertDate: 1} ,limit: 10000, skip: i * 10000}).forEach(function(Category) { out.push({ page: "/category/" + Category.title + "/" + Category._id, lastmod: Category.insertDate, changefreq: 'weekly' }); }); return out; }); } 

robots.txt显示正确的站点地图文件,但所有的站点地图是这样的:

 <urlset> </urlset> 

什么时候sitemaps.add()运行? 我认为它在服务器重新启动,但新的尝试令我失望,我想我的猜测是不正确的,如果sitemaps.add()运行,为什么它是空的。

你的问题似乎是文件夹结构。 你说你有/server/sitemaps.js和/server/startup.js,你希望这个站点地图可以在启动后运行,但是Meteor会按照字母顺序运行这些文件,所以在启动之前会有sitemap。 如果你把你的startup.js放在一个lib文件夹中,比如/server/lib/startup.js,你会得到想要的结果,因为Meteor会在别人之前运行lib文件夹。

这是正常的行为,Meteor.startup的代码只会在应用程序启动时运行一次。 如果你想重新运行这个function,你需要使用meteor方法来调用客户端的function,或者你可以使用类似cron job的东西来运行重复的工作,这是一个很棒的包https://atmospherejs.com/渗滤/同步-的cron