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

feat: preserve vite.middlewares connect instance after restarts #15166

Merged

Conversation

patak-dev
Copy link
Member

Description

Before releasing Vite 5, we decided to update our docs at:

This PR changed the backend integration handling of middlewares from

app.use(vite.middlewares)

to

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

The rationale was that the Vite server instance is already too magical, and we didn't want to make it more magical by preserving the vite.middlewares instance.

We've been discussing how to update all the projects using the previous recommendation and it is going to be challenging, there may always be bugs. We could rework the old server middelwares connect stack to log a warning but in that case, maybe better to just make the previous snippet work properly.

This PR rebinds the .stack of the newServer to preserve the connect instance. In the end, this doesn't look complex to me. And given that at one point we may offer a non-connect-based API, probably better to avoid disruption if the old snippet is easy to fix internally.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

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

@patak-dev patak-dev changed the title feat: preserver vite.middlewares connect instance after restarts feat: preserve vite.middlewares connect instance after restarts Nov 27, 2023
@patak-dev
Copy link
Member Author

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on 21ab87c: Open

suite result latest scheduled
analogjs success success
astro success success
histoire failure failure
ladle success success
laravel failure failure
marko success success
nuxt failure failure
nx success success
previewjs success success
qwik success success
rakkas success success
sveltekit failure failure
unocss success success
vike success success
vite-plugin-pwa success success
vite-plugin-react success success
vite-plugin-react-pages success success
vite-plugin-react-swc success success
vite-plugin-svelte success success
vite-plugin-vue success success
vite-setup-catalogue success success
vitepress success success
vitest success success

Comment on lines 960 to 969
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

@patak-dev patak-dev added the p3-minor-bug An edge case that only affects very specific usage (priority) label Nov 28, 2023
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

I don't really have a big counter-argument for this, other than it's making a new property magical. If others are fine with this, I'm also good with it.

@patak-dev
Copy link
Member Author

We discussed this PR in today's team meeting and decided we should move forward with it.

@patak-dev patak-dev merged commit 9474c4b into main Nov 29, 2023
15 checks passed
@patak-dev patak-dev deleted the feat/preserver-middlewares-connect-instance-after-restarts branch November 29, 2023 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants