@dooboostore/simple-boot-http-server-ssr 1.0.15 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -0
- package/dist/cjs/SimpleBootHttpSSRServer.js.map +2 -2
- package/dist/cjs/bundle-entry.js +0 -2
- package/dist/cjs/bundle-entry.js.map +2 -2
- package/dist/cjs/filters/SSRDomParserFilter.js.map +2 -2
- package/dist/cjs/filters/{SSRLinkDomDomFilter.js → SSRLinkDomFilter.js} +7 -7
- package/dist/cjs/filters/SSRLinkDomFilter.js.map +7 -0
- package/dist/cjs/filters/SSRSimpleWebComponentDomParserFilter.js +81 -0
- package/dist/cjs/filters/SSRSimpleWebComponentDomParserFilter.js.map +7 -0
- package/dist/cjs/filters/SSRSimpleWebComponentFilter.js +158 -28
- package/dist/cjs/filters/SSRSimpleWebComponentFilter.js.map +2 -2
- package/dist/cjs/filters/index.js +9 -3
- package/dist/cjs/filters/index.js.map +2 -2
- package/dist/cjs/index.js +0 -2
- package/dist/cjs/index.js.map +2 -2
- package/dist/esm/SimpleBootHttpSSRServer.js.map +2 -2
- package/dist/esm/bundle-entry.js +0 -1
- package/dist/esm/bundle-entry.js.map +2 -2
- package/dist/esm/filters/SSRDomParserFilter.js.map +2 -2
- package/dist/esm/filters/{SSRLinkDomDomFilter.js → SSRLinkDomFilter.js} +3 -3
- package/dist/esm/filters/SSRLinkDomFilter.js.map +7 -0
- package/dist/esm/filters/SSRSimpleWebComponentDomParserFilter.js +58 -0
- package/dist/esm/filters/SSRSimpleWebComponentDomParserFilter.js.map +7 -0
- package/dist/esm/filters/SSRSimpleWebComponentFilter.js +148 -28
- package/dist/esm/filters/SSRSimpleWebComponentFilter.js.map +2 -2
- package/dist/esm/filters/index.js +4 -1
- package/dist/esm/filters/index.js.map +2 -2
- package/dist/esm/index.js +0 -1
- package/dist/esm/index.js.map +2 -2
- package/dist/esm-bundle/dooboostore-simple-boot-http-server-ssr.esm.js +54537 -21104
- package/dist/esm-bundle/dooboostore-simple-boot-http-server-ssr.esm.js.map +4 -4
- package/dist/types/SimpleBootHttpSSRServer.d.ts +1 -1
- package/dist/types/SimpleBootHttpSSRServer.d.ts.map +1 -1
- package/dist/types/bundle-entry.d.ts +0 -1
- package/dist/types/bundle-entry.d.ts.map +1 -1
- package/dist/types/filters/SSRDomParserFilter.d.ts +3 -3
- package/dist/types/filters/SSRDomParserFilter.d.ts.map +1 -1
- package/dist/types/filters/{SSRLinkDomDomFilter.d.ts → SSRLinkDomFilter.d.ts} +5 -5
- package/dist/types/filters/SSRLinkDomFilter.d.ts.map +1 -0
- package/dist/types/filters/SSRSimpleWebComponentDomParserFilter.d.ts +37 -0
- package/dist/types/filters/SSRSimpleWebComponentDomParserFilter.d.ts.map +1 -0
- package/dist/types/filters/SSRSimpleWebComponentFilter.d.ts +23 -3
- package/dist/types/filters/SSRSimpleWebComponentFilter.d.ts.map +1 -1
- package/dist/types/filters/index.d.ts +4 -1
- package/dist/types/filters/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/umd-bundle/dooboostore-simple-boot-http-server-ssr.umd.js +54592 -21160
- package/dist/umd-bundle/dooboostore-simple-boot-http-server-ssr.umd.js.map +4 -4
- package/package.json +17 -16
- package/src/SimpleBootHttpSSRServer.ts +2 -3
- package/src/bundle-entry.ts +0 -1
- package/src/filters/SSRDomParserFilter.ts +4 -4
- package/src/filters/{SSRLinkDomDomFilter.ts → SSRLinkDomFilter.ts} +3 -3
- package/src/filters/SSRSimpleWebComponentDomParserFilter.ts +106 -0
- package/src/filters/SSRSimpleWebComponentFilter.ts +242 -39
- package/src/filters/index.ts +5 -2
- package/src/index.ts +0 -1
- package/README.MD +0 -167
- package/dist/cjs/decorators/LoadAroundBefore.js +0 -54
- package/dist/cjs/decorators/LoadAroundBefore.js.map +0 -7
- package/dist/cjs/decorators/SaveAroundAfter.js +0 -53
- package/dist/cjs/decorators/SaveAroundAfter.js.map +0 -7
- package/dist/cjs/decorators/index.js +0 -24
- package/dist/cjs/decorators/index.js.map +0 -7
- package/dist/cjs/filters/SSRLinkDomDomFilter.js.map +0 -7
- package/dist/esm/decorators/LoadAroundBefore.js +0 -31
- package/dist/esm/decorators/LoadAroundBefore.js.map +0 -7
- package/dist/esm/decorators/SaveAroundAfter.js +0 -29
- package/dist/esm/decorators/SaveAroundAfter.js.map +0 -7
- package/dist/esm/decorators/index.js +0 -3
- package/dist/esm/decorators/index.js.map +0 -7
- package/dist/esm/filters/SSRLinkDomDomFilter.js.map +0 -7
- package/dist/types/decorators/LoadAroundBefore.d.ts +0 -2
- package/dist/types/decorators/LoadAroundBefore.d.ts.map +0 -1
- package/dist/types/decorators/SaveAroundAfter.d.ts +0 -3
- package/dist/types/decorators/SaveAroundAfter.d.ts.map +0 -1
- package/dist/types/decorators/index.d.ts +0 -3
- package/dist/types/decorators/index.d.ts.map +0 -1
- package/dist/types/filters/SSRLinkDomDomFilter.d.ts.map +0 -1
- package/src/decorators/LoadAroundBefore.ts +0 -28
- package/src/decorators/SaveAroundAfter.ts +0 -27
- package/src/decorators/index.ts +0 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dooboostore/simple-boot-http-server-ssr",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.16",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -70,7 +70,6 @@
|
|
|
70
70
|
"reflect-metadata": "^0.2.2"
|
|
71
71
|
},
|
|
72
72
|
"devDependencies": {
|
|
73
|
-
"@types/jsdom": "^21.1.7",
|
|
74
73
|
"@babel/core": "^7.24.0",
|
|
75
74
|
"@babel/plugin-proposal-class-properties": "^7.18.6",
|
|
76
75
|
"@babel/plugin-proposal-decorators": "^7.24.0",
|
|
@@ -78,28 +77,30 @@
|
|
|
78
77
|
"@babel/preset-env": "^7.24.0",
|
|
79
78
|
"@babel/preset-typescript": "^7.24.0",
|
|
80
79
|
"@types/jest": "^26.0.22",
|
|
81
|
-
"
|
|
80
|
+
"@types/jsdom": "^21.1.7",
|
|
81
|
+
"@types/node": "^20",
|
|
82
|
+
"@types/node-gzip": "^1.1.0",
|
|
82
83
|
"@typescript-eslint/eslint-plugin": "^4.16.1",
|
|
83
84
|
"@typescript-eslint/parser": "^4.16.1",
|
|
85
|
+
"esbuild": "^0.23.0",
|
|
86
|
+
"esbuild-plugin-tsc": "^0.5.0",
|
|
87
|
+
"raw-loader": "^4.0.2",
|
|
84
88
|
"tslib": "^2.8.1",
|
|
85
|
-
"@types/node": "^20",
|
|
86
89
|
"typescript": "^5.9.2",
|
|
87
|
-
"@
|
|
88
|
-
"
|
|
89
|
-
"
|
|
90
|
-
"@dooboostore/
|
|
91
|
-
"@dooboostore/
|
|
92
|
-
"@dooboostore/core-web": "1.0.10",
|
|
93
|
-
"@dooboostore/simple-web-component": "1.0.20",
|
|
90
|
+
"@dooboostore/core": "1.0.32",
|
|
91
|
+
"@dooboostore/core-web": "1.0.12",
|
|
92
|
+
"@dooboostore/simple-boot": "1.0.56",
|
|
93
|
+
"@dooboostore/dom-render": "1.0.113",
|
|
94
|
+
"@dooboostore/dom-parser": "1.0.7",
|
|
94
95
|
"@dooboostore/simple-boot-front": "1.0.131",
|
|
95
|
-
"@dooboostore/simple-boot-http-server": "1.0.
|
|
96
|
-
"@dooboostore/
|
|
97
|
-
"@dooboostore/
|
|
98
|
-
"@dooboostore/dom-render": "1.0.113"
|
|
96
|
+
"@dooboostore/simple-boot-http-server": "1.0.25",
|
|
97
|
+
"@dooboostore/simple-web-component": "1.0.37",
|
|
98
|
+
"@dooboostore/core-node": "1.0.12"
|
|
99
99
|
},
|
|
100
100
|
"dependencies": {
|
|
101
101
|
"jsdom": "^26.0.0",
|
|
102
|
-
"linkedom": "0.18.12"
|
|
102
|
+
"linkedom": "0.18.12",
|
|
103
|
+
"playwright": "^1.59.1"
|
|
103
104
|
},
|
|
104
105
|
"scripts": {
|
|
105
106
|
"clean": "rm -rf dist",
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { SimpleBootHttpServer } from '@dooboostore/simple-boot-http-server/SimpleBootHttpServer';
|
|
2
|
-
// import { SimstanceManager } from '@dooboostore/simple-boot/simstance/SimstanceManager';
|
|
3
2
|
import { HttpSSRServerOption } from './option/HttpSSRServerOption';
|
|
4
3
|
import { ConstructorType } from '@dooboostore/core';
|
|
5
4
|
import {SimConfig} from "@dooboostore/simple-boot/decorators/SimDecorator";
|
|
@@ -9,8 +8,8 @@ export class SimpleBootHttpSSRServer extends SimpleBootHttpServer {
|
|
|
9
8
|
super(option)
|
|
10
9
|
}
|
|
11
10
|
|
|
12
|
-
run(otherInstanceSim?: Map<ConstructorType<any> | Function | SimConfig |
|
|
13
|
-
const oi = new Map<ConstructorType<any> | Function | SimConfig |
|
|
11
|
+
run(otherInstanceSim?: Map<ConstructorType<any> | Function | SimConfig | symbol, any>) {
|
|
12
|
+
const oi = new Map<ConstructorType<any> | Function | SimConfig | symbol, any>();
|
|
14
13
|
oi.set(SimpleBootHttpSSRServer, this);
|
|
15
14
|
otherInstanceSim?.forEach((value, key) => oi.set(key, value));
|
|
16
15
|
return super.run(oi);
|
package/src/bundle-entry.ts
CHANGED
|
@@ -16,7 +16,7 @@ import { DomParserInitializer } from '../initializers/DomParserInitializer';
|
|
|
16
16
|
import { JsdomInitializer } from '../initializers';
|
|
17
17
|
import { SimConfig } from '@dooboostore/simple-boot/decorators/SimDecorator';
|
|
18
18
|
|
|
19
|
-
export type
|
|
19
|
+
export type DomParserFactoryAndParams = {
|
|
20
20
|
frontDistPath: string;
|
|
21
21
|
frontDistIndexFileName?: string;
|
|
22
22
|
factorySimFrontOption: (window: any) => SimFrontOption;
|
|
@@ -43,7 +43,7 @@ export class SSRDomParserFilter implements Filter {
|
|
|
43
43
|
// private intervalId?: NodeJS.Timeout;
|
|
44
44
|
|
|
45
45
|
constructor(
|
|
46
|
-
public config:
|
|
46
|
+
public config: DomParserFactoryAndParams,
|
|
47
47
|
public otherInstanceSim?: Map<ConstructorType<any> | Function | SimConfig | Symbol, any>
|
|
48
48
|
) {
|
|
49
49
|
config.frontDistIndexFileName = config.frontDistIndexFileName || 'index.html';
|
|
@@ -97,10 +97,10 @@ export class SSRDomParserFilter implements Filter {
|
|
|
97
97
|
const option = this.config.factorySimFrontOption(DomRenderProxy.final(window));
|
|
98
98
|
const domExcludes = [RequestResponse, ...(this.config.domExcludes || [])];
|
|
99
99
|
const simpleBootFront = await this.config.factory.create(option, this.config.using, domExcludes);
|
|
100
|
-
otherInstanceSim ??= new Map<Function | ConstructorType<any> | SimConfig |
|
|
100
|
+
otherInstanceSim ??= new Map<Function | ConstructorType<any> | SimConfig | symbol, any>();
|
|
101
101
|
this.otherInstanceSim?.forEach((v, k) => otherInstanceSim!.set(k, v));
|
|
102
102
|
|
|
103
|
-
simpleBootFront.run(otherInstanceSim);
|
|
103
|
+
simpleBootFront.run(otherInstanceSim as any);
|
|
104
104
|
// (simpleBootFront as any).generation = this.poolGeneration;
|
|
105
105
|
return simpleBootFront;
|
|
106
106
|
}
|
|
@@ -15,7 +15,7 @@ import { DomRenderProxy } from '@dooboostore/dom-render';
|
|
|
15
15
|
import { parseHTML } from 'linkedom';
|
|
16
16
|
import { LinkedomInitializer } from '../initializers/LinkedomInitializer';
|
|
17
17
|
|
|
18
|
-
export type
|
|
18
|
+
export type LinkDomFactoryAndParams = {
|
|
19
19
|
frontDistPath: string;
|
|
20
20
|
frontDistIndexFileName?: string;
|
|
21
21
|
factorySimFrontOption: (window: any) => SimFrontOption;
|
|
@@ -33,7 +33,7 @@ export type FactoryAndParams = {
|
|
|
33
33
|
};
|
|
34
34
|
};
|
|
35
35
|
|
|
36
|
-
export class
|
|
36
|
+
export class SSRLinkDomFilter implements Filter {
|
|
37
37
|
private simpleBootFrontPool: SimpleBootFront[] = [];
|
|
38
38
|
private simpleBootFrontQueue = new AsyncBlockingQueue<SimpleBootFront>();
|
|
39
39
|
// private indexHTML: string;
|
|
@@ -42,7 +42,7 @@ export class SSRLinkDomDomFilter implements Filter {
|
|
|
42
42
|
private intervalId?: NodeJS.Timeout;
|
|
43
43
|
|
|
44
44
|
constructor(
|
|
45
|
-
public config:
|
|
45
|
+
public config: LinkDomFactoryAndParams,
|
|
46
46
|
public otherInstanceSim?: Map<ConstructorType<any>, any>
|
|
47
47
|
) {
|
|
48
48
|
config.frontDistIndexFileName = config.frontDistIndexFileName || 'index.html';
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import {RequestResponse} from '@dooboostore/simple-boot-http-server/models/RequestResponse';
|
|
2
|
+
import {HttpHeaders} from '@dooboostore/simple-boot-http-server/codes/HttpHeaders';
|
|
3
|
+
import {Filter} from '@dooboostore/simple-boot-http-server/filters/Filter';
|
|
4
|
+
import {Mimes} from '@dooboostore/simple-boot-http-server/codes/Mimes';
|
|
5
|
+
import {HttpStatus} from '@dooboostore/simple-boot-http-server/codes/HttpStatus';
|
|
6
|
+
import {SimpleBootHttpServer} from '@dooboostore/simple-boot-http-server/SimpleBootHttpServer';
|
|
7
|
+
import {DomParserInitializer} from '../initializers/DomParserInitializer';
|
|
8
|
+
|
|
9
|
+
export type SWCSSRDomParserConfig = {
|
|
10
|
+
frontDistPath: string;
|
|
11
|
+
frontDistIndexFileName?: string;
|
|
12
|
+
welcomUrl?: string;
|
|
13
|
+
ssrExcludeFilter?: (rr: RequestResponse) => boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Function to register components for each request.
|
|
16
|
+
*/
|
|
17
|
+
registerComponents?: (window: any) => Promise<void> | void;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* SSR Filter specifically for Simple Web Component (SWC).
|
|
22
|
+
* It utilizes @dooboostore/dom-parser for rendering and DSD support.
|
|
23
|
+
*/
|
|
24
|
+
export class SSRSimpleWebComponentDomParserFilter implements Filter {
|
|
25
|
+
private welcomUrl = 'http://localhost';
|
|
26
|
+
|
|
27
|
+
constructor(public config: SWCSSRDomParserConfig) {
|
|
28
|
+
this.welcomUrl = config.welcomUrl || this.welcomUrl;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async onInit(app: SimpleBootHttpServer) {}
|
|
32
|
+
|
|
33
|
+
async onDestroy() {}
|
|
34
|
+
|
|
35
|
+
async proceedBefore({ rr }: { rr: RequestResponse; app: SimpleBootHttpServer; carrier: Map<string, any> }) {
|
|
36
|
+
if (this.config.ssrExcludeFilter?.(rr)) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (rr.reqHasAcceptHeader(Mimes.TextHtml) || rr.reqHasAcceptHeader(Mimes.All)) {
|
|
41
|
+
const url = rr.reqUrlObj({ host: 'localhost' });
|
|
42
|
+
const targetUrl = url.toString() ?? this.welcomUrl;
|
|
43
|
+
|
|
44
|
+
// 1. Initialize Virtual DOM Environment
|
|
45
|
+
const domParserInitializer = new DomParserInitializer(this.config.frontDistPath, this.config.frontDistIndexFileName || 'index.html', { url: targetUrl });
|
|
46
|
+
const window = await domParserInitializer.run();
|
|
47
|
+
|
|
48
|
+
console.log('vvvv22aa');
|
|
49
|
+
// web component 경우 자기 tagName을 생성자 에게 HTMLElementBase에넘겨줘야되기떄문에
|
|
50
|
+
// const getTagName = (type: ConstructorType<any>) => {
|
|
51
|
+
// const zz = getElementConfig(type);
|
|
52
|
+
// return zz.name;
|
|
53
|
+
// };
|
|
54
|
+
// (window as any).HTMLElement = class extends HTMLElementBase {
|
|
55
|
+
// constructor(...args: any[]) {
|
|
56
|
+
// const ctor = new.target as any; // 이런 슈가 기능이...
|
|
57
|
+
// const resolvedTagName = getTagName(ctor);
|
|
58
|
+
// super(resolvedTagName, args[0]);
|
|
59
|
+
// }
|
|
60
|
+
// }
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
// 2. Register Components if provided
|
|
65
|
+
// We use SwcApplication inside the callback or directly here.
|
|
66
|
+
if (this.config.registerComponents) {
|
|
67
|
+
await this.config.registerComponents(window);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// window.document.querySelector('.sidebar-space').innerHTML = '씨발놈아.';
|
|
71
|
+
// await new Promise<void>((resolve, reject) => setTimeout(resolve, 100));
|
|
72
|
+
// await new Promise<void>((resolve, reject) => setTimeout(resolve, 5000));
|
|
73
|
+
// window.document.body.innerHTML = '';
|
|
74
|
+
// window.document.body.setAttribute('ssr-use', 'true');
|
|
75
|
+
// 3. Generate Final HTML
|
|
76
|
+
const html = this.makeHTML(window);
|
|
77
|
+
// console.log('html: 💈', html);
|
|
78
|
+
await this.writeOkHtmlAndEnd({ rr }, html);
|
|
79
|
+
} finally {
|
|
80
|
+
// 4. Cleanup
|
|
81
|
+
window.close();
|
|
82
|
+
domParserInitializer.destroy();
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
makeHTML(window: any) {
|
|
90
|
+
let html = window.document.documentElement.outerHTML;
|
|
91
|
+
if (!/^<!DOCTYPE html>/i.test(html)) {
|
|
92
|
+
html = '<!DOCTYPE html>\n' + html;
|
|
93
|
+
}
|
|
94
|
+
return html;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async writeOkHtmlAndEnd({ rr, status = HttpStatus.Ok }: { rr: RequestResponse; status?: HttpStatus }, html: string) {
|
|
98
|
+
rr.resStatusCode(status);
|
|
99
|
+
rr.resSetHeader(HttpHeaders.ContentType, Mimes.TextHtml);
|
|
100
|
+
await rr.resEnd(html);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async proceedAfter() {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -4,10 +4,9 @@ import { Filter } from '@dooboostore/simple-boot-http-server/filters/Filter';
|
|
|
4
4
|
import { Mimes } from '@dooboostore/simple-boot-http-server/codes/Mimes';
|
|
5
5
|
import { HttpStatus } from '@dooboostore/simple-boot-http-server/codes/HttpStatus';
|
|
6
6
|
import { SimpleBootHttpServer } from '@dooboostore/simple-boot-http-server/SimpleBootHttpServer';
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
10
|
-
import {getElementConfig} from "@dooboostore/simple-web-component";
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import fs from 'fs';
|
|
9
|
+
import {HttpMethod} from "@dooboostore/simple-boot-http-server";
|
|
11
10
|
|
|
12
11
|
export type SWCSSRConfig = {
|
|
13
12
|
frontDistPath: string;
|
|
@@ -15,76 +14,280 @@ export type SWCSSRConfig = {
|
|
|
15
14
|
welcomUrl?: string;
|
|
16
15
|
ssrExcludeFilter?: (rr: RequestResponse) => boolean;
|
|
17
16
|
/**
|
|
17
|
+
* (Deprecated for Playwright)
|
|
18
18
|
* Function to register components for each request.
|
|
19
|
+
* Playwright evaluates real JS files loaded from your index.html,
|
|
20
|
+
* so component registration happens automatically in the browser context.
|
|
19
21
|
*/
|
|
20
22
|
registerComponents?: (window: any) => Promise<void> | void;
|
|
23
|
+
playwright?: {
|
|
24
|
+
waitUntil?: 'load' | 'domcontentloaded' | 'networkidle' | 'commit';
|
|
25
|
+
waitForSelector?: string;
|
|
26
|
+
waitForTimeout?: number;
|
|
27
|
+
timeout?: number;
|
|
28
|
+
ignoreTags?: string[];
|
|
29
|
+
};
|
|
21
30
|
};
|
|
22
31
|
|
|
23
32
|
/**
|
|
24
33
|
* SSR Filter specifically for Simple Web Component (SWC).
|
|
25
|
-
* It utilizes
|
|
34
|
+
* It utilizes Playwright to render the page robustly and perfectly matching browser specs.
|
|
35
|
+
*
|
|
36
|
+
* ### Playwright Setup & Requirements
|
|
37
|
+
*
|
|
38
|
+
* To use this filter, Playwright must be installed in your server environment:
|
|
39
|
+
* 1. Install playwright package: `npm install playwright` or `pnpm add playwright`
|
|
40
|
+
* 2. Install browsers: `npx playwright install chromium`
|
|
41
|
+
*
|
|
42
|
+
* If running inside a Docker container (e.g., Alpine Linux), you must use an image that supports Playwright
|
|
43
|
+
* or manually install OS dependencies. Playwright provides official Docker images: `mcr.microsoft.com/playwright:v1.x.x-focal`.
|
|
26
44
|
*/
|
|
27
45
|
export class SSRSimpleWebComponentFilter implements Filter {
|
|
28
46
|
private welcomUrl = 'http://localhost';
|
|
47
|
+
private browser?: any;
|
|
29
48
|
|
|
30
49
|
constructor(public config: SWCSSRConfig) {
|
|
31
50
|
this.welcomUrl = config.welcomUrl || this.welcomUrl;
|
|
32
51
|
}
|
|
33
52
|
|
|
34
|
-
async onInit(app: SimpleBootHttpServer) {
|
|
53
|
+
async onInit(app: SimpleBootHttpServer) {
|
|
54
|
+
// 1. Initialize Playwright browser when the server starts
|
|
55
|
+
const { chromium } = await import('playwright');
|
|
56
|
+
this.browser = await chromium.launch({
|
|
57
|
+
headless: true,
|
|
58
|
+
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-web-security'] // Allow CORS for local dev
|
|
59
|
+
});
|
|
60
|
+
console.log('🚀 Playwright Browser initialized for SSR Filter');
|
|
61
|
+
}
|
|
35
62
|
|
|
36
|
-
async onDestroy() {
|
|
63
|
+
async onDestroy() {
|
|
64
|
+
if (this.browser) {
|
|
65
|
+
await this.browser.close();
|
|
66
|
+
console.log('🛑 Playwright Browser closed');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
37
69
|
|
|
38
|
-
async proceedBefore({ rr }: { rr: RequestResponse; app: SimpleBootHttpServer; carrier: Map<string, any> }) {
|
|
70
|
+
async proceedBefore({ rr, app, carrier }: { rr: RequestResponse; app: SimpleBootHttpServer; carrier: Map<string, any> }) {
|
|
39
71
|
if (this.config.ssrExcludeFilter?.(rr)) {
|
|
40
|
-
return
|
|
72
|
+
return true; // Bypass SSR and continue server chain
|
|
41
73
|
}
|
|
42
74
|
|
|
43
|
-
|
|
44
|
-
|
|
75
|
+
console.log('playwright2222222', rr.reqHeader('x-ssr-browser'));
|
|
76
|
+
|
|
77
|
+
// 2. Prevent Infinite Loop:
|
|
78
|
+
// If the request comes from Playwright itself, bypass SSR and serve the original static SPA file.
|
|
79
|
+
if (rr.reqHeader('x-ssr-browser') === 'playwright') {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (rr.reqIsMethod(HttpMethod.GET) && rr.reqHasContentTypeHeader(Mimes.TextHtml)) {
|
|
84
|
+
// Build target URL for Playwright to visit
|
|
85
|
+
// Use the server's actual listening address and port if available
|
|
86
|
+
const protocol = app.option.protocol || 'http';
|
|
87
|
+
const host = app.option.hostname || '127.0.0.1';
|
|
88
|
+
const port = app.option.port || 80;
|
|
89
|
+
|
|
90
|
+
const url = rr.reqUrlObj({ host: `${host}:${port}` });
|
|
91
|
+
url.protocol = protocol;
|
|
92
|
+
|
|
45
93
|
const targetUrl = url.toString() ?? this.welcomUrl;
|
|
46
94
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
// web component 경우 자기 tagName을 생성자 에게 HTMLElementBase에넘겨줘야되기떄문에
|
|
52
|
-
const getTagName = (type: ConstructorType<any>) => {
|
|
53
|
-
const zz = getElementConfig(type);
|
|
54
|
-
return zz.name;
|
|
55
|
-
};
|
|
56
|
-
(window as any).HTMLElement = class extends HTMLElementBase {
|
|
57
|
-
constructor(...args: any[]) {
|
|
58
|
-
const ctor = new.target as any; // 이런 슈가 기능이...
|
|
59
|
-
const resolvedTagName = getTagName(ctor);
|
|
60
|
-
super(resolvedTagName, args[0]);
|
|
61
|
-
}
|
|
95
|
+
if (!this.browser) {
|
|
96
|
+
console.error('Playwright browser is not initialized. Bypassing SSR.');
|
|
97
|
+
return true;
|
|
62
98
|
}
|
|
63
99
|
|
|
100
|
+
// Create a fresh context for isolation
|
|
101
|
+
// IMPORTANT: We do NOT use `extraHTTPHeaders` globally because it attaches the header to EVERY request
|
|
102
|
+
// Playwright makes (including CORS requests to FontAwesome or API requests).
|
|
103
|
+
// Instead, we will set the header ONLY for the initial navigation request to our server.
|
|
104
|
+
const context = await this.browser.newContext();
|
|
105
|
+
const page = await context.newPage();
|
|
64
106
|
|
|
65
107
|
try {
|
|
66
|
-
//
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
108
|
+
// Intercept ONLY requests going to our local server and append the bypass header
|
|
109
|
+
await page.route('**/*', route => {
|
|
110
|
+
const request = route.request();
|
|
111
|
+
|
|
112
|
+
// Only append the x-ssr-browser header for requests to the local server that are NOT API requests
|
|
113
|
+
const isLocal = request.url().startsWith(targetUrl) || request.url().includes('localhost') || request.url().includes('127.0.0.1');
|
|
114
|
+
const isApi = request.url().includes('/api/');
|
|
115
|
+
|
|
116
|
+
if (isLocal && !isApi) {
|
|
117
|
+
const headers = {
|
|
118
|
+
...request.headers(),
|
|
119
|
+
'x-ssr-browser': 'playwright'
|
|
120
|
+
};
|
|
121
|
+
route.continue({ headers }).catch(() => {});
|
|
122
|
+
} else {
|
|
123
|
+
// For external requests (like font-awesome CORS) or API calls, do not add the custom header
|
|
124
|
+
route.continue().catch(() => {});
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// 3. Navigate to the page and wait for the framework to finish rendering
|
|
129
|
+
// but normally Playwright just navigates to the targetUrl and loads the real SPA resources.
|
|
130
|
+
|
|
131
|
+
// 💡 Playwright ↔ Framework 통신 설정
|
|
132
|
+
// 프론트엔드의 `bootfactory.ts` 안의 `onRouteChanged` 등에서
|
|
133
|
+
// `if (window.onPlaywrightReady) window.onPlaywrightReady()` 라고 호출해주면,
|
|
134
|
+
// 아래의 Promise가 즉시 resolve 되도록 연결합니다.
|
|
135
|
+
let resolvePlaywrightReady: () => void;
|
|
136
|
+
const playwrightReadyPromise = new Promise<void>(resolve => {
|
|
137
|
+
resolvePlaywrightReady = resolve;
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
await page.exposeFunction('onPlaywrightReady', () => {
|
|
141
|
+
console.log('⚡️ [SSR:Playwright] Received "onPlaywrightReady" signal from framework!');
|
|
142
|
+
resolvePlaywrightReady();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// 3. Navigate to the page and wait for the framework to finish rendering
|
|
146
|
+
// Since the server will return the `index.html` (due to the bypass header), Playwright will download
|
|
147
|
+
// your main.js / bundle.js, evaluate it, and the Custom Elements will register themselves naturally!
|
|
148
|
+
await page.goto(targetUrl, {
|
|
149
|
+
waitUntil: this.config.playwright?.waitUntil ?? 'networkidle',
|
|
150
|
+
timeout: this.config.playwright?.timeout ?? 15000
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// 💡 Framework Load Wait (Crucial for SWC)
|
|
154
|
+
// Wait for EITHER the framework to call `window.onPlaywrightReady()` OR a specific innerHTML condition
|
|
155
|
+
await Promise.race([
|
|
156
|
+
playwrightReadyPromise, // 직접 exposeFunction에서 트리거된 Promise
|
|
157
|
+
|
|
158
|
+
page.waitForFunction(
|
|
159
|
+
() => {
|
|
160
|
+
// Fallback 1: If explicit signal variable is set
|
|
161
|
+
return (window as any)._playwrightReadySignalFired === true;
|
|
162
|
+
},
|
|
163
|
+
{ timeout: this.config.playwright?.timeout ?? 15000 }
|
|
164
|
+
),
|
|
165
|
+
|
|
166
|
+
page.waitForFunction(
|
|
167
|
+
() => {
|
|
168
|
+
// Fallback 2: If no signal, just wait until body has content
|
|
169
|
+
return document.body.innerHTML.trim().length > 100;
|
|
170
|
+
},
|
|
171
|
+
{ timeout: this.config.playwright?.timeout ?? 15000 }
|
|
172
|
+
)
|
|
173
|
+
]).catch(() => console.log('Wait for app mount timeout... proceeding anyway'));
|
|
174
|
+
|
|
175
|
+
// 강제로 컴포넌트 렌더링 및 폰트어썸 등 리소스 로딩 대기 시간 추가 (안전 장치)
|
|
176
|
+
// onPlaywrightReady 신호가 떨어져도, 비동기 렌더링(SwcLoop 등)이 DOM에 완전히 반영되는 데
|
|
177
|
+
// 약간의 Microtask 지연이 있을 수 있으므로 아주 짧게만 기다려 줍니다.
|
|
178
|
+
// await new Promise(resolve => setTimeout(resolve, 300));
|
|
179
|
+
|
|
180
|
+
// Optional: Wait for a specific component to be visible
|
|
181
|
+
if (this.config.playwright?.waitForSelector) {
|
|
182
|
+
await page.waitForSelector(this.config.playwright.waitForSelector, {
|
|
183
|
+
timeout: this.config.playwright?.timeout ?? 15000
|
|
184
|
+
});
|
|
70
185
|
}
|
|
71
186
|
|
|
72
|
-
//
|
|
73
|
-
|
|
187
|
+
// Optional: Hardcoded wait time if explicitly set (fallback)
|
|
188
|
+
if (this.config.playwright?.waitForTimeout) {
|
|
189
|
+
await page.waitForTimeout(this.config.playwright.waitForTimeout);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// 4. Set SSR Attribute before extracting HTML
|
|
193
|
+
await page.evaluate(() => {
|
|
194
|
+
document.body.setAttribute('ssr-use', 'true');
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// 5. Generate Final HTML
|
|
198
|
+
const html = await page.evaluate(ignoreTags => {
|
|
199
|
+
const skipTags = new Set(ignoreTags || []);
|
|
200
|
+
|
|
201
|
+
function serializeNode(node: Node, inBody: boolean = false): string {
|
|
202
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
203
|
+
const text = node.textContent || '';
|
|
204
|
+
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
205
|
+
}
|
|
206
|
+
if (node.nodeType === Node.COMMENT_NODE) {
|
|
207
|
+
return `<!--${node.textContent}-->`;
|
|
208
|
+
}
|
|
209
|
+
if (node.nodeType !== Node.ELEMENT_NODE) {
|
|
210
|
+
return '';
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const el = node as Element;
|
|
214
|
+
const tag = el.tagName.toLowerCase();
|
|
215
|
+
const isBody = tag === 'body';
|
|
216
|
+
const isInsideBody = inBody || isBody;
|
|
217
|
+
|
|
218
|
+
// 🚫 Skip ignored tags (like <style>) ONLY if we are inside the <body>.
|
|
219
|
+
// This ensures <style> tags in the <head> (global styles) are preserved!
|
|
220
|
+
if (isInsideBody && skipTags.has(tag)) {
|
|
221
|
+
return '';
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
let s = `<${tag}`;
|
|
225
|
+
|
|
226
|
+
// 1. Serialize Attributes
|
|
227
|
+
for (let i = 0; i < el.attributes.length; i++) {
|
|
228
|
+
const attr = el.attributes[i];
|
|
229
|
+
s += ` ${attr.name}="${attr.value.replace(/"/g, '"')}"`;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Handle void elements (self-closing)
|
|
233
|
+
const voidElements = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr']);
|
|
234
|
+
if (voidElements.has(tag)) {
|
|
235
|
+
s += '>';
|
|
236
|
+
return s;
|
|
237
|
+
}
|
|
238
|
+
s += '>';
|
|
239
|
+
|
|
240
|
+
// 2. Serialize Shadow DOM (Declarative Shadow DOM)
|
|
241
|
+
// MUST KEEP for SSR: Without this, the user receives an empty shell until JS loads and re-renders everything.
|
|
242
|
+
// This allows the browser to paint the full UI (including styles inside the component) immediately.
|
|
243
|
+
if (el.shadowRoot) {
|
|
244
|
+
s += `<template shadowrootmode="${el.shadowRoot.mode}">`;
|
|
245
|
+
for (let i = 0; i < el.shadowRoot.childNodes.length; i++) {
|
|
246
|
+
s += serializeNode(el.shadowRoot.childNodes[i], isInsideBody);
|
|
247
|
+
}
|
|
248
|
+
s += '</template>';
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// 3. Serialize Light DOM Children
|
|
252
|
+
for (let i = 0; i < el.childNodes.length; i++) {
|
|
253
|
+
s += serializeNode(el.childNodes[i], isInsideBody);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
s += `</${tag}>`;
|
|
257
|
+
return s;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
let result = '<!DOCTYPE html>\n<html';
|
|
261
|
+
const htmlNode = document.documentElement;
|
|
262
|
+
for (let i = 0; i < htmlNode.attributes.length; i++) {
|
|
263
|
+
const attr = htmlNode.attributes[i];
|
|
264
|
+
result += ` ${attr.name}="${attr.value.replace(/"/g, '"')}"`;
|
|
265
|
+
}
|
|
266
|
+
result += '>\n';
|
|
267
|
+
|
|
268
|
+
if (document.head) result += serializeNode(document.head, false);
|
|
269
|
+
result += '\n';
|
|
270
|
+
if (document.body) result += serializeNode(document.body, true);
|
|
271
|
+
|
|
272
|
+
result += '\n</html>';
|
|
273
|
+
return result;
|
|
274
|
+
}, this.config.playwright?.ignoreTags || []);
|
|
275
|
+
console.log(`💈 [SSR:Playwright] Successfully rendered ${targetUrl}`);
|
|
74
276
|
|
|
75
|
-
await this.writeOkHtmlAndEnd({ rr }, html);
|
|
277
|
+
await this.writeOkHtmlAndEnd({ rr }, this.makeHTML(html));
|
|
278
|
+
} catch (e) {
|
|
279
|
+
console.error(`Playwright SSR Error for ${targetUrl}:`, e);
|
|
280
|
+
// If Playwright fails (e.g., timeout), we bypass SSR so the client at least gets the CSR app
|
|
281
|
+
return true;
|
|
76
282
|
} finally {
|
|
77
|
-
|
|
78
|
-
(window as any).close?.();
|
|
79
|
-
domParserInitializer.destroy();
|
|
283
|
+
await context.close();
|
|
80
284
|
}
|
|
81
|
-
return false;
|
|
285
|
+
return false; // Tell SimpleBootHttpServer to stop routing because we've handled the response
|
|
82
286
|
}
|
|
83
287
|
return true;
|
|
84
288
|
}
|
|
85
289
|
|
|
86
|
-
makeHTML(
|
|
87
|
-
let html = window.document.documentElement.outerHTML;
|
|
290
|
+
makeHTML(html: string) {
|
|
88
291
|
if (!/^<!DOCTYPE html>/i.test(html)) {
|
|
89
292
|
html = '<!DOCTYPE html>\n' + html;
|
|
90
293
|
}
|
package/src/filters/index.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
export * from '@dooboostore/simple-boot-http-server/filters/IntentSchemeFilter';
|
|
2
1
|
export * from './SSRFilter';
|
|
3
|
-
export * from './SSRWorker';
|
|
2
|
+
export * from './SSRWorker';
|
|
3
|
+
export * from './SSRSimpleWebComponentFilter';
|
|
4
|
+
export * from './SSRSimpleWebComponentDomParserFilter';
|
|
5
|
+
export * from './SSRDomParserFilter';
|
|
6
|
+
export * from './SSRLinkDomFilter';
|
package/src/index.ts
CHANGED