Git Product home page Git Product logo

Comments (7)

demchenkoalex avatar demchenkoalex commented on June 2, 2024 2

Released in v.1.6.0. Thanks guys!

from react-native-link-preview.

ammarahm-ed avatar ammarahm-ed commented on June 2, 2024 1

@TPXP @demchenkoalex A simple way to do this would be:

  let abortControllerTimeout: NodeJS.Timeout = null;
    const abort = new AbortController();
    

    let request = fetch(url, {
      headers: {
        'User-Agent':
          'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
      },
      signal: abort.signal
    });
    // Abort fetch request after 5 seconds.
    abortControllerTimeout = setTimeout(() => {
      abort.abort();
    }, 5000);
    let response = await request;
    clearTimeout(abortControllerTimeout);

  

Ofcourse the timeout can be modified. I think 5 seconds is enough for a normal request to fetch data but it can be more in case of very slow connections.

from react-native-link-preview.

demchenkoalex avatar demchenkoalex commented on June 2, 2024 1

hey @ammarahm-ed please do, thank you! :)

from react-native-link-preview.

demchenkoalex avatar demchenkoalex commented on June 2, 2024

Hey @TPXP thanks for bringing this up! Just googled this problem and it seems like fetch is very limited, I don't plan to add huge dependencies like fetch-blob or something, and the only workaround I found/can think of is adding timeout and break the request if timeout is met. WDYT?

from react-native-link-preview.

TPXP avatar TPXP commented on June 2, 2024

Hello, thank you very much for your feedback. I've also looked into it and I confirm fetch seems a little limited (although I haven't checked the React-Native implementation, but I doubt it implements non-standard features like request cancelling and progress reports).

I'm already using rn-fetch-blob in my app so adding it would not be an issue for me. Their fetch API supports cancelling requests. I understand your point about app size though, perhaps we can make it an optional dependency and leverage request cancelling if it's present?

If rn-fetch-blob is not present, a timeout would be a good workaround. (That's how OpenGraphScraper worked)

from react-native-link-preview.

ammarahm-ed avatar ammarahm-ed commented on June 2, 2024

I have updated the example to something that actually works. If you think it's a good solution, i can send a PR.

from react-native-link-preview.

shawarmaz avatar shawarmaz commented on June 2, 2024

is it possible to know whether or not the request timed out ? currently it's just returning an empty object. Which could be confused with "succeeded but empty"

from react-native-link-preview.

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.