Writing

Cross-posting to dev.to, Hashnode and Medium with a canonical URL

By Mohamed Gado · · 5 min read

Each article I write lives on my own site first, then gets a copy on dev.to, Hashnode and Medium. Each copy carries a canonical link back to the original, so the copies bring in readers without competing with it in search. A canonical is a hint to search engines, not a command, but it's the signal all three platforms support. Only dev.to has an API I can use, so the other two are a paste with a checklist. Here's the routine for 24 articles, and the spots where each platform quietly drops the canonical, the code blocks or the tables.

What a canonical URL does on a cross-post#

A copy of an article on another domain is duplicate content. Search engines pick one version to rank. The canonical link (<link rel="canonical" href="..."> in the copy's head) tells them which version you want them to pick. Without it, a big platform can outrank your own site with your own article.

Each platform sets that tag from a field in the post settings. If you skip the field, the copy points to itself.

dev.to: the API, with two traps#

dev.to (Forem) has a real API. One POST creates a published post with the canonical set:

export function devtoArticle(a) {
  return {
    title: a.title,
    body_markdown: a.body,
    published: true,
    canonical_url: a.url,
    description: a.description,
    tags: devtoTags(a.tags),
  }
}

Two things bit me:

  • The RSS import makes drafts, sometimes without the canonical. If you connect your feed in dev.to's settings, new articles show up as drafts. My script publishes those drafts with the canonical filled in, instead of creating a second copy.
  • Front matter beats the API field. An imported draft's body_markdown starts with front matter saying published: false. A PUT that sends published: true without a new body returns 200 and leaves the draft a draft. Send the body every time.

dev.to also limits how fast you can create posts. One every 35 seconds stays under it. Tags are capped at 4, lowercase, letters and digits only, so Next.js becomes nextjs.

To never post twice, the script lists my posts and skips any article whose canonical URL or title already has a copy. Checking the title as well catches imported drafts that came in without a canonical.

Hashnode: paste the Markdown, set the canonical by hand#

Hashnode's API and bulk import are Pro features, so on the free plan each post is a paste. My script writes a Markdown copy of each article with every site-relative link made absolute. A link to /writing/x in a copy would otherwise point at Hashnode.

In the post settings there are three fields to fill:

  • The canonical URL. It's the "originally published" option in the article settings.
  • The SEO title. I use the same short title as the site's <title>.
  • The SEO description. Hashnode caps it at 150 characters. My site allows 160, and today's three articles ran 153 to 158, so each needed a trimmed copy. I now write descriptions at 150 or less so one version works everywhere.

To check what's already on Hashnode, the blog's RSS feed isn't enough: mine lists only the 20 newest posts, and a few Hashnode slugs differ from mine. The blog's sitemap lists every post. I fetch each URL and compare its canonical, which tells me which articles have a copy.

Medium: paste HTML, because the importer drops code#

Medium has no API for new integrations, and its "import a story" tool dropped every code block from my pages. For a technical blog that rules it out. What works is pasting HTML into the editor, so the script also writes a plain HTML copy of each article, with three fixes for Medium's editor:

export async function plainHtml(markdown) {
  const file = await unified()
    .use(remarkParse)
    .use(remarkGfm)
    .use(remarkRehype)
    .use(tablesToLists)
    .use(rehypeStringify)
    .process(markdown)
  return String(file)
    // Medium splits a code block into separate boxes at every empty line.
    .replace(/<pre>[\s\S]*?<\/pre>/g, (pre) => pre.replace(/\n(?=\n)/g, '\n '))
    // The Windows HTML clipboard garbles raw UTF-8, so non-ASCII goes in as entities.
    .replace(/[^\x00-\x7f]/gu, (ch) => `&#${ch.codePointAt(0)};`)
}
  • Tables become lists. Medium has no tables, and pasted cells run together into one line. A small rehype plugin turns each row into a bullet like "RSC payload — 280 KB".
  • Empty lines inside code get a no-break space, so each code block stays one box.
  • Non-ASCII becomes HTML entities, because the clipboard step mangles UTF-8.

On Windows, Set-Clipboard -AsHtml puts the file on the clipboard as rich HTML. That switch exists only in Windows PowerShell 5.1, not in PowerShell 7, so call it through powershell.exe:

powershell.exe -NoProfile -Command "Set-Clipboard -AsHtml -Value (Get-Content -Raw 'article.html')"

Then type the title, press Enter and paste. Before publishing, I count the <pre> elements in the editor against the code blocks in the source.

Medium's canonical setting is only in the story's settings page after publishing: Advanced Settings → "This story was originally published elsewhere", then Edit canonical link, paste the URL, Save canonical link. Topics allow 5 per story and 25 characters each. Commas don't separate them; press Enter after each one.

Check the canonical on the live copy#

A form that says "saved" isn't proof. For each copy, I read the canonical from the published page:

  • dev.to and Hashnode: fetch the post URL and read link[rel=canonical].
  • Medium: it returns 403 to scripted requests, so I run the same fetch from a tab that's already open on medium.com and parse the HTML there.
const html = await (await fetch('/p/<post-id>')).text()
new DOMParser().parseFromString(html, 'text/html').querySelector('link[rel=canonical]')?.getAttribute('href')

That check, plus a code block count per copy, is what I call done. Every copy URL also goes into a dated note in the repo, so nothing gets posted twice.

Takeaways#

  • Write once on your own domain, and give every copy a canonical link back to it.
  • dev.to: use the API, and send the body on every update, because its front matter beats the published field.
  • Hashnode on the free plan: paste Markdown with absolute links, and keep descriptions at 150 characters or less.
  • Medium: paste HTML, not the importer. Turn tables into lists, set the canonical after publishing, then read it back from the live page.