Skip to content

Commit ace5f12

Browse files
authored
Merge pull request #820 from OpenKnowledgeMaps/bugfix/puppeteer-snapshots
snapshot bugfixes
2 parents b0bde3a + 431ecc9 commit ace5f12

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

server/services/getChartSVG.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ function timeout(ms) {
66
};
77

88
(async() => {
9-
const browser = await puppeteer.launch();
10-
const page = await browser.newPage();
11-
await page.setViewport({width: 1920, height: 1080})
12-
await page.goto(process.argv[2], {waitUntil: 'networkidle2'});
13-
await timeout(1000)
14-
await page.screenshot({path: process.argv[3], clip: { x: 0, y: 0, width: 1150, height: 1080 }});
15-
browser.close();
9+
try {
10+
const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] });
11+
await timeout(1000)
12+
const page = await browser.newPage();
13+
await timeout(1000)
14+
await page.setViewport({width: 1920, height: 1080})
15+
await page.goto(process.argv[2], {waitUntil: 'networkidle2'});
16+
await timeout(1000)
17+
await page.screenshot({path: process.argv[3], clip: { x: 0, y: 0, width: 1150, height: 1080 }});
18+
browser.close();
19+
} catch (error) {
20+
console.error('Error occurred while generating chart SVG:', error);
21+
}
1622

1723
})();

server/services/snapshot/headstart_snapshot.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,40 @@
2727
data_config.options = options_<?php echo htmlspecialchars($_GET['service']); ?>.dropdowns;
2828
}
2929
</script>
30-
<script type="text/javascript" src="../../../dist/headstart.js"></script>
31-
<link type="text/css" rel="stylesheet" href="../../../dist/headstart.css"></link>
30+
31+
<?php include "../../../dist/headstart.php"; ?>
32+
33+
<!-- The script below attempts to launch headstart by calling its start method.
34+
However, because headstart loads asynchronously, it is necessary to wait some time. -->
3235
<script type="text/javascript">
33-
headstart.start();
36+
const DELAY_BETWEEN_ATTEMPTS_MS = 250;
37+
const LIMIT_OF_ATTEMPTS = 10;
38+
let numberOfAttempts = 0;
39+
40+
function checkThatInitializeMethodReady() {
41+
return typeof headstart === "object" && typeof headstart.start === "function";
42+
}
43+
44+
function initializeHeadstart() {
45+
if (checkThatInitializeMethodReady()) {
46+
headstart.start();
47+
return;
48+
}
49+
50+
if (numberOfAttempts < LIMIT_OF_ATTEMPTS) {
51+
let timerId = setTimeout(() => {
52+
clearTimeout(timerId);
53+
numberOfAttempts += 1;
54+
initializeHeadstart();
55+
}, DELAY_BETWEEN_ATTEMPTS_MS);
56+
57+
return;
58+
}
59+
60+
console.error("Unable to load headstart or its start method is not a function!");
61+
}
62+
63+
document.addEventListener("DOMContentLoaded", initializeHeadstart);
3464
</script>
3565
</body>
3666
</html>

0 commit comments

Comments
 (0)