Skip to content
This repository was archived by the owner on Jul 24, 2025. It is now read-only.
This repository was archived by the owner on Jul 24, 2025. It is now read-only.

Tries to execute window.$RefreshReg$ during SSR #11

@thebuilder

Description

@thebuilder

Hey - This is cool addition to Vite.
Trying the Vite 4 beta on a solution, where Vite is running through a middleware to do serverside rendering.

I have the following code, to inject the React preamble. For some reason, it seems like the plugin tries to instantiate it.

  if (import.meta.env.DEV && import.meta.env.MODE !== 'test') {
    /**
     * Inject the React Refresh runtime. This is required to get HMR working on dev
     * https://vitejs.dev/guide/backend-integration.html
     **/
    output.push(
      `<script type='module' src='${assetUrl}@vite/client'></script>`
    );
    output.push(`<script type='module'>
  import RefreshRuntime from "${assetUrl}@react-refresh"
  RefreshRuntime.injectIntoGlobalHook(window)
  window.$RefreshReg$ = () => {}
  window.$RefreshSig$ = () => (type) => type
  window.__vite_plugin_react_preamble_installed__ = true
</script>`);
  }

Results in me getting:

ReferenceError: window is not defined
    at eval (/src/entry-server.ts:6:3)
    at async instantiateModule (/node_modules/.pnpm/vite@4.0.0-beta.2/node_modules/vite/dist/node/chunks/dep-666774f5.js:53227:9)

Seems to be caused by this specific line (inside the template string):

  window.$RefreshReg$ = () => {}

If i remove that from the preamble, it renders correctly.

I've tried a few different scenarios, and it seems that the line being anywhere inside the code causes it to fail.

As a comment

// window.$RefreshReg$ = () => {}

As a string

'window.$RefreshReg$ = () => {}'

Without assignment

// window.$RefreshReg$

If change it to something else, like window.$RefreshRg$, the site renders.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions