Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to read the 'cookie' property from 'Document': Cookies are disabled inside 'data:' URLs. #279

Closed
m-nathani opened this issue Jan 8, 2024 · 3 comments

Comments

@m-nathani
Copy link

m-nathani commented Jan 8, 2024

馃悰 Bug Report

To Reproduce

A minimal reproducible example.

We found a report on sentry about failing to read cookies cookies on specific env mentioned below.

import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import HttpApi from 'i18next-http-backend';
import { initReactI18next } from 'react-i18next';
...

export const initI18next = ({ languages, defaultLanguage }) => {
  const detectionOptions = {
    order: ['cookie', 'localStorage', 'navigator'],
    lookupCookie: 'i18next-widget',
    lookupLocalStorage: 'i18nextLng-widget',
    caches: ['localStorage', 'cookie'],
    excludeCacheFor: ['cimode'],
  };

  ...

  i18nInstance = i18n.use(HttpApi).use(LanguageDetector).use(initReactI18next);
  i18nInstance
    .init({
      load: 'currentOnly',
      supportedLngs,
      cleanCode: true,
      interpolation: {
        escapeValue: false,
      },
      detection: detectionOptions,
      ns: ['translation', 'common'],
      react: {
        useSuspense: true,
      },
      backend: {
        // locale files can be found in the `public` directory
        loadPath: `${getFilePath()}/locales/{{lng}}/{{ns}}.json`,
        // We don't want to cache the translation files
        requestOptions: {
          cache: 'no-store',
        },
        // Required when widget is embedded in partner website?
        crossDomain: true,
      },
    })
    .then(() => {
      // TODO: Lazy load moment locale?
      moment.locale(i18nInstance?.language);
    });

  return i18nInstance;
};

Expected behavior

Plugin for browser detection should have checks for reading cookies if they are enabled.

something similar to navigator.cookieEnabled :
https://developer.mozilla.org/en-US/docs/Web/API/Navigator/cookieEnabled

P.S: will add these check in my code too.. however would be great to handle in detector itself to handle fallback.

Your Environment

  • runtime version: i.e. node v20
  • i18next version:
    "i18next": "^23.6.0",
    "i18next-browser-languagedetector": "^7.1.0",
    "i18next-http-backend": "^2.3.1",
    "react-i18next": "^13.3.1",
@adrai
Copy link
Member

adrai commented Jan 8, 2024

You can do this yourself:

const detectionOptions = {
    order: navigator.cookieEnabled ? ['cookie', 'localStorage', 'navigator'] : ['localStorage', 'navigator'],
    lookupCookie: 'i18next-widget',
    lookupLocalStorage: 'i18nextLng-widget',
    caches: navigator.cookieEnabled ? ['localStorage', 'cookie'] : ['localStorage'],
    excludeCacheFor: ['cimode'],
  };

@adrai
Copy link
Member

adrai commented Jan 8, 2024

fyi: similar discussion: js-cookie/js-cookie#823 (comment)

@adrai adrai closed this as completed Jan 8, 2024
@m-nathani
Copy link
Author

fyi: similar discussion: js-cookie/js-cookie#823 (comment)

Way to go, for creating a robust api ! 馃槅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants