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 and defer seem to lead to Uncaught Error: @vitejs/plugin-react can't detect preamble. Something is wrong. and TypeError: window.__remixClearCriticalCss is not a function #7863

Closed
1 task done
ZipBrandon opened this issue Nov 1, 2023 · 17 comments

Comments

@ZipBrandon
Copy link

What version of Remix are you using?

2.2.0

Are all your remix dependencies & dev-dependencies using the same version?

  • Yes

Steps to Reproduce

  1. Clone https://github.com/ZipBrandon/remix-vite-cant-detect-preamble
  2. npm i && npm run dev
  3. open the page and open devtools console.
  4. notice on refresh that Uncaught Error: @vitejs/plugin-react can't detect preamble. Something is wrong. and TypeError: window.__remixClearCriticalCss is not a function appear in console.
  5. In the loader.ts, uncomment the json() path and notice that document reloads no longer throw errors.

Expected Behavior

No issues in the console

Actual Behavior

Uncaught Error: @vitejs/plugin-react can't detect preamble. Something is wrong. and TypeError: window.__remixClearCriticalCss is not a function in the console.

@marbemac
Copy link
Contributor

marbemac commented Nov 1, 2023

Check out #7842

@pcattori pcattori added the vite label Nov 1, 2023
@aimz-yeah
Copy link

aimz-yeah commented Nov 1, 2023

The above fixes the issue on create-remix@latest but i'm still getting the error when tested on a much larger codebase. This error appears to break all client-side JS

@marbemac
Copy link
Contributor

marbemac commented Nov 1, 2023

@Fireclunge - the preamble log, or the remixClearCriticalCss is not a function log? or both? When loading all routes, or specific routes?

@aimz-yeah
Copy link

@Fireclunge - the preamble log, or the remixClearCriticalCss is not a function log? or both? When loading all routes, or specific routes?

image

The preamble log. Some of my routes go through multiple loaders each often each calling APIs from an external backend at each layer so i'm not sure if its the complexity that's causing the error or something else entirely.

The build code seems to fail here;

if (!window.__vite_plugin_react_preamble_installed__) {
    throw new Error( "..." ) 
} 

@ZipBrandon
Copy link
Author

ZipBrandon commented Nov 1, 2023

@Fireclunge I'm on a fairly large codebase (656 routes + large component libraries) and it seems to be working on my side.
Here's my patch package @remix-run+dev+2.2.0.patch

index 3f025ab..b07df68 100644
--- a/node_modules/@remix-run/dev/dist/vite/plugin.js
+++ b/node_modules/@remix-run/dev/dist/vite/plugin.js
@@ -515,7 +515,7 @@ const remixVitePlugin = (options = {}) => {
     load(id) {
       if (id === vmod.resolve(remixReactProxyId)) {
         // TODO: ensure react refresh is initialized before `<Scripts />`
-        return ['import { createElement } from "react";', 'export * from "@remix-run/react";', 'export const LiveReload = process.env.NODE_ENV !== "development" ? () => null : ', '() => createElement("script", {', ' type: "module",', " suppressHydrationWarning: true,", " dangerouslySetInnerHTML: { __html: `", `   import RefreshRuntime from "${vmod.url(hmrRuntimeId)}"`, "   RefreshRuntime.injectIntoGlobalHook(window)", "   window.$RefreshReg$ = () => {}", "   window.$RefreshSig$ = () => (type) => type", "   window.__vite_plugin_react_preamble_installed__ = true", " `}", "});"].join("\n");
+        return ['import { createElement } from "react";', 'export * from "@remix-run/react";', 'export const LiveReload = process.env.NODE_ENV !== "development" ? () => null : ', '() => createElement("script", {', ' type: "module",', " async: true,", " suppressHydrationWarning: true,", " dangerouslySetInnerHTML: { __html: `", `   import RefreshRuntime from "${vmod.url(hmrRuntimeId)}"`, "   RefreshRuntime.injectIntoGlobalHook(window)", "   window.$RefreshReg$ = () => {}", "   window.$RefreshSig$ = () => (type) => type", "   window.__vite_plugin_react_preamble_installed__ = true", " `}", "});"].join("\n");
       }
     }
   }, {

@marbemac
Copy link
Contributor

marbemac commented Nov 1, 2023

@Fireclunge have you swapped LiveReload and Scripts in your root.tsx? (rendering livereload before scripts)

@aimz-yeah
Copy link

I reinstalled using the above patch file to make sure it wasn't a skill issue and regardless of the ordering of LiveReload and Scripts the same error occurs 😢

@aimz-yeah
Copy link

I found out what was causing the error. One of my libraries was throwing a process is not defined error so I manually defined it in vite.config.ts.

export default defineConfig({
  define: {
    "process.env": {},
  },
  plugins: [remix(), tsconfigPaths()],
});

This was messing with the turnary logic in the patch when testing for process.env.NODE_ENV !== "development" - so I removed the turnary.

Here's my patch file that 'fixes'

diff --git a/node_modules/@remix-run/dev/dist/vite/plugin.js b/node_modules/@remix-run/dev/dist/vite/plugin.js
index 3f025ab..518e8e3 100644
--- a/node_modules/@remix-run/dev/dist/vite/plugin.js
+++ b/node_modules/@remix-run/dev/dist/vite/plugin.js
@@ -515,7 +515,7 @@ const remixVitePlugin = (options = {}) => {
     load(id) {
       if (id === vmod.resolve(remixReactProxyId)) {
         // TODO: ensure react refresh is initialized before `<Scripts />`
-        return ['import { createElement } from "react";', 'export * from "@remix-run/react";', 'export const LiveReload = process.env.NODE_ENV !== "development" ? () => null : ', '() => createElement("script", {', ' type: "module",', " suppressHydrationWarning: true,", " dangerouslySetInnerHTML: { __html: `", `   import RefreshRuntime from "${vmod.url(hmrRuntimeId)}"`, "   RefreshRuntime.injectIntoGlobalHook(window)", "   window.$RefreshReg$ = () => {}", "   window.$RefreshSig$ = () => (type) => type", "   window.__vite_plugin_react_preamble_installed__ = true", " `}", "});"].join("\n");
+        return ['import { createElement } from "react";', 'export * from "@remix-run/react";', 'export const LiveReload = () => createElement("script", {', ' type: "module",', " async: true,", " suppressHydrationWarning: true,", " dangerouslySetInnerHTML: { __html: `", `   import RefreshRuntime from "${vmod.url(hmrRuntimeId)}"`, "   RefreshRuntime.injectIntoGlobalHook(window)", "   window.$RefreshReg$ = () => {}", "   window.$RefreshSig$ = () => (type) => type", "   window.__vite_plugin_react_preamble_installed__ = true", " `}", "});"].join("\n");
       }
     }
   }, {

Obviously not a proper fix, but hopefully gives some insight into how this happened

@ZipBrandon
Copy link
Author

@Fireclunge I had an issue where I had a utils.ts that had various functions in it. One of the functions was for actions/loaders and pulled in inspect. The solution for me was to take these server based functions and put them in a new utils.server.ts file which got them out of client/browser code. Could this be a possible solution for what you are facing?

@aimz-yeah
Copy link

@Fireclunge I had an issue where I had a utils.ts that had various functions in it. One of the functions was for actions/loaders and pulled in inspect. The solution for me was to take these server based functions and put them in a new utils.server.ts file which got them out of client/browser code. Could this be a possible solution for what you are facing?

Perfect, working on migrating loader functions to .server.ts seems to be fixing it. Thank you both for your help on this

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Nov 5, 2023

It seems I can still reproduce this intermittently by reloading a page for 10 to 20 times even without defer.
I made a reproduction on Github CI here hi-ogawa/test-remix-vite-hmr-runtime#1

@markdalgleish
Copy link
Member

This is fixed by #7919.

@piotrkulpinski
Copy link

I can still see the error after updating Remix to experimental.

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Nov 11, 2023

I think experimental release is different. nightly is the one releasing https://github.com/remix-run/remix/tree/dev, which should include the fix.

Copy link
Contributor

🤖 Hello there,

We just published version 2.3.0-pre.0 which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Copy link
Contributor

🤖 Hello there,

We just published version 2.3.0 which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@anyuruf
Copy link

anyuruf commented Jul 28, 2024

I had a similar issue with my Remix 2.10.3 / 2.10.2 and 2.8.1 and turns out :
The Remix Vite plugin is only intended for use in your application's development server and production builds. While there are other Vite-based tools such as Vitest and Storybook that make use of the Vite config file, the Remix Vite plugin has not been designed for use with these tools. We currently recommend excluding the plugin when used with other Vite-based tools. As "Plugin usage with other Vite-based tools" (e.g. Vitest, Storybook) section @ https://remix.run/docs/en/main/guides/vite#troubleshooting
test-error

I had to change the remix() plugin section of the vite.config file to include !process.env.VITEST ? remix () : react check my public repo for setup https://github.com/anyuruf/kinstree-remix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants