vite-react-ssg 0.7.0-beta.1 → 0.7.0
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/LICENSE +21 -21
- package/README.md +591 -555
- package/bin/vite-react-ssg.js +3 -3
- package/dist/chunks/jsdomGlobal.cjs +80 -80
- package/dist/chunks/jsdomGlobal.mjs +80 -80
- package/dist/client/single-page.d.cts +2 -2
- package/dist/client/single-page.d.mts +2 -2
- package/dist/client/single-page.d.ts +2 -2
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/node.cjs +111 -60
- package/dist/node.d.cts +1 -1
- package/dist/node.d.mts +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/node.mjs +112 -61
- package/dist/shared/{vite-react-ssg.870c683b.d.cts → vite-react-ssg.4d155759.d.cts} +2 -0
- package/dist/shared/{vite-react-ssg.870c683b.d.mts → vite-react-ssg.4d155759.d.mts} +2 -0
- package/dist/shared/{vite-react-ssg.870c683b.d.ts → vite-react-ssg.4d155759.d.ts} +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,555 +1,591 @@
|
|
|
1
|
-
# Vite React SSG
|
|
2
|
-
|
|
3
|
-
Static-site generation for React on Vite.
|
|
4
|
-
|
|
5
|
-
See demo(also document): [docs](https://vite-react-ssg.netlify.app/)
|
|
6
|
-
|
|
7
|
-
**🎈 Support for [`@tanstack/router`](https://tanstack.com/router/latest/docs/framework/react/overview) and [`wouter`](https://github.com/molefrog/wouter) is in progress!**
|
|
8
|
-
|
|
9
|
-
[](https://www.npmjs.com/package/vite-react-ssg)
|
|
10
|
-
|
|
11
|
-
# Table of contents
|
|
12
|
-
|
|
13
|
-
- [Usage](#usage)
|
|
14
|
-
- [Use CSR during development](#use-csr-during-development)
|
|
15
|
-
- [Extra route options](#extra-route-options)
|
|
16
|
-
- [`entry`](#entry)
|
|
17
|
-
- [`getStaticPaths`](#getstaticpaths)
|
|
18
|
-
- [Data fetch](#data-fetch)
|
|
19
|
-
- [lazy](#lazy)
|
|
20
|
-
- [`<ClientOnly/>`](#clientonly)
|
|
21
|
-
- [Document head](#document-head)
|
|
22
|
-
- [Reactive head](#reactive-head)
|
|
23
|
-
- [Public Base Path](#public-base-path)
|
|
24
|
-
- [CSS in JS](#css-in-js)
|
|
25
|
-
- [Critical CSS](#critical-css)
|
|
26
|
-
- [Configuration](#configuration)
|
|
27
|
-
- [Custom Routes to Render](#custom-routes-to-render)
|
|
28
|
-
- [Https](#https)
|
|
29
|
-
- [Roadmap](#roadmap)
|
|
30
|
-
- [Credits](#credits)
|
|
31
|
-
|
|
32
|
-
## Usage
|
|
33
|
-
|
|
34
|
-
<pre>
|
|
35
|
-
<b>npm i -D vite-react-ssg</b> <em>react-router-dom</em>
|
|
36
|
-
</pre>
|
|
37
|
-
|
|
38
|
-
```diff
|
|
39
|
-
// package.json
|
|
40
|
-
{
|
|
41
|
-
"scripts": {
|
|
42
|
-
- "build": "vite build"
|
|
43
|
-
+ "build": "vite-react-ssg build"
|
|
44
|
-
// If you need ssr when dev
|
|
45
|
-
- "dev": "vite",
|
|
46
|
-
+ "dev": "vite-react-ssg dev",
|
|
47
|
-
|
|
48
|
-
// OR if you want to use another vite config file
|
|
49
|
-
+ "build": "vite-react-ssg build -c another-vite.config.ts"
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
```ts
|
|
55
|
-
// src/main.ts
|
|
56
|
-
import { ViteReactSSG } from 'vite-react-ssg'
|
|
57
|
-
import routes from './App.tsx'
|
|
58
|
-
|
|
59
|
-
export const createRoot = ViteReactSSG(
|
|
60
|
-
// react-router-dom data routes
|
|
61
|
-
{ routes },
|
|
62
|
-
// function to have custom setups
|
|
63
|
-
({ router, routes, isClient, initialState }) => {
|
|
64
|
-
// do something.
|
|
65
|
-
},
|
|
66
|
-
)
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
```tsx
|
|
70
|
-
// src/App.tsx
|
|
71
|
-
import React from 'react'
|
|
72
|
-
import type { RouteRecord } from 'vite-react-ssg'
|
|
73
|
-
import './App.css'
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
<
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
</
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
<
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
import {
|
|
312
|
-
import {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
import {
|
|
336
|
-
import
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
```
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
```
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
*
|
|
430
|
-
*
|
|
431
|
-
*
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
*
|
|
442
|
-
*
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
*
|
|
448
|
-
*
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
*
|
|
454
|
-
*
|
|
455
|
-
*
|
|
456
|
-
*
|
|
457
|
-
*
|
|
458
|
-
*/
|
|
459
|
-
|
|
460
|
-
/**
|
|
461
|
-
*
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
*
|
|
466
|
-
*
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
*
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
*
|
|
475
|
-
*
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
*
|
|
483
|
-
*
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
*
|
|
489
|
-
*
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
1
|
+
# Vite React SSG
|
|
2
|
+
|
|
3
|
+
Static-site generation for React on Vite.
|
|
4
|
+
|
|
5
|
+
See demo(also document): [docs](https://vite-react-ssg.netlify.app/)
|
|
6
|
+
|
|
7
|
+
**🎈 Support for [`@tanstack/router`](https://tanstack.com/router/latest/docs/framework/react/overview) and [`wouter`](https://github.com/molefrog/wouter) is in progress!**
|
|
8
|
+
|
|
9
|
+
[](https://www.npmjs.com/package/vite-react-ssg)
|
|
10
|
+
|
|
11
|
+
# Table of contents
|
|
12
|
+
|
|
13
|
+
- [Usage](#usage)
|
|
14
|
+
- [Use CSR during development](#use-csr-during-development)
|
|
15
|
+
- [Extra route options](#extra-route-options)
|
|
16
|
+
- [`entry`](#entry)
|
|
17
|
+
- [`getStaticPaths`](#getstaticpaths)
|
|
18
|
+
- [Data fetch](#data-fetch)
|
|
19
|
+
- [lazy](#lazy)
|
|
20
|
+
- [`<ClientOnly/>`](#clientonly)
|
|
21
|
+
- [Document head](#document-head)
|
|
22
|
+
- [Reactive head](#reactive-head)
|
|
23
|
+
- [Public Base Path](#public-base-path)
|
|
24
|
+
- [CSS in JS](#css-in-js)
|
|
25
|
+
- [Critical CSS](#critical-css)
|
|
26
|
+
- [Configuration](#configuration)
|
|
27
|
+
- [Custom Routes to Render](#custom-routes-to-render)
|
|
28
|
+
- [Https](#https)
|
|
29
|
+
- [Roadmap](#roadmap)
|
|
30
|
+
- [Credits](#credits)
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
<pre>
|
|
35
|
+
<b>npm i -D vite-react-ssg</b> <em>react-router-dom</em>
|
|
36
|
+
</pre>
|
|
37
|
+
|
|
38
|
+
```diff
|
|
39
|
+
// package.json
|
|
40
|
+
{
|
|
41
|
+
"scripts": {
|
|
42
|
+
- "build": "vite build"
|
|
43
|
+
+ "build": "vite-react-ssg build"
|
|
44
|
+
// If you need ssr when dev
|
|
45
|
+
- "dev": "vite",
|
|
46
|
+
+ "dev": "vite-react-ssg dev",
|
|
47
|
+
|
|
48
|
+
// OR if you want to use another vite config file
|
|
49
|
+
+ "build": "vite-react-ssg build -c another-vite.config.ts"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
// src/main.ts
|
|
56
|
+
import { ViteReactSSG } from 'vite-react-ssg'
|
|
57
|
+
import routes from './App.tsx'
|
|
58
|
+
|
|
59
|
+
export const createRoot = ViteReactSSG(
|
|
60
|
+
// react-router-dom data routes
|
|
61
|
+
{ routes },
|
|
62
|
+
// function to have custom setups
|
|
63
|
+
({ router, routes, isClient, initialState }) => {
|
|
64
|
+
// do something.
|
|
65
|
+
},
|
|
66
|
+
)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
// src/App.tsx
|
|
71
|
+
import React from 'react'
|
|
72
|
+
import type { RouteRecord } from 'vite-react-ssg'
|
|
73
|
+
import './App.css'
|
|
74
|
+
import Layout from './Layout'
|
|
75
|
+
|
|
76
|
+
export const routes: RouteRecord[] = [
|
|
77
|
+
{
|
|
78
|
+
path: '/',
|
|
79
|
+
element: <Layout />,
|
|
80
|
+
entry: 'src/Layout.tsx',
|
|
81
|
+
children: [
|
|
82
|
+
{
|
|
83
|
+
path: 'a',
|
|
84
|
+
lazy: () => import('./pages/a'),
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
index: true,
|
|
88
|
+
Component: React.lazy(() => import('./pages/index')),
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
path: 'nest/:b',
|
|
92
|
+
lazy: () => {
|
|
93
|
+
const Component = await import('./pages/nest/[b]')
|
|
94
|
+
return { Component }
|
|
95
|
+
},
|
|
96
|
+
// To determine which paths will be pre-rendered
|
|
97
|
+
getStaticPaths: () => ['nest/b1', 'nest/b2'],
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
},
|
|
101
|
+
]
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Use CSR during development
|
|
105
|
+
|
|
106
|
+
Vite React SSG provide SSR (Server-Side Rendering) during development to ensure consistency between development and production as much as possible.
|
|
107
|
+
|
|
108
|
+
But if you want to use CSR during development, just:
|
|
109
|
+
|
|
110
|
+
```diff
|
|
111
|
+
// package.json
|
|
112
|
+
{
|
|
113
|
+
"scripts": {
|
|
114
|
+
- "dev": "vite-react-ssg dev",
|
|
115
|
+
+ "dev": "vite",
|
|
116
|
+
"build": "vite-react-ssg build"
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Single Page SSG
|
|
122
|
+
|
|
123
|
+
For SSG of an index page only (i.e. without `react-router-dom`); import `vite-react-ssg/single-page` instead.
|
|
124
|
+
|
|
125
|
+
```tsx
|
|
126
|
+
// src/main.tsx
|
|
127
|
+
import { ViteReactSSG } from 'vite-react-ssg/single-page'
|
|
128
|
+
import App from './App.tsx'
|
|
129
|
+
|
|
130
|
+
export const createRoot = ViteReactSSG(<App />)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Extra route options
|
|
134
|
+
|
|
135
|
+
The RouteObject of vite-react-ssg is based on react-router, and vite-react-ssg receives some additional properties.
|
|
136
|
+
|
|
137
|
+
#### `getStaticPaths`
|
|
138
|
+
|
|
139
|
+
The `getStaticPaths()` function should return an array of path
|
|
140
|
+
to determine which paths will be pre-rendered by vite-react-ssg.
|
|
141
|
+
|
|
142
|
+
This function is only valid for dynamic route.
|
|
143
|
+
|
|
144
|
+
```tsx
|
|
145
|
+
const route = {
|
|
146
|
+
path: 'nest/:b',
|
|
147
|
+
Component: React.lazy(() => import('./pages/nest/[b]')),
|
|
148
|
+
entry: 'src/pages/nest/[b].tsx',
|
|
149
|
+
// To determine which paths will be pre-rendered
|
|
150
|
+
getStaticPaths: () => ['nest/b1', 'nest/b2'],
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
#### `entry`
|
|
155
|
+
|
|
156
|
+
**You are not required to use this field. It is only necessary when "prehydration style loss" occurs.**
|
|
157
|
+
It should be the path from root to the target file.
|
|
158
|
+
|
|
159
|
+
eg: `src/pages/page1.tsx`
|
|
160
|
+
|
|
161
|
+
## lazy
|
|
162
|
+
|
|
163
|
+
These options work well with the `lazy` field.
|
|
164
|
+
|
|
165
|
+
```tsx
|
|
166
|
+
// src/pages/[page].tsx
|
|
167
|
+
export function Component() {
|
|
168
|
+
return (
|
|
169
|
+
<div>{/* your component */}</div>
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function getStaticPaths() {
|
|
174
|
+
return ['page1', 'page2']
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
// src/routes.ts
|
|
180
|
+
const routes = [
|
|
181
|
+
{
|
|
182
|
+
path: '/:page',
|
|
183
|
+
lazy: () => import('./pages/[page]')
|
|
184
|
+
}
|
|
185
|
+
]
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
See [example](./examples/lazy-pages/src/App.tsx).
|
|
189
|
+
|
|
190
|
+
## Data fetch
|
|
191
|
+
|
|
192
|
+
You can use react-router-dom's `loader` to fetch data at build time and use `useLoaderData` to get the data in the component.
|
|
193
|
+
|
|
194
|
+
In production, the `loader` will only be executed at build time, and the data will be fetched by the manifest generated at build time during the browser navigations .
|
|
195
|
+
|
|
196
|
+
In the development environment, the `loader` also runs only on the server.It provides data to the HTML during initial server rendering, and during browser route navigations , it makes calls to the server by initiating a fetch on the service.
|
|
197
|
+
|
|
198
|
+
```tsx
|
|
199
|
+
import { useLoaderData } from 'react-router-dom'
|
|
200
|
+
|
|
201
|
+
export default function Docs() {
|
|
202
|
+
const data = useLoaderData() as Awaited<ReturnType<typeof loader>>
|
|
203
|
+
|
|
204
|
+
return (
|
|
205
|
+
<>
|
|
206
|
+
<div>{data.key}</div>
|
|
207
|
+
{/* eslint-disable-next-line react-dom/no-dangerously-set-innerhtml */}
|
|
208
|
+
<div dangerouslySetInnerHTML={{ __html: data.packageCodeHtml }} style={{ textAlign: 'start' }}></div>
|
|
209
|
+
</>
|
|
210
|
+
)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export const Component = Docs
|
|
214
|
+
|
|
215
|
+
export const entry = 'src/pages/json.tsx'
|
|
216
|
+
|
|
217
|
+
export async function loader() {
|
|
218
|
+
// The code here will not be executed on the client side, and the modules imported will not be sent to the client.
|
|
219
|
+
const fs = (await import('node:fs'))
|
|
220
|
+
const cwd = process.cwd()
|
|
221
|
+
const json = (await import('../docs/test.json')).default
|
|
222
|
+
|
|
223
|
+
const packageJson = await fs.promises.readFile(`${cwd}/package.json`, 'utf-8')
|
|
224
|
+
const { codeToHtml } = await import('shiki')
|
|
225
|
+
const packageJsonHtml = await codeToHtml(packageJson, { lang: 'json', theme: 'vitesse-light' })
|
|
226
|
+
|
|
227
|
+
return {
|
|
228
|
+
...json,
|
|
229
|
+
packageCodeHtml: packageJsonHtml,
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
See [example | with-loader](./examples/with-loader/src/pages/[docs].tsx).
|
|
235
|
+
|
|
236
|
+
## `<ClientOnly/>`
|
|
237
|
+
|
|
238
|
+
If you need to render some component in browser only, you can wrap your component with `<ClientOnly>`.
|
|
239
|
+
|
|
240
|
+
```tsx
|
|
241
|
+
import { ClientOnly } from 'vite-react-ssg'
|
|
242
|
+
|
|
243
|
+
function MyComponent() {
|
|
244
|
+
return (
|
|
245
|
+
<ClientOnly>
|
|
246
|
+
{() => {
|
|
247
|
+
return <div>{window.location.href}</div>
|
|
248
|
+
}}
|
|
249
|
+
</ClientOnly>
|
|
250
|
+
)
|
|
251
|
+
}
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
> It's important that the children of `<ClientOnly>` is not a JSX element, but a function that returns an element.
|
|
255
|
+
> Because React will try to render children, and may use the client's API on the server.
|
|
256
|
+
|
|
257
|
+
## Document head
|
|
258
|
+
|
|
259
|
+
You can use `<Head/>` to manage all of your changes to the document head. It takes plain HTML tags and outputs plain HTML tags. It is a wrapper around [React Helmet](https://github.com/nfl/react-helmet).
|
|
260
|
+
|
|
261
|
+
```tsx
|
|
262
|
+
import { Head } from 'vite-react-ssg'
|
|
263
|
+
|
|
264
|
+
function MyHead() {
|
|
265
|
+
return (
|
|
266
|
+
<Head>
|
|
267
|
+
<meta property="og:description" content="My custom description" />
|
|
268
|
+
<meta charSet="utf-8" />
|
|
269
|
+
<title>My Title</title>
|
|
270
|
+
<link rel="canonical" href="http://mysite.com/example" />
|
|
271
|
+
</Head>
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
Nested or latter components will override duplicate usages:
|
|
277
|
+
|
|
278
|
+
```tsx
|
|
279
|
+
import { Head } from 'vite-react-ssg'
|
|
280
|
+
|
|
281
|
+
function MyHead() {
|
|
282
|
+
return (
|
|
283
|
+
<parent>
|
|
284
|
+
<Head>
|
|
285
|
+
<title>My Title</title>
|
|
286
|
+
<meta name="description" content="Helmet application" />
|
|
287
|
+
</Head>
|
|
288
|
+
<child>
|
|
289
|
+
<Head>
|
|
290
|
+
<title>Nested Title</title>
|
|
291
|
+
<meta name="description" content="Nested component" />
|
|
292
|
+
</Head>
|
|
293
|
+
</child>
|
|
294
|
+
</parent>
|
|
295
|
+
)
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
Outputs:
|
|
300
|
+
|
|
301
|
+
```html
|
|
302
|
+
<head>
|
|
303
|
+
<title>Nested Title</title>
|
|
304
|
+
<meta name="description" content="Nested component" />
|
|
305
|
+
</head>
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
### Reactive head
|
|
309
|
+
|
|
310
|
+
```tsx
|
|
311
|
+
import { useState } from 'react'
|
|
312
|
+
import { Head } from 'vite-react-ssg'
|
|
313
|
+
|
|
314
|
+
export default function MyHead() {
|
|
315
|
+
const [state, setState] = useState(false)
|
|
316
|
+
|
|
317
|
+
return (
|
|
318
|
+
<Head>
|
|
319
|
+
<meta charSet="UTF-8" />
|
|
320
|
+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
|
321
|
+
<title>head test {state ? 'A' : 'B'}</title>
|
|
322
|
+
{/* You can also set the 'body' attributes here */}
|
|
323
|
+
<body className={`body-class-in-head-${state ? 'a' : 'b'}`} />
|
|
324
|
+
</Head>
|
|
325
|
+
)
|
|
326
|
+
}
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
## Public Base Path
|
|
330
|
+
|
|
331
|
+
Just set `base` in vite.config.ts like:
|
|
332
|
+
|
|
333
|
+
```ts
|
|
334
|
+
// vite.config.ts
|
|
335
|
+
import { defineConfig } from 'vite'
|
|
336
|
+
import react from '@vitejs/plugin-react-swc'
|
|
337
|
+
|
|
338
|
+
// https://vitejs.dev/config/
|
|
339
|
+
export default defineConfig({
|
|
340
|
+
plugins: [react()],
|
|
341
|
+
base: '/base-path',
|
|
342
|
+
})
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
```ts
|
|
346
|
+
// main.ts
|
|
347
|
+
import { ViteReactSSG } from 'vite-react-ssg'
|
|
348
|
+
import { routes } from './App'
|
|
349
|
+
import './index.css'
|
|
350
|
+
|
|
351
|
+
export const createRoot = ViteReactSSG(
|
|
352
|
+
{
|
|
353
|
+
routes,
|
|
354
|
+
// pass your BASE_URL
|
|
355
|
+
basename: import.meta.env.BASE_URL,
|
|
356
|
+
},
|
|
357
|
+
)
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
Vite React SSG will give it to the react-router's `basename`.
|
|
361
|
+
|
|
362
|
+
See: [react-router's create-browser-router](https://reactrouter.com/en/main/routers/create-browser-router#basename)
|
|
363
|
+
|
|
364
|
+
[Example](./examples/lazy-pages/vite.config.ts)
|
|
365
|
+
|
|
366
|
+
## CSS in JS
|
|
367
|
+
|
|
368
|
+
Use the `getStyleCollector` option to specify an SSR/SSG style collector. Currently only supports `styled-components`.
|
|
369
|
+
|
|
370
|
+
```tsx
|
|
371
|
+
import { ViteReactSSG } from 'vite-react-ssg'
|
|
372
|
+
import getStyledComponentsCollector from 'vite-react-ssg/style-collectors/styled-components'
|
|
373
|
+
import { routes } from './App.js'
|
|
374
|
+
import './index.css'
|
|
375
|
+
|
|
376
|
+
export const createRoot = ViteReactSSG(
|
|
377
|
+
{ routes },
|
|
378
|
+
() => { },
|
|
379
|
+
{ getStyleCollector: getStyledComponentsCollector }
|
|
380
|
+
)
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
You can provide your own by looking at the [implementation](./src/style-collectors/) of any of the existing collectors.
|
|
384
|
+
|
|
385
|
+
## Critical CSS
|
|
386
|
+
|
|
387
|
+
Vite React SSG has built-in support for generating [Critical CSS](https://web.dev/extract-critical-css/) inlined in the HTML via the [`critters`](https://github.com/GoogleChromeLabs/critters) package.
|
|
388
|
+
Install it with:
|
|
389
|
+
|
|
390
|
+
```bash
|
|
391
|
+
npm i -D critters
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
Critical CSS generation will automatically be enabled for you.
|
|
395
|
+
|
|
396
|
+
To configure `critters`, pass [its options](https://github.com/GoogleChromeLabs/critters#usage) into `ssgOptions.crittersOptions` in `vite.config.ts`:
|
|
397
|
+
|
|
398
|
+
```ts
|
|
399
|
+
// vite.config.ts
|
|
400
|
+
export default defineConfig({
|
|
401
|
+
ssgOptions: {
|
|
402
|
+
crittersOptions: {
|
|
403
|
+
// E.g., change the preload strategy
|
|
404
|
+
preload: 'media',
|
|
405
|
+
// Other options: https://github.com/GoogleChromeLabs/critters#usage
|
|
406
|
+
},
|
|
407
|
+
},
|
|
408
|
+
})
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
## Configuration
|
|
412
|
+
|
|
413
|
+
You can pass options to Vite SSG in the `ssgOptions` field of your `vite.config.js`
|
|
414
|
+
|
|
415
|
+
```js
|
|
416
|
+
// vite.config.js
|
|
417
|
+
|
|
418
|
+
export default {
|
|
419
|
+
plugins: [],
|
|
420
|
+
ssgOptions: {
|
|
421
|
+
script: 'async',
|
|
422
|
+
},
|
|
423
|
+
}
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
```ts
|
|
427
|
+
interface ViteReactSSGOptions {
|
|
428
|
+
/**
|
|
429
|
+
* Set the scripts' loading mode. Only works for `type="module"`.
|
|
430
|
+
*
|
|
431
|
+
* @default 'sync'
|
|
432
|
+
*/
|
|
433
|
+
script?: 'sync' | 'async' | 'defer' | 'async defer'
|
|
434
|
+
/**
|
|
435
|
+
* Build format.
|
|
436
|
+
*
|
|
437
|
+
* @default 'esm'
|
|
438
|
+
*/
|
|
439
|
+
format?: 'esm' | 'cjs'
|
|
440
|
+
/**
|
|
441
|
+
* The path of the main entry file (relative to the project root).
|
|
442
|
+
*
|
|
443
|
+
* @default 'src/main.ts'
|
|
444
|
+
*/
|
|
445
|
+
entry?: string
|
|
446
|
+
/**
|
|
447
|
+
* Mock browser global variables (window, document, etc...) from SSG.
|
|
448
|
+
*
|
|
449
|
+
* @default false
|
|
450
|
+
*/
|
|
451
|
+
mock?: boolean
|
|
452
|
+
/**
|
|
453
|
+
* Apply formatter to the generated index file.
|
|
454
|
+
*
|
|
455
|
+
* **It will cause Hydration Failed.**
|
|
456
|
+
*
|
|
457
|
+
* @default 'none'
|
|
458
|
+
*/
|
|
459
|
+
formatting?: 'minify' | 'prettify' | 'none'
|
|
460
|
+
/**
|
|
461
|
+
* Vite environmeng mode.
|
|
462
|
+
*/
|
|
463
|
+
mode?: string
|
|
464
|
+
/**
|
|
465
|
+
* Directory style of the output directory.
|
|
466
|
+
*
|
|
467
|
+
* flat: `/foo` -> `/foo.html`
|
|
468
|
+
* nested: `/foo` -> `/foo/index.html`
|
|
469
|
+
*
|
|
470
|
+
* @default 'flat'
|
|
471
|
+
*/
|
|
472
|
+
dirStyle?: 'flat' | 'nested'
|
|
473
|
+
/**
|
|
474
|
+
* Generate for all routes, including dynamic routes.
|
|
475
|
+
* If enabled, you will need to configGure your serve
|
|
476
|
+
* manually to handle dynamic routes properly.
|
|
477
|
+
*
|
|
478
|
+
* @default false
|
|
479
|
+
*/
|
|
480
|
+
includeAllRoutes?: boolean
|
|
481
|
+
/**
|
|
482
|
+
* Options for the critters packages.
|
|
483
|
+
*
|
|
484
|
+
* @see https://github.com/GoogleChromeLabs/critters
|
|
485
|
+
*/
|
|
486
|
+
crittersOptions?: CrittersOptions | false
|
|
487
|
+
/**
|
|
488
|
+
* Custom function to modify the routes to do the SSG.
|
|
489
|
+
*
|
|
490
|
+
* Works only when `includeAllRoutes` is set to false.
|
|
491
|
+
*
|
|
492
|
+
* Defaults to a handler that filters out all the dynamic routes.
|
|
493
|
+
* When passing your custom handler, you should also take care of the dynamic routes yourself.
|
|
494
|
+
*/
|
|
495
|
+
includedRoutes?: (paths: string[], routes: Readonly<RouteRecord[]>) => Promise<string[]> | string[]
|
|
496
|
+
/**
|
|
497
|
+
* Callback to be called before every page render.
|
|
498
|
+
*
|
|
499
|
+
* It can be used to transform the project's `index.html` file before passing it to the renderer.
|
|
500
|
+
*
|
|
501
|
+
* To do so, you can change the 'index.html' file contents (passed in through the `indexHTML` parameter), and return it.
|
|
502
|
+
* The returned value will then be passed to renderer.
|
|
503
|
+
*/
|
|
504
|
+
onBeforePageRender?: (route: string, indexHTML: string, appCtx: ViteReactSSGContext<true>) => Promise<string | null | undefined> | string | null | undefined
|
|
505
|
+
/**
|
|
506
|
+
* Callback to be called on every rendered page.
|
|
507
|
+
*
|
|
508
|
+
* It can be used to transform the current route's rendered HTML.
|
|
509
|
+
*
|
|
510
|
+
* To do so, you can transform the route's rendered HTML (passed in through the `renderedHTML` parameter), and return it.
|
|
511
|
+
* The returned value will be used as the HTML of the route.
|
|
512
|
+
*/
|
|
513
|
+
onPageRendered?: (route: string, renderedHTML: string, appCtx: ViteReactSSGContext<true>) => Promise<string | null | undefined> | string | null | undefined
|
|
514
|
+
|
|
515
|
+
onFinished?: () => Promise<void> | void
|
|
516
|
+
/**
|
|
517
|
+
* The application's root container `id`.
|
|
518
|
+
*
|
|
519
|
+
* @default `root`
|
|
520
|
+
*/
|
|
521
|
+
rootContainerId?: string
|
|
522
|
+
/**
|
|
523
|
+
* The size of the SSG processing queue.
|
|
524
|
+
*
|
|
525
|
+
* @default 20
|
|
526
|
+
*/
|
|
527
|
+
concurrency?: number
|
|
528
|
+
}
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
See [src/types.ts](./src/types.ts). for more options available.
|
|
532
|
+
|
|
533
|
+
### Custom Routes to Render
|
|
534
|
+
|
|
535
|
+
You can use the `includedRoutes` hook to include or exclude route paths to render, or even provide some completely custom ones.
|
|
536
|
+
|
|
537
|
+
```js
|
|
538
|
+
// vite.config.js
|
|
539
|
+
|
|
540
|
+
export default {
|
|
541
|
+
plugins: [],
|
|
542
|
+
ssgOptions: {
|
|
543
|
+
includedRoutes(paths, routes) {
|
|
544
|
+
// exclude all the route paths that contains 'foo'
|
|
545
|
+
return paths.filter(i => !i.includes('foo'))
|
|
546
|
+
},
|
|
547
|
+
},
|
|
548
|
+
}
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
```js
|
|
552
|
+
// vite.config.js
|
|
553
|
+
|
|
554
|
+
export default {
|
|
555
|
+
plugins: [],
|
|
556
|
+
ssgOptions: {
|
|
557
|
+
includedRoutes(paths, routes) {
|
|
558
|
+
// use original route records
|
|
559
|
+
return routes.flatMap(route => {
|
|
560
|
+
return route.name === 'Blog'
|
|
561
|
+
? myBlogSlugs.map(slug => `/blog/${slug}`)
|
|
562
|
+
: route.path
|
|
563
|
+
})
|
|
564
|
+
},
|
|
565
|
+
},
|
|
566
|
+
}
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
```ts
|
|
570
|
+
export default defineConfig({
|
|
571
|
+
server: {
|
|
572
|
+
https: true,
|
|
573
|
+
},
|
|
574
|
+
})
|
|
575
|
+
```
|
|
576
|
+
|
|
577
|
+
## Roadmap
|
|
578
|
+
|
|
579
|
+
- [x] Preload assets
|
|
580
|
+
- [x] Document head
|
|
581
|
+
- [x] SSR in dev environment
|
|
582
|
+
- [x] More Client components, such as `<ClientOnly />`
|
|
583
|
+
- [x] `getStaticPaths` for dynamic routes
|
|
584
|
+
|
|
585
|
+
## Credits
|
|
586
|
+
|
|
587
|
+
This project inspired by [vite-ssg](https://github.com/antfu/vite-ssg), thanks to [@antfu](https://github.com/antfu) for his awesome work.
|
|
588
|
+
|
|
589
|
+
## License
|
|
590
|
+
|
|
591
|
+
[MIT](./LICENSE) License © 2023 [Riri](https://github.com/Daydreamer-riri)
|