2020-02-12 21:31:22 +00:00
|
|
|
import * as plugins from './smartssr.plugins';
|
|
|
|
import * as paths from './smartssr.paths';
|
|
|
|
|
|
|
|
import { serializeFunction } from './smartssr.function.serialize';
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export class SmartSSR {
|
|
|
|
public browser: plugins.smartpuppeteer.puppeteer.Browser;
|
|
|
|
public async start() {
|
|
|
|
this.browser = await plugins.smartpuppeteer.getEnvAwareBrowserInstance();
|
|
|
|
}
|
|
|
|
public async stop() {
|
|
|
|
if (this.browser) {
|
|
|
|
await plugins.smartdelay.delayFor(3000);
|
|
|
|
await this.browser.close();
|
|
|
|
this.browser = null;
|
|
|
|
} else {
|
|
|
|
console.log('browser was not in started mode');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async renderPage(urlArg: string) {
|
2020-03-01 13:50:28 +00:00
|
|
|
const overallTimeMeasurement = new plugins.smarttime.HrtMeasurement();
|
|
|
|
overallTimeMeasurement.start();
|
2020-02-12 21:31:22 +00:00
|
|
|
const resultDeferred = plugins.smartpromise.defer<string>();
|
2020-02-25 07:48:54 +00:00
|
|
|
const context = await this.browser.createIncognitoBrowserContext();
|
|
|
|
const page = await context.newPage();
|
|
|
|
page.on('console', msg => {
|
|
|
|
console.log(`${urlArg}: ${msg.text()}`);
|
|
|
|
});
|
2020-02-12 21:31:22 +00:00
|
|
|
|
|
|
|
page.on('load', async (...args) => {
|
|
|
|
// await plugins.smartdelay.delayFor(2000);
|
|
|
|
await page.$eval('body', serializeFunction);
|
|
|
|
const pageContent = await page.content();
|
|
|
|
const renderedPageString = pageContent;
|
|
|
|
resultDeferred.resolve(renderedPageString);
|
2020-02-12 21:32:25 +00:00
|
|
|
plugins.smartfile.memory.toFsSync(
|
|
|
|
renderedPageString,
|
|
|
|
plugins.path.join(paths.noGitDir, 'test.html')
|
|
|
|
);
|
2020-02-12 21:31:22 +00:00
|
|
|
});
|
|
|
|
|
2020-03-01 13:50:28 +00:00
|
|
|
const renderTimeMeasurement = new plugins.smarttime.HrtMeasurement();
|
|
|
|
renderTimeMeasurement.start();
|
2020-02-12 21:31:22 +00:00
|
|
|
await page.goto(urlArg);
|
2020-02-12 21:32:25 +00:00
|
|
|
const result = await resultDeferred.promise;
|
2020-03-01 13:50:28 +00:00
|
|
|
renderTimeMeasurement.stop();
|
|
|
|
|
|
|
|
// lets clean up async
|
|
|
|
context.close();
|
|
|
|
|
|
|
|
overallTimeMeasurement.stop();
|
|
|
|
console.log(`Overall it took ${overallTimeMeasurement.milliSeconds} milliseconds to render ${urlArg}`);
|
|
|
|
console.log(`The rendering alone took ${renderTimeMeasurement.milliSeconds} milliseconds for ${urlArg}`)
|
2020-02-12 21:31:22 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|