Has anyone encountered a "ReferenceError: document is not defined" when using Annotorious 3.x with OpenSeaDragon and Next.js? #455
Replies: 1 comment 6 replies
-
Yes, I have seen the problem. First: make absolutely sure that any OpenSeadragon- or Annotorious-related code runs only in the browser, never in SSR! Even then: unfortunately, OpenSeadragon does seem to access To repeat: this is not caused by Annotorious, but OpenSeadragon (see the error message). It happens as soon as OpenSeadragon gets imported as a dependency. (Note that even if you don't import OpenSeadragon yourself, it gets imported as a transitive dependency as soon as you import Below is how I worked around this limitation in an Astro project, using an async import. However I believe that Next has better utilities to "mark" libraries that only work in the browser and skip importing them in SSR accordingly.
|
Beta Was this translation helpful? Give feedback.
-
I'm using the following versions in my project:
Next.js: ^14.2.13
React: 18.2.0
@annotorious/react: ^3.0.11
@annotorious/openseadragon: ^3.0.11
When I try to render the page, I encounter the following error during server-side rendering (SSR):
⨯ ReferenceError: document is not defined
at C:\Users\lsoho\Git\MyProject\node_modules\openseadragon\build\openseadragon\openseadragon.js:970:29
at C:\Users\lsoho\Git\MyProject\node_modules\openseadragon\build\openseadragon\openseadragon.js:973:6
at Object. (C:\Users\lsoho\Git\MyProject\node_modules\openseadragon\build\openseadragon\openseadragon.js:1071:2)
at Module._compile (node:internal/modules/cjs/loader:1369:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
at Module.load (node:internal/modules/cjs/loader:1206:32)
at Module._load (node:internal/modules/cjs/loader:1022:12)
at cjsLoader (node:internal/modules/esm/translators:366:17)
at ModuleWrap. (node:internal/modules/esm/translators:315:7)
at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
And here's my Code:
When I init a new Next.js project without any other packages, I still meet the issue
Beta Was this translation helpful? Give feedback.
All reactions