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
'Window is not defined' where do I put client-side only scripts? #30
Comments
Hi, .vue file <script>
if (process.browser) {
require('aframe')
}
export default {
}
</script> nuxt.config.js build: {
vendor: ['aframe']
} Thanks for your report, the documentation of the website will be completed soon. |
Ah ha, brilliant! Thanks! |
Hmm a-frame doesn't play nice with vue. It doesn't load properly. |
I have experinced similar issues as well. I was trying to import a vue UI framework I've also treid |
I think that every vue plugin should be able to work both client-side and server-side since the version 2.0 of Vue.js. I asked the team behind @andyhu what library are you talking about which is working with next.js but not nuxt.js? |
@Atinux I mean I'm pretty sure that Update: I've found that in |
I hope they will add theses check so I'm closing this issue since the question has been answer by @alexchopin by using |
I know this issue is closed and all, but I just want to point out an alternate solution - Vue's lifecycle hooks. All hooks except for |
why is this still not in the docs? I followed what was there and was still having issues importing a vue plugin. this solution cleared things up the plugin in question: https://github.com/Wanderxx/vue-fullcalendar |
I have a follow up question that is a bit relevant to this issue and didn't want to clutter up the project by opening another one. I have code that needs to query browser-specific media and streaming capabilities. Where would that code best be located such that it is guaranteed to run when the component appears on the client side (whether it has previously been rendered by the server or not). I imagine this is also an issue for anyone trying to integrate with WebGL and other WebAPI technologies. Anyone have some advice? |
@dkushner we have a special option for JS to be run only on client-side with |
@Atinux, perfect thanks! Just happened to miss that portion of the docs. |
@Atinux, actually I'm sorry it looks like that documentation is explicitly for Vue plugins. Do I need to wrap every external library I use as a Vue plugin in order to leverage this? For instance THREE and SocketIO will both rely on UA-specific objects being present like |
@dkushner yep |
Currently, nuxt.js |
Vue-FullCalendar
|
This is the best answer I've ever seen. Thanks |
How would that work if we are using import instead of require? If I put my import inside the if statement I get:
|
You have to use |
Thanks @Atinux. I also found I can use import by moving my code out from the page into a component and use |
@yun313350095 I tried But is this real solution? I mean, I have to put this if block every method, where I want to something for client-side interaction. @andrewharvey, for i18n plugin, i did same changes like nuxt.config.js - |
Hello, I'm very confused and having really bad times 😞 What if, I have to add some components after the first contentful paint ? I've something like this:
But I can't figure out how to load these components in front... // vue instance
components: {
// What I have to do?
} Any help would be appreciated |
@EmixMaxime
in HTML,
Hope this will help |
@sudhir600 Hum, that's loads the component in the server side, I have the |
You have to lazy-import your components only when the code is running on the client, like this: <script>
export default {
components: {
postShare: process.client && import('~/components/anotherFolder/postShare.vue')
}
</script> |
@andrewharvey could you provide more details or an example of this implementation using the |
The only way I found to make scripts load only on the client side is to require them in one of the hooks that are not executed on the server side. So for example with <script>
export default {
data () {
return {
mapboxgl: null
}
},
mounted () {
this.maboxgl = require('mapbox-gl')
}
}
</script> |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I'm trying to run a-frame in one of my vue pages but I'm getting an error in my browser:
ReferenceError: window is not defined
I'm guessing this is because nuxt is trying to run the aframe module while rendering the page on the server.
The text was updated successfully, but these errors were encountered: