Skip to content

Commit

Permalink
feat(chrome): add configurable timeout status code
Browse files Browse the repository at this point in the history
Closes: 418/1
  • Loading branch information
Nandor Borics committed Apr 28, 2021
1 parent 9b68999 commit 9997284
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
.elasticbeanstalk/
.ebextensions/
.idea/
.vscode/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## 5.9.0 - 2021-04-28
### Added
- added `timeoutStatusCode` to `tab.prerender` and to `this.options`. We are returning with this status code if a page won't load in `pageLoadTimeout`.

## 5.8.0 - 2020-07-27
### Added
- added `timeSpentConnectingToBrowser`, `timeSpentOpeningTab`, `timeSpentLoadingUrl`, `timeSpentParsingPage`, `timeUntilError` to `req.prerender` to allow for debugging of certain issues with the server spending too much time in different lifecycle sections
Expand Down
8 changes: 7 additions & 1 deletion lib/browsers/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ chrome.setUpEvents = function(tab) {

tab.prerender.pageLoadInfo.logEntries.push({
...message,
// to keep consistent with Log.LogEntry
// to keep consistent with Log.LogEntry
lineNumber: message.line,
timestamp: new Date().getTime()
});
Expand Down Expand Up @@ -440,6 +440,12 @@ chrome.loadUrlThenWaitForPageLoadEvent = function(tab, url) {
if (!finished) {
finished = true;
util.log('page timed out', tab.prerender.url);

const timeoutStatusCode = tab.prerender.timeoutStatusCode || this.options.timeoutStatusCode;
if (timeoutStatusCode) {
tab.prerender.statusCode = timeoutStatusCode;
}

resolve();
}
}, pageLoadTimeout);
Expand Down
4 changes: 3 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const util = require('./util.js');
const zlib = require('zlib');
const validUrl = require('valid-url');

const WAIT_AFTER_LAST_REQUEST = process.env.WAIT_AFTER_LAST_REQUEST || 500;
Expand All @@ -21,6 +20,8 @@ const BROWSER_TRY_RESTART_PERIOD = process.env.BROWSER_TRY_RESTART_PERIOD || 600

const BROWSER_DEBUGGING_PORT = process.env.BROWSER_DEBUGGING_PORT || 9222;

const TIMEOUT_STATUS_CODE = process.env.TIMEOUT_STATUS_CODE

const server = exports = module.exports = {};


Expand All @@ -40,6 +41,7 @@ server.init = function(options) {
printBackground: true
};
this.options.browserDebuggingPort = this.options.browserDebuggingPort || BROWSER_DEBUGGING_PORT;
this.options.timeoutStatusCode = this.options.timeoutStatusCode || TIMEOUT_STATUS_CODE;

this.browser = require('./browsers/chrome');

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Prerender.io",
"name": "prerender",
"description": "Service to prerender Javascript rendered pages for SEO",
"version": "5.8.4",
"version": "5.9.0",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -22,7 +22,7 @@
},
"scripts": {
"start": "node server.js",
"test": "./node_modules/.bin/mocha"
"test": "./node_modules/.bin/mocha ./test/**/*-spec.js"
},
"engines": {
"node": ">=10"
Expand Down
52 changes: 52 additions & 0 deletions test/browsers/chrome-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const chrome = require("../../lib/browsers/chrome"),
sinon = require("sinon"),
assert = require("assert");

describe("chrome", function() {
describe("loadUrlThenWaitForPageLoadEvent", function() {
let tab;
let sandbox;

beforeEach(function() {
sandbox = sinon.createSandbox();

tab = sandbox.stub();
tab.prerender = sandbox.stub();
tab.prerender.pageDoneCheckInterval = 1000;
tab.prerender.pageLoadTimeout = 1;

tab.Page = sandbox.stub();
tab.Page.enable = sandbox.stub();
tab.Page.enable.resolves(1);
tab.Page.addScriptToEvaluateOnNewDocument = sandbox.stub();
tab.Page.navigate = sandbox.stub();
tab.Page.navigate.resolves(1);

tab.Emulation = sandbox.stub();
tab.Emulation.setDeviceMetricsOverride = sandbox.stub();
});

afterEach(function() {
sandbox.restore();
});

it("Should NOT change tabs status code", async function() {
const expectedStatusCode = 123;
tab.prerender.statusCode = expectedStatusCode;

await chrome.loadUrlThenWaitForPageLoadEvent(tab, "the-url");

assert.strictEqual(tab.prerender.statusCode, expectedStatusCode);
});

it("Should change tabs status code to the predefined value", async function() {
const expectedStatusCode = 222;
tab.prerender.statusCode = 111;
tab.prerender.timeoutStatusCode = expectedStatusCode;

await chrome.loadUrlThenWaitForPageLoadEvent(tab, "the-url");

assert.strictEqual(tab.prerender.statusCode, expectedStatusCode);
});
});
});

0 comments on commit 9997284

Please sign in to comment.