OP-Engineering/link-preview-js

Slow getLinkPreview

0hio-creator opened this issue · 2 comments

Hi,

I'm not sure if this is expected.
Running this on react-native. I'm able to get a response. But the time it takes is very long.

On an IOS simulator, I see that getting the link preview takes about 2.5s
On an Android device, it's closer to 4s.

I've tried editing the headers user-agent cycling between none, Twitterbot & googlebot to no avail.

I was using 2.1.10 and updated to 3.0.3 but with no luck.

Code snippet bellow. Essentially fetching metadata on the component load and rendering an image when the metadata is returned.

Any help on improving performance would be great!

import { getLinkPreview, getPreviewFromContent } from "link-preview-js";

export const MetaImage = ({url}) => {
    
const [imageUrl, setImageUrl] = useState("")
    const fetchData = async () => {
        var ts = moment().valueOf()

        let data = await getLinkPreview(url, {
            headers: {
                "user-agent": "Twitterbot"
            }
        })
        console.log(data)
        if(
            data!=undefined &&
            data.images!=undefined &&
            typeof(data.images)=="object"&&
            typeof(data.images[0])=="string"
        ){
            console.log("fetch time: " + (moment().valueOf()-ts))
            setImageUrl(data.images[0])

        }
    }

    useEffect(()=>{
        fetchData()
    },[])

    console.log("imageurl: " + imageUrl)
    if(imageUrl.length==0){
        return (
            <View>
                <View
                    style={{width:50, height:50, backgroundColor:colors.lightGrey}}
                />
                <Text>{url}</Text>
            </View>
        )
    } else {
        return (
            <View>
                <Image
                    source={{
                        uri:imageUrl,
                        cache:'force-cache'
                    }}
                    style={{width:50, height:50, resizeMode:"contain"}}
                />
                <Text>{url}</Text>
            </View>
        )
    }

}

Doubt this has to do with the library, but if you provide a reproducible example I can take a look

No response from author, going to close this, it's pretty much impossible this library is the cause of slow requests. The only case I could think of is that the page trying to be parsed is huge and you are running this on a low-end device, on which case there is nothing I can do.