vite-ssg-optimized 0.24.2-optimized.1
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 -0
- package/README.md +436 -0
- package/bin/vite-ssg.js +3 -0
- package/dist/chunks/jsdomGlobal.cjs +91 -0
- package/dist/chunks/jsdomGlobal.mjs +85 -0
- package/dist/client/single-page.cjs +52 -0
- package/dist/client/single-page.d.cts +12 -0
- package/dist/client/single-page.d.mts +12 -0
- package/dist/client/single-page.d.ts +12 -0
- package/dist/client/single-page.mjs +50 -0
- package/dist/index.cjs +88 -0
- package/dist/index.d.cts +12 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.mjs +86 -0
- package/dist/node/cli.cjs +52 -0
- package/dist/node/cli.d.cts +2 -0
- package/dist/node/cli.d.mts +2 -0
- package/dist/node/cli.d.ts +2 -0
- package/dist/node/cli.mjs +45 -0
- package/dist/node.cjs +17 -0
- package/dist/node.d.cts +9 -0
- package/dist/node.d.mts +9 -0
- package/dist/node.d.ts +9 -0
- package/dist/node.mjs +11 -0
- package/dist/shared/build.worker.cjs +72 -0
- package/dist/shared/build.worker.d.cts +29 -0
- package/dist/shared/build.worker.d.mts +29 -0
- package/dist/shared/build.worker.d.ts +29 -0
- package/dist/shared/build.worker.mjs +69 -0
- package/dist/shared/vite-ssg-optimized.0250a125.cjs +27 -0
- package/dist/shared/vite-ssg-optimized.12fd9d22.d.cts +9 -0
- package/dist/shared/vite-ssg-optimized.31f4c9de.cjs +1460 -0
- package/dist/shared/vite-ssg-optimized.579feabb.mjs +34 -0
- package/dist/shared/vite-ssg-optimized.5912142e.mjs +24 -0
- package/dist/shared/vite-ssg-optimized.892682c8.d.mts +9 -0
- package/dist/shared/vite-ssg-optimized.950926bc.d.ts +9 -0
- package/dist/shared/vite-ssg-optimized.bee8a5a9.cjs +37 -0
- package/dist/shared/vite-ssg-optimized.cf5cb4ee.d.cts +167 -0
- package/dist/shared/vite-ssg-optimized.cf5cb4ee.d.mts +167 -0
- package/dist/shared/vite-ssg-optimized.cf5cb4ee.d.ts +167 -0
- package/dist/shared/vite-ssg-optimized.dc2a4a38.mjs +1449 -0
- package/package.json +144 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Anthony Fu<https://github.com/antfu>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
# Vite SSG
|
|
2
|
+
|
|
3
|
+
Static-site generation for Vue 3 on Vite.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/vite-ssg)
|
|
6
|
+
|
|
7
|
+
> ℹ️ **Vite 2 is supported from `v0.2.x`, Vite 1's support is discontinued.**
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
> **This library requires Node.js version >= 14**
|
|
12
|
+
|
|
13
|
+
<pre>
|
|
14
|
+
<b>npm i -D vite-ssg</b> <em>vue-router @unhead/vue</em>
|
|
15
|
+
</pre>
|
|
16
|
+
|
|
17
|
+
```diff
|
|
18
|
+
// package.json
|
|
19
|
+
{
|
|
20
|
+
"scripts": {
|
|
21
|
+
"dev": "vite",
|
|
22
|
+
- "build": "vite build"
|
|
23
|
+
+ "build": "vite-ssg build"
|
|
24
|
+
|
|
25
|
+
// OR if you want to use another vite config file
|
|
26
|
+
+ "build": "vite-ssg build -c another-vite.config.ts"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
// src/main.ts
|
|
33
|
+
import { ViteSSG } from 'vite-ssg'
|
|
34
|
+
import App from './App.vue'
|
|
35
|
+
|
|
36
|
+
// `export const createApp` is required instead of the original `createApp(App).mount('#app')`
|
|
37
|
+
export const createApp = ViteSSG(
|
|
38
|
+
// the root component
|
|
39
|
+
App,
|
|
40
|
+
// vue-router options
|
|
41
|
+
{ routes },
|
|
42
|
+
// function to have custom setups
|
|
43
|
+
({ app, router, routes, isClient, initialState }) => {
|
|
44
|
+
// install plugins etc.
|
|
45
|
+
},
|
|
46
|
+
)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Single Page SSG
|
|
50
|
+
|
|
51
|
+
For SSG of an index page only (i.e. without `vue-router`); import `vite-ssg/single-page` instead, and only install `@unhead/vue` (`npm i -D vite-ssg @unhead/vue`).
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
// src/main.ts
|
|
55
|
+
import { ViteSSG } from 'vite-ssg/single-page'
|
|
56
|
+
import App from './App.vue'
|
|
57
|
+
|
|
58
|
+
// `export const createApp` is required instead of the original `createApp(App).mount('#app')`
|
|
59
|
+
export const createApp = ViteSSG(App)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### `<ClientOnly/>`
|
|
63
|
+
|
|
64
|
+
The `ClientOnly` component is registered globally when the app is created.
|
|
65
|
+
|
|
66
|
+
```html
|
|
67
|
+
<client-only>
|
|
68
|
+
<your-client-side-components />
|
|
69
|
+
<template #placeholder>
|
|
70
|
+
<your-placeholder-components />
|
|
71
|
+
</template>
|
|
72
|
+
</client-only>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Document head
|
|
76
|
+
|
|
77
|
+
We ship [`@unhead/vue`](https://unhead.harlanzw.com/integrations/vue/setup) to manage the document-head out of the box. You can use it directly in your pages/components.
|
|
78
|
+
For example:
|
|
79
|
+
|
|
80
|
+
```html
|
|
81
|
+
<template>
|
|
82
|
+
<button @click="count++">Click</button>
|
|
83
|
+
</template>
|
|
84
|
+
|
|
85
|
+
<script setup>
|
|
86
|
+
import { useHead } from '@unhead/vue'
|
|
87
|
+
|
|
88
|
+
useHead({
|
|
89
|
+
title: 'Website Title',
|
|
90
|
+
meta: [
|
|
91
|
+
{
|
|
92
|
+
name: 'description',
|
|
93
|
+
content: 'Website description',
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
})
|
|
97
|
+
</script>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
That's all! No configuration is needed. Vite SSG will automatically handle the server-side rendering and merging.
|
|
101
|
+
|
|
102
|
+
See [`@unhead/vue`'s docs](https://unhead.unjs.io/setup/vue/installation) for more usage information about `useHead`.
|
|
103
|
+
|
|
104
|
+
## Critical CSS
|
|
105
|
+
|
|
106
|
+
Vite SSG has built-in support for generating [Critical CSS](https://web.dev/extract-critical-css/) inlined in the HTML via the [`beasties`](https://github.com/danielroe/beasties) package.
|
|
107
|
+
Install it with:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
npm i -D beasties
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Critical CSS generation will automatically be enabled for you.
|
|
114
|
+
|
|
115
|
+
To configure `beasties`, pass [its options](https://github.com/danielroe/beasties#usage) into `ssgOptions.beastiesOptions` in `vite.config.ts`:
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
// vite.config.ts
|
|
119
|
+
export default defineConfig({
|
|
120
|
+
ssgOptions: {
|
|
121
|
+
beastiesOptions: {
|
|
122
|
+
// E.g., change the preload strategy
|
|
123
|
+
preload: 'media',
|
|
124
|
+
// Other options: https://github.com/danielroe/beasties#usage
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
})
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Initial State
|
|
131
|
+
|
|
132
|
+
The initial state comprises data that is serialized with your server-side generated HTML and is hydrated in
|
|
133
|
+
the browser when accessed. This data can be data fetched from a CDN, an API, etc, and is typically needed
|
|
134
|
+
as soon as the application starts or is accessed for the first time.
|
|
135
|
+
|
|
136
|
+
The main advantage of setting the application's initial state is that the statically generated pages do not
|
|
137
|
+
need to refetch the data as it is fetched and serialized into the page's HTML at build time.
|
|
138
|
+
|
|
139
|
+
The initial state is a plain JavaScript object that can be set during SSR. I.e. when statically generating
|
|
140
|
+
the pages like this:
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
// src/main.ts
|
|
144
|
+
|
|
145
|
+
// ...
|
|
146
|
+
|
|
147
|
+
export const createApp = ViteSSG(
|
|
148
|
+
App,
|
|
149
|
+
{ routes },
|
|
150
|
+
({ app, router, routes, isClient, initialState }) => {
|
|
151
|
+
// ...
|
|
152
|
+
|
|
153
|
+
if (import.meta.env.SSR) {
|
|
154
|
+
// Set initial state during server side
|
|
155
|
+
initialState.data = { cats: 2, dogs: 3 }
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
// Restore or read the initial state on the client side in the browser
|
|
159
|
+
console.log(initialState.data) // => { cats: 2, dogs: 3 }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// ...
|
|
163
|
+
},
|
|
164
|
+
)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Typically, you will use this with an application store, such as
|
|
168
|
+
[Vuex](https://vuex.vuejs.org/) or [Pinia](https://pinia.esm.dev/).
|
|
169
|
+
See below for examples:
|
|
170
|
+
|
|
171
|
+
<details><summary>When using Pinia</summary>
|
|
172
|
+
|
|
173
|
+
Following [Pinia's guide](https://pinia.esm.dev/ssr), you will to adapt your `main.{ts,js}` file to look
|
|
174
|
+
like this:
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
import { createPinia } from 'pinia'
|
|
178
|
+
import routes from 'virtual:generated-pages'
|
|
179
|
+
// main.ts
|
|
180
|
+
import { ViteSSG } from 'vite-ssg'
|
|
181
|
+
|
|
182
|
+
import App from './App.vue'
|
|
183
|
+
// use any store you configured that you need data from on start-up
|
|
184
|
+
import { useRootStore } from './store/root'
|
|
185
|
+
|
|
186
|
+
export const createApp = ViteSSG(
|
|
187
|
+
App,
|
|
188
|
+
{ routes },
|
|
189
|
+
({ app, router, initialState }) => {
|
|
190
|
+
const pinia = createPinia()
|
|
191
|
+
app.use(pinia)
|
|
192
|
+
|
|
193
|
+
if (import.meta.env.SSR)
|
|
194
|
+
initialState.pinia = pinia.state.value
|
|
195
|
+
else
|
|
196
|
+
pinia.state.value = initialState.pinia || {}
|
|
197
|
+
|
|
198
|
+
router.beforeEach((to, from, next) => {
|
|
199
|
+
const store = useRootStore(pinia)
|
|
200
|
+
if (!store.ready)
|
|
201
|
+
// perform the (user-implemented) store action to fill the store's state
|
|
202
|
+
store.initialize()
|
|
203
|
+
next()
|
|
204
|
+
})
|
|
205
|
+
},
|
|
206
|
+
)
|
|
207
|
+
```
|
|
208
|
+
</details>
|
|
209
|
+
|
|
210
|
+
<details><summary>When using Vuex</summary>
|
|
211
|
+
<p>
|
|
212
|
+
|
|
213
|
+
```ts
|
|
214
|
+
import routes from 'virtual:generated-pages'
|
|
215
|
+
// main.ts
|
|
216
|
+
import { ViteSSG } from 'vite-ssg'
|
|
217
|
+
import { createStore } from 'vuex'
|
|
218
|
+
import App from './App.vue'
|
|
219
|
+
|
|
220
|
+
// Normally, you should definitely put this in a separate file
|
|
221
|
+
// in order to be able to use it everywhere
|
|
222
|
+
const store = createStore({
|
|
223
|
+
// ...
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
export const createApp = ViteSSG(
|
|
227
|
+
App,
|
|
228
|
+
{ routes },
|
|
229
|
+
({ app, router, initialState }) => {
|
|
230
|
+
app.use(store)
|
|
231
|
+
|
|
232
|
+
if (import.meta.env.SSR)
|
|
233
|
+
initialState.store = store.state
|
|
234
|
+
else
|
|
235
|
+
store.replaceState(initialState.store)
|
|
236
|
+
|
|
237
|
+
router.beforeEach((to, from, next) => {
|
|
238
|
+
// perform the (user-implemented) store action to fill the store's state
|
|
239
|
+
if (!store.getters.ready)
|
|
240
|
+
store.dispatch('initialize')
|
|
241
|
+
|
|
242
|
+
next()
|
|
243
|
+
})
|
|
244
|
+
},
|
|
245
|
+
)
|
|
246
|
+
```
|
|
247
|
+
</p></details>
|
|
248
|
+
|
|
249
|
+
For an example on how to use a store with an initial state in a single page app,
|
|
250
|
+
see [the single page example](./examples/single-page/src/main.ts).
|
|
251
|
+
|
|
252
|
+
### State Serialization
|
|
253
|
+
|
|
254
|
+
By default, the state is deserialized and serialized by using `JSON.stringify` and `JSON.parse` respectively.
|
|
255
|
+
If this approach works for you, you should definitely stick to it as it yields far better performance.
|
|
256
|
+
|
|
257
|
+
You may use the `transformState` option in the `ViteSSGClientOptions` options object as shown below.
|
|
258
|
+
A valid approach besides `JSON.stringify` and `JSON.parse` is
|
|
259
|
+
[`@nuxt/devalue`](https://github.com/nuxt-contrib/devalue) (which is used by Nuxt.js):
|
|
260
|
+
|
|
261
|
+
```ts
|
|
262
|
+
import devalue from '@nuxt/devalue'
|
|
263
|
+
import { ViteSSG } from 'vite-ssg'
|
|
264
|
+
|
|
265
|
+
// ...
|
|
266
|
+
import App from './App.vue'
|
|
267
|
+
|
|
268
|
+
export const createApp = ViteSSG(
|
|
269
|
+
App,
|
|
270
|
+
{ routes },
|
|
271
|
+
({ app, router, initialState }) => {
|
|
272
|
+
// ...
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
transformState(state) {
|
|
276
|
+
return import.meta.env.SSR ? devalue(state) : state
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
)
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
**A minor remark when using `@nuxt/devalue`:** In case, you are getting an error because of a `require`
|
|
283
|
+
within the package `@nuxt/devalue`, you have to add the following piece of config to your Vite config:
|
|
284
|
+
|
|
285
|
+
```ts
|
|
286
|
+
// vite.config.ts
|
|
287
|
+
// ...
|
|
288
|
+
|
|
289
|
+
export default defineConfig({
|
|
290
|
+
resolve: {
|
|
291
|
+
alias: {
|
|
292
|
+
'@nuxt/devalue': '@nuxt/devalue/dist/devalue.js',
|
|
293
|
+
},
|
|
294
|
+
},
|
|
295
|
+
// ...
|
|
296
|
+
})
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### Async Components
|
|
300
|
+
|
|
301
|
+
Some applications may make use of Vue features that cause components to render asynchronously (e.g. [`suspense`](https://v3.vuejs.org/guide/migration/suspense.html)). When these features are used in ways that can influence `initialState`, the `onSSRAppRendered` may be used in order to ensure that all async operations are complete during the initial application render.
|
|
302
|
+
For example:
|
|
303
|
+
|
|
304
|
+
```ts
|
|
305
|
+
const { app, router, initialState, isClient, onSSRAppRendered } = ctx
|
|
306
|
+
|
|
307
|
+
const pinia = createPinia()
|
|
308
|
+
app.use(pinia)
|
|
309
|
+
|
|
310
|
+
if (isClient) {
|
|
311
|
+
pinia.state.value = (initialState.pinia) || {}
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
onSSRAppRendered(() => {
|
|
315
|
+
initialState.pinia = pinia.state.value
|
|
316
|
+
})
|
|
317
|
+
}
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
## Configuration
|
|
321
|
+
|
|
322
|
+
You can pass options to Vite SSG in the `ssgOptions` field of your `vite.config.js`
|
|
323
|
+
|
|
324
|
+
```js
|
|
325
|
+
// vite.config.js
|
|
326
|
+
|
|
327
|
+
export default {
|
|
328
|
+
plugins: [],
|
|
329
|
+
ssgOptions: {
|
|
330
|
+
script: 'async',
|
|
331
|
+
},
|
|
332
|
+
}
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
See [src/types.ts](./src/types.ts). for more options available.
|
|
336
|
+
|
|
337
|
+
### Custom Routes to Render
|
|
338
|
+
|
|
339
|
+
You can use the `includedRoutes` hook to include or exclude route paths to render, or even provide some completely custom ones.
|
|
340
|
+
|
|
341
|
+
```js
|
|
342
|
+
// vite.config.js
|
|
343
|
+
|
|
344
|
+
export default {
|
|
345
|
+
plugins: [],
|
|
346
|
+
ssgOptions: {
|
|
347
|
+
includedRoutes(paths, routes) {
|
|
348
|
+
// exclude all the route paths that contains 'foo'
|
|
349
|
+
return paths.filter(i => !i.includes('foo'))
|
|
350
|
+
},
|
|
351
|
+
},
|
|
352
|
+
}
|
|
353
|
+
```
|
|
354
|
+
```js
|
|
355
|
+
// vite.config.js
|
|
356
|
+
|
|
357
|
+
export default {
|
|
358
|
+
plugins: [],
|
|
359
|
+
ssgOptions: {
|
|
360
|
+
includedRoutes(paths, routes) {
|
|
361
|
+
// use original route records
|
|
362
|
+
return routes.flatMap((route) => {
|
|
363
|
+
return route.name === 'Blog'
|
|
364
|
+
? myBlogSlugs.map(slug => `/blog/${slug}`)
|
|
365
|
+
: route.path
|
|
366
|
+
})
|
|
367
|
+
},
|
|
368
|
+
},
|
|
369
|
+
}
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
Alternatively, you may export the `includedRoutes` hook from your server entry file. This will be necessary if fetching your routes requires the use of environment variables managed by Vite.
|
|
373
|
+
|
|
374
|
+
```ts
|
|
375
|
+
// main.ts
|
|
376
|
+
|
|
377
|
+
import { ViteSSG } from 'vite-ssg'
|
|
378
|
+
import App from './App.vue'
|
|
379
|
+
|
|
380
|
+
export const createApp = ViteSSG(
|
|
381
|
+
App,
|
|
382
|
+
{ routes },
|
|
383
|
+
({ app, router, initialState }) => {
|
|
384
|
+
// ...
|
|
385
|
+
},
|
|
386
|
+
)
|
|
387
|
+
export async function includedRoutes(paths, routes) {
|
|
388
|
+
// Sensitive key is managed by Vite - this would not be available inside
|
|
389
|
+
// vite.config.js as it runs before the environment has been populated.
|
|
390
|
+
const apiClient = new MyApiClient(import.meta.env.MY_API_KEY)
|
|
391
|
+
|
|
392
|
+
return Promise.all(
|
|
393
|
+
routes.flatMap(async (route) => {
|
|
394
|
+
return route.name === 'Blog'
|
|
395
|
+
? (await apiClient.fetchBlogSlugs()).map(slug => `/blog/${slug}`)
|
|
396
|
+
: route.path
|
|
397
|
+
}),
|
|
398
|
+
)
|
|
399
|
+
}
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
## Comparison
|
|
403
|
+
|
|
404
|
+
### Use [Vitepress](https://github.com/vuejs/vitepress) when you want:
|
|
405
|
+
|
|
406
|
+
- Zero config, out of the box SSG
|
|
407
|
+
- A single-purpose documentation site
|
|
408
|
+
- Lightweight ([No double payload](https://twitter.com/youyuxi/status/1274834280091389955))
|
|
409
|
+
|
|
410
|
+
### Use Vite SSG when you want:
|
|
411
|
+
|
|
412
|
+
- More control on the build process and tooling
|
|
413
|
+
- The flexible plugin system
|
|
414
|
+
- Multi-purpose application with some SSG to improve SEO and loading speed
|
|
415
|
+
|
|
416
|
+
Cons:
|
|
417
|
+
|
|
418
|
+
- Double payload
|
|
419
|
+
|
|
420
|
+
## Example
|
|
421
|
+
|
|
422
|
+
See [Vitesse](https://github.com/antfu/vitesse).
|
|
423
|
+
|
|
424
|
+
## Thanks to the prior work
|
|
425
|
+
|
|
426
|
+
- [vitepress](https://github.com/vuejs/vitepress/tree/master/src/node/build)
|
|
427
|
+
- [vue3-vite-ssr-example](https://github.com/tbgse/vue3-vite-ssr-example)
|
|
428
|
+
- [vite-ssr](https://github.com/frandiox/vite-ssr)
|
|
429
|
+
|
|
430
|
+
## Contribution
|
|
431
|
+
|
|
432
|
+
Please refer to https://github.com/antfu/contribute.
|
|
433
|
+
|
|
434
|
+
## License
|
|
435
|
+
|
|
436
|
+
MIT License © 2020-PRESENT [Anthony Fu](https://github.com/antfu)
|
package/bin/vite-ssg.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const JSDOM = require('jsdom');
|
|
4
|
+
|
|
5
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
6
|
+
|
|
7
|
+
const JSDOM__default = /*#__PURE__*/_interopDefaultCompat(JSDOM);
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
MIT License
|
|
11
|
+
|
|
12
|
+
Copyright for portions of global-jsdom are held by Rico Sta. Cruz, 2016 as part of
|
|
13
|
+
jsdom-global. All other copyright for global-jsdom are held by jonathan schatz, 2017.
|
|
14
|
+
|
|
15
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
16
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
17
|
+
in the Software without restriction, including without limitation the rights
|
|
18
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
19
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
20
|
+
furnished to do so, subject to the following conditions:
|
|
21
|
+
|
|
22
|
+
The above copyright notice and this permission notice shall be included in all
|
|
23
|
+
copies or substantial portions of the Software.
|
|
24
|
+
|
|
25
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
26
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
27
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
28
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
29
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
30
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
31
|
+
SOFTWARE.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
const defaultHtml = '<!doctype html><html><head><meta charset="utf-8"></head><body></body></html>';
|
|
36
|
+
|
|
37
|
+
// define this here so that we only ever dynamically populate KEYS once.
|
|
38
|
+
|
|
39
|
+
const KEYS = [];
|
|
40
|
+
|
|
41
|
+
function jsdomGlobal(html = defaultHtml, options = {}) {
|
|
42
|
+
// Idempotency
|
|
43
|
+
if (global.navigator
|
|
44
|
+
&& global.navigator.userAgent
|
|
45
|
+
&& global.navigator.userAgent.includes('Node.js')
|
|
46
|
+
&& global.document
|
|
47
|
+
&& typeof global.document.destroy === 'function') {
|
|
48
|
+
return global.document.destroy
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// set a default url if we don't get one - otherwise things explode when we copy localstorage keys
|
|
52
|
+
if (!('url' in options))
|
|
53
|
+
Object.assign(options, { url: 'http://localhost:3000' });
|
|
54
|
+
|
|
55
|
+
// enable pretendToBeVisual by default since react needs
|
|
56
|
+
// window.requestAnimationFrame, see https://github.com/jsdom/jsdom#pretending-to-be-a-visual-browser
|
|
57
|
+
if (!('pretendToBeVisual' in options))
|
|
58
|
+
Object.assign(options, { pretendToBeVisual: true });
|
|
59
|
+
|
|
60
|
+
const jsdom = new JSDOM__default.JSDOM(html, options);
|
|
61
|
+
const { window } = jsdom;
|
|
62
|
+
const { document } = window;
|
|
63
|
+
|
|
64
|
+
// generate our list of keys by enumerating document.window - this list may vary
|
|
65
|
+
// based on the jsdom version. filter out internal methods as well as anything
|
|
66
|
+
// that node already defines
|
|
67
|
+
|
|
68
|
+
if (KEYS.length === 0) {
|
|
69
|
+
KEYS.push(...Object.getOwnPropertyNames(window).filter(k => !k.startsWith('_')).filter(k => !(k in global)));
|
|
70
|
+
// going to add our jsdom instance, see below
|
|
71
|
+
KEYS.push('$jsdom');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
KEYS.forEach(key => global[key] = window[key]);
|
|
75
|
+
|
|
76
|
+
// setup document / window / window.console
|
|
77
|
+
global.document = document;
|
|
78
|
+
global.window = window;
|
|
79
|
+
window.console = global.console;
|
|
80
|
+
|
|
81
|
+
// add access to our jsdom instance
|
|
82
|
+
global.$jsdom = jsdom;
|
|
83
|
+
|
|
84
|
+
const cleanup = () => KEYS.forEach(key => delete global[key]);
|
|
85
|
+
|
|
86
|
+
document.destroy = cleanup;
|
|
87
|
+
|
|
88
|
+
return cleanup
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
exports.jsdomGlobal = jsdomGlobal;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import JSDOM from 'jsdom';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
MIT License
|
|
5
|
+
|
|
6
|
+
Copyright for portions of global-jsdom are held by Rico Sta. Cruz, 2016 as part of
|
|
7
|
+
jsdom-global. All other copyright for global-jsdom are held by jonathan schatz, 2017.
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
const defaultHtml = '<!doctype html><html><head><meta charset="utf-8"></head><body></body></html>';
|
|
30
|
+
|
|
31
|
+
// define this here so that we only ever dynamically populate KEYS once.
|
|
32
|
+
|
|
33
|
+
const KEYS = [];
|
|
34
|
+
|
|
35
|
+
function jsdomGlobal(html = defaultHtml, options = {}) {
|
|
36
|
+
// Idempotency
|
|
37
|
+
if (global.navigator
|
|
38
|
+
&& global.navigator.userAgent
|
|
39
|
+
&& global.navigator.userAgent.includes('Node.js')
|
|
40
|
+
&& global.document
|
|
41
|
+
&& typeof global.document.destroy === 'function') {
|
|
42
|
+
return global.document.destroy
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// set a default url if we don't get one - otherwise things explode when we copy localstorage keys
|
|
46
|
+
if (!('url' in options))
|
|
47
|
+
Object.assign(options, { url: 'http://localhost:3000' });
|
|
48
|
+
|
|
49
|
+
// enable pretendToBeVisual by default since react needs
|
|
50
|
+
// window.requestAnimationFrame, see https://github.com/jsdom/jsdom#pretending-to-be-a-visual-browser
|
|
51
|
+
if (!('pretendToBeVisual' in options))
|
|
52
|
+
Object.assign(options, { pretendToBeVisual: true });
|
|
53
|
+
|
|
54
|
+
const jsdom = new JSDOM.JSDOM(html, options);
|
|
55
|
+
const { window } = jsdom;
|
|
56
|
+
const { document } = window;
|
|
57
|
+
|
|
58
|
+
// generate our list of keys by enumerating document.window - this list may vary
|
|
59
|
+
// based on the jsdom version. filter out internal methods as well as anything
|
|
60
|
+
// that node already defines
|
|
61
|
+
|
|
62
|
+
if (KEYS.length === 0) {
|
|
63
|
+
KEYS.push(...Object.getOwnPropertyNames(window).filter(k => !k.startsWith('_')).filter(k => !(k in global)));
|
|
64
|
+
// going to add our jsdom instance, see below
|
|
65
|
+
KEYS.push('$jsdom');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
KEYS.forEach(key => global[key] = window[key]);
|
|
69
|
+
|
|
70
|
+
// setup document / window / window.console
|
|
71
|
+
global.document = document;
|
|
72
|
+
global.window = window;
|
|
73
|
+
window.console = global.console;
|
|
74
|
+
|
|
75
|
+
// add access to our jsdom instance
|
|
76
|
+
global.$jsdom = jsdom;
|
|
77
|
+
|
|
78
|
+
const cleanup = () => KEYS.forEach(key => delete global[key]);
|
|
79
|
+
|
|
80
|
+
document.destroy = cleanup;
|
|
81
|
+
|
|
82
|
+
return cleanup
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export { jsdomGlobal };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const vue$1 = require('@unhead/vue');
|
|
4
|
+
const vue = require('vue');
|
|
5
|
+
const ClientOnly = require('../shared/vite-ssg-optimized.0250a125.cjs');
|
|
6
|
+
const state = require('../shared/vite-ssg-optimized.bee8a5a9.cjs');
|
|
7
|
+
|
|
8
|
+
function ViteSSG(App, fn, options = {}) {
|
|
9
|
+
const {
|
|
10
|
+
transformState,
|
|
11
|
+
registerComponents = true,
|
|
12
|
+
useHead = true,
|
|
13
|
+
rootContainer = "#app"
|
|
14
|
+
} = options;
|
|
15
|
+
const isClient = typeof window !== "undefined";
|
|
16
|
+
async function createApp(client = false) {
|
|
17
|
+
const app = client ? vue.createApp(App) : vue.createSSRApp(App);
|
|
18
|
+
let head;
|
|
19
|
+
if (useHead) {
|
|
20
|
+
head = client ? vue$1.createHead() : vue$1.createServerHead();
|
|
21
|
+
app.use(head);
|
|
22
|
+
}
|
|
23
|
+
const appRenderCallbacks = [];
|
|
24
|
+
const onSSRAppRendered = client ? () => {
|
|
25
|
+
} : (cb) => appRenderCallbacks.push(cb);
|
|
26
|
+
const triggerOnSSRAppRendered = () => {
|
|
27
|
+
return Promise.all(appRenderCallbacks.map((cb) => cb()));
|
|
28
|
+
};
|
|
29
|
+
const context = { app, head, isClient, router: void 0, routes: void 0, initialState: {}, onSSRAppRendered, triggerOnSSRAppRendered, transformState };
|
|
30
|
+
if (registerComponents)
|
|
31
|
+
app.component("ClientOnly", ClientOnly.ClientOnly);
|
|
32
|
+
if (client) {
|
|
33
|
+
await ClientOnly.documentReady();
|
|
34
|
+
context.initialState = transformState?.(window.__INITIAL_STATE__ || {}) || state.deserializeState(window.__INITIAL_STATE__);
|
|
35
|
+
}
|
|
36
|
+
await fn?.(context);
|
|
37
|
+
const initialState = context.initialState;
|
|
38
|
+
return {
|
|
39
|
+
...context,
|
|
40
|
+
initialState
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (isClient) {
|
|
44
|
+
(async () => {
|
|
45
|
+
const { app } = await createApp(true);
|
|
46
|
+
app.mount(rootContainer, true);
|
|
47
|
+
})();
|
|
48
|
+
}
|
|
49
|
+
return createApp;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
exports.ViteSSG = ViteSSG;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Component } from 'vue';
|
|
2
|
+
import { V as ViteSSGContext, a as ViteSSGClientOptions } from '../shared/vite-ssg-optimized.cf5cb4ee.cjs';
|
|
3
|
+
export { R as RouterOptions, b as ViteSSGOptions } from '../shared/vite-ssg-optimized.cf5cb4ee.cjs';
|
|
4
|
+
import '@unhead/vue';
|
|
5
|
+
import 'beasties';
|
|
6
|
+
import 'critters';
|
|
7
|
+
import 'vue-router';
|
|
8
|
+
import 'html-minifier-terser';
|
|
9
|
+
|
|
10
|
+
declare function ViteSSG(App: Component, fn?: (context: ViteSSGContext<false>) => Promise<void> | void, options?: ViteSSGClientOptions): (client?: boolean) => Promise<ViteSSGContext<false>>;
|
|
11
|
+
|
|
12
|
+
export { ViteSSG, ViteSSGClientOptions, ViteSSGContext };
|