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

fix: skip redirect and error fallback on middleware mode #4057

Merged
merged 4 commits into from Jul 2, 2021

Conversation

antfu
Copy link
Member

@antfu antfu commented Jun 30, 2021

Description

When serving vite server as middleware mode with base, it can't be used with sub routes as the base middleware will always redirect / to the base.

Consider base is set to /admin/ in the following examples.

// the default handle is been override with vite's fallback
// a.k.a only vite server work, but not the server original handlers
app.use(server.middlewares)
// the original handlers works, but as the base in `req.url` passed to vite is striped off by `connect`
// which cause the middleware resolve to `/admin/admin/` 
app.use('/admin/', server.middlewares)

Neither of these usages work. I think the baseMiddware is more like a friendly guide and warning for standalone vite server but not actually for middleware mode. Skipped the middleware in this PR.

Additional context

Current workaround:

  const middlewares = server.middlewares

  // remove Vite's base middleware since it's already handled by connect route
  // it's right before '/__open-in-editor' middleware
  const viteBaseMiddlewareIndex = middlewares.stack.findIndex(i => i.route === '/__open-in-editor') - 1
  if (viteBaseMiddlewareIndex >= 0) {
    middlewares.stack.splice(viteBaseMiddlewareIndex, 1)
  }

  app.use('/admin/', middlewares)

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@antfu antfu marked this pull request as draft June 30, 2021 23:33
@antfu antfu marked this pull request as ready for review July 1, 2021 00:26
@patak-dev patak-dev added the p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) label Jul 1, 2021
@antfu antfu merged commit d156a9f into vitejs:main Jul 2, 2021
@antfu antfu deleted the fix/middleware-redirect branch July 2, 2021 09:04
aleclarson pushed a commit to aleclarson/vite that referenced this pull request Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants