Comments (9)
After testing stuff a little bit more, I changed the require from cheerio-without-node-native
to cheerio
, unfortunately the error still remains.
But I found out that if you try cheerio.load(body)
when body
is undefined
, this is what causes the issue. By simply doing cheerio.load(body || '')
I managed to get this answer:
contentType: "text/html; charset=ISO-8859-1"
description: undefined
favicons: ["https://www.google.com/favicon.ico"]
images: []
mediaType: "website"
siteName: undefined
title: ""
url: "https://www.google.com/"
videos: []
__proto__: Object
It's better but there are still issues, I'm missing a few data (description, siteName, title,....)
Will keep you posted if I find a fix, but I would appreciate some feedback on my findings :)
UPDATE:
By testing with another link (https://www.reddit.com
), I get this result:
contentType: "text/html; charset=utf-8"
description: "Reddit is a network of communities based on people's interests. Find communities you're interested in, and become part of an online community!"
favicons: (13) ["https://www.redditstatic.com/desktop2x/img/favicon/android-icon-192x192.png", "https://www.redditstatic.com/desktop2x/img/favicon/favicon-32x32.png", "https://www.redditstatic.com/desktop2x/img/favicon/favicon-96x96.png", "https://www.redditstatic.com/desktop2x/img/favicon/favicon-16x16.png", "https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-57x57.png", "https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-60x60.png", "https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-72x72.png", "https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-76x76.png", "https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-114x114.png", "https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-120x120.png", "https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-144x144.png", "https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-152x152.png", "https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-180x180.png"]
images: ["https://www.redditstatic.com/icon.png"]
mediaType: "website"
siteName: "reddit"
title: "reddit"
url: "https://www.reddit.com/"
This means the description and everything works fine for Google, and to be sure I compared with the preview that Facebook's Messenger displays and there's no description etc.
Unfortunately by doing the same to compare for the Reddit link, it looks like the title isn't good. Messenger displays reddit: the front page of the internet
which is the title you see in your browser tab but in the response I get from your package, I only get reddit
.
Any idea of what could be causing this ?
from link-preview-js.
Let's go by parts:
- You cannot replace
cheerio-without-node
forcheerio
, because as the name suggestcheerio
requires node libraries which are not available on RN. - As you can see you can get responses from other sites just fine, I also tested again on the simulator and it's working, this clearly is some limitation on cheerio's side when parsing non valid HTML, which apparently is the case for
https://www.google.com
but other links however are working just fine, unfortunately work on cheerio-without-node has stopped, I might replace it if I find a suitable alternative but this seems like an error on the site page rather than on this library, I will however catch the error, since that doesn't seem to be working atm. - The title is being parsed by the following function:
const getTitle = function (doc) {
var title = doc("meta[property='og:title']").attr('content');
if (!title) {
title = doc('title').text();
}
return title;
};
I don't know how other apps/libraries parse it, but this is the way this library does it, what the sites put in those fields it's up to the sites, not me.
Thanks for noticing the bug!
from link-preview-js.
I've updated/re-written the library in typescript, the error is now correctly being thrown when there is a parsing error, I cannot do anything to fix google.com though, let me know if you have any other question.
from link-preview-js.
Just so you know, you can use node libraries in RN by using rn-nodeify
. Not ideal, but if you got not other choice this is the way to go.
from link-preview-js.
@ospfranco
Also, I tried with your new version (2.0.0
) and the error is well caught.
But do you think you could use rn-nodeify
to make cheerio
work instead of cheerio-without-node-native
just like I did ?
This would allow you to do cheerio.load(body || '')
and ensure we can still get basic informations from the text response, like for instance the site picture etc.
from link-preview-js.
If you look at the response you got, there are several fields missing, at the same time, this library is not RN only, adding nodeify to it might break stuff for node environments and increase the package size.
So that being said, I don't really see the benefit of doing this, in order to fix a single broken HTML document, google will probably publish a new version in no time and then the change is for nothing, happened with youtube a year ago and problem went away on its own.
from link-preview-js.
however if you feel strongly about it, you can create a PR, test it, I'll review it and potentially merge it.
from link-preview-js.
If you look at the response you got, there are several fields missing, at the same time, this library is not RN only, adding nodeify to it might break stuff for node environments and increase the package size.
So that being said, I don't really see the benefit of doing this, in order to fix a single broken HTML document, google will probably publish a new version in no time and then the change is for nothing, happened with youtube a year ago and problem went away on its own.
You're right, it was a stupid question.
Great work anyway :)
from link-preview-js.
I've literally lost hours, trying to use google.com
as my example domain, which I've learned that isn't working right now. Maybe a warning about trying with some other domain than google.com
would be helpful.
from link-preview-js.
Related Issues (20)
- It doesn't pass robot protection on some websites HOT 2
- SSRF HOT 17
- I'm using Sveltekit - working perfectly in Netlify but not in CloudFlare Pages HOT 1
- 2.1.17 Version Need to Bump HOT 2
- Timeout not respected HOT 7
- Incompatible patch versions HOT 1
- Receiving incorrect response for a specific website HOT 5
- Getting "`abort_controller_1.default` is not a constructor" error - Trying to use with Nuxt 3. HOT 1
- In the handleRedirects option, the demo code has a conditional that is missing, causing an error on some redirects. Just need to add another OR statement ```|| "www." + forwardedURLObj.hostname === urlObj.hostname``` to handle another edge case HOT 5
- A way to not throw an error if there's no URL in the text HOT 2
- getVideos crashes when there are video tags but no type or secure_url tags HOT 3
- Slow getLinkPreview HOT 2
- Yarn add error: Cannot read properties of undefined (reading 'name') HOT 1
- Follow multiple redirects (maybe with a configurable max) HOT 4
- Attention Required! | Cloudflare HOT 3
- replace url module with new URL HOT 2
- Error - Image SVG HOT 1
- Not working with windows-1251 charset HOT 2
- react native android issue HOT 2
- Crash: getLinkPreview crashes when resolving LinkedIn profile urls HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from link-preview-js.