Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sitemap not created until server restart in meteor

I am using meteor to create simple blog system. For sitemaps files I'm using this package.

I added some initialize data in server startup function (create some post) and used below code (server/sitemaps.js) in server to create sitemaps for each category (e.g. sitemap1.xml for first category and 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 startup like this: (server/startup.js)

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

But sitemaps didn't exist until server restart (my robots.txt files also empty) but when server restarted sitemaps and robots.txt content created for me.

I think posts inserted after sitemaps.js but what's the problem guys and how to fix that?

New try:

I try new solution like below but this code also didn't work. (I want to create seperate sitemap file for each 10000 category to prevent big sitemap and google sitemap error):

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 show sitemap files correctly but all sitemap is empty like this:

<urlset> </urlset>

When does sitemaps.add() run? I think it does on server restart but New try was disappointed me and I think my guess is incorrect and If sitemaps.add() was run why it's empty.

like image 592
b24 Avatar asked Aug 14 '15 06:08

b24


2 Answers

Your problem seems to be the folder structure. You said you have /server/sitemaps.js and /server/startup.js and you wish that sitemaps would run after your startup, but the thing is Meteor will run those files alphabetically, so sitemap comes before startup. If you place your startup.js inside a lib folder, such as /server/lib/startup.js, you'll get the desired results, because Meteor will run lib folder before others.

like image 53
Marcelo Schmidt Avatar answered Oct 04 '22 14:10

Marcelo Schmidt


It's normal behavior, the code at Meteor.startup will run just once at app start. If you're looking to re run this function you either need to use meteor method to call the function from the client or you can you use something like cron job to run repeat jobs here is a great package https://atmospherejs.com/percolate/synced-cron

like image 37
Mark Uretsky Avatar answered Oct 04 '22 13:10

Mark Uretsky