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's dev server returns 404 if the request does not contain an Accept header #9520

Closed
7 tasks done
ghost91- opened this issue Aug 3, 2022 · 6 comments · Fixed by #15025
Closed
7 tasks done

Vite's dev server returns 404 if the request does not contain an Accept header #9520

ghost91- opened this issue Aug 3, 2022 · 6 comments · Fixed by #15025
Labels
enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@ghost91-
Copy link

ghost91- commented Aug 3, 2022

Describe the bug

When making a request to Vite's dev server without adding the Accept header, the server always returns a 404. According to RFC 7231, the Accept header is optional:

A request without any Accept header field implies that the user agent will accept any media type in response.

For that reason, I would expect Vite's dev server to just respond regularly if the Accept header is missing.

I am running into this problem when trying to use Playwright with Vite. Playwright has functionality to run a command to start the server (just calling vite, basically, in this case), and then polls the server URL until it receives a successful response. However, it performs the HTTP requests with a simple http.get (http from Node.js), and does not include an Accept header, so Vite's dev server always responds with a 404 and playwright runs into a timeout waiting for the server.

Reproduction

https://github.com/ghost91-/vite-dev-server-problem

System Info

System:
    OS: Linux 5.18 Arch Linux
    CPU: (12) x64 Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
    Memory: 23.20 GB / 31.10 GB
    Container: Yes
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.15.1 - /run/user/1000/fnm_multishells/8953_1659556049790/bin/node
    npm: 8.13.2 - /run/user/1000/fnm_multishells/8953_1659556049790/bin/npm
  Browsers:
    Chromium: 103.0.5060.134
    Firefox: 103.0.1

Used Package Manager

npm

Logs

No response

Validations

@bluwy bluwy added enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority) and removed pending triage labels Dec 10, 2022
@stefanocrosta
Copy link

Hi all! It looks like there's a similarly related bug with the specs of Accept

A request with Accept: application/json; */* should return the HTML page instead it returns 404

@SamuelTilly
Copy link

Looks like this also affects 302 redirects where Accept header is application/json, text/plain, */*?
Getting 404 when a proxied request returns 302 with Location header set to /

@segevfiner
Copy link
Contributor

This also effects wait-on & curl which use Accept: */* and curl. See jeffbski/wait-on#78

@tnrich
Copy link

tnrich commented Jun 22, 2023

Just ran into this issue and spent a good half hour tracking it down. Plz fix 🙏

@mirhmousavi
Copy link

Please add a 4-hour investigation and a full-day frustration

@jafin
Copy link
Contributor

jafin commented Aug 1, 2023

This helped me as I was getting similar to OP. May be a workaround, adjust as desired.
In my case 404 was returned from vite dev server when header included application/json, I just blat it with */* to workaround
Editing the vite.config.ts to rewrite the accept header.

function fixAcceptHeader404() {
    return {
        name: 'fix-accept-header-404', // issue with vite dev server: https://github.com/vitejs/vite/issues/9520
        configureServer(server) {
            server.middlewares.use((req, _res, next) => {
                if (req.headers.accept == 'application/json, text/plain, */*') {
                    req.headers.accept = '*/*';
                }
                next();
            });
        },
    };
}

export default defineConfig({
    plugins: [
        //...
        fixAcceptHeader404(),
    ],

samanehsan added a commit to DataBiosphere/jade-data-repo-ui that referenced this issue Aug 21, 2023
samanehsan added a commit to DataBiosphere/jade-data-repo-ui that referenced this issue Aug 21, 2023
Work around wait-on 404 error

Originally from: vitejs/vite#9520 (comment)
samanehsan added a commit to DataBiosphere/jade-data-repo-ui that referenced this issue Aug 21, 2023
Work around wait-on 404 error

Originally from: vitejs/vite#9520 (comment)
samanehsan added a commit to DataBiosphere/jade-data-repo-ui that referenced this issue Aug 30, 2023
* Install vite related libraries and uninstall react-scripts

* Update package.json scripts

* Create vite config

* Fix index.html

* Get UI to run locally

* Fix eslint

* Fix cypress tests

* Upgrade to Cypress 12 and fix component tests

* Fix cypress e2e tests

* Fix middleware

* Remove commented code

* Ignore sonarcloud errors

- The gcloud libraries are not pinned to a version so
  we cannot use an integrity hash here
- Fontawesome no longer supports SRI hashes

* Clean up unused dependencies

* Use localhost instead of 127.0.0.1

Also reenable linting on server start

* Fix e2e test wait-on local server

Work around wait-on 404 error

Originally from: vitejs/vite#9520 (comment)

* Try replacing wait-on

* Update to from node 14->16

Fixes build issue with vite

* Remove unnecessary --production flag

vite builds production by default

* Use default vite react plugin config

* Try defining process

* Try updating jsondiffpatch version

* Remove unused packages

* Update .node-version

* Fix cypress.config.ts

* Package clean-up
@github-actions github-actions bot locked and limited conversation to collaborators Dec 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants