Skip to content

Commit

Permalink
astro-redirect-from updates
Browse files Browse the repository at this point in the history
  • Loading branch information
kremalicious committed Oct 25, 2023
1 parent 794a95d commit 61b2495
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions content/articles/2023-09-23-astro-redirect-from/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
date: 2023-09-23T19:20:50.153Z
updated: 2023-10-25T00:04:03.153Z

title: Redirect Plugin for Markdown Pages in Astro
image: ./astro-redirect-from-teaser.png
Expand All @@ -15,15 +16,15 @@ toc: true

Integration plugin for [Astro](https://astro.build) to create redirects based on a list in your Markdown frontmatter, mimicking the behavior of [jekyll-redirect-from](https://github.com/jekyll/jekyll-redirect-from) and [gatsby-redirect-from](/gatsby-redirect-from).

Imagine you've just revamped your blog, and some of your old URLs have changed. You don't want to lose the SEO juice, nor do you want to leave your readers with broken links. This is where redirects come into play. But managing redirects can be cumbersome, especially if you have to do it manually or through server configurations.
Imagine you've just revamped your blog, and some of your old URLs have changed. You don't want to lose the SEO juice, nor do you want to leave your readers with broken links. This is where redirects come into play. But managing redirects can be cumbersome, especially if you have to do it manually or through server and deployment tools configurations.

This plugin automates this process by reading the `redirect_from` field in the frontmatter of your Markdown files and updating Astro's configuration to handle these redirects automatically.
This plugin automates this process allowing you to add a `redirect_from` list in the frontmatter of your Markdown files and the plugin will update Astro's configuration to handle these redirects automatically.

## How it Works

By adding a `redirect_from` list in your Markdown frontmatter, the plugin integrates these redirects into Astro's [`redirects`](https://docs.astro.build/en/reference/configuration-reference/#redirects) configuration automatically, whether you're running the development server or building your project.
By adding a `redirect_from` list in your Markdown frontmatter, the plugin integrates these redirects into Astro's [`redirects`](https://docs.astro.build/en/reference/configuration-reference/#redirects) configuration automatically.

The plugin operates during the [`astro:config:setup`](https://docs.astro.build/en/reference/integrations-reference/#astroconfigsetup) lifecycle hook. It scans all Markdown files for the `redirect_from` key and updates Astro's configuration using [`updateConfig()`](https://docs.astro.build/en/reference/integrations-reference/#updateconfig-option). This ensures that any existing redirects are merged with new ones generated by the plugin.
The plugin operates during the [`astro:config:setup`](https://docs.astro.build/en/reference/integrations-reference/#astroconfigsetup) lifecycle hook. It scans all Markdown files for the `redirect_from` key and updates Astro's configuration using [`updateConfig()`](https://docs.astro.build/en/reference/integrations-reference/#updateconfig-option). This ensures that any existing redirects in `astro.config.mjs` are merged with the ones generated by the plugin.

Astro takes over from there, handling the redirects based on your site's build mode and in combination with any other integrations you might be using:

Expand All @@ -40,8 +41,6 @@ This plugin works with various Astro hosting integrations, most of them generate
| Vercel | [@astrojs/vercel](https://docs.astro.build/en/guides/integrations-guide/vercel/) |
| Cloudflare | [@astrojs/cloudflare](https://docs.astro.build/en/guides/integrations-guide/cloudflare/) |

Because Astro integrations are run in the order they are defined in the `integrations` array, this plugin should come before any other integrations which make use of the `redirects` config.

## Prerequisites

The plugin is designed to work without configuration, especially if your project aligns with Astro's default settings.
Expand Down Expand Up @@ -73,9 +72,8 @@ import redirectFrom from 'astro-redirect-from'
export default defineConfig({
// ...
integrations: [
// make sure this is listed BEFORE any hosting integration
redirectFrom()
]
],
// ...
)}
```
Expand Down Expand Up @@ -132,9 +130,9 @@ function getSlugFromFilePath(filePath: string) {
// - folder name if file name is index.md, or
// - file name
if (parsedPath.base === 'index.md' || parsedPath.base === 'index.mdx') {
slug = `/${parsedPath.dir}`
slug = parsedPath.dir
} else {
slug = `/${parsedPath.dir}/${parsedPath.name}`
slug = `${parsedPath.dir}/${parsedPath.name}`
}

return slug
Expand Down

1 comment on commit 61b2495

@vercel
Copy link

@vercel vercel bot commented on 61b2495 Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

blog – ./

blog-kremalicious.vercel.app
kremalicious.vercel.app
blog-git-main-kremalicious.vercel.app

Please sign in to comment.