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

TypeError: Failed to fetch dynamically imported module #157

Closed
chrisozgo99 opened this issue Jul 24, 2023 · 15 comments
Closed

TypeError: Failed to fetch dynamically imported module #157

chrisozgo99 opened this issue Jul 24, 2023 · 15 comments

Comments

@chrisozgo99
Copy link

I'm getting the following error when trying to inject some HTML via this chrome extension boilerplate:

Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: chrome-extension://{my-extension-id}/assets/js/index.js

It only happens about 10% of the time, which is strange. I think it has something to do with the Vite configuration, although, it is difficult to debug with it only happening occasionally.

Might it have something to do with the customDynamicImport() function, or some other custom configuration of this boilerplate? Any help would be appreciated!

@github-actions
Copy link

Thank you for your contribution. We will check and reply to you as soon as possible.

@Jonghakseo
Copy link
Owner

Can you give me a little more information on what you mean by injecting some HTML?

@chrisozgo99
Copy link
Author

@Jonghakseo sure! You can actually check out the entire repo here. The file in question is in src/pages/content/components/index.tsx. I'm adding a React component to a website (Gmail) specified by my content script.

Specifically, in that file, I'm add a React component via the addIconToComposeWindow() function inside the render() function.

As far as I can tell, the only file that is being dynamically imported is from your boilerplate code, here. Based on the comment in that file, it seems like this has to be done. But it also causes that error:

Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: chrome-extension://{my-extension-id}/assets/js/index.js

Again, it only happens about 10% of the time and seemingly at random so it's difficult to reproduce. But any ideas on how to fix the issue would be greatly appreciated.

@chrisozgo99
Copy link
Author

chrisozgo99 commented Jul 25, 2023

As additional context, it seems like the only time this happens is upon making a file change. The webpage reloads automatically and executes the rollup (via the npm run dev script), but then if you don't reload the extension in the chrome://extensions/ webpage, it throws that error.

So I'm guessing this would not affect anything in production because there won't be any changes to the files, but it serves as a minor inconvenience in development.

I'm new to Vite and Chrome Extensions though, so if there is anything above that doesn't add up or make sense, please help me to understand.

@stale
Copy link

stale bot commented Aug 29, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Aug 29, 2023
@lwh795
Copy link

lwh795 commented Sep 14, 2023

I encountered the same problem on my vite project,has this problem been solved?

@Jonghakseo
Copy link
Owner

I apologise for the delay in responding. Let's take a look.

@lookis
Copy link

lookis commented Oct 10, 2023

I found customDynamicImport doesn't work with await import, here is error:

src/pages/background/index.js:7681:16: ERROR: Expected ":" but found "dynamicImport"

Expected ":" but found "dynamicImport"
7679|                  const { FewShotPromptTemplate } = await
7680|          {
7681|            const dynamicImport = (path) => import(path);
   |                  ^
7682|            dynamicImport(
7683|            '../../../assets/js/few_shot.js')};

and this will resolve my problem

  1. update vite >= 3.2
  2. add modulePreload: false in vite.config.ts build part
  3. change custom-dynamic-import.ts:
import type { PluginOption } from "vite";

export default function customDynamicImport(): PluginOption {
  return {
    name: "custom-dynamic-import",
    renderDynamicImport({ moduleId }) {
      if (!moduleId.includes("node_modules")) {  // <- dont modify any import from node_modules
        if (process.env.__FIREFOX__) {
          return {
            left: `
          {
            const dynamicImport = (path) => import(path);
            dynamicImport(browser.runtime.getURL('./') + 
            `,
            right: ".split('../').join(''))}",
          };
        }
        return {
          left: `
          {
            const dynamicImport = (path) => import(path);
            dynamicImport(
            `,
          right: ")}",
        };
      }
      return {
        left: "import(",
        right: ")",
      };
    },
  };
}

@Jonghakseo Jonghakseo mentioned this issue Oct 10, 2023
@Jonghakseo
Copy link
Owner

@lookis

I've been testing with the solution you suggested and it seems to be working well. I'll raise a PR to reflect it, unless you have any other comments.

Jonghakseo added a commit that referenced this issue Oct 12, 2023
@Jonghakseo
Copy link
Owner

@all-contributors please add @lookis for bug

@allcontributors
Copy link
Contributor

@Jonghakseo

I've put up a pull request to add @lookis! 🎉

@Jonghakseo
Copy link
Owner

@all-contributors please add @lookis for code

@allcontributors
Copy link
Contributor

@Jonghakseo

I've put up a pull request to add @lookis! 🎉

@Jonghakseo
Copy link
Owner

@all-contributors please add @chrisozgo99 for bug

@allcontributors
Copy link
Contributor

@Jonghakseo

I've put up a pull request to add @chrisozgo99! 🎉

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

No branches or pull requests

4 participants