AstroとSvelteでStaticサイトを作って、GitHub Actions で定期的に情報を取得更新するようにした

/
#Astro#Svelte#GitHubActions#CloudflarePages

はじめに

Astro が ver2.2 に達した投稿を見かけたので、React 製の oriverk.dev を Astro 製にリプレースしました。

Astroとは

Astro is the all-in-one web framework designed for speed. Pull your content from anywhere and deploy everywhere, all powered by your favorite UI components and libraries.

速度重視で、他の UI フレームワークも使えるオールインワンの Web フレームワークです。実際に Astro では SSG と SSR の両方を作ることが出来、React や Vue、Svelte なども混ぜて使うことができます。

コード先頭に frontmatter の様なものがあることを除けば、基本は Svelte や Vue の様で、if 文や繰り返し箇所は React の様な感じです。

index.astro
index.astro
1
---
2
import Layout from '../layouts/Layout.astro';
3
import Card from '../components/Card.astro';
4
---
5
6
<Layout title="Welcome to Astro.">
7
<main>
8
<h1>Welcome to <span class="text-gradient">Astro</span></h1>
9
<ul role="list" class="link-card-grid">
10
{[...Array(2)].map((_, i) => (
11
<Card
12
href={`https://astro.build/docs/${i === 0 ? '' : i}`}
13
title={`Documentation ${i === 0 ? '' : i}`}
14
/>
15
))}
16
</ul>
17
</main>
18
</Layout>
19
20
<style>
21
main {
22
margin: auto;
23
padding: 1.5rem;
24
max-width: 60ch;
25
}
26
h1 {
27
font-size: 3rem;
28
font-weight: 800;
29
margin: 0;
30
}
31
</style>

AstroとSvelteを使った感想

よかったこと

  • Astro 自体が非常に単純で理解しやすい(Next.js 比)
  • astro.Config で md ファイルの取り扱いが設定でき、あれやこれやと until function を書かずとも frontmatter を取得したり、html にコンパイルできる
  • よいこととは実感してないけどリポジトリサイズが非常に小さい

よくはなかったこと・ふつごうだったこと

  • .astro での event handling には document.querySelector などと書く必要がある
  • Astro の構文が React と Vue/Svelte の中間みたいで、if 文や each 文を書くときに困惑する
    • 経験により解消されるとは思う
  • .astro 上で UI フレームワークコンポネントを呼び出す際に、両者との微妙な違いにより困ることがある
  • GitHub 上で script や style 領域がハイライトされない
    • Svelte も Vue もされない

Image from Gyazo

サイトについて

主に次のような機能をもったサイトにしたいと考えました。

  • Static Site である
  • GitHub の Pinned Repos と Contribution Calendar(GitHub 草)を表示できる
  • blog.oriverk.dev のコンテンツを取得表示できる
  • Cloudflare Pages にデプロイし、サイトデータを自動で更新できる

また、以前に oriverk.dev を React で作ったときの感じを踏襲したいとも考えていました。

Image from Gyazo

主に使用したもの

Astro だけでもサイトは作れますが、Astro と他 UI フレームワークを使った場合の感じを知りたかったので、軽量さに共通点を持つ Svelte を UI フレーム枠に採用しました。全体的な view?の/pages は astro ファイルで作り、components は svelte という風に使い分けました。

Init astro app

undefined
1
npm create astro@latest -- --template basics

Astro と Astronaut を掛けているのか、Houston という名前の顔文字が可愛いかったです。

npm create astro@latest
basics
undefined
1
npm i -D npm-run-all
2
npm i -D @commitlint/{config-conventional,cli}
3
# echo '{"extends": ["@commitlint/config-conventional"]}' > .commitlintrc.json
4
5
npm install -D eslint @typescript-eslint/parser eslint-plugin-{astro,jsx-a11y,import} eslint-import-resolver-typescript
6
npm install -D prettier prettier-plugin-astro eslint-config-prettier
7
# echo {} > .prettierrc.json
8
9
npx husky-init && npm install
10
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
11
12
npx astro add svelte
13
14
npm i sass cssnano autoprefixer

code linterの設定

Astro と Svelte を混ぜるので当然なのですが、両者用の設定が必要でした。なので、init svelte app の箇所に加えて

undefined
1
npm i -D eslint-plugin-svelte3 prettier-plugin-svelte

ESLint Config

他レポジトリで使っていた svelte 用の Config と混ぜる形で作りました。Svelte は今年に入って触ったばかりなので、設定が正しい状態にあるかはわかりませんが、動いてます。

.eslintrc.yml
.eslintrc.yml
1
extends:
2
- plugin:astro/recommended
3
- plugin:jsx-a11y/recommended
4
- plugin:import/recommended
5
- plugin:import/typescript
6
- prettier
7
overrides:
8
- files:
9
- '*.astro'
10
parser: astro-eslint-parser
11
parserOptions:
12
parser: '@typescript-eslint/parser'
13
extraFileExtensions:
14
- .astro
15
rules: {}
16
- files:
17
- '*.svelte'
18
processor: svelte3/svelte3
19
parserOptions:
20
parser: '@typescript-eslint/parser'
21
extraFileExtensions:
22
- .svelte
23
rules: {}
24
settings:
25
svelte3/typescript: true
26
parser: '@typescript-eslint/parser'
27
parserOptions:
28
ecmaVersion: latest
29
sourceType: module
30
plugins:
31
- svelte3
32
- '@typescript-eslint'
33
ignorePatterns:
34
- './dist/**/*'
35
settings: {}

Prettier Config

.prettierrc.yml
1
trailingComma: es5
2
tabWidth: 2
3
semi: false
4
singleQuote: true
5
plugins:
6
- prettier-plugin-astro
7
- prettier-plugin-svelte
8
pluginSearchDirs: false

ChatGPTとGitHub GraphQL API

GitHub GraphQL API クエリ作成には ChatGPT を利用しました。ChatGPT のバージョンは 3.5 でデータは 2021 年 9 月までのものらしく、例えば 21 年 10 月以降に変わった内容については正確には答えることができません。なので、ChatGPT が出力したクエリを GitHub GraphQl API Explorer で試して正常に動くかを確認し、クエリを調整することにしました。

undefined
1
GitHub GraphQL API を用いて、ユーザ名oriverkのpinned repository と contribution calendar のデータを取得せよ
ChatGPT-3.5 出力結果
undefined
1
query {
2
user(login: "oriverk") {
3
pinnedItems(first: 6) {
4
nodes {
5
... on Repository {
6
name
7
description
8
url
9
stargazers {
10
totalCount
11
}
12
forkCount
13
}
14
}
15
}
16
contributionsCollection {
17
contributionCalendar {
18
totalContributions
19
weeks {
20
contributionDays {
21
contributionCount
22
date
23
}
24
}
25
}
26
}
27
}
28
}

Explorer で検証した後、公式ドキュメントを片手に適宜クエリを修正して利用しました。

Contribution Calendar(GitHub草)

Image from Gyazo

Svelte 製ライブラリの多くが更新を止めていたので、自作しました。

RSS fetcher

基本的に CatNose 氏の下記「RSS 集約サイト」に倣いました。なので割愛します。

GitHub ActionsによるCloudflare Pagesへの定期的デプロイ

package.json
1
{
2
"scripts": {
3
"dev": "astro dev --project tsconfig.json",
4
"start": "astro dev",
5
"prebuild": "run-s prebuild:*",
6
"build": "astro build",
7
"preview": "astro preview",
8
}
9
}

GitHub Actions でもこれを利用するようにしました。

workflows/deploy.yml
deploy.yml
1
name: continuous-deployment
2
on:
3
push:
4
branches:
5
- main
6
- dev
7
schedule:
8
- cron: "0 2 * * *"
9
workflow_dispatch:
10
11
jobs:
12
publish:
13
runs-on: ubuntu-latest
14
permissions:
15
contents: read
16
deployments: write
17
name: build and deploy to Cloudflare Pages
18
steps:
19
- name: checkout
20
uses: actions/checkout@v3
21
22
# Run a build step here if your project requires
23
- name: setup node
24
uses: actions/setup-node@v3
25
with:
26
node-version: 18
27
28
- name: install packages and build
29
run: |
30
npm install
31
npm run build
32
env:
33
MODE: production
34
SECRET_GITHUB_PERSONAL_ACCESS_TOKEN: ${{ secrets.SECRET_GITHUB_PERSONAL_ACCESS_TOKEN }}
35
PUBLIC_GA_MEASUREMENT_ID: ${{ secrets.PUBLIC_GA_MEASUREMENT_ID }}
36
37
- name: deploy to Cloudflare Pages
38
uses: cloudflare/pages-action@v1
39
with:
40
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
41
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
42
projectName: astro-site
43
directory: dist

Image from Gyazo

AstroでGoogle Analytics

エラー類

Astro と UI FW のどちらに起因するか見極める必要性があり、この点は大変だなあと感じました。

date-fns/locale

svelte と date-fns

undefined
1
Directory import '/home/oriverk/Codes/oriverk/astro-site/node_modules/date-fns/locale/ja' is not supported resolving ES modules imported from /home/oriverk/Codes/oriverk/astro-site/dist/entry.mjs
2
Did you mean to import date-fns/locale/ja/index.js?
typescript
1
import { ja } from 'date-fns/locale'
2
import ja from 'date-fns/locale/ja/index.js'

CSS Logical Media Query error

Media Queries Level 4 からの次の様な書き方は、Svelte においては次のバージョンから使える模様。

undefined
1
@media (max-width: 30em) { ... }

サイトキャプチャ

Image from Gyazo

Image from Gyazo

Image from Gyazo