kadirahq / flow-router

Carefully Designed Client Side Router for Meteor
MIT License
1.09k stars 195 forks source link

Can FlowRouter be made to dynamically generate xml? #575

Closed UmbraLimi closed 8 years ago

UmbraLimi commented 8 years ago

I'd like to dynamically generate (without creating a file at all) a sitemap with the following:

FlowRouter.route("/sitemap.xml", {
   action: function(params, queryparams) {
      const URL_Prefix = "http://wowsa.wow";
      let links = [];

      links.push('<?xml version="1.0" encoding="UTF-8"?>');
      links.push('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');

      let cursor = Programs.find({});
      cursor.forEach(function(program) {
         links.push("<url>");
         links.push("  <loc>" + URL_Prefix + program.route + "</loc>");
         links.push("</url>");
      }, this);

      links.push("</urlset>");
      return links.join("\r\n");
   }
});

Is this a valid route? Can you "return" the content you want this way?

I am using FlowRouter SSR at the moment.

Thanks in advance for any help/suggestions :)

aaronsmulktis commented 7 years ago

hey man did you ever figure this out? I'm curious too... real curious

UmbraLimi commented 7 years ago

I did and I will try to post it here tomorrow. I ended up using picker as it caught the routing on the server.

aaronsmulktis commented 7 years ago

nice seems like that'll work for me too

UmbraLimi commented 7 years ago

I'm pasting from a Windows text editor so the formatting is a mess - apologies.

Hope it helps. This was from a site built with Meteor v1.2.1

Meteor.startup(function() { Picker.route('/sitemap.xml', function(params, req, res, next) { res.writeHead(200, {'Content-Type': 'application/xml'}); res.end(Sitemaps.stream_sitemap()); });

Picker.route('/robots.txt', function(params, req, res, next) { res.writeHead(200, {'Content-Type': 'text/plain'}); // or 'text/html' res.end(Sitemaps.stream_robots_txt()); });

Picker.route('/BingSiteAuth.xml', function(params, req, res, next) { res.writeHead(200, {'Content-Type': 'application/xml'}); res.end(Sitemaps.stream_bingsiteauth()); }); }

Sitemaps = function() {

var _init = function() {
};

let _assemble_sitemap_string = function() { let URL_Prefix = React_Helpers.get_misc_value("URL-Prefix", "utilities.js"); let links = [];

    links.push('<?xml version="1.0" encoding="UTF-8"?>');
    links.push('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');

  let cursor;
    // Programs
    cursor = Programs.find({});
    cursor.forEach(function(record) {
        links.push("<url>");
        links.push("  <loc>" + URL_Prefix + record.route + "</loc>");
     links.push("  <lastmod>" + moment(record.updated_on).format() + "</lastmod>");
     links.push("  <changefreq>" + "weekly" + "</changefreq>");
     links.push("  <priority>" + "0.8" + "</priority>");
        links.push("</url>");
    }, this)
    links.push("</urlset>");
  return links.join("\r\n");
};

let _assemble_robots_txt_string = function() { let rows = []; rows.push("sitemap: http://zzzzzzzzzzzzzzz.com/sitemap.xml"); rows.push("User-agent: *"); rows.push("Disallow: /alm/"); return rows.join("\r\n"); };

let _assemble_bingsiteauth_string = function() { let rows = []; rows.push('<?xml version="1.0" encoding="UTF-8"?>'); // may not need the encoding rows.push(''); rows.push(' zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'); rows.push(''); return rows.join("\r\n"); };

let _write_sitemap = function() { console.log("|-- creating /sitemap.xml file"); let fs = Npm.require('fs'); let smap = _assemble_sitemap_string();

    fs.writeFileSync("/sitemap.xml", smap, 'utf8');
    console.log("|-- finished /sitemap.xml file");

};

let _stream_bingsiteauth = function() { return _assemble_bingsiteauth_string(); };

let _stream_sitemap = function() { return _assemble_sitemap_string(); }; let _stream_robots_txt = function() { return _assemble_robots_txt_string(); }; // ----------------------------------------------------------------- return { // these are the public methods of Misc init: function() { },

    write_sitemap: function() {
        return _write_sitemap();
    },
    stream_sitemap: function() {
        return _stream_sitemap();
    },
    stream_robots_txt: function() {
        return _stream_robots_txt();
    },
    stream_bingsiteauth: function() {
        return _stream_bingsiteauth();
    },
}

}();