Skip to content

Commit 6c1886c

Browse files
committed
some environments, throws when accessing document.cookie
1 parent e6e4089 commit 6c1886c

5 files changed

+53
-31
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 8.0.1
2+
3+
- some environments, throws when accessing document.cookie
4+
15
### 8.0.0
26

37
- chore: set browsers target to defaults [286](https://github.com/i18next/i18next-browser-languageDetector/pull/286)

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2023 i18next
3+
Copyright (c) 2024 i18next
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

i18nextBrowserLanguageDetector.js

+36-28
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121

2222
// eslint-disable-next-line no-control-regex
2323
const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
24-
const serializeCookie = (name, val, options) => {
25-
const opt = options || {};
26-
opt.path = opt.path || '/';
24+
const serializeCookie = function (name, val) {
25+
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
26+
path: '/'
27+
};
28+
const opt = options;
2729
const value = encodeURIComponent(val);
2830
let str = `${name}=${value}`;
2931
if (opt.maxAge > 0) {
@@ -179,7 +181,6 @@
179181
if (lookupLocalStorage && localStorageAvailable()) {
180182
return window.localStorage.getItem(lookupLocalStorage) || undefined; // Undefined ensures type consistency with the previous version of this function
181183
}
182-
183184
return undefined;
184185
},
185186
// Deconstruct the options object and extract the lookupLocalStorage property
@@ -305,35 +306,43 @@
305306
}
306307
};
307308

308-
function getDefaults() {
309-
return {
310-
order: ['querystring', 'cookie', 'localStorage', 'sessionStorage', 'navigator', 'htmlTag'],
311-
lookupQuerystring: 'lng',
312-
lookupCookie: 'i18next',
313-
lookupLocalStorage: 'i18nextLng',
314-
lookupSessionStorage: 'i18nextLng',
315-
// cache user language
316-
caches: ['localStorage'],
317-
excludeCacheFor: ['cimode'],
318-
// cookieMinutes: 10,
319-
// cookieDomain: 'myDomain'
309+
let canCookies = false;
310+
try {
311+
// eslint-disable-next-line no-unused-expressions
312+
document.cookie;
313+
canCookies = true;
314+
// eslint-disable-next-line no-empty
315+
} catch (e) {}
316+
const order = ['querystring', 'cookie', 'localStorage', 'sessionStorage', 'navigator', 'htmlTag'];
317+
if (!canCookies) order.splice(1, 1);
318+
const getDefaults = () => ({
319+
order,
320+
lookupQuerystring: 'lng',
321+
lookupCookie: 'i18next',
322+
lookupLocalStorage: 'i18nextLng',
323+
lookupSessionStorage: 'i18nextLng',
324+
// cache user language
325+
caches: ['localStorage'],
326+
excludeCacheFor: ['cimode'],
327+
// cookieMinutes: 10,
328+
// cookieDomain: 'myDomain'
320329

321-
convertDetectedLanguage: l => l
322-
};
323-
}
330+
convertDetectedLanguage: l => l
331+
});
324332
class Browser {
325333
constructor(services) {
326334
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
327335
this.type = 'languageDetector';
328336
this.detectors = {};
329337
this.init(services, options);
330338
}
331-
init(services) {
339+
init() {
340+
let services = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
341+
languageUtils: {}
342+
};
332343
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
333344
let i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
334-
this.services = services || {
335-
languageUtils: {}
336-
}; // this way the language detector can be used without i18next
345+
this.services = services;
337346
this.options = defaults(options, this.options || {}, getDefaults());
338347
if (typeof this.options.convertDetectedLanguage === 'string' && this.options.convertDetectedLanguage.indexOf('15897') > -1) {
339348
this.options.convertDetectedLanguage = l => l.replace('-', '_');
@@ -355,8 +364,8 @@
355364
this.detectors[detector.name] = detector;
356365
return this;
357366
}
358-
detect(detectionOrder) {
359-
if (!detectionOrder) detectionOrder = this.options.order;
367+
detect() {
368+
let detectionOrder = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.options.order;
360369
let detected = [];
361370
detectionOrder.forEach(detectorName => {
362371
if (this.detectors[detectorName]) {
@@ -369,9 +378,8 @@
369378
if (this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0
370379
return detected.length > 0 ? detected[0] : null; // a little backward compatibility
371380
}
372-
373-
cacheUserLanguage(lng, caches) {
374-
if (!caches) caches = this.options.caches;
381+
cacheUserLanguage(lng) {
382+
let caches = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.options.caches;
375383
if (!caches) return;
376384
if (this.options.excludeCacheFor && this.options.excludeCacheFor.indexOf(lng) > -1) return;
377385
caches.forEach(cacheName => {

i18nextBrowserLanguageDetector.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@ import htmlTag from './browserLookups/htmlTag.js';
88
import path from './browserLookups/path.js';
99
import subdomain from './browserLookups/subdomain.js';
1010

11+
// some environments, throws when accessing document.cookie
12+
let canCookies = false;
13+
try {
14+
// eslint-disable-next-line no-unused-expressions
15+
document.cookie;
16+
canCookies = true;
17+
// eslint-disable-next-line no-empty
18+
} catch (e) {}
19+
const order = ['querystring', 'cookie', 'localStorage', 'sessionStorage', 'navigator', 'htmlTag'];
20+
if (!canCookies) order.splice(1, 1);
1121
const getDefaults = () => ({
12-
order: ['querystring', 'cookie', 'localStorage', 'sessionStorage', 'navigator', 'htmlTag'],
22+
order,
1323
lookupQuerystring: 'lng',
1424
lookupCookie: 'i18next',
1525
lookupLocalStorage: 'i18nextLng',

0 commit comments

Comments
 (0)