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

Dev Server says "Upgrade Required", browser has CORS-issues since Vite version 2.9.0 #8602

Closed
7 tasks done
passchn opened this issue Jun 15, 2022 · 8 comments
Closed
7 tasks done

Comments

@passchn
Copy link

passchn commented Jun 15, 2022

Describe the bug

Hello Vite!

I'm the author of this plugin for Vite and Silverstripe CMS, this plugin for CakePHP, and I use this configuration.

Until now, everything worked fine with the dev server, hot module replacement etc. However, after running yarn upgrade i noticed that the dev server is not working anymore. The browser is complaining about cors-issues - see screenshot below:

Bildschirmfoto 2022-06-15 um 10 47 33

I tried to downgrade vite step by step and the issue seems to be present from version 2.9.0. Is there any configuration that I missed which has to be present?

As I use Vite in php apps, I serve my PHP with MAMP (e.g. https://my-domain.local) and the js from vite gets served by the vite dev server from localhost:3000, through this configuration:

    server: {
        hmr: {
            protocol: 'ws',
            host: 'localhost',
            port: 3000,
        },
    },

When accessing one of the localhost urls, I now only get an Upgrade required warning. In the network tab I see that the browser got a http 426 response from the dev server, similar to this issue: #5770

However, reinstalling did not solve the problem. I have now locked the version in package.json to vite 2.8.6 which works for now, but I hope I will be able to keep Vite up to date in the future.

If this is a bug, I am happy to help with further information. Otherwise it would be great to get a hint about how to solve this problem.

Reproduction

https://github.com/brandcom/silverstripe-vite

System Info

System:
    OS: macOS 12.3.1
    CPU: (8) arm64 Apple M1
    Memory: 158.61 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 17.0.1 - /opt/homebrew/bin/node
    Yarn: 1.22.18 - /opt/homebrew/bin/yarn
    npm: 8.3.0 - /opt/homebrew/bin/npm
  Browsers:
    Brave Browser: 102.1.39.120
    Chrome: 102.0.5005.115
    Firefox: 101.0.1
    Safari: 15.4
  npmPackages:
    @vitejs/plugin-legacy: ^1.8.2 => 1.8.2 
    @vitejs/plugin-vue: ^2.3.3 => 2.3.3 
    vite: 2.8.6 => 2.8.6

Used Package Manager

yarn

Logs

vite --debug
  vite:config bundled config file loaded in 18.10ms +0ms
  vite:config using resolved config: {
  vite:config   plugins: [
  vite:config     'vite:pre-alias',
  vite:config     'alias',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:worker-import-meta-url',
  vite:config     'vite:asset',
  vite:config     'vite:vue',
  vite:config     'vite:legacy-env',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:client-inject',
  vite:config     'vite:import-analysis'
  vite:config   ],
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: './public/',
  vite:config     assetsDir: 'build',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     cssTarget: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     sourcemap: false,
  vite:config     rollupOptions: { input: './public_src/main.ts' },
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: false,
  vite:config     manifest: true,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] }
  vite:config   },
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     hmr: { protocol: 'ws', host: 'localhost', port: 3000 },
  vite:config     fs: { strict: true, allow: [Array], deny: [Array] }
  vite:config   },
  vite:config   define: {
  vite:config     __VUE_OPTIONS_API__: true,
  vite:config     __VUE_PROD_DEVTOOLS__: false,
  vite:config     'import.meta.env.LEGACY': false
  vite:config   },
  vite:config   ssr: { external: [ 'vue', '@vue/server-renderer' ] },
  vite:config   configFile: '/Users/pascalschneider/Sites/xyz.local/vite.config.ts',
  vite:config   configFileDependencies: [ 'vite.config.ts' ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     server: {}
  vite:config   },
  vite:config   root: '/Users/pascalschneider/Sites/xyz.local',
  vite:config   base: '/',
  vite:config   resolve: { dedupe: undefined, alias: [ [Object], [Object] ] },
  vite:config   publicDir: '/Users/pascalschneider/Sites/xyz.local/public',
  vite:config   cacheDir: '/Users/pascalschneider/Sites/xyz.local/node_modules/.vite',
  vite:config   command: 'serve',
  vite:config   mode: 'development',
  vite:config   isProduction: false,
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: undefined,
  vite:config     proxy: undefined,
  vite:config     cors: undefined,
  vite:config     headers: undefined
  vite:config   },
  vite:config   env: { BASE_URL: '/', MODE: 'development', DEV: true, PROD: false },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   packageCache: Map(0) {},
  vite:config   createResolver: [Function: createResolver],
  vite:config   optimizeDeps: {
  vite:config     esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
  vite:config   },
  vite:config   worker: {
  vite:config     format: 'iife',
  vite:config     plugins: [
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object]
  vite:config     ],
  vite:config     rollupOptions: {}
  vite:config   }
  vite:config } +3ms
  vite:deps Hash is consistent. Skipping. Use --force to override. +0ms

  vite v2.8.6 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

  ready in 129ms.

Validations

@github-actions
Copy link

Hello @passchn. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 3 days.

@passchn
Copy link
Author

passchn commented Jun 16, 2022

I prepared a StackBlitz here: https://stackblitz.com/edit/vitejs-vite-sdtxq1?file=README.md

The browser also shows "Upgrade required" with this hmr-config for versions from 2.9.0 and higher.

Note that a php-setup would be a little different. The php is served by Apache and Vite would serve the scripts on it's own port.

The console on StackBlitz is now logging the following information which I did not get before:

Could not auto-determine entry point from rollupOptions or html files and there are no explicit optimizeDeps.include patterns. Skipping dependency pre-bundling.

@sapphi-red
Copy link
Member

sapphi-red commented Jun 16, 2022

I assume #7282 is affecting this. Commenting out this line fixes it. It seems there is a edge case bug.

  server: {
    hmr: {
      protocol: 'ws',
      host: 'localhost',
      // port: 3000,
    },
  },

Does this match your intention? (server.port = 3000 (default) and server.hmr.port = 3000 (default))
No, server.hmr.port's default was 24678.

@passchn
Copy link
Author

passchn commented Jun 16, 2022

Ok, I tested it with my php setup and that seems to be the problem indeed.

This does not work:

    server: {
        hmr: {
            protocol: 'ws',
            host: 'localhost',
            port: 3000,
        },
    },

But this works:

    server: {
        port: 3000,
        hmr: {
            protocol: 'ws',
            host: 'localhost',
        },
    },

I need to have a fixed port as php needs to "mirror" the config as well.

Thank you!

@patak-dev
Copy link
Member

@passchn it would be helpful if you could review and comment on #7977, we would like to move forward with this change in v3

@passchn passchn closed this as completed Jun 16, 2022
@sapphi-red
Copy link
Member

It was not a bug. It was working correctly.
When server.port = undefined && server.hmr.port = 3000, server.hmr.port is 3000 and it conflicts with server.port's default so server.port becomes 3001.


IIUC your requiements are:

  • Fix server.port to 3000
  • server.hmr.port does not matter
    • you don't need to proxy websocket

So this config will meet your requirements.

    server: {
        port: 3000,
        strictPort: true
    },

@passchn
Copy link
Author

passchn commented Jun 16, 2022

No, it does not seem to work on my setup. When deleting the server.hmr settings, I get a lot of console errors, and the hot module replacement, css updates etc do not work. I have to reload the page manually to see css changes.

Bildschirmfoto 2022-06-16 um 12 07 26

@sapphi-red
Copy link
Member

Oh, that's true. It needs server.hmr settings.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 1, 2022
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

3 participants