Skip to content

Commit 5d69ad3

Browse files
authored
fix(storage): better detect ssr envs (#197)
Change the way that ssr/node envs are detected
1 parent 6a70ed7 commit 5d69ad3

4 files changed

Lines changed: 42 additions & 28 deletions

File tree

package-lock.json

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131
"localforage-cordovasqlitedriver": "1.7.0"
3232
},
3333
"devDependencies": {
34-
"@angular/compiler": "9",
35-
"@angular/compiler-cli": "9",
36-
"@angular/core": "9",
37-
"@types/node": "12",
34+
"@angular/common": "^9.1.12",
35+
"@angular/compiler": "^9.1.12",
36+
"@angular/compiler-cli": "^9.1.12",
37+
"@angular/core": "^9.1.12",
38+
"@types/node": "^12.12.54",
3839
"canonical-path": "0.0.2",
3940
"conventional-changelog-cli": "^2.0.1",
4041
"cpr": "^2.0.0",

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NgModule, ModuleWithProviders } from '@angular/core';
1+
import { NgModule, ModuleWithProviders, PLATFORM_ID } from '@angular/core';
22
import {
33
getDefaultConfig,
44
provideStorage,
@@ -19,7 +19,7 @@ export class IonicStorageModule {
1919
{
2020
provide: Storage,
2121
useFactory: provideStorage,
22-
deps: [StorageConfigToken]
22+
deps: [StorageConfigToken, PLATFORM_ID ]
2323
}
2424
]
2525
};

src/storage.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { InjectionToken } from '@angular/core';
1+
import { InjectionToken, Inject, PLATFORM_ID } from '@angular/core';
2+
import { isPlatformServer } from '@angular/common';
23

34
import * as LocalForage from 'localforage';
45

@@ -109,9 +110,12 @@ export class Storage {
109110
* Possible driver options are: ['sqlite', 'indexeddb', 'websql', 'localstorage'] and the
110111
* default is that exact ordering.
111112
*/
112-
constructor(config: StorageConfig) {
113+
constructor(
114+
config: StorageConfig,
115+
@Inject(PLATFORM_ID) private platformId: Object
116+
) {
113117
this._dbPromise = new Promise((resolve, reject) => {
114-
if (typeof process !== 'undefined') {
118+
if (isPlatformServer(this.platformId)) {
115119
const noopDriver = getNoopDriver();
116120
resolve(noopDriver);
117121
return;
@@ -133,7 +137,7 @@ export class Storage {
133137
this._driver = db.driver();
134138
resolve(db);
135139
})
136-
.catch(reason => reject(reason));
140+
.catch((reason) => reject(reason));
137141
});
138142
}
139143

@@ -154,8 +158,8 @@ export class Storage {
154158
}
155159

156160
/** @hidden */
157-
private _getDriverOrder(driverOrder) {
158-
return driverOrder.map(driver => {
161+
private _getDriverOrder(driverOrder: string[]) {
162+
return driverOrder.map((driver: string) => {
159163
switch (driver) {
160164
case 'sqlite':
161165
return CordovaSQLiteDriver._driver;
@@ -175,7 +179,7 @@ export class Storage {
175179
* @returns Returns a promise with the value of the given key
176180
*/
177181
get(key: string): Promise<any> {
178-
return this._dbPromise.then(db => db.getItem(key));
182+
return this._dbPromise.then((db) => db.getItem(key));
179183
}
180184

181185
/**
@@ -185,7 +189,7 @@ export class Storage {
185189
* @returns Returns a promise that resolves when the key and value are set
186190
*/
187191
set(key: string, value: any): Promise<any> {
188-
return this._dbPromise.then(db => db.setItem(key, value));
192+
return this._dbPromise.then((db) => db.setItem(key, value));
189193
}
190194

191195
/**
@@ -194,29 +198,29 @@ export class Storage {
194198
* @returns Returns a promise that resolves when the value is removed
195199
*/
196200
remove(key: string): Promise<any> {
197-
return this._dbPromise.then(db => db.removeItem(key));
201+
return this._dbPromise.then((db) => db.removeItem(key));
198202
}
199203

200204
/**
201205
* Clear the entire key value store. WARNING: HOT!
202206
* @returns Returns a promise that resolves when the store is cleared
203207
*/
204208
clear(): Promise<void> {
205-
return this._dbPromise.then(db => db.clear());
209+
return this._dbPromise.then((db) => db.clear());
206210
}
207211

208212
/**
209213
* @returns Returns a promise that resolves with the number of keys stored.
210214
*/
211215
length(): Promise<number> {
212-
return this._dbPromise.then(db => db.length());
216+
return this._dbPromise.then((db) => db.length());
213217
}
214218

215219
/**
216220
* @returns Returns a promise that resolves with the keys in the store.
217221
*/
218222
keys(): Promise<string[]> {
219-
return this._dbPromise.then(db => db.keys());
223+
return this._dbPromise.then((db) => db.keys());
220224
}
221225

222226
/**
@@ -227,7 +231,7 @@ export class Storage {
227231
forEach(
228232
iteratorCallback: (value: any, key: string, iterationNumber: Number) => any
229233
): Promise<void> {
230-
return this._dbPromise.then(db => db.iterate(iteratorCallback));
234+
return this._dbPromise.then((db) => db.iterate(iteratorCallback));
231235
}
232236
}
233237

@@ -237,7 +241,7 @@ export function getDefaultConfig() {
237241
name: '_ionicstorage',
238242
storeName: '_ionickv',
239243
dbKey: '_ionickey',
240-
driverOrder: ['sqlite', 'indexeddb', 'websql', 'localstorage']
244+
driverOrder: ['sqlite', 'indexeddb', 'websql', 'localstorage'],
241245
};
242246
}
243247

@@ -258,9 +262,12 @@ export const StorageConfigToken = new InjectionToken<any>(
258262
);
259263

260264
/** @hidden */
261-
export function provideStorage(storageConfig: StorageConfig): Storage {
265+
export function provideStorage(
266+
storageConfig: StorageConfig,
267+
platformID: Object
268+
): Storage {
262269
const config = !!storageConfig ? storageConfig : getDefaultConfig();
263-
return new Storage(config);
270+
return new Storage(config, platformID);
264271
}
265272

266273
function getNoopDriver() {
@@ -273,7 +280,7 @@ function getNoopDriver() {
273280
clear: noop,
274281
length: () => 0,
275282
keys: () => [],
276-
iterate: noop
283+
iterate: noop,
277284
};
278285
return driver;
279-
}
286+
}

0 commit comments

Comments
 (0)