diff --git a/spec/TagCacheSpec.js b/spec/TagCacheSpec.js index 5a2b4fc..94b1922 100644 --- a/spec/TagCacheSpec.js +++ b/spec/TagCacheSpec.js @@ -73,7 +73,9 @@ describe('tag_cache', function() { // Try to fetch tags when server crashed runs(function() { setupGlimrCrashedServer(); - delete Glimr.state.loadedTags["keywords_cache_normal"]; + clearGlimrState(); + + Glimr.state.currentURLCacheKey = "6666cd76f9"; // Check the keywords_cache_normal to see why this value was chosen isDone = false; @@ -281,4 +283,51 @@ describe('tag_cache', function() { expect(tags).toContain("apple"); }); }); + + it("should make network requests if navigated to different URL", function() { + var isDone = false; + var tags; + + // Fetch tags normally + runs(function() { + document.location.hash = "#!/"; + Glimr.setTagCacheTimeInSeconds(300); + Glimr.getTags("keywords_cache_normal", function(fetchedTags) { + isDone = true; + tags = fetchedTags; + }); + }); + + waitsFor(function() { + return isDone; + }); + + runs(function() { + expect(tags.length).toBe(4); + expect(tags).toContain("tag_10"); + expect(tags).toContain("tag_12"); + expect(tags).toContain("tag_1"); + expect(tags).toContain("tag_2"); + + // Simulate in-page navigation + document.location.hash = "#!/example/path"; + + isDone = false; + Glimr.getTags("keywords_cache_normal", function(fetchedTags) { + isDone = true; + tags = fetchedTags; + }); + }); + + waitsFor(function() { + return isDone; + }); + + runs(function() { + expect(tags.length).toBe(3); + expect(tags).toContain("tag_10"); + expect(tags).toContain("tag_12"); + expect(tags).toContain("/example/path"); + }); + }); }); diff --git a/spec/mock_responses/keywords_cache_normal.json b/spec/mock_responses/keywords_cache_normal.json index 0a18636..6395011 100644 --- a/spec/mock_responses/keywords_cache_normal.json +++ b/spec/mock_responses/keywords_cache_normal.json @@ -4,5 +4,6 @@ "6666cd76f96956469e7be39d750cc7d9": ["tag_1", "tag_2"], "d41d8cd98f00b204e9800998ecf8427e": ["tag_3", "tag_4"], "752efa2ae35bbcaa9fea2f7b27f65de8": ["tag_5", "tag_6"], + "848777223b0405239013218c0f0dc715": ["/example/path"] } } diff --git a/src/glimr.js b/src/glimr.js index c47fb1f..02c6a11 100644 --- a/src/glimr.js +++ b/src/glimr.js @@ -235,18 +235,19 @@ }; Gp.getTags = function(pixelId, callback) { - if (this.state.loadedTags[pixelId]) { - var response = this.state.loadedTags[pixelId]; + var pageCacheId = pixelId + this.currentURLCacheKey(); + if (this.state.loadedTags[pageCacheId]) { + var response = this.state.loadedTags[pageCacheId]; callback(response[0], response[1]); return; } - if (typeof this.state.loadingTags[pixelId] !== "undefined") { - this.state.loadingTags[pixelId].push(callback); + if (typeof this.state.loadingTags[pageCacheId] !== "undefined") { + this.state.loadingTags[pageCacheId].push(callback); return; } - this._requestTags(pixelId, callback, Library.bindFunction(this, function(data) { + this._requestTags(pixelId, pageCacheId, callback, Library.bindFunction(this, function(data) { var tags = []; var tagMappings = {}; var toCache = tags; @@ -277,10 +278,10 @@ } } - this.state.loadedTags[pixelId] = [tags, tagMappings]; + this.state.loadedTags[pageCacheId] = [tags, tagMappings]; - var callbacks = this.state.loadingTags[pixelId]; - delete this.state.loadingTags[pixelId]; + var callbacks = this.state.loadingTags[pageCacheId]; + delete this.state.loadingTags[pageCacheId]; for (var j = 0; j < callbacks.length; j += 1) { callbacks[j](tags, tagMappings); @@ -308,7 +309,7 @@ } }; - Gp._requestTags = function(pixelId, userCallback, parseCallback) { + Gp._requestTags = function(pixelId, pageCacheId, userCallback, parseCallback) { this.initGlimrId(); var pixelLastUpdated = this.getPixelLastUpdated(pixelId); @@ -335,8 +336,8 @@ } } - this.state.loadingTags[pixelId] = []; - this.state.loadingTags[pixelId].push(userCallback); + this.state.loadingTags[pageCacheId] = []; + this.state.loadingTags[pageCacheId].push(userCallback); Library.JSONP(requestUrl, parseCallback); };