Compare commits
31 Commits
Author | SHA1 | Date | |
---|---|---|---|
cc5f93305b | |||
67052baad0 | |||
08fc62d06f | |||
975e463fd6 | |||
3bc93871f9 | |||
49db495bb9 | |||
d0ee536301 | |||
344e213c41 | |||
0ae13c970e | |||
0502e7bc99 | |||
c0a425e16d | |||
90089357cf | |||
e36aed4b2b | |||
4d2aa7a0ee | |||
759fa1ff43 | |||
042caeeeb5 | |||
6ddfaffbff | |||
567507c88b | |||
35f7bdd984 | |||
602fa2b7ff | |||
b1aae3e5b9 | |||
6b381d6115 | |||
25006901d9 | |||
ee613836d1 | |||
4b53173618 | |||
46a0220273 | |||
46a6c903e1 | |||
134151eb70 | |||
ac1c3ac53d | |||
09c23d04f4 | |||
3f359cb58c |
22657
package-lock.json
generated
22657
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
20
package.json
20
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartssr",
|
"name": "@pushrocks/smartssr",
|
||||||
"version": "1.0.19",
|
"version": "1.0.35",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "a smart server side renderer supporting shadow dom",
|
"description": "a smart server side renderer supporting shadow dom",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -10,22 +10,22 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/ --web)",
|
"test": "(tstest test/ --web)",
|
||||||
"build": "(tsbuild --web)",
|
"build": "(tsbuild --web)",
|
||||||
"format": "(gitzone format)"
|
"serve": "tsrun scripts/serve.ts"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.25",
|
"@gitzone/tsbuild": "^2.1.26",
|
||||||
"@gitzone/tstest": "^1.0.52",
|
"@gitzone/tstest": "^1.0.54",
|
||||||
"@pushrocks/smartserve": "^1.1.41",
|
"@pushrocks/smartserve": "^1.1.41",
|
||||||
"@pushrocks/tapbundle": "^3.2.9",
|
"@pushrocks/tapbundle": "^3.2.14",
|
||||||
"@types/node": "^14.14.20",
|
"@types/node": "^16.6.1",
|
||||||
"tslint": "^6.1.3",
|
"tslint": "^6.1.3",
|
||||||
"tslint-config-prettier": "^1.15.0"
|
"tslint-config-prettier": "^1.15.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartdelay": "^2.0.10",
|
"@pushrocks/smartdelay": "^2.0.13",
|
||||||
"@pushrocks/smartfile": "^8.0.8",
|
"@pushrocks/smartfile": "^8.0.10",
|
||||||
"@pushrocks/smartpromise": "^3.1.3",
|
"@pushrocks/smartpromise": "^3.1.6",
|
||||||
"@pushrocks/smartpuppeteer": "^1.0.19",
|
"@pushrocks/smartpuppeteer": "^1.0.27",
|
||||||
"@pushrocks/smarttime": "^3.0.38"
|
"@pushrocks/smarttime": "^3.0.38"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
|
17
test/test.ts
17
test/test.ts
@ -9,26 +9,17 @@ tap.test('should create a valid smartssr instance', async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should start the smartssr instance', async () => {
|
tap.test('should render central.eu', async (tools) => {
|
||||||
await testSSRInstance.start();
|
await testSSRInstance.renderPage('https://lossless.com');
|
||||||
});
|
|
||||||
|
|
||||||
tap.skip.test('should render central.eu', async (tools) => {
|
|
||||||
await testSSRInstance.renderPage(
|
|
||||||
'https://central.eu/article/5e76873b9cf69b7bf6bc78bc/Introducing%3A%20central.eu'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should render lossless.com', async () => {
|
tap.test('should render lossless.com', async () => {
|
||||||
await testSSRInstance.renderPage('https://lossless.com');
|
await testSSRInstance.renderPage('https://lossless.com');
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.skip.test('should render https://lossless.gmbh', async () => {
|
tap.test('should render https://lossless.gmbh', async () => {
|
||||||
const renderedPage = await testSSRInstance.renderPage('https://lossless.gmbh');
|
const renderedPage = await testSSRInstance.renderPage('https://lossless.gmbh');
|
||||||
});
|
console.log(renderedPage);
|
||||||
|
|
||||||
tap.test('should stop the smartssr instance', async () => {
|
|
||||||
await testSSRInstance.stop();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -11,7 +11,6 @@ export interface ISmartSSROptions {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export class SmartSSR {
|
export class SmartSSR {
|
||||||
public browser: plugins.smartpuppeteer.IncognitoBrowser;
|
|
||||||
public options: ISmartSSROptions;
|
public options: ISmartSSROptions;
|
||||||
|
|
||||||
constructor(optionsArg?: ISmartSSROptions) {
|
constructor(optionsArg?: ISmartSSROptions) {
|
||||||
@ -23,55 +22,57 @@ export class SmartSSR {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public async start() {
|
|
||||||
this.browser = new plugins.smartpuppeteer.IncognitoBrowser();
|
|
||||||
await this.browser.start();
|
|
||||||
}
|
|
||||||
public async stop() {
|
|
||||||
if (this.browser) {
|
|
||||||
await plugins.smartdelay.delayFor(3000);
|
|
||||||
await this.browser.stop();
|
|
||||||
this.browser = null;
|
|
||||||
} else {
|
|
||||||
console.log('browser was not in started mode');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async renderPage(urlArg: string) {
|
public async renderPage(urlArg: string) {
|
||||||
const overallTimeMeasurement = new plugins.smarttime.HrtMeasurement();
|
const overallTimeMeasurement = new plugins.smarttime.HrtMeasurement();
|
||||||
overallTimeMeasurement.start();
|
overallTimeMeasurement.start();
|
||||||
const resultDeferred = plugins.smartpromise.defer<string>();
|
const resultDeferred = plugins.smartpromise.defer<string>();
|
||||||
const context = await this.browser.getNewIncognitoContext();
|
const browser = new plugins.smartpuppeteer.IncognitoBrowser();
|
||||||
const page = await context.newPage();
|
|
||||||
page.on('console', (msg) => {
|
|
||||||
console.log(`${urlArg}: ${msg.text()}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
const renderTimeMeasurement = new plugins.smarttime.HrtMeasurement();
|
const renderTimeMeasurement = new plugins.smarttime.HrtMeasurement();
|
||||||
renderTimeMeasurement.start();
|
let renderedPageString: string;
|
||||||
await page.goto(urlArg, {
|
|
||||||
waitUntil: 'networkidle2',
|
|
||||||
timeout: 0
|
|
||||||
});
|
|
||||||
|
|
||||||
let screenshotBuffer: Buffer;
|
let screenshotBuffer: Buffer;
|
||||||
|
await browser.start();
|
||||||
|
try {
|
||||||
|
const context = await browser.getNewIncognitoContext();
|
||||||
|
const page = await context.newPage();
|
||||||
|
|
||||||
if (this.options.debug) {
|
// lets protect against left open tabs
|
||||||
screenshotBuffer = await page.screenshot({
|
plugins.smartdelay.delayFor(30000).then(async () => {
|
||||||
encoding: 'binary',
|
try {
|
||||||
|
await browser.stop();
|
||||||
|
} catch {}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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',
|
||||||
|
}) as Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 browser.stop();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
await browser.stop();
|
||||||
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
|
|
||||||
context.close();
|
|
||||||
|
|
||||||
overallTimeMeasurement.stop();
|
overallTimeMeasurement.stop();
|
||||||
console.log(
|
console.log(
|
||||||
@ -91,6 +92,6 @@ export class SmartSSR {
|
|||||||
fs.writeFileSync(plugins.path.join(paths.noGitDir, 'test.png'), screenshotBuffer);
|
fs.writeFileSync(plugins.path.join(paths.noGitDir, 'test.png'), screenshotBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return resultDeferred.promise;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
declare var document: Document;
|
declare var document: Document;
|
||||||
export function serializeFunction(rootNode) {
|
export function serializeFunction(rootNode: Node) {
|
||||||
const uuidv4 = () => {
|
const uuidv4 = () => {
|
||||||
return 'unixxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
return 'unixxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
||||||
const r = (Math.random() * 16) | 0;
|
const r = (Math.random() * 16) | 0;
|
||||||
@ -26,7 +26,7 @@ export function serializeFunction(rootNode) {
|
|||||||
};
|
};
|
||||||
const loopProtection: any[] = [];
|
const loopProtection: any[] = [];
|
||||||
|
|
||||||
function serializeNode(nodeArg: HTMLElement, logThis = false) {
|
function serializeNode(nodeArg: HTMLElement | any, logThis = false) {
|
||||||
if (loopProtection.includes(nodeArg)) {
|
if (loopProtection.includes(nodeArg)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -39,16 +39,18 @@ export function serializeFunction(rootNode) {
|
|||||||
const nodeUUID = uuidv4();
|
const nodeUUID = uuidv4();
|
||||||
|
|
||||||
nodeArg.classList.add(nodeUUID);
|
nodeArg.classList.add(nodeUUID);
|
||||||
|
|
||||||
|
// find all slots
|
||||||
const slots = nodeArg.shadowRoot.querySelectorAll('slot');
|
const slots = nodeArg.shadowRoot.querySelectorAll('slot');
|
||||||
|
|
||||||
// handle slot element
|
// handle slot element
|
||||||
const slotsForMove: HTMLSlotElement[] = [];
|
const slotsForMove: HTMLSlotElement[] = [];
|
||||||
slots.forEach((slot) => {
|
slots.forEach((slot: any) => {
|
||||||
slotsForMove.push(slot);
|
slotsForMove.push(slot);
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const slot of slotsForMove) {
|
for (const slot of slotsForMove) {
|
||||||
const slottedLightNodesForMove = [];
|
const slottedLightNodesForMove: any[] = [];
|
||||||
slot.assignedNodes().forEach((lightNode) => slottedLightNodesForMove.push(lightNode));
|
slot.assignedNodes().forEach((lightNode) => slottedLightNodesForMove.push(lightNode));
|
||||||
slottedLightNodesForMove.forEach((lightNode) =>
|
slottedLightNodesForMove.forEach((lightNode) =>
|
||||||
slot.parentNode.insertBefore(lightNode, slot)
|
slot.parentNode.insertBefore(lightNode, slot)
|
||||||
@ -59,7 +61,20 @@ export function serializeFunction(rootNode) {
|
|||||||
const childNodes = nodeArg.shadowRoot.childNodes;
|
const childNodes = nodeArg.shadowRoot.childNodes;
|
||||||
// tslint:disable-next-line: prefer-for-of
|
// tslint:disable-next-line: prefer-for-of
|
||||||
const noteForAppending: HTMLElement[] = [];
|
const noteForAppending: HTMLElement[] = [];
|
||||||
childNodes.forEach((childNode) => {
|
|
||||||
|
// lets care about static css first
|
||||||
|
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);
|
||||||
|
noteForAppending.push(styleTag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
childNodes.forEach((childNode: ChildNode) => {
|
||||||
if (childNode instanceof HTMLElement) {
|
if (childNode instanceof HTMLElement) {
|
||||||
if (childNode.tagName === 'STYLE') {
|
if (childNode.tagName === 'STYLE') {
|
||||||
childNode.textContent = prependCss(nodeUUID, childNode.textContent);
|
childNode.textContent = prependCss(nodeUUID, childNode.textContent);
|
||||||
@ -69,6 +84,9 @@ export function serializeFunction(rootNode) {
|
|||||||
noteForAppending.push(childNode);
|
noteForAppending.push(childNode);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
while (nodeArg.firstChild) {
|
||||||
|
nodeArg.removeChild(nodeArg.firstChild);
|
||||||
|
}
|
||||||
noteForAppending.forEach((childNode) => {
|
noteForAppending.forEach((childNode) => {
|
||||||
nodeArg.append(childNode);
|
nodeArg.append(childNode);
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user