1
0
Fork 0

new post: astro-redirect-from

This commit is contained in:
Matthias Kretschmann 2023-09-23 20:14:56 +01:00
parent 7746ce4a28
commit 24c6a26616
Signed by: m
GPG Key ID: 606EEEF3C479A91F
6 changed files with 202 additions and 47 deletions

View File

@ -2,7 +2,7 @@
date: 2020-05-22T14:08:00.367Z
updated: 2020-05-23T11:35:12+02:00
title: Redirect plugin for Markdown Pages in Gatsby
title: Redirect Plugin for Markdown Pages in Gatsby
image: ./gatsby-redirect-from-teaser.png
changelog: kremalicious/gatsby-redirect-from

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

View File

@ -0,0 +1,166 @@
---
date: 2023-09-23T19:20:50.153Z
title: Redirect Plugin for Markdown Pages in Astro
image: ./astro-redirect-from-teaser.png
tags:
- astro
- development
- goodies
changelog: kremalicious/astro-redirect-from
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.
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.
## 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.
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.
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:
> For statically-generated sites with no adapter installed, this will produce a client redirect using a `<meta http-equiv="refresh">` tag and does not support status codes.
>
> When using SSR or with a static adapter in `output: static` mode, status codes are supported. Astro will serve redirected `GET` requests with a status of `301` and use a status of `308` for any other request method.
> [Astro Configuration Reference: redirects](https://docs.astro.build/en/reference/configuration-reference/#redirects)
This plugin works with various Astro hosting integrations, most of them generate further redirect files in the places they require so this plugin works in combination with them:
| Provider | Plugin |
| ---------- | ---------------------------------------------------------------------------------------- |
| Netlify | [@astrojs/netlify](https://docs.astro.build/en/guides/integrations-guide/netlify/) |
| 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.
- Astro v3 (v2 probably works too, but is not tested)
- Markdown files should be in a directory (default is `src/pages/`)
- Slugs are extracted either from the frontmatter or the file/folder path
## Installation
```bash
cd yourproject/
# Using NPM
npx astro add astro-redirect-from
# Using Yarn
yarn astro add astro-redirect-from
# Using PNPM
pnpm astro add astro-redirect-from
```
If installing manually:
```bash
npm i astro-redirect-from
```
Then load the plugin in your Astro config file, making sure this plugin comes before any other integrations which make use of the `redirects` config:
```js title="astro.config.mjs"
import { defineConfig } from 'astro/config'
import redirectFrom from 'astro-redirect-from'
export default defineConfig({
// ...
integrations: [
// make sure this is listed before any hosting integration
redirectFrom()
]
// ...
)}
```
That's it. Your redirects will be automatically added the next time you run `astro dev` or `astro build`. If any of them have a `redirect_from` field, that is.
[See Usage](#usage)
### Options
All options are optional and can be passed in Astro's config file:
```js title="astro.config.mjs"
import { defineConfig } from 'astro/config'
import redirectFrom from 'astro-redirect-from'
import { getMySlug } from './your-slug-function'
export default defineConfig({
// ...
integrations: [
redirectFrom({
contentDir: './content',
getSlug: getMySlug
})
]
// ...
)}
```
#### `contentDir: string`
_Default: `src/pages/`_
Specify a different directory for your Markdown files, relative to the project root.
#### `getSlug: (filePath: string) => string`
_Default: `getSlugFromFilePath()`, see below_
If you need a custom slug structure, pass a function to construct your slug from the file path.
If you use a `slug` field in your frontmatter, this will be preferred by the plugin and passing any `getSlug` function will have no effect in that case.
The default function is a great starting point:
```typescript
function getSlugFromFilePath(filePath: string) {
const parsedPath = path.parse(filePath)
let slug
// construct slug as full path from either:
// - folder name if file name is index.md, or
// - file name
if (parsedPath.base === 'index.md' || parsedPath.base === 'index.mdx') {
slug = `/${parsedPath.dir}`
} else {
slug = `/${parsedPath.dir}/${parsedPath.name}`
}
return slug
}
```
## Usage
In your Markdown file's frontmatter, use the key `redirect_from` followed by a list.
```yaml
---
redirect_from:
- /old-url-1
- /old-url-2
- /old-url-3.html
---
```
## Check out & contribute
Head over to GitHub to take a peek into the code, or to report some bugs.
<p class="content-download">
<a class="icon-github btn btn-primary" href="https://github.com/kremalicious/astro-redirect-from">GitHub</a>
</p>

79
package-lock.json generated
View File

@ -18,7 +18,7 @@
"@rainbow-me/rainbowkit": "^1.0.11",
"astro": "^3.1.2",
"astro-expressive-code": "^0.26.0",
"astro-redirect-from": "^0.2.1",
"astro-redirect-from": "^0.2.3",
"date-fns": "^2.30.0",
"dms2dec": "^1.1.0",
"fast-exif": "^2.0.1",
@ -80,6 +80,34 @@
"node": "18"
}
},
"../astro-redirect-from": {
"version": "0.2.3",
"license": "MIT",
"dependencies": {
"astro": ">= 3",
"globby": "^13.2.2",
"gray-matter": "^4.0.3"
},
"devDependencies": {
"@types/node": "^20.6.3",
"@typescript-eslint/eslint-plugin": "^6.7.2",
"auto-changelog": "^2.4.0",
"eslint": "^8.49.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"prettier": "^3.0.3",
"release-it": "^16.1.5",
"typescript": "^5.2.2",
"vite": "^4.4.9"
},
"engines": {
"node": ">=18.14.1",
"npm": ">=6.14.0"
},
"peerDependencies": {
"astro": ">= 3"
}
},
"node_modules/@aashutoshrathi/word-wrap": {
"version": "1.2.6",
"dev": true,
@ -4119,50 +4147,8 @@
}
},
"node_modules/astro-redirect-from": {
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/astro-redirect-from/-/astro-redirect-from-0.2.1.tgz",
"integrity": "sha512-9pxLhutTVo7mPILrSrROmr5PkzK5qwM5116rYDK4MYEcPGDjoSieoiJ/6+0zNMUzJnmauTFHh6FNgURi16qcVg==",
"dependencies": {
"astro": ">= 3",
"globby": "^13.2.2",
"gray-matter": "^4.0.3"
},
"engines": {
"node": ">=18.14.1",
"npm": ">=6.14.0"
},
"peerDependencies": {
"astro": ">= 3"
}
},
"node_modules/astro-redirect-from/node_modules/globby": {
"version": "13.2.2",
"resolved": "https://registry.npmjs.org/globby/-/globby-13.2.2.tgz",
"integrity": "sha512-Y1zNGV+pzQdh7H39l9zgB4PJqjRNqydvdYCDG4HFXM4XuvSaQQlEc91IU1yALL8gUTDomgBAfz3XJdmUS+oo0w==",
"dependencies": {
"dir-glob": "^3.0.1",
"fast-glob": "^3.3.0",
"ignore": "^5.2.4",
"merge2": "^1.4.1",
"slash": "^4.0.0"
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/astro-redirect-from/node_modules/slash": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/slash/-/slash-4.0.0.tgz",
"integrity": "sha512-3dOsAHXXUkQTpOYcoAxLIorMTp4gIQr5IW3iVb7A7lFIp0VHhnynm9izx6TssdrIcVIESAlVjtnO2K8bg+Coew==",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
"resolved": "../astro-redirect-from",
"link": true
},
"node_modules/astro/node_modules/@astrojs/compiler": {
"version": "2.1.0",
@ -5897,6 +5883,7 @@
},
"node_modules/dir-glob": {
"version": "3.0.1",
"dev": true,
"license": "MIT",
"dependencies": {
"path-type": "^4.0.0"
@ -8212,6 +8199,7 @@
},
"node_modules/ignore": {
"version": "5.2.4",
"dev": true,
"license": "MIT",
"engines": {
"node": ">= 4"
@ -11766,6 +11754,7 @@
},
"node_modules/path-type": {
"version": "4.0.0",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=8"

View File

@ -38,7 +38,7 @@
"@rainbow-me/rainbowkit": "^1.0.11",
"astro": "^3.1.2",
"astro-expressive-code": "^0.26.0",
"astro-redirect-from": "^0.2.1",
"astro-redirect-from": "^0.2.3",
"date-fns": "^2.30.0",
"dms2dec": "^1.1.0",
"fast-exif": "^2.0.1",