Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Social media embeds #107

Closed
jbmoelker opened this issue Jan 12, 2024 · 0 comments · Fixed by #111
Closed

Social media embeds #107

jbmoelker opened this issue Jan 12, 2024 · 0 comments · Fixed by #111
Assignees

Comments

@jbmoelker
Copy link
Member

jbmoelker commented Jan 12, 2024

User story

As a content editor,
I want to embed content from social media,
so that I can easily enrich a web page with external content.

Possible solution

Use oembed, an open format designed to allow embedding content from a website into another page. OEmbed is supported by many providers (also available as npm package). An OEmbed endpoint returns HTML that can be embedded for a given resource URL (like a URL of a tweet on Twitter).

Example OEmbed usage by astro-embed-twitter:

---
import './Tweet.css';

export interface Props {
	id: string;
}
const { id } = Astro.props;

async function fetchTweet(id: string) {
	try {
		const oembedUrl = new URL('https://publish.twitter.com/oembed');
		oembedUrl.searchParams.set('url', id);
		oembedUrl.searchParams.set('omit_script', 'true');
		oembedUrl.searchParams.set('dnt', 'true');
		return (await fetch(oembedUrl).then((res) => res.json())) as {
			url: string;
			author_name: string;
			author_url: string;
			html: string;
		};
	} catch (e) {
		console.error(
			`[error]  astro-embed
         ${e.status} - ${e.statusText}: Failed to fetch tweet ${id}`
		);
	}
}

const tweet = await fetchTweet(id);
---

{tweet && <astro-embed-tweet set:html={tweet.html} />}

References

Questions?

@jbmoelker jbmoelker mentioned this issue Jan 14, 2024
4 tasks
@jbmoelker jbmoelker self-assigned this Jan 18, 2024
jbmoelker added a commit that referenced this issue Jan 22, 2024
# Changes

- Adds an Embed Block that renders external content based on the block's
OEmbed data.
- Supports [~300 content providers](https://oembed.com/providers.json)
(like Twitter, Flickr, YouTube, etc) using the
[OEmbed](https://oembed.com/) protocol.
  - Renders a noscript embed version which is dynamically enhanced.
  - Lazy loads embed scripts and iframes to improve performance.
  - Provides a mechanism to define custom renderer per provider.
- Adds specific implementations for Twitter, YouTube and Vimeo (the last
two reusing the Video Embed Block).
- If OEmbed data is unavailable, a link is displayed with the page title
of the given URL.
- Embed Block data is preloaded in the CMS using the [DatoCMS OEmbed
Plugin](https://github.com/voorhoede/datocms-plugin-oembed/).

# Associated issue

Resolves #107 

# How to test

1. Open preview link
2. Navigate to [page with
embeds](https://feat-embed-block.head-start.pages.dev/en/embeds/)
3. Verify the different embeds are rendered correctly
4. Verify embeds are lazy loaded when scrolled into view
5. Verify embeds show a basic (non-enhanced) version when JS is disabled
6. Review Embed Block model in the CMS
7. Add an Embed Block to a page
8. Verify a preview is displayed and the embed data is set
9. Verify the new embed is rendered on the page

# Checklist

- [x] I have performed a self-review of my own code
- [x] I have made sure that my PR is easy to review (not too big,
includes comments)
- [x] I have made updated relevant documentation files (in project
README, docs/, etc)
- ~~I have added a decision log entry if the change affects the
architecture or changes a significant technology~~
- ~~I have notified a reviewer~~

<!-- Please strike through and check off all items that do not apply
(rather than removing them) -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant