Giter VIP home page Giter VIP logo

Comments (18)

adrai avatar adrai commented on June 5, 2024

Strange, there's also a dedicated test to test non-fetch environments: npm run test:xmlhttpreq
Can you create a reproducable example?

from i18next-http-backend.

defko avatar defko commented on June 5, 2024

Okay, I see where the issue is coming from. I am using webpack and it supports CommonJS so require is available in that context, therefore it is able to export node-fetch.
I assume you don't have such a setup in your test env and that is why you don't experience with this problem.

//getFetch.cjs
if (typeof require !== 'undefined') {
  var f = fetchApi || require('node-fetch')
  if (f.default) f = f.default
  exports.default = f
  module.exports = exports.default
}

Do you still need an example or is this description provides enough context?

from i18next-http-backend.

adrai avatar adrai commented on June 5, 2024

Any idea how this could be fixed?

from i18next-http-backend.

defko avatar defko commented on June 5, 2024

Could we check if it is node env maybe

const isNode =
  typeof process !== 'undefined' &&
  process.versions != null &&
  process.versions.node != null;

But as I see you support Deno as well, so in that case I don't know if it will work maybe better to check if it is NOT a browser, like

const isBrowser = 
  typeof window !== 'undefined' && 
  typeof window.document !== 'undefined';

I did not check this, but I suppose the tests should fail if it is not the right check.

from i18next-http-backend.

adrai avatar adrai commented on June 5, 2024

Can you test this in your environment?
just modify it directly in the node_modules...
I think this will not work, because when running webpack this is node that runs it...

from i18next-http-backend.

defko avatar defko commented on June 5, 2024

These solutions could work

const isNode =
  typeof process !== 'undefined' &&
  process.versions != null &&
  process.versions.node != null;

if (isNode && !fetchApi && fetchNode) fetchApi = fetchNode.default || fetchNode

or

const isBrowser =
  typeof window !== 'undefined' &&
  typeof window.document !== 'undefined';

if (!isBrowser && !fetchApi && fetchNode) fetchApi = fetchNode.default || fetchNode

Only now the XHR request fall into the error case as x.statusText === "OK" in

//lib/request.js
callback(x.statusText, { status: x.status, data: x.responseText })

and it is handled as err by

//index.js
    this.options.request(this.options, url, undefined, (err, res) => {
      if (res && res.status >= 500 && res.status < 600) return callback('failed loading ' + url, true /* retry */)
      if (res && res.status >= 400 && res.status < 500) return callback('failed loading ' + url, false /* no retry */)
      if (err) return callback(err, false)

But it could be tackled as a different bug I assume

from i18next-http-backend.

adrai avatar adrai commented on June 5, 2024

This sounds all a bit hacky...
So to recap... You have this only in cypress?
I don't know cypress.
I would love to have a small reproducable example. Is it possible for you to create it?

from i18next-http-backend.

defko avatar defko commented on June 5, 2024

Yep it is getting too confusing. I will put something together in next week.

Anyway thank you for the fast responses!

from i18next-http-backend.

adrai avatar adrai commented on June 5, 2024

alternatively, you could try to omit the require of node-fetch, with something like https://webpack.js.org/plugins/ignore-plugin/

from i18next-http-backend.

defko avatar defko commented on June 5, 2024

Hi @adrai

I just pushed a really simple example to reproduce the issue:
https://github.com/defko/i18next-xhr-webpack

from i18next-http-backend.

adrai avatar adrai commented on June 5, 2024

should be fixed with v1.0.14

from i18next-http-backend.

defko avatar defko commented on June 5, 2024

Thank you @adrai

from i18next-http-backend.

adrai avatar adrai commented on June 5, 2024

If you like this module don’t forget to star this repo. Make a tweet, share the word or have a look at our https://locize.com to support the devs of this project.

There are many ways to help this project πŸ™

from i18next-http-backend.

defko avatar defko commented on June 5, 2024

Hi @adrai,

First of all thank you for the quick fix, but looks like there is still an issue with a current solution. You can check the same example. As you can see there, the translation is still not working.

After a quick debug the issue is the XHR request set the error even if the request is success.
See loadUrl function in index.js,

 this.options.request(this.options, url, undefined, (err, res) => {
     // Here err==='OK'
      if (res && res.status >= 500 && res.status < 600) return callback('failed loading ' + url, true /* retry */)
      if (res && res.status >= 400 && res.status < 500) return callback('failed loading ' + url, false /* no retry */)
      if (err) return callback(err, false)

Do y want me to open a different bug for this?

from i18next-http-backend.

adrai avatar adrai commented on June 5, 2024

sorry, I missed that... will fix it asap

from i18next-http-backend.

adrai avatar adrai commented on June 5, 2024

should be fixed with v1.0.15

from i18next-http-backend.

defko avatar defko commented on June 5, 2024

Thanks! It is working flawlessly now

from i18next-http-backend.

adrai avatar adrai commented on June 5, 2024

Nice to hear πŸ‘

from i18next-http-backend.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.