Permalink
Browse files

Merge pull request #10 from bshelton229/feature/quota

Catch exceptions when saving to localStorage, which should catch over quota errors.
  • Loading branch information...
2 parents b430f59 + 9911e62 commit 75733a4a65a1f1c75e0b6fb9d21b93347361795d @paulirish paulirish committed Oct 17, 2012
Showing with 11 additions and 1 deletion.
  1. +11 −1 jquery-ajax-localstorage-cache.js
View
12 jquery-ajax-localstorage-cache.js
@@ -40,7 +40,17 @@ $.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
options.success = function( data ) {
var strdata = data;
if ( this.dataType.indexOf( 'json' ) === 0 ) strdata = JSON.stringify( data );
- localStorage.setItem( cacheKey, strdata );
+
+ // Save the data to localStorage catching exceptions (possibly QUOTA_EXCEEDED_ERR)
+ try {
+ localStorage.setItem( cacheKey, strdata );
+ } catch (e) {
+ // Remove any incomplete data that may have been saved before the exception was caught
+ localStorage.removeItem( cacheKey );
+ localStorage.removeItem( cacheKey + 'cachettl' );
+ if ( options.cacheError ) options.cacheError( e, cacheKey, strdata );
+ }
+
if ( options.realsuccess ) options.realsuccess( data );
};

1 comment on commit 75733a4

@dlavayen

Hi Paul, I had problems with line 30 and 42 of the last commit, the variable is dataType or dataTypes?

Please sign in to comment.