From c95d087289375f8bd5bc61727c2c84e4abea8aaa Mon Sep 17 00:00:00 2001 From: Chris Garrity Date: Wed, 29 Jan 2020 16:11:58 -0500 Subject: [PATCH] Ensure that the old scratchlanguage cookie is removed if it exists. On init, remove the old cookie if it exists. On the project page, make sure `handleSetLanguage` also sets the domain. --- src/init.js | 11 +++++++++++ src/views/preview/project-view.jsx | 6 +++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/init.js b/src/init.js index d7f9984bb..bf798094a 100644 --- a/src/init.js +++ b/src/init.js @@ -18,6 +18,17 @@ const jar = require('./lib/jar'); if (['pt', 'pt-pt', 'PT', 'PT-PT'].indexOf(obj) !== -1) { obj = 'pt-br'; // default Portuguese users to Brazilian Portuguese due to our user base. Added in 2.2.5. } + } else { + // delete the old cookie (just hostname) by setting it to null and expiring in the past + /* eslint-disable max-len */ + document.cookie = `scratchlanguage=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=${window.location.hostname}`; + /* eslint-enable max-len */ + // create the new cookie + let opts = {}; + if (window.location.hostname !== 'localhost') { + opts = {domain: `.${window.location.hostname}`}; + } + jar.set('scratchlanguage', obj, opts); } return obj; }; diff --git a/src/views/preview/project-view.jsx b/src/views/preview/project-view.jsx index b9471eeaf..676e1b897 100644 --- a/src/views/preview/project-view.jsx +++ b/src/views/preview/project-view.jsx @@ -626,7 +626,11 @@ class Preview extends React.Component { ); } handleSetLanguage (locale) { - jar.set('scratchlanguage', locale); + let opts = {}; + if (window.location.hostname !== 'localhost') { + opts = {domain: `.${window.location.hostname}`}; + } + jar.set('scratchlanguage', locale, opts); } handleUpdateProjectId (projectId, callback) { this.setState({projectId: projectId}, () => {