Giter Site home page Giter Site logo

Comments (29)

alexchopin avatar alexchopin commented on April 28, 2024 32

Hi,
Yes, this is exactly due to the server-side rendering.
If you need to specify that you want to import a ressource only on the client-side inside, you need to you process.browser variable.

.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.

from nuxt.js.

yun-cn avatar yun-cn commented on April 28, 2024 12

Vue-FullCalendar
app/plugins/vue-fullcalendar.js

import Vue from 'vue'

if (process.browser) {
  window.onNuxtReady(() => {
    const VueFullCalendar = require('vue-fullcalendar')
    Vue.use(VueFullCalendar)
  })
}

from nuxt.js.

zspecza avatar zspecza commented on April 28, 2024 11

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 beforeCreate and created do not even run on the server. If you're doing any client-side manipulation or inspection and referencing built-in browser globals like window or document, then you should do those things in these hooks. This solution removes the need to inspect environment variables.

from nuxt.js.

alexchopin avatar alexchopin commented on April 28, 2024 8

Documentation

from nuxt.js.

dotnetCarpenter avatar dotnetCarpenter commented on April 28, 2024 7

Currently, nuxt.js 1.0.0-rc11, it is process.browser and not process.BROWSER_BUILD inside a component.

from nuxt.js.

dzcpy avatar dzcpy commented on April 28, 2024 2

I have experinced similar issues as well. I was trying to import a vue UI framework element-ui. However, it references window object and several native browser functions like addEventListener etc., so it breaks the server rendering. I'm thinking if there is any way to polyfill it somehow. Because for such kind of UI framework, if we cannot render it on server side, it doesn't quite make sense and is probably not usable for SEO purpose.

I've also treid next.js with antd ( a UI framework built on React.js which similar to element-ui ) and it worked almost perfectly ( except that next.js is using glamor as a css-in-js solution which is pretty hacky). When I have time I'll dig more for the reason why it's working in next.js but not nuxt.js.

from nuxt.js.

somebodytolove avatar somebodytolove commented on April 28, 2024 2

Thanks @Atinux. I also found I can use import by moving my code out from the page into a component and use ssr: false when loading that plugin in the nuxt.config.js.

@andrewharvey could you provide more details or an example of this implementation using the import statement?

from nuxt.js.

Atinux avatar Atinux commented on April 28, 2024 1

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 element-ui but they said it's not their priority, but I don't think it will be complicated to make it compatible with server-side rendering, since we will only have to add if statement where a client-side only function is called.

@andyhu what library are you talking about which is working with next.js but not nuxt.js?

from nuxt.js.

dzcpy avatar dzcpy commented on April 28, 2024 1

@Atinux I mean I'm pretty sure that antd has some window object references as well. But when I tried it, it did work with next.js. However it probably because that antd is more modular and I missed the browser specific part of the variable references. Anyway I'm going to test more.

Update: I've found that in antd it's using if (typeof window !== 'undefined') to check if it's in the browser. So as you said, it's the plugin's issue and it shouldn't directly reference window object without envirument detection. element-ui IMO is a bit buggy.

from nuxt.js.

lmj0011 avatar lmj0011 commented on April 28, 2024 1

Hi,
Yes, this is exactly due to the server-side rendering.
If you need to specify that you want to import a ressource only on the client-side inside, you need to you process.BROWSER_BUILD variable.[...]

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

from nuxt.js.

MontageD avatar MontageD commented on April 28, 2024 1

This is the best answer I've ever seen. Thanks

from nuxt.js.

andrewharvey avatar andrewharvey commented on April 28, 2024 1

I'm closing this issue since the question has been answer by @alexchopin by using process.BROWSER_BUILD.

How would that work if we are using import instead of require? If I put my import inside the if statement I get:

NuxtServerError Module build failed: SyntaxError: 'import' and 'export' may only appear at the top level

from nuxt.js.

Atinux avatar Atinux commented on April 28, 2024 1

You have to use require at the moment @andrewharvey and ask for the library author to add SSR support (by giving him this link if it's a Vue.js library: https://ssr.vuejs.org/en/universal.html)

from nuxt.js.

ChristophAnastasiades avatar ChristophAnastasiades commented on April 28, 2024 1

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 mapbox-gl you first set a parameter mapboxgl into the data () object. Then inside your hook (for example mounted ()) you require mapbox-gl and assign it to the new parameter.

<script>
export default {
  data () {
    return {
      mapboxgl: null
    }
  },
  mounted () {
    this.maboxgl = require('mapbox-gl')
  }
}
</script>

from nuxt.js.

7ammer avatar 7ammer commented on April 28, 2024

Ah ha, brilliant! Thanks!

from nuxt.js.

7ammer avatar 7ammer commented on April 28, 2024

Hmm a-frame doesn't play nice with vue. It doesn't load properly.

from nuxt.js.

Atinux avatar Atinux commented on April 28, 2024

I hope they will add theses check so element-ui could be used on the server-side too and render the proper HTML code!

I'm closing this issue since the question has been answer by @alexchopin by using process.BROWSER_BUILD.

from nuxt.js.

dkushner avatar dkushner commented on April 28, 2024

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?

from nuxt.js.

Atinux avatar Atinux commented on April 28, 2024

@dkushner we have a special option for JS to be run only on client-side with ssr: false option: https://nuxtjs.org/guide/plugins#client-side-only

from nuxt.js.

dkushner avatar dkushner commented on April 28, 2024

@Atinux, perfect thanks! Just happened to miss that portion of the docs.

from nuxt.js.

dkushner avatar dkushner commented on April 28, 2024

@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 document and window. So do each of these libraries then become a separate plugin?

from nuxt.js.

lmj0011 avatar lmj0011 commented on April 28, 2024

@dkushner yep

from nuxt.js.

andrewharvey avatar andrewharvey commented on April 28, 2024

You have to use require at the moment @andrewharvey and ask for the library author to add SSR support (by giving him this link if it's a Vue.js library: https://ssr.vuejs.org/en/universal.html)

Thanks @Atinux. I also found I can use import by moving my code out from the page into a component and use ssr: false when loading that plugin in the nuxt.config.js.

from nuxt.js.

sudhir600 avatar sudhir600 commented on April 28, 2024

@yun313350095 I tried process.browser but it didn't work. However, process.BROWSER_BUILD is working fine for me.

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 -
{ src: '~plugins/i18n.js', ssr: false },
but now i am getting error as fallbackLocale is undefined.

from nuxt.js.

mxmaxime avatar mxmaxime commented on April 28, 2024

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:

    <no-ssr>
      <div id="post-footer">

        <post-share/>

        <div class="post-footer-suggestions" v-if="suggestionToShow">
          <p class="h2">ToRead</p>

          <post-suggestion/>

        </div>

      </div>
    </no-ssr>

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

from nuxt.js.

sudhir600 avatar sudhir600 commented on April 28, 2024

@EmixMaxime

<script>
import postShare from '~/components/anotherFolder/postShare.vue'

export default {
      components: {
           postShare
      }
</script>

in HTML,

<div id="post-footer">
        <postShare />
</div>

Hope this will help

from nuxt.js.

mxmaxime avatar mxmaxime commented on April 28, 2024

@sudhir600 Hum, that's loads the component in the server side, I have the window undefined error.

from nuxt.js.

pbastowski avatar pbastowski commented on April 28, 2024

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>

from nuxt.js.

lock avatar lock commented on April 28, 2024

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.

from nuxt.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.