Vue2leaflet: Using SSR results in an error "window is not defined"

Created on 5 Aug 2018  路  5Comments  路  Source: vue-leaflet/Vue2Leaflet

The .vue imports LMap fine, but when added to components {} it causing the error:

ReferenceError: window is not defined
at Object. (/Users/michael/workspace/winwisely/node_modules/vue2-leaflet/dist/vue2-leaflet.min.js:1:314)

Steps to Reproduce

Expected Results

Should not fail when added to components. This would allow us to use an SSR check on whether the component should be rendered.

Actual Results

ReferenceError: window is not defined
at Object. (/Users/michael/workspace/winwisely/node_modules/vue2-leaflet/dist/vue2-leaflet.min.js:1:314)

Browsers Affected

Server side bug

Versions

quasar: 0.17.6
vue2-leaflet: 1.0.2

Most helpful comment

<no-ssr>
    <l-map :zoom="zoom" :center="center">
        <l-tile-layer :url="url"/>
        <l-marker :lat-lng="marker"/>
        <l-icon-default :image-path="path"/>
    </l-map>
</no-ssr>
<script>
    let Vue2Leaflet = {}
    if (process.client)
        Vue2Leaflet = require('vue2-leaflet')

export default {
        components: {
            'l-map': Vue2Leaflet.LMap,
            'l-tile-layer': Vue2Leaflet.LTileLayer,
            'l-marker': Vue2Leaflet.LMarker
        },
data() {
            return {
                zoom: 14,
                path: '/images/',
                center: [47.413220, -1.219482],
                url: 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
                marker: (process.client)?L.latLng(47.413220, -1.219482):null,
            }
        },

All 5 comments

Leaflet need the window object when imported. Leaflet does import it as soon as it started on the on several components including LMap.

You can solve with the no-ssr component from egoist (works with Nuxt )

I'm using quasar and not nuxt.js. Its possible nuxt's no-ssr module supports marking javascript as not to be loaded on the server, but quasar seems unable to do that. And from looking at the nuxt/no-ssr docs it looks like their solution is actually to check the process variable to see if it is running in the browser, so I did something similar for quasar like this

<script>
let Vue2Leaflet = {}

if (!process.env.SERVER) {
  console.log('loading vue2-leaflet')
  Vue2Leaflet = require('vue2-leaflet')
}
export default {
  name: 'CampaignMap',
  components: {
    'l-map': Vue2Leaflet.LMap,
    'l-tile-layer': Vue2Leaflet.LTileLayer,
    'l-marker': Vue2Leaflet.LMarker
  },
....

since this works and the problem seems to be in the leaflet code i will close this issue.

<no-ssr>
    <l-map :zoom="zoom" :center="center">
        <l-tile-layer :url="url"/>
        <l-marker :lat-lng="marker"/>
        <l-icon-default :image-path="path"/>
    </l-map>
</no-ssr>
<script>
    let Vue2Leaflet = {}
    if (process.client)
        Vue2Leaflet = require('vue2-leaflet')

export default {
        components: {
            'l-map': Vue2Leaflet.LMap,
            'l-tile-layer': Vue2Leaflet.LTileLayer,
            'l-marker': Vue2Leaflet.LMarker
        },
data() {
            return {
                zoom: 14,
                path: '/images/',
                center: [47.413220, -1.219482],
                url: 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
                marker: (process.client)?L.latLng(47.413220, -1.219482):null,
            }
        },

@mohammadnazari110 are you using it in nuxt? are you using nuxt-leaflet?

@DonNicoJs yes i use

Was this page helpful?
0 / 5 - 0 ratings

Related issues

udos picture udos  路  4Comments

DmitriyVTitov picture DmitriyVTitov  路  4Comments

martinwithyou picture martinwithyou  路  4Comments

mnboos picture mnboos  路  3Comments

zaqisilverano picture zaqisilverano  路  4Comments