🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Content & MDX••By Komal Nakrani

Astro External Links: Open Them in a New Tab

Make Astro external links open in a new tab with rel noopener: use rehype-external-links with unified(), a Sätteri hast plugin, or a link component in .astro files.

#Astro#External Links#Markdown#rehype

To make external links in Astro Markdown open in a new tab, add target="_blank" and rel="noopener noreferrer" to every link whose href starts with http. For .md and .mdx content, do it with a plugin: rehype-external-links on the unified() processor, or a small hast plugin on Astro 7’s default Sätteri processor. In .astro files, use a link component.

Key Takeaways

  • Markdown has no syntax for target, so a plugin adds it at build time.
  • rehype-external-links handles target, rel and an optional icon with the unified() processor.
  • On the default Sätteri processor, a 15-line hast plugin does the same job faster.
  • Always pair target="_blank" with rel="noopener"; add noreferrer if you don’t want to send the referrer.
npm install @astrojs/markdown-remark rehype-external-links
// astro.config.mjs
import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';
import { unified } from '@astrojs/markdown-remark';
import rehypeExternalLinks from 'rehype-external-links';

export default defineConfig({
  site: 'https://www.example.com',
  integrations: [mdx()],
  markdown: {
    processor: unified({
      rehypePlugins: [
        [rehypeExternalLinks, { target: '_blank', rel: ['noopener', 'noreferrer'] }],
      ],
    }),
  },
});

The plugin treats any absolute http or https URL as external. If you sometimes write absolute links to your own domain, pass a test function that returns false for them. It also accepts content to append an icon or a visually hidden “(opens in a new tab)” label, which is worth doing for accessibility. MDX inherits the processor, so both formats are covered.

Option 2: a Sätteri hast plugin

Astro 7 uses Sätteri by default, which doesn’t run rehype plugins. If you’d rather keep it, write the same logic as a hast plugin:

// src/markdown/external-links.mjs
import { defineHastPlugin } from 'satteri';

const ownHost = 'www.example.com';

export const externalLinks = defineHastPlugin({
  name: 'external-links',
  element: {
    filter: ['a'],
    visit(node, ctx) {
      const href = String(node.properties?.href ?? '');
      if (!/^https?:\/\//.test(href)) return;
      if (new URL(href).hostname === ownHost) return;
      ctx.setProperty(node, 'target', '_blank');
      ctx.setProperty(node, 'rel', 'noopener noreferrer');
    },
  },
});
// astro.config.mjs
import { defineConfig } from 'astro/config';
import { satteri } from '@astrojs/markdown-satteri';
import { externalLinks } from './src/markdown/external-links.mjs';

export default defineConfig({
  markdown: {
    processor: satteri({ hastPlugins: [externalLinks] }),
  },
});

filter: ['a'] means only anchor elements reach your JavaScript, which keeps it fast on large sites. The Markdown pipeline explainer covers Sätteri plugins in more detail.

Plugins only touch Markdown and MDX. For links in components and layouts, wrap the logic once:

---
// src/components/SmartLink.astro
import type { HTMLAttributes } from 'astro/types';

type Props = HTMLAttributes<'a'>;
const { href = '', ...rest } = Astro.props;
const url = String(href);
const isExternal = /^https?:\/\//.test(url) && new URL(url).origin !== Astro.site?.origin;
---
<a
  href={url}
  {...rest}
  {...(isExternal && { target: '_blank', rel: 'noopener noreferrer' })}
>
  <slot />{isExternal && <span class="sr-only"> (opens in a new tab)</span>}
</a>

Astro.site comes from the site option, so set it in your config. You can also pass this component to MDX through the components prop so [text](url) in MDX uses it.

It’s a judgement call. Opening a new tab keeps readers on your page, which many content sites want, but it takes control away from the visitor and can confuse screen reader users if it isn’t announced. A reasonable middle ground: open new tabs only for links in long-form content, announce it with visually hidden text or an icon with a label, and leave navigation links alone. Our Astro accessibility checklist covers link text and focus in more depth, and the remark and rehype plugins guide lists other link-related plugins. Most blog themes render post content through <Content />, so the plugin approach applies to them unchanged.

FAQ

Markdown can’t express target, so either write the link as HTML (<a href="..." target="_blank" rel="noopener">) or add a plugin that sets target and rel on every external link at build time.

Is rel=“noopener” still needed?

Modern browsers apply noopener behavior to target="_blank" by default, but adding it explicitly costs nothing and protects older browsers. Add noreferrer too if you don’t want destination sites to see your page as the referrer.

The default Sätteri processor doesn’t run rehype plugins. Set markdown.processor to unified() from @astrojs/markdown-remark and pass the plugin in rehypePlugins, or use a Sätteri hast plugin instead.