Skip to content

Commit

Permalink
Remove duplicate global_bar_seen cookies
Browse files Browse the repository at this point in the history
We had a bug which meant that the global_bar_seen cookie was sometimes set more than once.
This bug has now been fixed (#2026), but some users will be left with these duplicate cookies and therefore will continue to see the issue.

This commit introduces a function that checks for these duplicate cookies and deletes them if they're present.
  • Loading branch information
Vanita Barrett committed Feb 10, 2020
1 parent 7ec7903 commit 7249fcc
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions app/assets/javascripts/global-bar-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,32 @@ var globalBarInit = {
return new RegExp(paths.join("|")).test(window.location.pathname)
},

checkDuplicateCookie: function() {
var cookies = document.cookie.split(';')
var matches = 0

for (var i = 0; i < cookies.length; i++) {
if (cookies[i] && cookies[i].indexOf('global_bar_seen') !== -1) {
matches++
}
}

if (matches > 1) {
var possiblePaths = window.location.pathname.split("/")
var pathString= ""

// The duplicate cookie will have a path set to something other than "/".
// The cookie will only surface on that path or it's sub-paths
// As there isn't a way of directly finding out the path, we need to try cookie deletion with all path combinations possible on the current URL.
for (var i = 0; i < possiblePaths.length; i++) {
if (possiblePaths[i] !== "") {
pathString = pathString + "/" + possiblePaths[i]
document.cookie = 'global_bar_seen=; expires=Thu, 01 Jan 1970 00:00:01 GMT;path=' + pathString
}
}
}
},

setBannerCookie: function() {
var cookieCategory = window.GOVUK.getCookieCategory(GLOBAL_BAR_SEEN_COOKIE)
var cookieConsent = GOVUK.getConsentCookie()
Expand All @@ -52,6 +78,11 @@ var globalBarInit = {

init: function() {
if (!globalBarInit.blacklistedUrl()) {
// We had a bug which meant that the global_bar_seen cookie was sometimes set more than once.
// This bug has now been fixed, but some users will be left with these duplicate cookies and therefore will continue to see the issue.
// We need to check for duplicate cookies so we can delete them
globalBarInit.checkDuplicateCookie()

if (globalBarInit.getLatestCookie() === null) {
globalBarInit.setBannerCookie()
globalBarInit.makeBannerVisible()
Expand Down

0 comments on commit 7249fcc

Please sign in to comment.