Skip to content

Dynamic import produces "Importing a module script failed" #14775

@RogierdeRuijter

Description

@RogierdeRuijter

Astro Info

Astro                    v5.15.7
Vite                     v6.4.1
Node                     v24.6.0
System                   macOS (arm64)
Package Manager          npm
Output                   server
Adapter                  @astrojs/node (v9.5.0)
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

Safari

Describe the Bug

Dynamic imports cause "Importing a module script failed" when written in an Astro context for between 3 - 5% users in Safari. If the script below is added to an Astro page, the error will appear in some users using Safari.

// index.astro
<script>
  import("../scripts/myProcessedModule").then((module) => {
    module.myProcessedFunction();
  });
</script>

// myProcessedModule.ts
export const myProcessedFunction = () => {
  console.log("This is the processed function from myProcessedModule.");
}

To test if the code above causes the described error the code was deployed to the production servers that I work with on Vercel. Here I could see that this issue affects a certain percentage of Safari users. This can be seen in Sentry, the logging tooling we use.

When I deploy a dynamic import that is written outside of the Astro context, like so:

// index.astro
<script src="/myPublicIndex.js" is:inline></script>

// myPublicIndex.js
import("/myPublicModule.js").then((module) => {
  module.myPublicFunction();
});

// myPublicModule.js
export const myPublicFunction = () => {
  console.log("This is a public function");
};

I do not see the "Importing a module script failed" error appear in the Sentry logging.

This makes me believe that something in how Astro processes and then fetches this dynamic import makes the "Importing a module script failed" appear. It seems similar to the issues reported around the top level await issue from Safari. But when I look through the build code in the dist directory I don't spot any top level awaits, therefore I think the issue is somewhere else but I can't spot where.

It is a hard to reproduce bug because it only appears in production with a large amount of Safari users, nevertheless I would really appreciate some guidance on this issue because right now I can't use dynamic imports within Astro.

I added a reproduction that shows how the code that produces an error and a way that does not. A similar issue is this one

What's the expected result?

Dynamic imports do not produce the "Importing a module script failed" in Safari.

Link to Minimal Reproducible Example

https://github.com/RogierdeRuijter/Astro-dynamic-imports

Participation

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

Labels

help wantedPlease help with this issue!in reviewHas been triaged but not yet decided if it is a bug or not.needs triageIssue needs to be triaged

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions