zipfworks/ember-prerender

View on GitHub
lib/plugins/httpHeaders.js

Summary

Maintainability
B
4 hrs
Test Coverage
module.exports = {
  beforeSend: function(req, res, page, next) {
    if (page.html) {
      var headerMatch = /<meta property=['"]prerender:([^'"]+)['"] content=['"]([^'"]+)['"]>/g;
      var head = page.html.split('</head>', 1).pop();
      var match;

      while ((match = headerMatch.exec(head))) {
        switch (match[1]) {
          case 'status-code':
            page.statusCode = parseInt(match[2], 10);
            if (page.statusCode == 301 || page.statusCode == 302) {
              page.html = 'Moved';
            }
            break;
          case 'header':
            var pos = match[2].indexOf(': ');
            if (pos > 0) {
              var headerName = match[2].slice(0, pos);
              var headerValue = match[2].slice(pos+2);
              if (headerName === 'Location') {
                headerValue = decodeURIComponent(headerValue);
              }
              res.setHeader(headerName, headerValue);
            }
            break;
        }
      }
    }

    next();
  }
};