Sortable: Unable to support server-side rendering

Created on 18 Sep 2019  路  12Comments  路  Source: SortableJS/Sortable

Problem:

Unable to support server-side rendering

Version

1.10.0

Environment

chrome 76, nodejs v8.11.3

Reproduction link

https://codesandbox.io/embed/example-hello-world-802gp

Steps to reproduce

import sortablejs in server side rendering project

What is expected?

show server side rendering page

What is actually happening?

ReferenceError:
navigator is not defined

Most helpful comment

@owen-m1 but 1.10.0 is not checking the navigator object when you import it:
https://github.com/SortableJS/Sortable/blob/master/src/BrowserInfo.js#L2
When you import BrowserInfo.js, the server will throw:
navigator is not defined

All 12 comments

I think the reason is:
source code : https://github.com/SortableJS/Sortable/blob/master/src/BrowserInfo.js
Did not check browser environment锛宖or example: document or window variable

Any current stable version which isn't throwing
navigator is not defined ?

Any current stable version which isn't throwing
navigator is not defined ?

Higher than 1.8.4 version can not work in server-side rendering

Pin sortablejs to 1.8.3 and add this to your package.json to make sure any subdependencies get added. I'm using vuedraggable, so need to make sure all versions of sortablejs are pinned.

{
  "resolutions": {
    "sortablejs": "1.8.3"
  }
}

@MarcelloTheArcane yes, I am using vuedraggable also锛宎nd I have submitted a issue to the vuedraggable team

@MarcelloTheArcane I think this problem needs to be solved by the vuedraggable team.

Are you instantiating Sortable on the server?
Even 1.8.3 is checking the navigator object when you import it: https://github.com/SortableJS/Sortable/blob/1.8.3/Sortable.js#L95

@owen-m1 but 1.10.0 is not checking the navigator object when you import it:
https://github.com/SortableJS/Sortable/blob/master/src/BrowserInfo.js#L2
When you import BrowserInfo.js, the server will throw:
navigator is not defined

Ok. I will fix soon.

@Xzillion Do you know if this issue would occur if I used window.navigator.userAgent as opposed to just navigator.userAgent here?

I am trying to figure out what I should do here because it seems that the only reason it would have worked before in 1.9.0 is because Sortable would have thrown an error when the window object is not defined.

This was solved in 1.10.1

Was this page helpful?
0 / 5 - 0 ratings