@brillout/docpress 0.16.24 → 0.16.25
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/dist/types/Config.d.ts +1 -0
- package/package.json +1 -1
- package/renderer/onRenderClient.tsx +44 -0
- package/types/Config.ts +1 -0
- package/utils/addScript.ts +6 -1
package/dist/types/Config.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -52,6 +52,7 @@ async function onRenderClient(pageContext: PageContextClient) {
|
|
|
52
52
|
installSectionUrlHashs()
|
|
53
53
|
setHydrationIsFinished()
|
|
54
54
|
initGoogleAnalytics(pageContext)
|
|
55
|
+
initUmami(pageContext)
|
|
55
56
|
|
|
56
57
|
globalObject.isNotFirstRender = true
|
|
57
58
|
}
|
|
@@ -104,3 +105,46 @@ declare global {
|
|
|
104
105
|
gtag: (...args: any[]) => void
|
|
105
106
|
}
|
|
106
107
|
}
|
|
108
|
+
|
|
109
|
+
async function initUmami(pageContext: PageContextClient) {
|
|
110
|
+
// Only add script tag on first render
|
|
111
|
+
if (globalObject.isNotFirstRender) {
|
|
112
|
+
return
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Simple way to plug in umami
|
|
116
|
+
const umamiId = pageContext.config.docpress.umamiId;
|
|
117
|
+
if (!umamiId) {
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
try {
|
|
122
|
+
await addScript('https://cloud.umami.is/script.js', {
|
|
123
|
+
'data-website-id': umamiId,
|
|
124
|
+
})
|
|
125
|
+
} catch {
|
|
126
|
+
// Umami analytics unavailable
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** https://umami.is/docs/tracker-functions */
|
|
131
|
+
type Umami = {
|
|
132
|
+
track: {
|
|
133
|
+
(): void
|
|
134
|
+
(eventName: string): void
|
|
135
|
+
(data: object): void
|
|
136
|
+
(eventName: string, data: object): void
|
|
137
|
+
}
|
|
138
|
+
identify: {
|
|
139
|
+
(uniqueId: string): void
|
|
140
|
+
(data: object): string
|
|
141
|
+
(uniqueId: string, data: object): void
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
declare global {
|
|
146
|
+
interface Window {
|
|
147
|
+
/** Script load is not guaranteed */
|
|
148
|
+
umami?: Umami;
|
|
149
|
+
}
|
|
150
|
+
}
|
package/types/Config.ts
CHANGED
package/utils/addScript.ts
CHANGED
|
@@ -2,12 +2,17 @@ export { addScript }
|
|
|
2
2
|
|
|
3
3
|
import { genPromise } from './genPromise.js'
|
|
4
4
|
|
|
5
|
-
async function addScript(src: string) {
|
|
5
|
+
async function addScript(src: string, attributes?: Record<string, string>) {
|
|
6
6
|
const { promise, resolve, reject } = genPromise()
|
|
7
7
|
const script = document.createElement('script')
|
|
8
8
|
script.onload = () => resolve()
|
|
9
9
|
script.onerror = () => reject(new Error(`Failed to load script: ${src}`))
|
|
10
10
|
script.src = src
|
|
11
|
+
if (attributes) {
|
|
12
|
+
for (const [name, value] of Object.entries(attributes)) {
|
|
13
|
+
script.setAttribute(name, value)
|
|
14
|
+
}
|
|
15
|
+
}
|
|
11
16
|
document.head.appendChild(script)
|
|
12
17
|
return promise
|
|
13
18
|
}
|