2020-02-17

Clientside Webmentions in Gatsby

indieweb, webmention, gatsby, react

banner

Image by Gerd Altmann from Pixabay

Webmention is not new and I liked the idea after reading @swyx's post, Clientside Webmentions - Joining the IndieWeb with Svelte.

Introduction

When I created a GitHub issue, I intended to follow Getting started with Webmentions in Gatsby by Knut Melvær.

The article used gatsby-plugin-webmention, which exposes webmention data during build time.
So I decided to follow @swyx's implementation (in Svelte) to provide a client-side implementation.

Please follow Knut's post if you want to provide a better experience (, which I will, to add Microformat2.

Assumption

This post assumes you've read @swyx's post and the prerequisite mentioned in it.
(Setting up webmention.io, brid.gy etc.)

This post will focus on adding client-side webmention.

Webmention Component

Webmention component accepts a target URL. It wraps the component with ErrorBoundary in case it fails and to show the error message.

In a gist, Webmention accepts a target URL to show count & replies.

I used react-error-boundary by Brian Vaughn, a core React team member for convenience.

1const WebmentionFallbackComponent = ({ componentStack, error }) => (...)2
3function Webmention({ target }) {4  return (5    <ErrorBoundary FallbackComponent={WebmentionFallbackComponent}>6      <Heading as="h2" mb={[2, 2, 3, 3]}>7        Webmentions8      </Heading>9      <WebmentionCount target={target} />10      <WebmentionReplies target={target} />11    </ErrorBoundary>12  )13}

You can replace Heading with h1~6 as I am using Theme-UI and Heading comes from that library.

Now let's dive into the implementation of WebmentionCount & WebmentionReplies.

Adding Counts

WebmentionCount component has the following structure.

  1. initialCounts has the "shape" of what's returned from Webmention endpoint.
  2. useEffect runs on target change to load Webmentions
  3. return displays loading status, error message, and counts.
1const initialCounts = {2  count: 0,3  type: {4    like: 0,5    mention: 0,6    reply: 0,7    repost: 0,8  },9};10
11function WebmentionCount({ target }) {12  const [counts, setCounts] = useState(initialCounts);13
14  // Get counts on `target` change.15  useEffect(() => {16    async function getCounts() {}17
18    getCounts();19  }, [target]);20
21  return (22    <>23      {counts === initialCounts && <p>Loading counts...</p>}24      {counts === undefined && <p>Failed to load counts...</p>}25      {counts && (26        <div>27          <span role="img" aria-label="emoji">28            ❤️29          </span>{" "}30          {counts.type.like + counts.type.repost || 0}{" "}31          <span role="img" aria-label="emoji">32            💬33          </span>{" "}34          {counts.type.mention + counts.type.reply || 0}35        </div>36      )}37    </>38  );39}

The interesting part happens inside the useEffect hook, which fetches webmetions.

1useEffect(() => {2  async function getCounts() {3    const url = `https://webmention.io/api/count.json?target=${target}`;4    const responseCounts = await fetch(url).then(response => response.json());5
6    setCounts(previousCounts => {7      return {8        ...previousCounts,9        ...responseCounts,10        type: {11          ...previousCounts.type,12          ...responseCounts.type,13        },14      };15    });16  }17
18  getCounts();19}, [target]);

The endpoint is https://webmention.io/api/count.json?target=${target}.
@swyx had an issue with a warning that,

This is the endpoint to hit: https://webmention.io/api/count.json?target=URL_TO_YOUR_POST/. ⚠️ NOTE: You will need that trailing slash for this request to work! I probably wasted 2 hours figuring this out. -- Clientside Webmentions - Simple Count

In my case, the trailing / was already added from a slug, so there was no need for me to add it. So make sure that your endpoint ends with /

setCounts merges existing counts with counts retrieved from webmention.io.

Paginated Mentions

I've translated most of @swyx's Svelte code in React here.

WebmentionsReplies loads only 30 replies per page. You can load more with fetch more button below and when there is no more reply, it shows a message.

The following code snippet shows the structure of WebmentionReplies Component.

1function Replies({ replies }) {...}2
3function WebmentionReplies({ target }) {4  const [page, setPage] = useState(0)5  const [fetchState, setFetchState] = useState("fetching")6
7  const mergeReplies = (oldReplies, newReplies) => [8    ...oldReplies,9    ...newReplies,10  ]11  const [replies, setReplies] = useReducer(mergeReplies, [])12  const perPage = 3013
14  const getMentions = () => fetch(...)15  const incrementPage = () => setPage(previousPage => previousPage + 1)16  const fetchMore = () => ...17
18  // Load initial comments once19  useEffect(() => {20    getMentions()21      .then(newReplies => {22        setReplies(newReplies)23        setFetchState("done")24      })25      .then(incrementPage)26  }, [])27
28  return (29    <>30      {fetchState === "fetching" && <Text>Fetching Replies...</Text>}31      <Replies replies={replies} />32      {fetchState !== "nomore" ? (33        <Button onClick={fetchMore}>34          Fetch More...35        </Button>36      ) : (37        <Text>38          No further replies found.{" "}39          <ExternalLink40            to={`https://twitter.com/intent/tweet/?text=My%20thoughts%20on%20${target}`}41          >42            Tweet about this post43          </ExternalLink>{" "}44          and it will show up here!45        </Text>46      )}47    </>48  )49}

It's longer than WebmentionCounts but the structure is similar.
WebmentionReplies keeps track of three states.

  1. page - Not shown to the site visitor
  2. fetchState - Track whether replies are being loaded or an error occurred or we are done.
  3. replies - Merges loaded replies with newly retrieved replies.

🎈 Why "useReducer"?

The last replies needs some explanation as it looks "different" from setCount used in WebcomponentCounts component.

With useReducer, one normally destructures an array as

const [state, dispatch] = useReducer(reducer, initialState);

useReducer is a way for you to specify "how" to merge the state with a reducer. To make setReplies call easier, the reducer function, mergeReplies simply merges old replies with the new replies.

There is a nice article by Lee Warrick Bridging the Gap between React's useState, useReducer, and Redux, if you want to find out more.

Doing so would let me merge replies like setReplies(newReplies) without having to specify old replies.

1useEffect(() => {2  getMentions()3    .then(newReplies => {4      // This merges old replies witht he new ones5      setReplies(newReplies);6      setFetchState("done");7    })8    .then(incrementPage);9}, []);

We now know states involved, let's see how to get replies.

🎈 Getting Replies

⚠: I wrongly named the method getMentions (instead of getReplies).

The same gotcha applies for the URL, which should end with a trailing / here (but my slug/target already contains / so not used here).

1const getMentions = () =>2  fetch(`https://webmention.io/api/mentions?page=${page}&per-page=${perPage}&target=${target}`)3    .then(response => response.json())4    .then(json => [...json.links]);

The endpoint contains an object of links array (of the following shape), which is what's saved.

1links: [2  {3    source: "https://...",4    id: 757399,5    data: {6      author: {7        name: "Sung M. Kim",8        url: "https://twitter.com/dance2die",9        photo: "https://webmention.io/....jpg"10      },11      url: "https://twitter.com...",12      name: null,13      content: null,14      published: null,15      published_ts: null16    },17    activity: {18      type: "like",19      sentence: "Sung M. Kim favorited ...",20      sentence_html: '<a href="">...</a>'21    },22    target: "https://sung.codes/blog..."23  },24]

The button in return fetches more if there are more records to retrieve.

<Button onClick={fetchMore}>Fetch More...</Button>

fetchMore event handler merges new replies if there were more to retrieve.
In the end, the page number is increment with incrementPage, which causes the next render caused by a button click to use a new page number.

1const fetchMore = () =>2  getMentions()3    .then(newReplies => {4      if (newReplies.length) {5        setReplies(newReplies);6      } else {7        setFetchState("nomore");8      }9    })10    .then(incrementPage);

🎈 Replies Component

This component simply iterates replies and making it look pretty.
Most of the components (Flex, Box, Text) used are from Theme-UI so you can use div or other elements to structure and style it.

1function Replies({ replies }) {2  const replyElements = replies.map(link => (3    <li key={link.id} sx={{ margin: "1.6rem 0" }}>4      <Flex direcition="row">5        <ExternalLink6          to={link.data.author.url}7          sx={{ flexShrink: 0, cursor: "pointer" }}8        >9          <Image10            sx={{ borderRadius: "50%" }}11            width={40}12            src={link.data.author.photo || AvatarPlaceholder}13            alt={`avatar of ${link.data.author.name}`}14          />15        </ExternalLink>16        <Dangerous17          sx={{ padding: "0 1rem 0" }}18          html={link.activity.sentence_html}19          as="article"20        />21      </Flex>22    </li>23  ))24
25  return (26    <Box my={[2, 2, 3, 3]}>27      {replies && replies.length ? (28        <ul sx={{ listStyle: "none" }}>{replyElements}</ul>29      ) : (30        <Text>There is no reply...</Text>31      )}32    </Box>33  )

One thing to mention is Dangerous component, which is just a wrapper for dangerouslySetInnerHTML.

It needs to be sanitized (I haven't done it yet) or use different properties not to use raw HTML.
(as it's a security issue).

Source Code

The full source for the components above is listed below.

  • Webmention: https://github.com/dance2die/sung.codes/blob/master/src/components/Webmention/index.js
  • WebmentionCount: https://github.com/dance2die/sung.codes/blob/master/src/components/Webmention/WebmentionCount.js
  • WebmentionReplies: https://github.com/dance2die/sung.codes/blob/master/src/components/Webmention/WebmentionReplies.js

Parting Words

I am considering to remove "Disqus" at the bottom when "webmention" is all set up properly

That's all folks. If there are any mentions for this post, you can see it 👇
If not scroll down to in this post to see webmentions.


Image by Gerd Altmann from Pixabay