427 文字
2 分

Astro 5へのマイグレーションを行った

2024-12-08

公式のマイグレーションガイドが十分に詳しいため、特殊な実装をしていなければ、あまり問題なく移行できると思われる。

https://docs.astro.build/en/guides/upgrade-to/v5/

私個人は tsconfig の更新を後から行ったり、既存ファイルの名前によるエッジケースを踏んだため、若干手間取ったが、基本的には問題なく移行できた。

tsconfig の更新#

src/env.d.tsではなく、src/.astro/types.d.tsが利用されるようになった。

https://docs.astro.build/en/guides/upgrade-to/v5/#changed-typescript-configuration

},
"include": [".astro/types.d.ts", "**/*"],
"exclude": ["dist"]
}

この対応をしておかないと、新しいコンテンツレイヤー API などを利用しようとした際に型エラーが発生する。

新しいコンテンツレイヤー API への対応#

Astro 5 に移行する上で必須ではないが、対応を行った。

https://docs.astro.build/en/guides/upgrade-to/v5/#what-should-i-do-2

content config ファイルの移動#

src/content/config.tssrc/content.config.tsに移動した。

type の削除と loader の追加#

import { SITE } from "site.config";
import { defineCollection, z, type CollectionEntry } from "astro:content";
import { glob } from 'astro/loaders';
const blog = defineCollection({
type: "content",
loader: glob({
base:"./src/content/blog",
pattern:"\*_/_.md"
}),
schema: ({ image }) =>
z.object({
author: z.string().default(SITE.author),

post.slug の変更#

新しいコンテンツレイヤー API では、post.slug は post.id に置き換えられている。 型エラーに従い修正すれば良い。

既存の config.ts のようなファイルの変更#

同様の報告は見られないが、src/site.config.tsのようなファイルがある場合、コンテンツが正常に取得出来なくなる挙動が見られた。

元々あったsrc/site.config.tssrc/site.config.tsに変更したところ、正常に動作するようになった。

ViewTransitions の名前変更#

ClientRouter という名前に変更されたため変更が必要。

https://docs.astro.build/en/guides/upgrade-to/v5/

import { ViewTransitions } from 'astro:transitions';
import { ClientRouter } from 'astro:transitions';
<ViewTransitions />
<ClientRouter />
Astro 5へのマイグレーションを行った
https://blog.ohirunewani.com/posts/astro-v5-migration/
作者
hrdtbs
公開日
2024-12-08
ライセンス
CC BY-NC-SA 4.0