1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-12-22 09:13:35 +01:00

astro-redirect-from updates

This commit is contained in:
Matthias Kretschmann 2023-10-25 01:13:24 +01:00
parent 794a95db38
commit 61b2495659
Signed by: m
GPG Key ID: 606EEEF3C479A91F

View File

@ -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
@ -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:
@ -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.
@ -73,9 +72,8 @@ import redirectFrom from 'astro-redirect-from'
export default defineConfig({
// ...
integrations: [
// make sure this is listed BEFORE any hosting integration
redirectFrom()
]
],
// ...
)}
```
@ -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