From c0a425e16d17daf9231156fc7b7c94d331c25a85 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Mon, 16 Aug 2021 16:26:13 +0200 Subject: [PATCH] fix(core): update --- .gitlab-ci.yml | 1 - package.json | 2 +- ts/smartssr.classes.smartssr.ts | 87 +++++++++++++++++-------------- ts/smartssr.function.serialize.ts | 5 +- 4 files changed, 52 insertions(+), 43 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 88889cf..9a4467e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -36,7 +36,6 @@ auditProductionDependencies: - npmci command npm audit --audit-level=high --only=prod --production tags: - docker - allow_failure: true auditDevDependencies: image: registry.gitlab.com/hosttoday/ht-docker-node:npmci diff --git a/package.json b/package.json index f40e67d..af41a44 100644 --- a/package.json +++ b/package.json @@ -43,4 +43,4 @@ "browserslist": [ "last 1 chrome versions" ] -} +} \ No newline at end of file diff --git a/ts/smartssr.classes.smartssr.ts b/ts/smartssr.classes.smartssr.ts index a39793a..a2feccc 100644 --- a/ts/smartssr.classes.smartssr.ts +++ b/ts/smartssr.classes.smartssr.ts @@ -27,50 +27,57 @@ export class SmartSSR { overallTimeMeasurement.start(); const resultDeferred = plugins.smartpromise.defer(); const browser = new plugins.smartpuppeteer.IncognitoBrowser(); - await browser.start(); - const context = await browser.getNewIncognitoContext(); - const page = await context.newPage(); - - // lets protext against left open tabs - plugins.smartdelay.delayFor(30000).then(() => { - if (!page.isClosed) { - page.close(); - context.close(); - throw new Error(`failed to render ${urlArg}`); - } - - }); - - page.on('console', (msg) => { - console.log(`${urlArg}: ${msg.text()}`); - }); - const renderTimeMeasurement = new plugins.smarttime.HrtMeasurement(); - renderTimeMeasurement.start(); - await page.goto(urlArg, { - waitUntil: 'networkidle2', - timeout: 30000, - }); - + let renderedPageString: string; let screenshotBuffer: Buffer; + await browser.start(); + try { + const context = await browser.getNewIncognitoContext(); + const page = await context.newPage(); - if (this.options.debug) { - screenshotBuffer = await page.screenshot({ - encoding: 'binary', + // lets protect against left open tabs + plugins.smartdelay.delayFor(30000).then(async () => { + if (!page.isClosed) { + try { + await page.close(); + await context.close(); + } catch (e) { + console.log(e); + } finally { + await browser.stop(); + } + throw new Error(`failed to render ${urlArg}`); + } }); + + page.on('console', (msg) => { + console.log(`${urlArg}: ${msg.text()}`); + }); + + renderTimeMeasurement.start(); + await page.goto(urlArg, { + waitUntil: 'networkidle2', + timeout: 30000, + }); + + if (this.options.debug) { + screenshotBuffer = await page.screenshot({ + encoding: 'binary', + }); + } + + await page.$eval('body', serializeFunction); + const pageContent = await page.content(); + renderedPageString = pageContent; + resultDeferred.resolve(renderedPageString); + renderTimeMeasurement.stop(); + + // lets clean up async + await page.close(); + await context.close(); + } catch (e) { + console.log(e); } - - await page.$eval('body', serializeFunction); - const pageContent = await page.content(); - const renderedPageString = pageContent; - resultDeferred.resolve(renderedPageString); - - const result = await resultDeferred.promise; - renderTimeMeasurement.stop(); - - // lets clean up async - await page.close(); - await context.close(); await browser.stop(); overallTimeMeasurement.stop(); @@ -91,6 +98,6 @@ export class SmartSSR { fs.writeFileSync(plugins.path.join(paths.noGitDir, 'test.png'), screenshotBuffer); } - return result; + return resultDeferred.promise; } } diff --git a/ts/smartssr.function.serialize.ts b/ts/smartssr.function.serialize.ts index ea1d02e..24b53d4 100644 --- a/ts/smartssr.function.serialize.ts +++ b/ts/smartssr.function.serialize.ts @@ -63,7 +63,10 @@ export function serializeFunction(rootNode) { const noteForAppending: HTMLElement[] = []; // lets care about static css first - if ((nodeArg.constructor as any).styles && (nodeArg.constructor as any).styles instanceof Array) { + if ( + (nodeArg.constructor as any).styles && + (nodeArg.constructor as any).styles instanceof Array + ) { for (const objectArg of (nodeArg.constructor as any).styles) { const styleTag = document.createElement('style'); styleTag.textContent = prependCss(nodeUUID, objectArg.cssText);