The jquery.cookie dependency is removed from core and replaced with js-cookie 3.0.0-rc.0.
A backwards-compatible shim is provided using the same core/jquery.cookie
library name which will be removed by Drupal 10.0.0. Additionally the core/drupal.form
andcore/drupal.tabledrag
no longer depend on core/jquery.cookie
. Module and theme JavaScript that requires this functionality should explicitly depend on core/jquery.cookie
instead.
Module and theme developers that depend on core/jquery.cookie
should instead depend on core/js-cookie
Before
my_module.libraries.yml
my_library:
js:
js/my_library.js: {}
dependencies:
- core/jquery
- core/jquery.cookie
- core/drupal
js/my_library.es6.js
(($, Drupal) => {
Drupal.behaviors.myModule = {
attach: () => {
// Set a cookie.
$.cookie('cutest', 'red panda');
// Retrieve a cookie.
const myCookieValue = $.cookie('cutest');
// Remove a cookie.
$.removeCookie('cutest');
// Store and retrieve as a JSON object.
$.cookie.json = true;
$.cookie('cutest', { animal: 'red panda' });
const fluffiness = $.cookie('cutest');
},
};
})(jQuery, Drupal);
After
my_module.libraries.yml
my_library:
js:
js/my_library.js: {}
dependencies:
- core/drupal
- core/js-cookie
js/my_library.es6.js
((Drupal, cookies) => {
Drupal.behaviors.myModule = {
attach: () => {
// Set a cookie.
cookies.set('cutest', 'red panda');
// Retrieve a cookie.
const myCookieValue = cookies.get('cutest');
// Remove a cookie.
cookies.remove('cutest');
// Store and retrieve as a JSON object. Use of the getJSON method should be avoided as that will be deprecated in js-cookie 3.0.0.
cookies.set('cutest', JSON.stringify({ animal: 'red panda' }));
const cutest = JSON.parse(cookies.get('cutest'));
},
};
})(Drupal, window.Cookies);
Behavior changes when using js-cookie instead of the jquery.cookie shim
- It is RFC 6265 compliant, and handles encoding of cookie values differently particularly when encoding JSON strings. Only required characters are encoded.
- The cookie will be stored at the root path by default when using js-cookie.
- The use of raw, unencoded values is strongly discouraged for good web security, but it is possible to work with raw cookie values by using a converter. The backwards-compatible shim makes use of a converter to store and retrieve values the same way as jquery.cookie.
Note for 9.0.0-beta1 testers
When js-cookie was introduced in Drupal 9.0.0-beta1 is used version 2.2.1, but was updated to 3.0.0-rc0 prior to stable release. If you used js-cookie during the beta, your code may need to make some adjustments to be compatible with the new version:
- Use
JSON.stringify
andJSON.parse
to get and set JSON cookies rather than passing in an object directly and usingCookies.getJSON()
. - Check whether the
Cookies.withAttributes
function exists, and use this instead of passing in options such as expires or setting a non-standard, default path.
Comments
typo: Cookies.set, Cookies.get and Cookies.remove
I came across this replacing jquery.cookie by core/js-cookie
In the "after" section there is a typo. Correction:
// Set a cookie.
Cookies.set('cutest', 'red panda');
// Remove a cookie.
Cookies.remove('cutest');
// Retrieve a cookie.
const cutest = Cookies.get('cutest');
Thankyou kreatIL
Thankyou kreatIL
Thanks
typo: Cookies.set, Cookies.get and Cookies.remove needs to update in doc as well