theme-scripts
theme-scripts copied to clipboard
cookiesEnabled not present in cart.js
Shopify starter and Slate have a function that looks like the below.
import {cookiesEnabled} from '@shopify/theme-cart';
// Apply a specific class to the html element for browser support of cookies.
if (cookiesEnabled()) {
document.documentElement.className = document.documentElement.className.replace('supports-no-cookies', 'supports-cookies');
}
However the latest version of theme scripts / cart doesn't have that function cookiesEnabled exporting. This may be related to issue #5
I work around that error using navigator.cookiesEnabled
so a simple utility function i put in my own util.js looks like:
export function cookiesEnabled() {
return navigator.cookiesEnabled;
}
then in theme.js I remove the theme-cart import and import my own util.js
import { cookiesEnabled } from 'util';
if (cookiesEnabled()) {
document.documentElement.className = document.documentElement.className.replace('supports-no-cookies', 'supports-cookies');
}
Haven't taken the time to really look into it but I started getting this error within the last 24 hours. I did notice that cookiesEnabled
is still being exported from @shopify/theme-cart/dist/theme-cart.cjs.js
but nowhere else.
Yeah I ended up doing something similar @ogzcoder. Not a major issue but good to let the team know.
@ogzcoder Thanks for posting your workaround. Looks like it's actually navigator.cookieEnabled
(singular) rather than navigator.cookiesEnabled
(plural). MDN
This is probably a little less ideal, but I ended up going with the following in theme.js to keep everything in one place for now:
// Apply a specific class to the html element for browser support of cookies.
if (window.navigator.cookieEnabled) {
document.documentElement.className = document.documentElement.className.replace(
'supports-no-cookies',
'supports-cookies',
);
}
Updating an old theme and ran into this issue too.
Just for reference, this was the original code:
export function cookiesEnabled() {
let cookieEnabled = navigator.cookieEnabled;
if (!cookieEnabled) {
document.cookie = 'testcookie';
cookieEnabled = document.cookie.indexOf('testcookie') !== -1;
}
return cookieEnabled;
}