-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-app-running.js
123 lines (109 loc) · 3.53 KB
/
get-app-running.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const Zone = require("can-zone");
const xhrZone = require("can-zone/xhr");
const fetchZone = require("can-zone/fetch");
const RoutePushstate = require("can-route-pushstate");
const isNode = require("can-globals/is-node/is-node");
isNode(false);
const sharedZone = new Zone({ plugins: [xhrZone, fetchZone] })
function beforeEverything(){
if(globalThis.customElements) {
const oldDefine = customElements.define;
customElements.define = function(tag, ElementClass){
// can-view-callbacks creates elements with Reflect.construct
// that don't work with the StacheElement overwriting scheme in jsdom,
// so we have to work differently with those classes.
if(
ElementClass.toString().indexOf("function") === 0
&& ElementClass.name === "CustomElement"
) {
class RehydrateClass extends HTMLElement {
connectedCallback(...args){
if(this.getAttribute("can-ssg") === "inert") {
} else {
return ElementClass.prototype.connectedCallback.apply(this, args)
}
}
}
oldDefine.call(this, tag, RehydrateClass)
} else {
class RehydrateClass extends ElementClass {
initialize(...args){
if(this.getAttribute("can-ssg") === "inert") {
} else {
return super.initialize(...args)
}
}
connectedCallback(...args){
if(this.getAttribute("can-ssg") === "inert") {
} else {
return super.connectedCallback(...args)
}
}
disconnectedCallback(...args){
if(this.getAttribute("can-ssg") === "inert") {
} else {
return super.disconnectedCallback(...args)
}
}
}
oldDefine.call(this, tag, RehydrateClass)
}
}
}
}
beforeEverything();
module.exports = {
after(mainElementName){
sharedZone
.run(() => {
if (!document.body.getAttribute("can-ssg")) {
document.body.append(document.createElement(mainElementName));
}
})
.then(function (data) {
if (!globalThis.XHR_CACHE && data.xhr) {
const temp = document.createElement("div")
temp.innerHTML = `<script>${data.xhr}</script>`
document.body.appendChild(temp.lastChild)
}
if (!globalThis.FETCH_CACHE && data.fetch) {
const temp = document.createElement("div")
temp.innerHTML = `<script>${data.fetch}</script>`
document.body.appendChild(temp.lastChild)
}
})
if (document.body.getAttribute("can-ssg")) {
//setTimeout(function(){
new Zone({
plugins: [xhrZone, fetchZone],
})
.run(function () {
// Check if global flag is set to skip hydration
// This is required for testing static pages before hydration during e2e
if (globalThis.skipHydrationCanStacheElement) {
return
}
delete globalThis.canStacheElementInertPrerendered
const staticapp = document.querySelector(mainElementName)
const temp = document.createElement("div")
temp.innerHTML = "<"+mainElementName+"></"+mainElementName+">" // TODO: scrape static attrs from page too
const liveapp = temp.querySelector(mainElementName)
liveapp.style.display = "none"
staticapp.parentNode.insertBefore(liveapp, staticapp)
return { staticapp, liveapp }
})
.then(function (data) {
// Sets global flag that indicates that hydration has successfully been skipped
// This is required for testing static pages before hydration during e2e
if (!data.result) {
globalThis.skippedHydrationCanStacheElement = true
return
}
const { staticapp, liveapp } = data.result
staticapp.remove()
liveapp.style.display = ""
})
//},5000)
}
}
}