You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Are there any plans to support erasing records from localStorage (and sessionStorage)?
Thank you for your work on this plugin.
Proposed solution
This would involve modifying the cookie helper functions to extract keys from the Storage instances, filter matching cookies, and remove them.
The internal implementation wouldn't be too complicated to add (see example below). I just don't know how this should be configurable for users integrating this plugin.
The following is a stripped down working example I've tested on a client project (that does not implement the aformentioned configuration variants):
import{isLocalStorageAvailable,isSessionStorageAvailable}from'../utils/environment.js';import*asCookieConsentfrom'vanilla-cookieconsent';CookieConsent.run({// ...onConsent: ()=>{toggleCategoryAnalytics();},onChange: ({ changedCategories })=>{if(changedCategories.includes('analytics')){toggleCategoryAnalytics();}},});functiontoggleCategoryAnalytics(){constisAccepted=CookieConsent?.acceptedCategory('analytics');window.dataLayer?.push(['consent','update',{'analytics_storage': (isAccepted ? 'granted' : 'denied'),}]);if(!isAccepted){constcookies=CookieConsent.getConfig('categories')?.analytics?.autoClear?.cookies;if(cookies.length){eraseStorageItems(cookies);}}}functioneraseStorageItems(cookies){letlocalStorageKeys;letsessionStorageKeys;for(const{name: cookieName}ofcookies){if(isLocalStorageAvailable()){localStorageKeys??=getAllStorageKeys(window.localStorage);constfoundCookies=findMatchingCookies(localStorageKeys,cookieName);for(constfoundCookieoffoundCookies){window.localStorage.removeItem(foundCookie);}}if(isSessionStorageAvailable()){sessionStorageKeys??=getAllStorageKeys(window.sessionStorage);constfoundCookies=findMatchingCookies(sessionStorageKeys,cookieName);for(constfoundCookieoffoundCookies){window.sessionStorage.removeItem(foundCookie);}}}}/** * This function is copied from `findMatchingCookies()` from * {@link https://github.com/orestbida/cookieconsent/blob/v3.0.0-rc.17/src/utils/cookies.js vanilla-cookieconsent}. * * @param {string[]} allCookies * @param {string} cookieName */functionfindMatchingCookies(allCookies,cookieName){// ...}/** * Returns array with all the cookie names. * * This function is based on `getAllCookies()` from * {@link https://github.com/orestbida/cookieconsent/blob/v3.0.0-rc.17/src/utils/cookies.js vanilla-cookieconsent}. * * @param {Storage} storage * @param {?RegExp} [regex] * @returns {string[]} */functiongetAllStorageKeys(storage,regex){/** * @type {string[]} */constcookieNames=[];/** * Save only the item names */for(constnameofObject.keys(storage)){if(regex){try{regex.test(name)&&cookieNames.push(name);// eslint-disable-next-line no-empty}catch(e){}}else{cookieNames.push(name);}}returncookieNames;}
Additional details
No response
The text was updated successfully, but these errors were encountered:
I think adding an option to delete items in localstorage is useful, but I don't agree on session storage ones, since they are deleted at the end of the browsing session.
Description
Are there any plans to support erasing records from
localStorage
(andsessionStorage
)?Thank you for your work on this plugin.
Proposed solution
This would involve modifying the cookie helper functions to extract keys from the
Storage
instances, filter matching cookies, and remove them.The internal implementation wouldn't be too complicated to add (see example below). I just don't know how this should be configurable for users integrating this plugin.
Option 1: `[category].autoClear.clear*Storage`
Option 2: `[category].autoClear.cookies[].clear*Storage`
Option 3: `[category].autoClear.*Storage[]`
The following is a stripped down working example I've tested on a client project (that does not implement the aformentioned configuration variants):
Additional details
No response
The text was updated successfully, but these errors were encountered: