@@ -8,7 +8,6 @@ import popoverScript from "../../components/scripts/popover.inline"
8
8
import styles from "../../styles/custom.scss"
9
9
import popoverStyle from "../../components/styles/popover.scss"
10
10
import { BuildCtx } from "../../util/ctx"
11
- import { StaticResources } from "../../util/resources"
12
11
import { QuartzComponent } from "../../components/types"
13
12
import { googleFontHref , joinStyles } from "../../util/theme"
14
13
import { Features , transform } from "lightningcss"
@@ -69,13 +68,8 @@ async function joinScripts(scripts: string[]): Promise<string> {
69
68
return res . code
70
69
}
71
70
72
- function addGlobalPageResources (
73
- ctx : BuildCtx ,
74
- staticResources : StaticResources ,
75
- componentResources : ComponentResources ,
76
- ) {
71
+ function addGlobalPageResources ( ctx : BuildCtx , componentResources : ComponentResources ) {
77
72
const cfg = ctx . cfg . configuration
78
- const reloadScript = ctx . argv . serve
79
73
80
74
// popovers
81
75
if ( cfg . enablePopovers ) {
@@ -85,12 +79,12 @@ function addGlobalPageResources(
85
79
86
80
if ( cfg . analytics ?. provider === "google" ) {
87
81
const tagId = cfg . analytics . tagId
88
- staticResources . js . push ( {
89
- src : `https://www.googletagmanager.com/gtag/js?id=${ tagId } ` ,
90
- contentType : "external" ,
91
- loadTime : "afterDOMReady" ,
92
- } )
93
82
componentResources . afterDOMLoaded . push ( `
83
+ const gtagScript = document.createElement("script")
84
+ gtagScript.src = "https://www.googletagmanager.com/gtag/js?id=${ tagId } "
85
+ gtagScript.async = true
86
+ document.head.appendChild(gtagScript)
87
+
94
88
window.dataLayer = window.dataLayer || [];
95
89
function gtag() { dataLayer.push(arguments); }
96
90
gtag("js", new Date());
@@ -166,115 +160,72 @@ function addGlobalPageResources(
166
160
document.dispatchEvent(event)
167
161
` )
168
162
}
169
-
170
- let wsUrl = `ws://localhost:${ ctx . argv . wsPort } `
171
-
172
- if ( ctx . argv . remoteDevHost ) {
173
- wsUrl = `wss://${ ctx . argv . remoteDevHost } :${ ctx . argv . wsPort } `
174
- }
175
-
176
- if ( reloadScript ) {
177
- staticResources . js . push ( {
178
- loadTime : "afterDOMReady" ,
179
- contentType : "inline" ,
180
- script : `
181
- const socket = new WebSocket('${ wsUrl } ')
182
- // reload(true) ensures resources like images and scripts are fetched again in firefox
183
- socket.addEventListener('message', () => document.location.reload(true))
184
- ` ,
185
- } )
186
- }
187
- }
188
-
189
- interface Options {
190
- fontOrigin : "googleFonts" | "local"
191
- }
192
-
193
- const defaultOptions : Options = {
194
- fontOrigin : "googleFonts" ,
195
163
}
196
164
197
- export const ComponentResources : QuartzEmitterPlugin < Options > = ( opts ?: Partial < Options > ) => {
198
- const { fontOrigin } = { ...defaultOptions , ...opts }
165
+ // This emitter should not update the `resources` parameter. If it does, partial
166
+ // rebuilds may not work as expected.
167
+ export const ComponentResources : QuartzEmitterPlugin = ( ) => {
199
168
return {
200
169
name : "ComponentResources" ,
201
170
getQuartzComponents ( ) {
202
171
return [ ]
203
172
} ,
204
- async getDependencyGraph ( ctx , content , _resources ) {
205
- // This emitter adds static resources to the `resources` parameter. One
206
- // important resource this emitter adds is the code to start a websocket
207
- // connection and listen to rebuild messages, which triggers a page reload.
208
- // The resources parameter with the reload logic is later used by the
209
- // ContentPage emitter while creating the final html page. In order for
210
- // the reload logic to be included, and so for partial rebuilds to work,
211
- // we need to run this emitter for all markdown files.
212
- const graph = new DepGraph < FilePath > ( )
213
-
214
- for ( const [ _tree , file ] of content ) {
215
- const sourcePath = file . data . filePath !
216
- const slug = file . data . slug !
217
- graph . addEdge ( sourcePath , joinSegments ( ctx . argv . output , slug + ".html" ) as FilePath )
218
- }
219
-
220
- return graph
173
+ async getDependencyGraph ( _ctx , _content , _resources ) {
174
+ return new DepGraph < FilePath > ( )
221
175
} ,
222
- async emit ( ctx , _content , resources ) : Promise < FilePath [ ] > {
176
+ async emit ( ctx , _content , _resources ) : Promise < FilePath [ ] > {
223
177
const promises : Promise < FilePath > [ ] = [ ]
224
178
const cfg = ctx . cfg . configuration
225
179
// component specific scripts and styles
226
180
const componentResources = getComponentResources ( ctx )
227
181
let googleFontsStyleSheet = ""
228
- if ( fontOrigin === "local" ) {
182
+ if ( cfg . theme . fontOrigin === "local" ) {
229
183
// let the user do it themselves in css
230
- } else if ( fontOrigin === "googleFonts" ) {
231
- if ( cfg . theme . cdnCaching ) {
232
- resources . css . push ( googleFontHref ( cfg . theme ) )
233
- } else {
234
- let match
235
-
236
- const fontSourceRegex = / u r l \( ( h t t p s : \/ \/ f o n t s .g s t a t i c .c o m \/ s \/ [ ^ ) ] + \. ( w o f f 2 | t t f ) ) \) / g
237
-
238
- googleFontsStyleSheet = await (
239
- await fetch ( googleFontHref ( ctx . cfg . configuration . theme ) )
240
- ) . text ( )
241
-
242
- while ( ( match = fontSourceRegex . exec ( googleFontsStyleSheet ) ) !== null ) {
243
- // match[0] is the `url(path)`, match[1] is the `path`
244
- const url = match [ 1 ]
245
- // the static name of this file.
246
- const [ filename , ext ] = url . split ( "/" ) . pop ( ) ! . split ( "." )
247
-
248
- googleFontsStyleSheet = googleFontsStyleSheet . replace (
249
- url ,
250
- `/static/fonts/${ filename } .ttf` ,
251
- )
252
-
253
- promises . push (
254
- fetch ( url )
255
- . then ( ( res ) => {
256
- if ( ! res . ok ) {
257
- throw new Error ( `Failed to fetch font` )
258
- }
259
- return res . arrayBuffer ( )
260
- } )
261
- . then ( ( buf ) =>
262
- write ( {
263
- ctx,
264
- slug : joinSegments ( "static" , "fonts" , filename ) as FullSlug ,
265
- ext : `.${ ext } ` ,
266
- content : Buffer . from ( buf ) ,
267
- } ) ,
268
- ) ,
269
- )
270
- }
184
+ } else if ( cfg . theme . fontOrigin === "googleFonts" && ! cfg . theme . cdnCaching ) {
185
+ // when cdnCaching is true, we link to google fonts in Head.tsx
186
+ let match
187
+
188
+ const fontSourceRegex = / u r l \( ( h t t p s : \/ \/ f o n t s .g s t a t i c .c o m \/ s \/ [ ^ ) ] + \. ( w o f f 2 | t t f ) ) \) / g
189
+
190
+ googleFontsStyleSheet = await (
191
+ await fetch ( googleFontHref ( ctx . cfg . configuration . theme ) )
192
+ ) . text ( )
193
+
194
+ while ( ( match = fontSourceRegex . exec ( googleFontsStyleSheet ) ) !== null ) {
195
+ // match[0] is the `url(path)`, match[1] is the `path`
196
+ const url = match [ 1 ]
197
+ // the static name of this file.
198
+ const [ filename , ext ] = url . split ( "/" ) . pop ( ) ! . split ( "." )
199
+
200
+ googleFontsStyleSheet = googleFontsStyleSheet . replace (
201
+ url ,
202
+ `https://${ cfg . baseUrl } /static/fonts/${ filename } .ttf` ,
203
+ )
204
+
205
+ promises . push (
206
+ fetch ( url )
207
+ . then ( ( res ) => {
208
+ if ( ! res . ok ) {
209
+ throw new Error ( `Failed to fetch font` )
210
+ }
211
+ return res . arrayBuffer ( )
212
+ } )
213
+ . then ( ( buf ) =>
214
+ write ( {
215
+ ctx,
216
+ slug : joinSegments ( "static" , "fonts" , filename ) as FullSlug ,
217
+ ext : `.${ ext } ` ,
218
+ content : Buffer . from ( buf ) ,
219
+ } ) ,
220
+ ) ,
221
+ )
271
222
}
272
223
}
273
224
274
225
// important that this goes *after* component scripts
275
226
// as the "nav" event gets triggered here and we should make sure
276
227
// that everyone else had the chance to register a listener for it
277
- addGlobalPageResources ( ctx , resources , componentResources )
228
+ addGlobalPageResources ( ctx , componentResources )
278
229
279
230
const stylesheet = joinStyles (
280
231
ctx . cfg . configuration . theme ,
0 commit comments