Skip to content

Commit e49ef02

Browse files
patak-devbluwy
andauthored
docs(ssr): properly handle vite.middlewares after restart (#14917)
Co-authored-by: Bjorn Lu <[email protected]>
1 parent 84c5ff6 commit e49ef02

File tree

11 files changed

+37
-11
lines changed

11 files changed

+37
-11
lines changed

docs/config/server-options.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ async function createServer() {
249249
appType: 'custom', // don't include Vite's default HTML handling middlewares
250250
})
251251
// Use vite's connect instance as middleware
252-
app.use(vite.middlewares)
252+
app.use((req, res, next) => {
253+
vite.middlewares.handle(req, res, next)
254+
})
253255

254256
app.use('*', async (req, res) => {
255257
// Since `appType` is `'custom'`, should serve response here.

docs/guide/ssr.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,13 @@ async function createServer() {
8989

9090
// Use vite's connect instance as middleware. If you use your own
9191
// express router (express.Router()), you should use router.use
92-
app.use(vite.middlewares)
92+
app.use((req, res, next) => {
93+
// When the server restarts (for example after the user modifies
94+
// vite.config.js), `vite.middlewares` will be reassigned. Calling
95+
// `vite.middlewares` inside a wrapper handler ensures that the
96+
// latest Vite middlewares are always used.
97+
vite.middlewares.handle(req, res, next)
98+
})
9399

94100
app.use('*', async (req, res) => {
95101
// serve index.html - we will tackle this next

playground/css-lightningcss-proxy/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export async function createServer(root = process.cwd(), hmrPort) {
4545
appType: 'custom',
4646
})
4747
// use vite's connect instance as middleware
48-
app.use(vite.middlewares)
48+
app.use((req, res, next) => {
49+
vite.middlewares.handle(req, res, next)
50+
})
4951

5052
app.use('*', async (req, res, next) => {
5153
try {

playground/json/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ export async function createServer(root = process.cwd(), hmrPort) {
3737
},
3838
})
3939
// use vite's connect instance as middleware
40-
app.use(vite.middlewares)
40+
app.use((req, res, next) => {
41+
vite.middlewares.handle(req, res, next)
42+
})
4143

4244
app.use('*', async (req, res) => {
4345
try {

playground/optimize-missing-deps/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export async function createServer(root = process.cwd(), hmrPort) {
2626
},
2727
appType: 'custom',
2828
})
29-
app.use(vite.middlewares)
29+
app.use((req, res, next) => {
30+
vite.middlewares.handle(req, res, next)
31+
})
3032

3133
app.use('*', async (req, res) => {
3234
try {

playground/ssr-conditions/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export async function createServer(root = process.cwd(), hmrPort) {
3535
appType: 'custom',
3636
})
3737

38-
app.use(vite.middlewares)
38+
app.use((req, res, next) => {
39+
vite.middlewares.handle(req, res, next)
40+
})
3941

4042
app.use('*', async (req, res) => {
4143
try {

playground/ssr-deps/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ export async function createServer(root = process.cwd(), hmrPort) {
8484
],
8585
})
8686
// use vite's connect instance as middleware
87-
app.use(vite.middlewares)
87+
app.use((req, res, next) => {
88+
vite.middlewares.handle(req, res, next)
89+
})
8890

8991
app.use('*', async (req, res) => {
9092
try {

playground/ssr-html/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ export async function createServer(root = process.cwd(), hmrPort) {
6666
],
6767
})
6868
// use vite's connect instance as middleware
69-
app.use(vite.middlewares)
69+
app.use((req, res, next) => {
70+
vite.middlewares.handle(req, res, next)
71+
})
7072

7173
app.use('*', async (req, res, next) => {
7274
try {

playground/ssr-noexternal/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export async function createServer(
4444
},
4545
appType: 'custom',
4646
})
47-
app.use(vite.middlewares)
47+
app.use((req, res, next) => {
48+
vite.middlewares.handle(req, res, next)
49+
})
4850
}
4951

5052
app.use('*', async (req, res) => {

playground/ssr-pug/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export async function createServer(root = process.cwd(), hmrPort) {
4545
appType: 'custom',
4646
})
4747
// use vite's connect instance as middleware
48-
app.use(vite.middlewares)
48+
app.use((req, res, next) => {
49+
vite.middlewares.handle(req, res, next)
50+
})
4951

5052
app.use('*', async (req, res) => {
5153
try {

playground/ssr/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ export async function createServer(
3939
customLogger,
4040
})
4141
// use vite's connect instance as middleware
42-
app.use(vite.middlewares)
42+
app.use((req, res, next) => {
43+
vite.middlewares.handle(req, res, next)
44+
})
4345

4446
app.use('*', async (req, res, next) => {
4547
try {

0 commit comments

Comments
 (0)