Site map not created until server restart in meteor

I use a meteorite to create a simple blog system. For sitemaps, I use this package.

I added some initialized data to the server start function (I create a message) and used the code below ( server / sitemaps.js ) on the server to create sitemaps for each category (for example, sitemap1.xml for the first category, etc.):

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); }); }); 

And I have a launch like this: ( server / startup.js )

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

But the Sitemaps did not exist before the server rebooted (my robots.txt files were also empty), but when the server restarted the Sitemaps and the robots.txt content created for me.

I think the messages are inserted after sitemaps.js, but what are the guys problems and how to fix it?

New attempt:

I am trying a new solution as shown below, but this code also does not work. (I want to create a separate sitemap for each category 10000 to prevent a large sitemap and Sitemap error for google site):

 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 displays Sitemaps correctly, but the entire sitemap is empty:

 <urlset> </urlset> 

When is sitemaps.add () executed? I think this happens when the server restarts, but the new attempt was disappointed by me, and I think my guess is wrong and if the sitemaps.add () file was run, why is it empty.

+8
javascript sitemap meteor
source share
2 answers

Your problem is the folder structure. You said that you have /server/sitemaps.js and / server / startup.js, and you want the Sitemaps to start after your launch, but the fact is that Meteor will run these files in alphabetical order, so the map The site comes before launch. If you put your startup.js in the lib folder, for example /server/lib/startup.js, you will get the desired results, because Meteor will run the lib folder in front of others.

+3
source share

This is normal behavior, the code in Meteor.startup will only run once when the application starts. If you want to re-run this function, you either need to use the meteor method to call the function from the client, or you can use something like a cron job to run repeated jobs, here is a great package https://atmospherejs.com/percolate/synced- cron

+1
source share

All Articles