i18next / i18next-localstorage-backend

This is a i18next cache layer to be used in the browser. It will load and cache resources from localStorage and can be used in combination with the chained backend.
MIT License
88 stars 43 forks source link

Error: "window is not defined" while using this package in NextJS environment #30

Closed BertCatsburg closed 3 years ago

BertCatsburg commented 3 years ago

🐛 Bug Report

I'm getting an error on i18next-localstorage-backend:

(node:79871) UnhandledPromiseRejectionWarning: ReferenceError: window is not defined
    at getDefaults (/Users/bert/Project/projectname/node_modules/i18next-localstorage-backend/dist/cjs/i18nextLocalStorageBackend.js:51:12)

To Reproduce

Use this package in a NextJS environment.

Expected behavior

We expect that this package handles 'window' in SSR environments by, for example, checking for the 'window' variable and ignoring SSR.

Your Environment

Relevant packages in package.json:

 "dependencies": {

    "i18next": "^20.2.4",
    "i18next-chained-backend": "^3.0.2",
    "i18next-http-backend": "^1.2.4",
    "i18next-localstorage-backend": "^3.1.2",

    "next": "10.2.0",

    "react": "17.0.2",
    "react-children-utilities": "^2.5.0",
    "react-dom": "17.0.2",

    "react-i18next": "^11.8.15",

    "react-router-dom": "^5.2.0",

  },
adrai commented 3 years ago

i18next-localstorage-backend is only for browser usage... it was never designed to work also on server side.

BertCatsburg commented 3 years ago

That means that i18next-localstorage-backend cannot be used in NextJS projects?

Packages which are able to work on Server Side but have functionality only available on Client Side are usually testing if the 'window' variable is available before using it. So, the Server Side ignores the piece of code with the 'window', and it's picked up on Client Side and works as expected.

adrai commented 3 years ago

Feel free to provide a PR: https://github.com/i18next/i18next-localstorage-backend/blob/master/src/index.js#L35 for inspiration: https://github.com/i18next/i18next-browser-languageDetector/blob/master/src/browserLookups/localStorage.js#L1

BertCatsburg commented 3 years ago

See the Pull Request. It's my first one on public github, so please let me know if I forgot something in the process.