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

Benchmarks vs browser cache #21

Open
re-thc opened this issue Mar 5, 2020 · 6 comments
Open

Benchmarks vs browser cache #21

re-thc opened this issue Mar 5, 2020 · 6 comments

Comments

@re-thc
Copy link

re-thc commented Mar 5, 2020

Hi, when is a good time to use this backend vs relying on the browser cache? Localstorage isn't particularly fast. Has there been a benchmark that shows this is faster? Often re-fetching it is even faster than localstorage retrieval in cases like mobile. Thanks.

@jamuhl
Copy link
Member

jamuhl commented Mar 6, 2020

good question...which I can't really give an answer as I personally never had the need for caching in localStorage -> but there are devs using this...so must make sense in some scenarios

@darshakeyan
Copy link

darshakeyan commented Jun 17, 2022

Hi @jamuhl

I am using browser cache (local storage) in my product to store the all translations JSON file. I am facing issue while implementing this package to my product.

This is i18n.js file referring from https://www.i18next.com/how-to/caching#browser-caching-with-local-storage

i18n
  // pass the i18n instance to react-i18next.
  .use(Backend)
  .use(initReactI18next)
  // init i18next
  // for all options read: https://www.i18next.com/overview/configuration-options
  .init({
    backend: {
      backends: [
        LocalStorageBackend, // primary backend
        HttpApi, // fallback backend
      ],
      backendOptions: [
        {
          /* options for primary backend */
          expirationTime: 7 * 24 * 60 * 60 * 1000, // 7 days
        },
        {
          /* options for secondary backend */
        },
      ],
    },
    lng: 'en-US',
    fallbackLng: false,
    ns: [],
    // debug: true,
    load: 'currentOnly',
    keySeparator: '.',

    interpolation: {
      escapeValue: false, // react already safes from xss,
    },
  });

I am using useTranslation hook for translating the text to different language.

Example.js

import React, { useEffect, useRef } from 'react';
import { useTranslation } from 'react-i18next';

function Filters() {
  const { t: translateReport } = useTranslation('reports');
  return(
        <Button>
          {translateReport('filters.buttons.clearAll')}
        </Button>,
        <Button>
          {translateReport('filters.buttons.apply')}
        </Button>,
  );
}

here is the report.json translation file

{
  "filters": {
    "title": "Filters",
    "buttons": {
      "clearAll": "Clear All Filters",
      "apply": "Apply"
    },
    "submittedAt": "Submitted at",
 }
}

Whenever I change something on JSON file Its not reflecting to local storage. I have to clear the cache from local storage in browser and then again have reload whole react application then the changes got reflected in JSON file.

Currently I am thinking of two approach

  1. When JSON file modify needs to clear the local storage.
  2. When any JS file modify in code then will clear the local storage.

It will be very helpful if you share some insight here.

@jamuhl
Copy link
Member

jamuhl commented Jun 17, 2022

Your two approaches have two issues. The backend runs in the browser and has no idea about if you modified a JSON.

use expiration time or versions to invalidate the cache: https://github.com/i18next/i18next-localstorage-backend#cache-backend-options

@jamuhl
Copy link
Member

jamuhl commented Jun 17, 2022

And during development don't use it at all

@darshakeyan
Copy link

Thanks @jamuhl
I have misunderstand it actually now I got your point after reading below issue -
https://github.com/i18next/i18next-localstorage-backend/issues/3

Every release we have to change the version of JSON which will update the JSON file in production
correct me if I am wrong !

@jamuhl
Copy link
Member

jamuhl commented Jun 17, 2022

correct.

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

3 participants