Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(ssr): properly handle vite.middlewares after restart #14917

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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: 3 additions & 1 deletion docs/config/server-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ async function createServer() {
appType: 'custom', // don't include Vite's default HTML handling middlewares
})
// Use vite's connect instance as middleware
app.use(vite.middlewares)
app.use((req, res, next) => {
vite.middlewares.handle(req, res, next)
})

app.use('*', async (req, res) => {
// Since `appType` is `'custom'`, should serve response here.
Expand Down
8 changes: 7 additions & 1 deletion docs/guide/ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ 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(vite.middlewares)
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)
})

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

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

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

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

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

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

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

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

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

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

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