Skip to content

Commit 1e87665

Browse files
committed
chore: add customize server
1 parent b9820ea commit 1e87665

File tree

7 files changed

+156
-3
lines changed

7 files changed

+156
-3
lines changed

README.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,63 @@ Compiled with `vite-ssr build`, there are three ways to start the server:
126126

127127
3. Customize server
128128

129-
// TODO
129+
`vite-ssr-vue3/server` export a function `createRender` to allows you create a render function.
130+
<details>
131+
<summary>Express example</summary>
130132

131-
In the above three ways, we need to strongly depend on the server, although we can use `ssr.noExternal` to package dependencies, but the server cannot be packaged in, you must install server package `npm install express`
133+
```js
134+
// ./src/express.js
135+
// @ts-check
136+
const path = require('path')
137+
const express = require('express')
138+
const { createRender } = require('vite-ssr-vue3/server')
139+
140+
async function createServer() {
141+
const out = path.join(__dirname, '../dist')
142+
const resolve = p => path.resolve(out, p)
143+
const app = express()
144+
app.use(express.static(resolve('./client'), {
145+
index: false,
146+
maxAge: '1y',
147+
}))
148+
149+
const render = await createRender({ isProd: true, root: __dirname, outDir: '../dist' })
150+
const { createApp } = require(resolve('./server/main.cjs'))
151+
152+
app.use('*', async(req, res) => {
153+
try {
154+
const url = req.originalUrl
155+
156+
if (req.method !== 'GET' || url === '/sw.js' || url === '/favicon.ico')
157+
return
158+
159+
const context = await createApp(false)
160+
let html = await render(url, { context })
161+
162+
html += '\n<!-- From customize server -->'
163+
164+
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
165+
}
166+
catch (error) {
167+
-console.error(error)
168+
res.status(500).end(error.stack)
169+
}
170+
})
171+
172+
return { server: app }
173+
}
174+
175+
createServer().then(({ server }) =>
176+
server.listen(3000, () => {
177+
// eslint-disable-next-line no-console
178+
console.info('http://localhost:3000')
179+
}),
180+
)
181+
```
182+
183+
</details>
184+
185+
I three ways, we need to strongly depend on the server, although we can use `ssr.noExternal` to package dependencies, but the server cannot be packaged in, you must install server package `npm install express`
132186

133187
we can modify the server building entry file to avoid this, create a new file to wrapper your server instance(*`vite-ssr` will not works*), configure `ssr.noExternal: /./` to package all dependencies. if you want to runing anywhere, this is recommended.
134188

examples/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pnpm basic:dev
1414
pnpm basic:build
1515
pnpm basic:serve
1616
pnpm basic:serve:bin
17+
pnpm basic:serve:custom
1718

1819
pnpm basic:build:noexternal
1920
pnpm basic:preview
@@ -23,6 +24,7 @@ pnpm vl-naive:dev
2324
pnpm vl-naive:build
2425
pnpm vl-naive:serve
2526
pnpm vl-naive:serve:bin
27+
pnpm vl-naive:serve:custom
2628

2729
pnpm vl-naive:build:noexternal
2830
pnpm vl-naive:preview

examples/basic/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"build": "vite-ssr build",
88
"serve": "vite-ssr --mode production",
99
"serve:bin": "node ./bin/www",
10+
"serve:custom": "node ./src/express",
1011
"build:noexternal": "vite-ssr build --config ./vite.config.noexternal.ts",
1112
"preview": "node ./dist/server/server.cjs"
1213
},

examples/basic/src/express.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// @ts-check
2+
const path = require('path')
3+
const express = require('express')
4+
const { createRender } = require('vite-ssr-vue3/server')
5+
6+
async function createServer() {
7+
const out = path.join(__dirname, '../dist')
8+
const resolve = p => path.resolve(out, p)
9+
const app = express()
10+
app.use(express.static(resolve('./client'), {
11+
index: false,
12+
maxAge: '1y',
13+
}))
14+
15+
const render = await createRender({ isProd: true, root: __dirname, outDir: '../dist' })
16+
const { createApp } = require(resolve('./server/main.cjs'))
17+
18+
app.use('*', async(req, res) => {
19+
try {
20+
const url = req.originalUrl
21+
22+
if (req.method !== 'GET' || url === '/sw.js' || url === '/favicon.ico')
23+
return
24+
25+
const context = await createApp(false)
26+
let html = await render(url, { context })
27+
28+
html += '\n<!-- From customize server -->'
29+
30+
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
31+
}
32+
catch (error) {
33+
console.error(error)
34+
res.status(500).end(error.stack)
35+
}
36+
})
37+
38+
return { server: app }
39+
}
40+
41+
createServer().then(({ server }) =>
42+
server.listen(3000, () => {
43+
// eslint-disable-next-line no-console
44+
console.info('http://localhost:3000')
45+
}),
46+
)

examples/vitesse-lite-naive-ui/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"dev": "vite-ssr --port 3333",
1313
"build": "vite-ssr build",
1414
"serve": "vite-ssr --mode production",
15-
"serve:bin": "node ./bin/www.js",
15+
"serve:bin": "node ./bin/www",
16+
"serve:custom": "node ./src/express",
1617
"build:noexternal": "vite-ssr build --config ./vite.config.noexternal.ts",
1718
"preview": "node ./dist/server/server.cjs",
1819
"lint": "eslint .",
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// @ts-check
2+
const path = require('path')
3+
const express = require('express')
4+
const { createRender } = require('vite-ssr-vue3/server')
5+
6+
async function createServer() {
7+
const out = path.join(__dirname, '../dist')
8+
const resolve = p => path.resolve(out, p)
9+
const app = express()
10+
app.use(express.static(resolve('./client'), {
11+
index: false,
12+
maxAge: '1y',
13+
}))
14+
15+
const render = await createRender({ isProd: true, root: __dirname, outDir: '../dist' })
16+
const { createApp } = require(resolve('./server/main.cjs'))
17+
18+
app.use('*', async(req, res) => {
19+
try {
20+
const url = req.originalUrl
21+
22+
if (req.method !== 'GET' || url === '/sw.js' || url === '/favicon.ico')
23+
return
24+
25+
const context = await createApp(false)
26+
let html = await render(url, { context })
27+
28+
html += '\n<!-- From customize server -->'
29+
30+
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
31+
}
32+
catch (error) {
33+
console.error(error)
34+
res.status(500).end(error.stack)
35+
}
36+
})
37+
38+
return { server: app }
39+
}
40+
41+
createServer().then(({ server }) =>
42+
server.listen(3000, () => {
43+
// eslint-disable-next-line no-console
44+
console.info('http://localhost:3000')
45+
}),
46+
)

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@
4747
"basic:build": "pnpm build & pnpm -F basic build",
4848
"basic:serve": "pnpm basic:build & pnpm -F basic serve",
4949
"basic:serve:bin": "pnpm basic:build & pnpm -F basic serve:bin",
50+
"basic:serve:custom": "pnpm basic:build & pnpm -F basic serve:custom",
5051
"basic:build:noexternal": "pnpm build & pnpm -F basic build:noexternal",
5152
"basic:preview": "pnpm basic:build:noexternal & pnpm -F basic preview",
5253
"vl-naive:dev": "pnpm dev \"pnpm -F vitesse-lite-naive-ui dev\"",
5354
"vl-naive:build": "pnpm build & pnpm -F vitesse-lite-naive-ui build",
5455
"vl-naive:serve": "pnpm vl-naive:build & pnpm -F vitesse-lite-naive-ui serve",
56+
"vl-naive:serve:bin": "pnpm vl-naive:build & pnpm -F vitesse-lite-naive-ui serve:bin",
57+
"vl-naive:serve:custom": "pnpm vl-naive:build & pnpm -F vitesse-lite-naive-ui serve:custom",
5558
"vl-naive:build:noexternal": "pnpm build & pnpm -F vitesse-lite-naive-ui build:noexternal",
5659
"vl-naive:preview": "pnpm vl-naive:build:noexternal & pnpm -F vitesse-lite-naive-ui preview",
5760
"lint": "eslint .",

0 commit comments

Comments
 (0)