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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cached translation issues with Minimal TCF experience #5306

Merged
merged 7 commits into from
Sep 20, 2024

Conversation

gilluminate
Copy link
Contributor

@gilluminate gilluminate commented Sep 20, 2024

Closes PROD-2764

Description Of Changes

When the fides.js script gets cached, but a user changes their browser's language, we get into a weird state with multiple languages (especially after the GVL languages load correctly).

Code Changes

  • Update fides.js response headers to include Accept_Language in the Vary to instruct caches to reset when language changes
  • Address a few inconsistencies left over from when we switched to Minimal TCF
  • Determine if Minimal TCF Experience translation matches the user's locale (in the case of caching or window.fides_overrides.fides_locale) and load the available translation until full TCF Experience is ready with the correct language.

Steps to Confirm

  1. Throttle network in browser dev tools to help see when things are loading slower
  2. Load demo page with TCF enabled (eg. http://localhost:3001/fides-js-demo.html?geolocation=eea)
  3. Everything should load normally. Do not open the modal or save anything. We don't want to set a cookie yet.
  4. Change browser's preferred language in browser settings to something other than English that is available on your configured TCF experience (eg. French)
  5. Make sure "Disable Cache" is not active in your browser's network tab while throttling
  6. reload the page (not a hard reload, we want to test the browser cache)
  7. Page should load with the selected language in-spite of fides.js having been cached already (it should ignore the cache with the browser setting change).
  8. Revert browser to english. You can clear cookies and cache now.
  9. Update fides-js-demo.html:
    Replace:
// window.fides_overrides = {
//   fides_embed: true,
// };

With:

window.fides_overrides = {
  fides_locale: "fr",
};
  1. Reload the page now
  2. Since the server isn't aware of the page's window setting, it will first load the English banner and the language picker's globe icon should immediately start spinning to indicate a language is loading. Once the full experience loads, it should replace English with the configured language of French. This is just an edge case we need to handle gracefully.

Pre-Merge Checklist

  • All CI Pipelines Succeeded
  • Issue Requirements are Met
  • Update CHANGELOG.md

Copy link

vercel bot commented Sep 20, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
fides-plus-nightly ⬜️ Ignored (Inspect) Visit Preview Sep 20, 2024 4:50pm

@gilluminate gilluminate force-pushed the PROD-2764-mediavine-fides-js-client-code branch from 7ea867f to a525282 Compare September 20, 2024 00:13
Comment on lines 78 to 82
useEffect(() => {
if (!currentLocale && i18n.locale) {
setCurrentLocale(i18n.locale);
}
}, [currentLocale, i18n.locale, setCurrentLocale]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i18n-context was updated which made this unnecessary

Comment on lines 73 to 79
useEffect(() => {
if (!currentLocale) {
// initialize the i18n locale using the minimal experience
setCurrentLocale(minExperienceLocale);
}
}, [currentLocale, minExperienceLocale, setCurrentLocale]);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i18n-context was updated which made this unnecessary

Comment on lines -103 to -106
// We need to track if the experience and GVL translations are loading separately
// because they are loaded asynchronously and we need to know when both are done
// in order to set the i18n loading state.
let isExperienceLoading = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was causing the globe to spin unnecessarily while GVL translations loaded in the background. GVL is only needed for Modal, so this is overkill on the banner.

Copy link

cypress bot commented Sep 20, 2024

fides    Run #10023

Run Properties:  status check passed Passed #10023  •  git commit 84353fdef6 ℹ️: Merge 78060f3695458b6392e39b2ed05225c393baa1eb into ec2d33b9d50c3008e67b54f007e3...
Project fides
Branch Review refs/pull/5306/merge
Run status status check passed Passed #10023
Run duration 00m 37s
Commit git commit 84353fdef6 ℹ️: Merge 78060f3695458b6392e39b2ed05225c393baa1eb into ec2d33b9d50c3008e67b54f007e3...
Committer Jason Gill
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 4
⚠️ You've recorded test results over your free plan limit.
Upgrade your plan to view test results.
View all changes introduced in this branch ↗︎

Comment on lines 34 to 41
const onSetCurrentLocale = useCallback(
(locale: string) => {
i18nInstance.activate(locale);
setCurrentLocale(locale);
},
[i18nInstance, setCurrentLocale],
);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was all overkill and was causing some problems. Just use the ol' i18n.locale and i18n.activate as intended

@gilluminate gilluminate force-pushed the PROD-2764-mediavine-fides-js-client-code branch from 15de4d5 to 78060f3 Compare September 20, 2024 16:50
Copy link
Contributor

@lucanovera lucanovera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work! The vary header is present and working in Firefox, apparently chrome doesn't respect it :(, but even if the response is cached initially your 2nd fix handles it nicely so the users get the correct language. Approved!

@gilluminate gilluminate merged commit 1b1ab1f into main Sep 20, 2024
13 checks passed
@gilluminate gilluminate deleted the PROD-2764-mediavine-fides-js-client-code branch September 20, 2024 19:56
Copy link

cypress bot commented Sep 20, 2024

fides    Run #10032

Run Properties:  status check passed Passed #10032  •  git commit 1b1ab1fb62: Fix cached translation issues with Minimal TCF experience (#5306)
Project fides
Branch Review main
Run status status check passed Passed #10032
Run duration 00m 37s
Commit git commit 1b1ab1fb62: Fix cached translation issues with Minimal TCF experience (#5306)
Committer Jason Gill
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 4
⚠️ You've recorded test results over your free plan limit.
Upgrade your plan to view test results.
View all changes introduced in this branch ↗︎

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

Successfully merging this pull request may close these issues.

2 participants