Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions docs/config/server-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,7 @@ async function createServer() {
appType: 'custom', // don't include Vite's default HTML handling middlewares
})
// Use vite's connect instance as middleware
app.use((req, res, next) => {
vite.middlewares.handle(req, res, next)
})
app.use(vite.middlewares)

app.use('*', async (req, res) => {
// Since `appType` is `'custom'`, should serve response here.
Expand Down
4 changes: 1 addition & 3 deletions docs/guide/api-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ const vite = await createServer({
},
})

server.use((req, res, next) => {
vite.middlewares.handle(req, res, next)
})
parentServer.use(vite.middlewares)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code snippet was wrong, server should be parentServer

```

</details>
Expand Down
12 changes: 5 additions & 7 deletions docs/guide/ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,11 @@ async function createServer() {

// Use vite's connect instance as middleware. If you use your own
// express router (express.Router()), you should use router.use
app.use((req, res, next) => {
// When the server restarts (for example after the user modifies
// vite.config.js), `vite.middlewares` will be reassigned. Calling
// `vite.middlewares` inside a wrapper handler ensures that the
// latest Vite middlewares are always used.
vite.middlewares.handle(req, res, next)
})
// When the server restarts (for example after the user modifies
// vite.config.js), `vite.middlewares` is still going to be the same
// reference (with a new internal stack of Vite and plugin-injected
// middlewares. The following is valid even after restarts.
app.use(vite.middlewares)

app.use('*', async (req, res) => {
// serve index.html - we will tackle this next
Expand Down
7 changes: 7 additions & 0 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -957,9 +957,16 @@ async function restartServer(server: ViteDevServer) {
await server.close()

// Assign new server props to existing server instance
const middlewares = server.middlewares
newServer._configServerPort = server._configServerPort
newServer._currentServerPort = server._currentServerPort
Object.assign(server, newServer)

// Keep the same connect instance so app.use(vite.middlewares) works
// after a restart in middlewareMode (.route is always '/')
middlewares.stack = newServer.middlewares.stack
server.middlewares = middlewares

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference while reviewing the PR, this is the only code change in it. See the first commit: 0a9d8fb

// Rebind internal server variable so functions reference the user server
newServer._setInternalServer(server)

Expand Down
4 changes: 1 addition & 3 deletions playground/css-lightningcss-proxy/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ export async function createServer(root = process.cwd(), hmrPort) {
appType: 'custom',
})
// use vite's connect instance as middleware
app.use((req, res, next) => {
vite.middlewares.handle(req, res, next)
})
app.use(vite.middlewares)

app.use('*', async (req, res, next) => {
try {
Expand Down
4 changes: 1 addition & 3 deletions playground/json/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ export async function createServer(root = process.cwd(), hmrPort) {
},
})
// use vite's connect instance as middleware
app.use((req, res, next) => {
vite.middlewares.handle(req, res, next)
})
app.use(vite.middlewares)

app.use('*', async (req, res) => {
try {
Expand Down
4 changes: 1 addition & 3 deletions playground/optimize-missing-deps/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ export async function createServer(root = process.cwd(), hmrPort) {
},
appType: 'custom',
})
app.use((req, res, next) => {
vite.middlewares.handle(req, res, next)
})
app.use(vite.middlewares)

app.use('*', async (req, res) => {
try {
Expand Down
4 changes: 1 addition & 3 deletions playground/ssr-conditions/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export async function createServer(root = process.cwd(), hmrPort) {
appType: 'custom',
})

app.use((req, res, next) => {
vite.middlewares.handle(req, res, next)
})
app.use(vite.middlewares)

app.use('*', async (req, res) => {
try {
Expand Down
4 changes: 1 addition & 3 deletions playground/ssr-deps/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ export async function createServer(root = process.cwd(), hmrPort) {
],
})
// use vite's connect instance as middleware
app.use((req, res, next) => {
vite.middlewares.handle(req, res, next)
})
app.use(vite.middlewares)

app.use('*', async (req, res) => {
try {
Expand Down
4 changes: 1 addition & 3 deletions playground/ssr-html/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ export async function createServer(root = process.cwd(), hmrPort) {
],
})
// use vite's connect instance as middleware
app.use((req, res, next) => {
vite.middlewares.handle(req, res, next)
})
app.use(vite.middlewares)

app.use('*', async (req, res, next) => {
try {
Expand Down
4 changes: 1 addition & 3 deletions playground/ssr-noexternal/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ export async function createServer(
},
appType: 'custom',
})
app.use((req, res, next) => {
vite.middlewares.handle(req, res, next)
})
app.use(vite.middlewares)
}

app.use('*', async (req, res) => {
Expand Down
4 changes: 1 addition & 3 deletions playground/ssr-pug/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ export async function createServer(root = process.cwd(), hmrPort) {
appType: 'custom',
})
// use vite's connect instance as middleware
app.use((req, res, next) => {
vite.middlewares.handle(req, res, next)
})
app.use(vite.middlewares)

app.use('*', async (req, res) => {
try {
Expand Down
4 changes: 1 addition & 3 deletions playground/ssr/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ export async function createServer(
customLogger,
})
// use vite's connect instance as middleware
app.use((req, res, next) => {
vite.middlewares.handle(req, res, next)
})
app.use(vite.middlewares)

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