jakearchibald / idb

IndexedDB, but with promises
https://www.npmjs.com/package/idb
ISC License
6.31k stars 356 forks source link

Support of SSR (angular universal) with IDB package. #108

Closed Pardeep9stacks closed 3 years ago

Pardeep9stacks commented 5 years ago

Trying to use this package with Angular universal but facing this issue.

ReferenceError: IDBIndex is not defined

Probably this is not working on server, Can someone provide a possible workaround for this? Do I need to

import { openDb, deleteDb } from 'idb';

conditionally? or something else.

jakearchibald commented 5 years ago

Which version of the library are you using? It shouldn't throw on importing, even in a node environment.

It might throw if you try and use it. In which case feature-detect it with something like if (self.indexedDB)

waseber commented 5 years ago

I'm experiencing the same issue with Nuxt. I'm using IDB 4.0.3 The error specifically mentions the file node_modules\idb\build\cjs\index.js on line 15 const request = indexedDB.open(name, version);

waseber commented 5 years ago

FWIW, I ended up using regular IndexedDB but wrapping that component in Nuxt <no-ssr></no-ssr> tags.

Pardeep9stacks commented 5 years ago

@jakearchibald I am using IDB v3.0.2. Facing this issue while using with Angular Universal.

jakearchibald commented 5 years ago

Could someone create a minimal reproducible example of the problem for me to debug?

ngocsangyem commented 4 years ago

@jakearchibald This issue happens when I try to build my project in SSR ( Angular Universal ). Then I solve it by adding this condition in ts file:

isBrowser;
constructor(@Inject(PLATFORM_ID) private platformId) { 
   this.isBrowser = isPlatformBrowser(platformId);
}
if (this.isBrowser) { 
  // put your code which is access window variable 
} 

Check this out: https://github.com/angular/universal/issues/830

jakearchibald commented 3 years ago

Closing this. Happy to look at it if a minimal example can be provided. A minimal example includes only the things needed to recreate the issue, and has instructions for how to recreate the issue.