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

Vite uses and passes a different server instance to plugins after server restart #15589

Closed
7 tasks done
Artur- opened this issue Jan 12, 2024 · 5 comments
Closed
7 tasks done

Comments

@Artur-
Copy link
Contributor

Artur- commented Jan 12, 2024

Describe the bug

  1. Start Vite using a basepath of /VAADIN
  2. Request a non-existing file, e.g. curl http://127.0.0.1:5173/VAADIN/foo.js -v

Problem 1, index.html is returned:

< HTTP/1.1 200 OK
< Content-Type: text/html
< Content-Length: 1036
  1. Request the file again, e.g. curl http://127.0.0.1:5173/VAADIN/foo.js -v

Now it works correctly:

< HTTP/1.1 404 Not Found
< Content-Length: 0
  1. Restart Vite by pressing r+enter in the Vite console

  2. Request the file again, e.g. curl http://127.0.0.1:5173/VAADIN/foo.js -v

Problem 1, index.html is returned again. And no matter how many times you request the file, index.html is returned.

The reason this is a problem is that we proxy requests to Vite and when Vite does not return a file, we check in other locations. Now when Vite returns the file that does not exist, we do not check for the real foo.js and instead serve the wrong file (index.html)

Reproduction

Presumably related to something in the project as it does not reproduce in a simple project

Steps to reproduce

No response

System Info

System:
    OS: macOS 14.2
    CPU: (10) arm64 Apple M1 Max
    Memory: 673.84 MB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.9.0 - ~/.nvm/versions/node/v20.9.0/bin/node
    npm: 10.1.0 - ~/.nvm/versions/node/v20.9.0/bin/npm
    bun: 1.0.7 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 107.1.45.123
    Chrome: 120.0.6099.216
    Chrome Canary: 122.0.6241.4
    Edge: 120.0.2210.133
    Safari: 17.2
    Safari Technology Preview: 17.4
  npmPackages:
    @vitejs/plugin-react: 4.2.0 => 4.2.0 
    @vitejs/plugin-react-swc: 3.5.0 => 3.5.0 
    vite: 5.0.11 => 5.0.11

Used Package Manager

npm

Logs

No response

Validations

@Artur-
Copy link
Contributor Author

Artur- commented Jan 12, 2024

Ok, this is probably a bug in our configuration as it only reproduces with that. Seems like it tries to remove viteHtmlFallbackMiddleware and fails to do that on restart

@Artur-
Copy link
Contributor Author

Artur- commented Jan 12, 2024

The potential Vite issue here would be that server passed to

        transformIndexHtml: {
          order: 'pre',
          handler(_html, { server }) {

after a restart is not the same instance as the server used for determining which middlewares to use. Before a restart, they are the same

@Artur-
Copy link
Contributor Author

Artur- commented Jan 12, 2024

The behavior has changed in 5.0.4

@Artur-
Copy link
Contributor Author

Artur- commented Jan 12, 2024

So probably in #15166. Intentional @patak-dev? Should we fix our conf?

@Artur- Artur- changed the title Vite serves index.html for a file that does not exist, then it does not, then it does again Vite uses and passes a different server instance to plugins after server restart Jan 15, 2024
Artur- added a commit to vaadin/flow that referenced this issue Jan 15, 2024
The existing hacky way of removing the middleware has two issues:

1. It removes it AFTER the first request to a non-existing file so the first request for a non-existing resources index.html and subsequent requests return 404

2. It does not work with Vite 4.0.5+ after restart, see vitejs/vite#15589
@patak-dev
Copy link
Member

Yes, I think the fix you linked is correct here. 5.0.4 started to properly respect middlewares after a restart. Before, when using the documented way to setup the dev server for SSR: https://vitejs.dev/guide/ssr.html#setting-up-the-dev-server

  app.use(vite.middlewares)

you will always be using the middlewares created at server startup, and any changes after a restart were ignored.
Sorry for the glitch to your app in a patch. Let's close this issue.

@patak-dev patak-dev closed this as not planned Won't fix, can't repro, duplicate, stale Jan 15, 2024
mshabarov pushed a commit to vaadin/flow that referenced this issue Jan 22, 2024
The existing hacky way of removing the middleware has two issues:

1. It removes it AFTER the first request to a non-existing file so the first request for a non-existing resources index.html and subsequent requests return 404

2. It does not work with Vite 4.0.5+ after restart, see vitejs/vite#15589

Co-authored-by: Peter Czuczor <[email protected]>
vaadin-bot pushed a commit to vaadin/flow that referenced this issue Jan 22, 2024
The existing hacky way of removing the middleware has two issues:

1. It removes it AFTER the first request to a non-existing file so the first request for a non-existing resources index.html and subsequent requests return 404

2. It does not work with Vite 4.0.5+ after restart, see vitejs/vite#15589

Co-authored-by: Peter Czuczor <[email protected]>
vaadin-bot added a commit to vaadin/flow that referenced this issue Jan 22, 2024
)

The existing hacky way of removing the middleware has two issues:

1. It removes it AFTER the first request to a non-existing file so the first request for a non-existing resources index.html and subsequent requests return 404

2. It does not work with Vite 4.0.5+ after restart, see vitejs/vite#15589

Co-authored-by: Artur <[email protected]>
Co-authored-by: Peter Czuczor <[email protected]>
@github-actions github-actions bot locked and limited conversation to collaborators Jan 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants