Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/puppeteer timeout #503

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ Puppeteer (Headless Chrome) may fail due to sandboxing issues. To get around thi
you may use:

```json
"puppeteerArgs": ["--no-sandbox", "--disable-setuid-sandbox"]
"puppeteer": {
"args": ["--no-sandbox", "--disable-setuid-sandbox"]
}
```

Read more about [puppeteer troubleshooting](https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md).
Expand Down
28 changes: 16 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ const defaultOptions = {
concurrency: 4,
include: ["/"],
userAgent: "ReactSnap",
// 4 params below will be refactored to one: `puppeteer: {}`
// https://github.com/stereobooster/react-snap/issues/120
headless: true,
puppeteer: {
cache: true
args: [],
cache: true,
headless: true,
executablePath: undefined,
ignoreHTTPSErrors: false,
handleSIGINT: false,
timeout: 2 * 60 * 1000 // 2mins
},
puppeteerArgs: [],
puppeteerExecutablePath: undefined,
puppeteerIgnoreHTTPSErrors: false,
publicPath: "/",
minifyCss: {},
minifyHtml: {
Expand Down Expand Up @@ -82,10 +82,13 @@ const defaultOptions = {
* @param {{source: ?string, destination: ?string, include: ?Array<string>, sourceMaps: ?boolean, skipThirdPartyRequests: ?boolean }} userOptions
* @return {*}
*/
const defaults = userOptions => {
const defaults = (userOptions = {}) => {
const puppeteer = { ...defaultOptions.puppeteer, ...userOptions.puppeteer };

const options = {
...defaultOptions,
...userOptions
...userOptions,
puppeteer
};
options.destination = options.destination || options.source;

Expand Down Expand Up @@ -514,8 +517,9 @@ const fixParcelChunksIssue = ({
}) => {
return page.evaluate(
(basePath, http2PushManifest, inlineCss) => {
const localScripts = Array.from(document.scripts)
.filter(x => x.src && x.src.startsWith(basePath))
const localScripts = Array.from(document.scripts).filter(
x => x.src && x.src.startsWith(basePath)
);

const mainRegexp = /main\.[\w]{8}\.js/;
const mainScript = localScripts.find(x => mainRegexp.test(x.src));
Expand Down Expand Up @@ -840,7 +844,7 @@ const run = async (userOptions, { fs } = { fs: nativeFs }) => {
);
routePath = normalizePath(routePath);
if (routePath !== newPath) {
console.log(newPath)
console.log(newPath);
console.log(`💬 in browser redirect (${newPath})`);
addToQueue(newRoute);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"html-minifier": "4.0.0",
"minimalcss": "0.8.2",
"mkdirp": "0.5.1",
"puppeteer": "^1.8.0",
"puppeteer": "^5.3.1",
"serve-static": "1.14.1",
"sourcemapped-stacktrace-node": "2.1.8"
},
Expand Down
12 changes: 6 additions & 6 deletions run.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const {
const publicUrl = process.env.PUBLIC_URL || homepage;

const reactScriptsVersion = parseInt(
(devDependencies && devDependencies["react-scripts"])
|| (dependencies && dependencies["react-scripts"])
(devDependencies && devDependencies["react-scripts"]) ||
(dependencies && dependencies["react-scripts"])
);
let fixWebpackChunksIssue;
switch (reactScriptsVersion) {
Expand All @@ -26,13 +26,13 @@ switch (reactScriptsVersion) {
}

const parcel = Boolean(
(devDependencies && devDependencies["parcel-bundler"])
|| (dependencies && dependencies["parcel-bundler"])
(devDependencies && devDependencies["parcel-bundler"]) ||
(dependencies && dependencies["parcel-bundler"])
);

if (parcel) {
if (fixWebpackChunksIssue) {
console.log("Detected both Parcel and CRA. Fixing chunk names for CRA!")
console.log("Detected both Parcel and CRA. Fixing chunk names for CRA!");
} else {
fixWebpackChunksIssue = "Parcel";
}
Expand All @@ -45,4 +45,4 @@ run({
}).catch(error => {
console.error(error);
process.exit(1);
});
});
36 changes: 20 additions & 16 deletions src/puppeteer_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,16 @@ const enableLogging = opt => {
const getLinks = async opt => {
const { page } = opt;
const anchors = await page.evaluate(() =>
Array.from(document.querySelectorAll("a,link[rel='alternate']")).map(anchor => {
if (anchor.href.baseVal) {
const a = document.createElement("a");
a.href = anchor.href.baseVal;
return a.href;
Array.from(document.querySelectorAll("a,link[rel='alternate']")).map(
anchor => {
if (anchor.href.baseVal) {
const a = document.createElement("a");
a.href = anchor.href.baseVal;
return a.href;
}
return anchor.href;
}
return anchor.href;
})
)
);

const iframes = await page.evaluate(() =>
Expand Down Expand Up @@ -184,7 +186,12 @@ const crawl = async opt => {
// Port can be null, therefore we need the null check
const isOnAppPort = port && port.toString() === options.port.toString();

if (hostname === "localhost" && isOnAppPort && !uniqueUrls.has(newUrl) && !streamClosed) {
if (
hostname === "localhost" &&
isOnAppPort &&
!uniqueUrls.has(newUrl) &&
!streamClosed
) {
uniqueUrls.add(newUrl);
enqued++;
queue.write(newUrl);
Expand All @@ -194,13 +201,7 @@ const crawl = async opt => {
}
};

const browser = await puppeteer.launch({
headless: options.headless,
args: options.puppeteerArgs,
executablePath: options.puppeteerExecutablePath,
ignoreHTTPSErrors: options.puppeteerIgnoreHTTPSErrors,
handleSIGINT: false
});
const browser = await puppeteer.launch(options.puppeteer);

/**
* @param {string} pageUrl
Expand Down Expand Up @@ -238,7 +239,10 @@ const crawl = async opt => {
await page.setUserAgent(options.userAgent);
const tracker = createTracker(page);
try {
await page.goto(pageUrl, { waitUntil: "networkidle0" });
await page.goto(pageUrl, {
timeout: options.puppeteer.timeout,
waitUntil: "networkidle0"
});
} catch (e) {
e.message = augmentTimeoutError(e.message, tracker);
throw e;
Expand Down
10 changes: 6 additions & 4 deletions tests/__snapshots__/defaultOptions.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Object {
"externalServer": false,
"fixInsertRule": true,
"fixWebpackChunksIssue": "CRA1",
"headless": true,
"http2PushManifest": false,
"ignoreForPreload": Array [
"service-worker.js",
Expand All @@ -33,11 +32,14 @@ Object {
"preloadImages": false,
"publicPath": "/",
"puppeteer": Object {
"args": Array [],
"cache": true,
"executablePath": undefined,
"handleSIGINT": false,
"headless": true,
"ignoreHTTPSErrors": false,
"timeout": 120000,
},
"puppeteerArgs": Array [],
"puppeteerExecutablePath": undefined,
"puppeteerIgnoreHTTPSErrors": false,
"removeBlobs": true,
"removeScriptTags": false,
"removeStyleTags": false,
Expand Down
2 changes: 1 addition & 1 deletion tests/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const snapRun = (fs, options) =>
run(
{
// for Travis CI
puppeteerArgs: ["--no-sandbox", "--disable-setuid-sandbox"],
puppeteer: { args: ["--no-sandbox", "--disable-setuid-sandbox"] },
// sometimes web server from previous test have not enough time to shut down
// as a result you get `Error: listen EADDRINUSE :::45678`
// to prevent this we use random port
Expand Down
Loading