theme-scripts icon indicating copy to clipboard operation
theme-scripts copied to clipboard

cookiesEnabled not present in cart.js

Open isaac-martin opened this issue 5 years ago • 5 comments

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

isaac-martin avatar Oct 03 '18 21:10 isaac-martin

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.

ogzcoder avatar Oct 04 '18 06:10 ogzcoder

Yeah I ended up doing something similar @ogzcoder. Not a major issue but good to let the team know.

isaac-martin avatar Oct 04 '18 14:10 isaac-martin

@ogzcoder Thanks for posting your workaround. Looks like it's actually navigator.cookieEnabled (singular) rather than navigator.cookiesEnabled (plural). MDN

milesw avatar Dec 13 '18 10:12 milesw

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',
  );
}

Cam avatar Mar 04 '19 04:03 Cam

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;
}

cbodtorf avatar Nov 25 '19 13:11 cbodtorf