Inline modules cause full page refresh instead of HMR for SSR
See original GitHub issueDescribe the bug
When inlining code in a <script type="module">//some code</script>
HMR does not seem to work when in SSR mode. Inside the script is a call to import.meta.glob
to load other files (not sure if that is relevant). What I believe is happening is that the virtual module created always causes a full reload based on this PR. When the module actually references a file and is not virtual then HMR works appropriately. Can this be fixed?
Potential other relevant PR I found - https://github.com/vitejs/vite/pull/5279
Secondary question, which might be off topic: is it possible for an index.html (provided by an SSR server) to load a file from an npm package, rather than assuming it’s local to the repo root? My use case is providing a framework that people use (which uses Vite in SSR mode under the hood), and the index and entry is provided for them. They only need to add template files in their repo. So my entry is an inline module in the index.html due to the above issue. If I could not inline it then that would satisfy my use case as long as the script module can reference a file provided by my package (in node_modules).
Reproduction
https://github.com/mkilpatrick/vite-ssr-react-base/tree/inline-module
System Info
System:
OS: macOS 12.4
CPU: (10) arm64 Apple M1 Max
Memory: 279.39 MB / 64.00 GB
Shell: 5.1.16 - /usr/local/bin/bash
Binaries:
Node: 17.9.0 - ~/.nvm/versions/node/v17.9.0/bin/node
npm: 8.5.5 - ~/.nvm/versions/node/v17.9.0/bin/npm
Browsers:
Chrome: 103.0.5060.114
Safari: 15.5
npmPackages:
@vitejs/plugin-react: 2.0.0-beta.1 => 2.0.0-beta.1
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
So I can reference in the index.html like
<script type="module" src="/node_modules/@yext/pages/dist/dev/client/entry-client.js" />
but it doesn’t actually work because the ReactDOM import isn’t updated properly (even if I add @yext/pages to optimize deps).The output when referencing via node_modules gives
but when referencing a local entry it’s
What would actually be more ideal is to handle the request in the custom SSR server where I can do something like app.use(‘@yext/entry’) to handle the request. This works, but has the same issue with the imports looking like the node_modules case above. I tried
transformRequest
andtransformIndexHtml
but no luck. I think what’s happening is that Vite normally does transformCjsImport which I don’t have access to.I haven’t tried it, but using absolute paths to load modules might work.