vue-virtual-grid icon indicating copy to clipboard operation
vue-virtual-grid copied to clipboard

SSR nuxt

Open merijnponzo opened this issue 5 years ago • 11 comments
trafficstars

Hi does this work with nuxt / ssr? I've got

document is not defined 

Not sure if I implemented the virtual grid in the right way

  return styleElement
}

function addStyle (obj /* StyleObjectPart */) {
  var update, remove
  var styleElement = document.querySelector('style[' + ssrIdKey + '~="' + obj.id + '"]')

  if (styleElement) {
    if (isProduction) {
      // has SSR styles and in production mode.
      // simply do nothing.

merijnponzo avatar Oct 26 '20 17:10 merijnponzo

Hello,

Do you mind sending full debug log from your console and the part of the code where you use the grid, thanks :smile:

Mikescops avatar Oct 26 '20 17:10 Mikescops

Hi, I added virtual grid to my nuxt config

{ src: '~/plugins/vue-virtualgrid'}

within plugins: vue-virtualgrid.js

import Vue from 'vue'
import VirtualGrid from 'vue-virtual-grid'
Vue.use(VirtualGrid)

then within any nuxt page

<template>
<div>
  <VirtualGrid :items="{}" />
</div>
</template>
<script>
import VirtualGrid from 'vue-virtual-grid'

export default {
  components: { VirtualGrid },

I'm aware i should pass the renderdata as an object but passing an empty object should work for a starter ;-) this directly results into:

Screenshot 2020-10-27 at 09 18 53

thanks!

And may i ask, will it be possible to add slot content in the future, it would be a lot easier if i could use the item component just within the virtual grid component

merijnponzo avatar Oct 27 '20 08:10 merijnponzo

First of all, it should be an array of object, so passing [] should work indeed.

But from the error you get, it seems unrelated...

And may i ask, will it be possible to add slot content in the future, it would be a lot easier if i could use the item component just within the virtual grid component

Not sure what you are asking, can you rephrase?

Mikescops avatar Oct 27 '20 08:10 Mikescops

So after some searches.

This plugin do not support SSR as of now. You can use it with:

{ src: '~/plugins/vue-virtualgrid', ssr: false}

There is maybe an answer here: https://stackoverflow.com/questions/61409902/vue-how-to-build-bundle-for-nuxt-with-vue-cli-service

Let me know if that helps.

Mikescops avatar Oct 27 '20 08:10 Mikescops

@merijnponzo Were you able to get this working with nuxt? If so, would really appreciate if you could let us know the solution.

Screenshot 2020-11-26 at 16 09 09

MentalGear avatar Nov 26 '20 15:11 MentalGear

Alright, got it figured out. I still imported the component in the page I was using it, which doesn't work when it is already imported with Vue.use(VirtualGrid) in the /plugins directory.

Here's the complete solution for nuxt:

plugin file virtualgrid.js (in /plugins)

import VirtualGrid from 'vue-virtual-grid'

import Vue from 'vue'
Vue.use('VirtualGrid', VirtualGrid) // MUST match the name you use when calling the component, e.g. <VirtualGrid ...>

Where you want to use the component.

  • DO NOT use import 'VirtualGrid from ...' again
  • DO NOT define VirtualGrid under 'components: []'

Just use <VirtualGrid ... > on your page.

MentalGear avatar Nov 26 '20 15:11 MentalGear

Hi, i switched to vue-virtual-scroll-list due the lack of time for a deadline :-)

And i prefer using slots.

Best!

merijnponzo avatar Nov 26 '20 17:11 merijnponzo

Hi, i switched to vue-virtual-scroll-list due the lack of time for a deadline :-)

And i prefer using slots.

Best!

Thanks man for the reply. Mh, I would need Grid Layout support - I guess vue-virtual-scroll-list doesn't support that, or does it? Yeah, slots are pretty sweet. :)

MentalGear avatar Nov 26 '20 19:11 MentalGear

Seems like I spoke too soon. Even though I don't get a fatal nuxt error now, I get this in the debug console:

[Vue warn]: The client-side rendered virtual DOM tree is not matching server-rendered content. This is likely caused by incorrect HTML markup, for example nesting block-level elements inside < /p/ >, or missing

. Bailing hydration and performing full client-side render. warn @ vue.runtime.esm.js?2b0e:619 patch @ vue.runtime.esm.js?2b0e:6497 Vue._update @ vue.runtime.esm.js?2b0e:3945

vue.runtime.esm.js?2b0e:619 [Vue warn]: Unknown custom element: <VirtualGrid> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

MentalGear avatar Nov 26 '20 19:11 MentalGear

Alright, got rid of the "not registered" error. Apparently, one has to use Vue.component to register pure components, while Vue.use registers plugins that themselves internally register components.

Updated virtualgrid.js (/plugins folder)

import VirtualGrid from 'vue-virtual-grid'

import Vue from 'vue'
Vue.component('VirtualGrid', VirtualGrid) // instead of Vue.use

Source: https://stackoverflow.com/a/61163860

MentalGear avatar Nov 26 '20 20:11 MentalGear

I'm not sure I follow you @MentalGear , did you manage to make it work with SSR?

Mikescops avatar Nov 27 '20 08:11 Mikescops