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

Dynamic Component with External Script - 'Failed to load module script:' #1042

Closed
akil-rails opened this issue Jul 22, 2022 Discussed in #1041 · 3 comments · Fixed by #1045
Closed

Dynamic Component with External Script - 'Failed to load module script:' #1042

akil-rails opened this issue Jul 22, 2022 Discussed in #1041 · 3 comments · Fixed by #1045
Labels
bug Something isn't working build Related to the build system

Comments

@akil-rails
Copy link
Contributor

Discussed in #1041

Originally posted by akil-rails July 22, 2022
The guide explains:

To use code that assumes a browser environment on import, you need to dynamically import them in proper lifecycle hooks: how to use components th

<script>
export default {
  mounted() {
    import('./lib-that-access-window-on-import').then((module) => {
      // use code
    })
  }
}
</script>

When the library is directly referenced (instead of a dependency), the load fails. Is this the expected behaviour? If so, the docs could mention that.

mermaid:1 Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

<script>
export default {
  mounted() {
    import('https://cdn.skypack.dev/mermaid').then((module) => {
      // use code
    })
  }
}
</script>

Steps to reproduce

git clone [email protected]:akil-rails/vitepress-starter.git
cd vitepress-starter
pnpm i
pnpm run docs:build
pnpm run docs:serve
  1. Open the URL in chrome
  2. In chrome developer tools/console, you'll see the message
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
@brc-dd brc-dd added bug Something isn't working build Related to the build system labels Jul 22, 2022
@brc-dd
Copy link
Member

brc-dd commented Jul 22, 2022

Actually that error is because invalid modulepreload link is being added:

<link rel="modulepreload" href="/base/https://cdn.skypack.dev/mermaid">

@brc-dd brc-dd linked a pull request Jul 22, 2022 that will close this issue
@brc-dd
Copy link
Member

brc-dd commented Jul 22, 2022

Even with the fix, I don't think mermaid works like that.

@akil-rails
Copy link
Contributor Author

Even with the fix, I don't think mermaid works like that.

Yes, mermaid isn't working - I suppose this is because whitespace is stripped out .

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working build Related to the build system
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants