From feb023fc00b5213c60d1dca013409b1ceb07d778 Mon Sep 17 00:00:00 2001 From: Bogdan Cirlig Date: Fri, 19 Dec 2014 10:43:19 -0800 Subject: [PATCH 1/3] Added support to stop next middleware processing upon options.stopNext=true. Useful when same KOA website serves regular requests and also handles the pre-rendering interception --- README.md | 1 + index.js | 4 +++- package.json | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 614933c..8340009 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ var options = { protocol: 'http', // optional, default: this.protocol host: 'www.risingstack.com' // optional, default: this.host, prerenderToken: '' // optional or process.env.PRERENDER_TOKEN + stopNext: true // optional, if true it will not allow next middleware to continue and instead return the response. }; // Use as middleware diff --git a/index.js b/index.js index ef35b1a..cace8f0 100644 --- a/index.js +++ b/index.js @@ -167,7 +167,9 @@ module.exports = function preRenderMiddleware (options) { body = response[1] || ''; - yield* next; + if (!options.stopNext) { + yield * next; + } this.body = body.toString(); this.set('X-Prerender', 'true'); diff --git a/package.json b/package.json index c021250..5035dc2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "koa-prerender", - "version": "1.0.2", + "version": "1.0.3", "description": "", "main": "index.js", "scripts": { From 255bae6477ef478618e54dd12156ae65a4c6a42a Mon Sep 17 00:00:00 2001 From: Bogdan Cirlig Date: Fri, 19 Dec 2014 12:10:11 -0800 Subject: [PATCH 2/3] fixed a bug when there is no userAgent header the code crashes --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index cace8f0..3e72dcb 100644 --- a/index.js +++ b/index.js @@ -82,10 +82,6 @@ function shouldPreRender (options) { return options.url.indexOf(extension) !== -1; }); - var isBot = crawlerUserAgents.some(function (crawlerUserAgent) { - return options.userAgent.toLowerCase().indexOf(crawlerUserAgent.toLowerCase()) !== -1; - }); - // do not pre-rend when: if (!options.userAgent) { return false; @@ -95,6 +91,10 @@ function shouldPreRender (options) { return false; } + var isBot = crawlerUserAgents.some(function (crawlerUserAgent) { + return options.userAgent.toLowerCase().indexOf(crawlerUserAgent.toLowerCase()) !== -1; + }); + if (hasExtensionToIgnore) { return false; } From dcc94fef791c01492f6786e7f4008342117c26e2 Mon Sep 17 00:00:00 2001 From: Bogdan Cirlig Date: Fri, 19 Dec 2014 12:10:33 -0800 Subject: [PATCH 3/3] version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5035dc2..873936c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "koa-prerender", - "version": "1.0.3", + "version": "1.0.4", "description": "", "main": "index.js", "scripts": {