vite-plugin-web-extension icon indicating copy to clipboard operation
vite-plugin-web-extension copied to clipboard

When I add a router, the console will report an error

Open in-chong opened this issue 1 year ago • 11 comments

Firstly, thank you for providing such an excellent code repository

i have a problem,when i add a router,and i use useeffect in home.js,my console will report an error

Uncaught Error: React refresh runtime was loaded twice. Maybe you forgot the base path? at refresh-runtime.js?v=0259888e:368:9

I'm not sure why this is happening. I hope I can get your help

//Popup.js <MemoryRouter> <Routes> <Route path="/" element={<Home />} /> </Routes> </MemoryRouter>

// home.js const home = () => {

useEffect(() => { console.log('234') }, [])

return ( <div className='home_container mx-auto text-center'> <img src="/assets/images/icon-with-shadow.svg" className='w-32' /> <h1 className='text-3xl font-Poppins-Bold font-test2 font-test3 mr-1'>Home ) }

export default home

in-chong avatar Aug 10 '23 06:08 in-chong

Sorry, please follow the issue template, you haven't given me enough information to help you out.

I need a minimal reproduction (small but full project) to debug this.

aklinker1 avatar Aug 11 '23 14:08 aklinker1

image I just changed the export default function() to const testHome(),then will have this problem

in-chong avatar Aug 17 '23 12:08 in-chong

Yes, I've noticed the same. If you use the "export default function()" synytax, there is no issue. But if you name the function and export it later, it causes an issue. Image attached to clarify. Error

FarazFKhan avatar Aug 19 '23 07:08 FarazFKhan

Yes, I've noticed the same. If you use the "export default function()" synytax, there is no issue. But if you name the function and export it later, it causes an issue. Image attached to clarify.

Yeah, I will create and pin an issue about React. I don't know why it behaves like this. The react plugin seems to be doing something different than all the other framework's plugins.

So right now I don't have a solution for you, sorry.

aklinker1 avatar Aug 20 '23 19:08 aklinker1

And by the way, I can't reproduce the error itself by exporting a named variable later in the file @FarazFKhan. @in-chong I can't reproduce the error using the react template either.

Can you one of you share an entire project? Either upload a zip on your comment or share a github repo. But I can't reproduce this error using the template project.

Fast refresh doesn't work in some cases, but no log reporting "React refresh runtime was loaded twice".

https://github.com/aklinker1/vite-plugin-web-extension/assets/10101283/a918c4a7-4688-4d8a-a640-07d551762c7b

https://github.com/aklinker1/vite-plugin-web-extension/assets/10101283/4c0b3f3a-6af7-4535-901e-f163a5beb320

aklinker1 avatar Aug 20 '23 19:08 aklinker1

I commented out this code and it will run normally without prompting any errors,But you know, if I do this, my code won't be updated in real-time,So I guess if there are multiple imports of react refresh/runtime or related modules in the project? image

in-chong avatar Aug 21 '23 06:08 in-chong

image I noticed that vite's official website supports react refresh without the need for additional configuration. Perhaps this is an issue?

in-chong avatar Aug 21 '23 06:08 in-chong

Thank you for looking into this and getting back to us @aklinker1.

I did a fresh install and just changed the "src/pages/Popup.tsx" to what you can see in the screenshot attached. I only made changes to line 4 and line 20 in the screenshot. I don't think the "React refresh runtime was loaded twice" came up immediately. It's only after running "npm run build", then if I run the dev mode again using "npm run dev", then the console shows that error log referred to above. At this point, you can also see that the popup doesn't actually show any content inside of it (screenshot attached below to show this).

image

image

FarazFKhan avatar Aug 21 '23 13:08 FarazFKhan

I got the same issue and read under crxjs that they got issues with hmr with swc. So I just tried switching from "@vitejs/plugin-react-swc" to "@vitejs/plugin-react" and it looks like it solves this issue. Furthermore it seems like you can remove the resolve alias for "@react-refresh" with this change.

Edit: Unfortunately, it seems like with "@vitejs/plugin-react" hmr doesn't work at all.

sotasan avatar Dec 14 '23 01:12 sotasan

I think I found a way to reproduce the error: React refresh runtime was loaded twice. Maybe you forgot the base path?.

Here are the steps I took to reproduce it, example can be found here:

  1. pnpm create vite-plugin-web-extension
  • Project name: react-refresh-loaded-twice-issue
  • Template: react-ts
  • Package Manager: PNPM
  1. cd react-refresh-loaded-twice-issue
  2. pnpm dev
  3. Create src/providers.tsx with the content below
  4. Update src/popup.tsx and wrap the Popup component in the Providers component
// src/providers.tsx
export function Providers({children}: {children: React.ReactNode}) {
    return (
        <div className="providers">
            {children}
        </div>
    );
}
// src/popup.tsx
import React from "react";
import ReactDOM from "react-dom/client";
import Popup from "./pages/Popup";
import {Providers} from "./providers";

ReactDOM.createRoot(document.body).render(
  <React.StrictMode>
      <Providers>
            <Popup />
      </Providers>
  </React.StrictMode>
);

beeman avatar Apr 20 '24 21:04 beeman

I think I found a way to reproduce the error: React refresh runtime was loaded twice. Maybe you forgot the base path?.

Here are the steps I took to reproduce it, example can be found here:

  1. pnpm create vite-plugin-web-extension
  • Project name: react-refresh-loaded-twice-issue
  • Template: react-ts
  • Package Manager: PNPM
  1. cd react-refresh-loaded-twice-issue
  2. pnpm dev
  3. Create src/providers.tsx with the content below
  4. Update src/popup.tsx and wrap the Popup component in the Providers component
// src/providers.tsx
export function Providers({children}: {children: React.ReactNode}) {
    return (
        <div className="providers">
            {children}
        </div>
    );
}
// src/popup.tsx
import React from "react";
import ReactDOM from "react-dom/client";
import Popup from "./pages/Popup";
import {Providers} from "./providers";

ReactDOM.createRoot(document.body).render(
  <React.StrictMode>
      <Providers>
            <Popup />
      </Providers>
  </React.StrictMode>
);

FYI: After getting stuck here, I found WXT, a Next-gen Web Extension Framework by the same author and it's really great.

beeman avatar May 01 '24 11:05 beeman