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

"document is not defined" when using bootstrap js #148

Closed
axelthat opened this issue Sep 20, 2021 · 12 comments
Closed

"document is not defined" when using bootstrap js #148

axelthat opened this issue Sep 20, 2021 · 12 comments

Comments

@axelthat
Copy link

axelthat commented Sep 20, 2021

So I have this component which uses collapse.js of bootstrap,

<script lang="ts" setup>
import { onMounted } from "@vue/runtime-core"
import Collapse from "bootstrap/js/dist/collapse"

onMounted(() => {
  new Collapse(document.body)
})
</script>

<template>
  <div>
    <slot></slot>
  </div>
</template>

I get this error: ReferenceError: document is not defined. On nuxt, you could do,

onMounted(() => {
  if(process.client) {
    new Collapse(document.body)
  }
})

How do I do the same?

@axelthat
Copy link
Author

Okay this is weird. I console logged document and I got no error. I get error only when using the bootstrap js library.

@brillout
Copy link
Member

Your issue is not related to the plugin.

@brillout
Copy link
Member

But to answer your question: if (typeof window !== "undefined") window.document.body.

@axelthat
Copy link
Author

@brillout But it doesn't happen without SSR. I tried it on regular vite project and everything works fine. Even doing if (typeof window !== "undefined") doesn't work.

@brillout
Copy link
Member

It's an SSR problem, but not a vite-plugin-ssr problem.

Dig on bootstrap's side.

@axelthat
Copy link
Author

Okay thanks

@axelthat
Copy link
Author

For anyone that stumbles upon this issue:
twbs/bootstrap#33722
twbs/bootstrap#34265
twbs/bootstrap#33131

@brillout
Copy link
Member

Thanks for sharing. What solution did you end up choosing?

@axelthat
Copy link
Author

This one: twbs/bootstrap#33722

@brillout
Copy link
Member

From Discord:

By the way, i found that directly include bootstrap cdn in the documentHtml solved the issue also, just another workaround for quick and dirty solve.
[Source]

Reply:

It's a legit fix if you ask me. IIRC Bootstrap recommends loading bootstrap from their CDN; they will maintain the CDN links for the forseeable future.
[Source]

brillout added a commit that referenced this issue Sep 22, 2021
@axelthat
Copy link
Author

Yeah but that will include the whole bootstrap js file. I only needed some components. So did the lazy loading js way.

@brillout
Copy link
Member

Makes sense.

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

2 participants