I’m working on a project that dynamically inserts embedded tweets into a page when a button is pressed. The issue is that those tweets are gathered and the embed html blobs stored, and so can be deleted (or the author can go private) between gathering and display. When this happens a 404 is logged in the JS console, but that error never bubbles up to my code. Attempting to fetch() that url manually 200s, so I can’t pre-check. There doesn’t appear to be any way to add a callback for that eventuality with twttr.events.bind, but I could be wrong about that. For obvious reasons I’d like to avoid displaying tweets in any way if the author has made the tweet inaccessible.
I realize that this could be done via the standard API or the oEmbed API, but my page has no backend, and so I can’t safely store oauth credentials and I can’t hit oEmbed because of CORS restrictions.
I am an experienced backend dev, but a JavaScript novice, so I may be missing something obvious.
Any ideas or advice are much appreciated.
2 Likes
A7rRay
#2
@hbeckpdx – thanks for you questions. I assume you are using factory function. If you scroll down you will see documentation that shows how the createTweet factory function returns a Promise. The Promise either resolves with a reference to the DOM element containing the embed or undefined if the Tweet is unavailable.
Here is an example. The first embed works because it is associated with an available Tweet and it logs a reference to the DOM element containing it. The second one doesn’t load anything and logs undefined to the console. If the Promise resolves with undefined it means the Tweet is no longer available.
Let me know if you have any other questions.
2 Likes
Ah-ha, thank you so much. I had just been injecting the HTML and then calling twttr.widgets.load(<container>).
1 Like
A7rRay
#4
If you are using the load method you can combine it with the Events API. Any embeds that failed to render will include the class .twitter-tweet-error . Here is an Example
1 Like
system
Closed
#5
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.