question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inline modules cause full page refresh instead of HMR for SSR

See original GitHub issue

Describe 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

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mkilpatrickcommented, Aug 19, 2022

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

import * as ReactDOM from "/node_modules/react-dom/index.js?v=405ee521";
import * as React from "/node_modules/react/index.js?v=405ee521";

but when referencing a local entry it’s

import __vite__cjsImport0_reactDom from "/node_modules/.vite/deps/react-dom.js?v=53c7b456"; const ReactDOM = __vite__cjsImport0_reactDom;

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 and transformIndexHtml but no luck. I think what’s happening is that Vite normally does transformCjsImport which I don’t have access to.

0reactions
sapphi-redcommented, Aug 19, 2022

I haven’t tried it, but using absolute paths to load modules might work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HMR and Hot Reloading with the webpack-dev-server
Speedy compilation of client side assets; Optional HMR which means that the page will reload automatically when after compilation completes.
Read more >
Why does my browser refresh instead of just reload the ...
I am trying to set up Hot Module Replacement (HMR), but my browser refreshes instead of just hot swapping. Here is my webpack.config.js...
Read more >
Hot Module Replacement - webpack
It allows all kinds of modules to be updated at runtime without the need for a full refresh. This page focuses on implementation...
Read more >
Server-Side Rendering - SurviveJS
To make sure the project has the dependencies in place, install React and react-dom to render the application to the DOM. npm add...
Read more >
Basic Features: Fast Refresh - Next.js
Finally, if you edit a file that's imported by files outside of the React tree, Fast Refresh will fall back to doing a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found