Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024リニューアル:ImageCDN-Bun #389

Merged
merged 71 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
9adc370
_redirect作成をBunに変更
gentksb Jun 21, 2024
360f1a0
ssr enable
gentksb Jun 22, 2024
a128844
fix wasm module resolution
gentksb Jun 22, 2024
25a6c4e
add nodejs path resolve
gentksb Jun 22, 2024
21e9c69
node:path compat
gentksb Jun 22, 2024
b14ae14
native HTMLRewriter
gentksb Jun 22, 2024
43a25f7
temporary commit
gentksb Jun 22, 2024
697fc7e
外部フォントの利用
gentksb Jun 22, 2024
4db8de1
OGP完成
gentksb Jun 22, 2024
046288e
Amznコンポーネント作成
gentksb Jun 23, 2024
a61631e
LinkCardコンポーネント暫定
gentksb Jun 23, 2024
68099b6
replace Amazon Link Card
gentksb Jun 23, 2024
1291dee
replace LinkBox to LinkCard
gentksb Jun 23, 2024
3326979
ビルド回りSmall fix
gentksb Jun 23, 2024
d83aa78
切り分けのためにpnpmに戻る
gentksb Jun 23, 2024
d9af51f
ogp取得テストOK
gentksb Jun 23, 2024
5070a22
testable
gentksb Jun 25, 2024
01d3d47
small fix
gentksb Jun 25, 2024
a978ed5
test修正
gentksb Jun 25, 2024
a12a5d0
テストコマンド修正
gentksb Jun 25, 2024
ffccd1a
debug
gentksb Jun 25, 2024
e76e33a
complete env
gentksb Jun 25, 2024
c762425
debug
gentksb Jun 25, 2024
57b84e2
typo
gentksb Jun 25, 2024
a5ec069
typos
gentksb Jun 25, 2024
c27eeee
何故かエラー消えた
gentksb Jun 25, 2024
f2f1f1e
bump
gentksb Jul 6, 2024
aa22414
small fix
gentksb Jul 6, 2024
7506aae
type fix & revert temp code
gentksb Jul 6, 2024
fea6fdc
code revert
gentksb Jul 6, 2024
e8ded47
Build‘ and work
gentksb Jul 6, 2024
1ebc444
remove typecheck when build env
gentksb Jul 6, 2024
5e4d4cf
only include icons of astro-icon
gentksb Jul 6, 2024
85f1253
cf adapter v11 -> v10
gentksb Jul 6, 2024
b76b7ff
remove rehype excerpt
gentksb Jul 9, 2024
ab2b2cc
Bump
gentksb Jul 9, 2024
f142499
update script
gentksb Jul 17, 2024
f157424
chore: Update import statements to use named imports in Amzn.astro an…
gentksb Jul 17, 2024
7e151ee
build deploy test
gentksb Jul 17, 2024
1a6e944
edit wrangler.toml
gentksb Jul 17, 2024
6a476cc
edit wrangler.toml via downladed
gentksb Jul 17, 2024
398ea22
暫定SSG
gentksb Jul 18, 2024
a2ead21
ssg fix
gentksb Jul 18, 2024
cf72b07
replace workers-og
gentksb Jul 18, 2024
50aa030
lint fix
gentksb Jul 18, 2024
dc0dc9c
fix test
gentksb Jul 18, 2024
56ffe2d
fix test
gentksb Jul 18, 2024
408c32c
unpic test
gentksb Jul 19, 2024
492c972
imageServiceの切り替えを実装
gentksb Jul 19, 2024
da12224
delete unused mdx
gentksb Jul 19, 2024
3865dae
internal image service
gentksb Jul 19, 2024
f177caa
slash処理ミスを改善
gentksb Jul 19, 2024
7a988fd
AmazonコンポーネントOK
gentksb Jul 21, 2024
17d4e6f
fix
gentksb Jul 21, 2024
35d03e6
API完成
gentksb Jul 24, 2024
b74fb70
ブランドカラー実装
gentksb Jul 24, 2024
92db8c1
シンプルリンクを追加
gentksb Jul 28, 2024
3834896
エラーメッセージの送信を実装
gentksb Jul 31, 2024
5e04f14
change test path
gentksb Jul 31, 2024
06510dc
text-wrap属性を追加
gentksb Aug 6, 2024
e889fe4
remove partytown
gentksb Aug 7, 2024
47734e2
add xml description full length
gentksb Aug 7, 2024
d4d97f7
pagefind script works (WIP:レイアウト調整)
gentksb Aug 7, 2024
90c776c
太字閉じの修正
gentksb Aug 8, 2024
778ef44
Zaraz Twitter test
gentksb Aug 8, 2024
2b25157
instagram-zaraz test
gentksb Aug 8, 2024
397ebd2
GTM disabled
gentksb Aug 8, 2024
a02069e
Merge branch 'master' into migration-to-ssr-cfimage-biome
gentksb Aug 10, 2024
fc44d9a
pagefindレイアウト
gentksb Aug 10, 2024
842e84e
不要コード削除
gentksb Aug 10, 2024
3a6bcc7
Update README
gentksb Aug 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 0 additions & 1 deletion .envrc

This file was deleted.

34 changes: 12 additions & 22 deletions .github/workflows/lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,30 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 20

- uses: pnpm/action-setup@v2
name: Install pnpm
- uses: oven-sh/setup-bun@v2
with:
version: 8
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV

- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
bun-version: latest

- name: Install dependencies
run: pnpm install
run: bun install

- name: Run Lint
run: pnpm run lint
run: bun run lint

- name: Run Tests
run: pnpm run test
run: |
touch .dev.vars
echo "PARTNER_TAG=$PARTNER_TAG" >> .dev.vars
echo "PAAPI_SECRETKEY=$PAAPI_SECRETKEY" >> .dev.vars
echo "PAAPI_ACCESSKEY=$PAAPI_ACCESSKEY" >> .dev.vars
bun run test
env:
PARTNER_TAG: ${{ secrets.PARTNER_TAG }}
PAAPI_SECRETKEY: ${{ secrets.PAAPI_SECRETKEY }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pnpm-debug.log*
# cloudflare
.dev.vars
.wrangler/
worker-configuration.d.ts

# リダイレクトファイルはスクリプトでprebuild生成
public/_redirects
Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This is gensobunya's bike blog repository.

### Build for

Cloudflare Pages and Functions
Cloudflare Pages(w/ KV) and Functions

### local development

Expand All @@ -22,17 +22,11 @@ At first, run `npx wrangler login` before start local environment.
PAAPI_ACCESSKEY
PAAPI_SECRETKEY
PARTNER_TAG
CLOUDFLARE_KV_TOKEN
CLOUDFLARE_ACCOUNT_IDENTIFIER
OGP_DATASTORE_ID
```

- PAAPI_ACCESSKEY: PA-API v5 Access key ID
- PAAPI_SECRETKEY: PA-API v5 Secret key
- PARTNER_TAG: Amazon Associate Partner Tag
- CLOUDFLARE_KV_TOKEN: Cloudflare v4 API Token which need "KV edit" role
- CLOUDFLARE_ACCOUNT_IDENTIFIER: Cloudflare Account ID(not email)
- OGP_DATASTORE_ID: Cloudflare KV ID (not local-preview ID)

## Operation

Expand Down
49 changes: 34 additions & 15 deletions astro.config.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
import { defineConfig } from "astro/config"
import { defineConfig, passthroughImageService } from "astro/config"
import mdx from "@astrojs/mdx"
import sitemap from "@astrojs/sitemap"
import AutoImport from "astro-auto-import"
import tailwind from "@astrojs/tailwind"
import react from "@astrojs/react"
// import remarkDescription from "astro-remark-description"
import partytown from "@astrojs/partytown"
import icon from "astro-icon"
import { rehypeExcerptContent } from "./src/plugin/rehypeExcerpt"
import pagefind from "astro-pagefind"

// ローカル開発時は画像サービスをパススルー
const imageServiceConfig = import.meta.env.DEV
? passthroughImageService()
: {
entrypoint: "./src/entrypoint/cfImageService",
config: {
maxWidth: 800
}
}

// https://astro.build/config
export default defineConfig({
site: "https://blog.gensobunya.net/",
image: {
service: imageServiceConfig
},
integrations: [
AutoImport({
imports: [
"./src/components/mdx/LinkBox.astro",
"./src/components/mdx/LinkCard.astro",
"./src/components/mdx/Amzn.astro",
"./src/components/mdx/SimpleLinkCard.astro",
{
"./src/components/mdx/positive.tsx": ["PositiveBox"],
"./src/components/mdx/negative.tsx": ["NegativeBox"]
Expand All @@ -26,20 +39,26 @@ export default defineConfig({
sitemap(),
tailwind(),
react(),
icon(),
partytown({
config: {
forward: ["dataLayer.push"]
icon({
include: {
mdi: [
"calendar",
"twitter",
"instagram",
"youtube",
"web",
"open-in-new"
]
}
})
}),
pagefind()
],
markdown: {
rehypePlugins: [rehypeExcerptContent]
build: {
format: "file"
},
vite: {
ssr: {
// to fix react-icons ESM import error
noExternal: ["react-icons"]
build: {
minify: false
}
}
})
Binary file modified bun.lockb
Binary file not shown.
15 changes: 15 additions & 0 deletions functions/api/_middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// validate sec-fetch header

export const onRequest: PagesFunction = async (context) => {
const { request } = context
const { headers } = request
const secFetchMode = headers.get("sec-fetch-mode")
if (
secFetchMode !== "same-origin" &&
secFetchMode !== "cors" &&
secFetchMode !== "same-site"
) {
return new Response("Forbidden", { status: 403 })
}
return context.next()
}
57 changes: 57 additions & 0 deletions functions/api/getAmznPa/[asin].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { AmazonItemsResponse } from "amazon-paapi"
import { getAmazonProductInfo } from "../../src/getAmazonProductInfo"
import { postLogToSlack } from "../../src/postLogToSlack"

export const onRequestGet: PagesFunction<Env> = async (context) => {
const asin = context.params.asin
const isValidAsin = (asin: string): boolean => {
return /^[A-Z0-9]{10}$/.test(asin)
}
if (!asin || !isValidAsin || typeof asin !== "string") {
await postLogToSlack(
`${context.request.url} \n Invalid asin: ${asin}`,
context.env.SLACK_WEBHOOK_URL
)
throw new Error("asin is't valid.")
}
const { PAAPI_ACCESSKEY, PAAPI_SECRETKEY, PARTNER_TAG } = context.env
console.log(`Requested asin is ${asin}.`)

const kvCache: AmazonItemsResponse = await context.env.PAAPI_DATASTORE.get(
asin,
"json"
)

const productData = kvCache
? kvCache
: await getAmazonProductInfo(
asin,
PAAPI_ACCESSKEY,
PAAPI_SECRETKEY,
PARTNER_TAG
).catch(async (e) => {
await postLogToSlack(
`${context.request.url} \n error:${e}`,
context.env.SLACK_WEBHOOK_URL
)
throw new Error(e)
})

if (!kvCache) {
await context.env.PAAPI_DATASTORE.put(asin, JSON.stringify(productData), {
expirationTtl: 60 * 60 * 24
}).then(() => console.log(`write PAAPI Cache. asin: ${asin}`))
} else {
console.log(`hitted PAAPI KV cache`)
}

const response = new Response(JSON.stringify(productData), {
headers: {
"content-type": "application/json",
"cache-control": "public, max-age=86400",
"x-robots-tag": "noindex"
}
})

return response
}
43 changes: 43 additions & 0 deletions functions/api/getOgp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { getOgpMetaData } from "../src/getOgpMetaData"
import { postLogToSlack } from "../src/postLogToSlack"

export const onRequestGet: PagesFunction<Env> = async (context) => {
const searchParams = new URL(context.request.url).searchParams
const url = searchParams.get("url")

const cache = await context.env.OGP_DATASTORE.get(url)

if (cache) {
console.log(`hitted OGP KV cache`)
return new Response(cache, {
status: 200,
headers: {
"content-type": "application/json; charset=UTF-8",
"X-Robots-Tag": "noindex",
"cache-control": "public, max-age=86400"
}
})
}

const body = await getOgpMetaData(url).catch(async (e) => {
await postLogToSlack(
`${context.request.url} \n error:${e}`,
context.env.SLACK_WEBHOOK_URL
)
throw new Error(e)
})

const bodyString = JSON.stringify(body)
await context.env.OGP_DATASTORE.put(url, bodyString, {
expirationTtl: 60 * 60 * 24 * 7 // 1 week
}).then(() => console.log(`write OGP Cache. url: ${url}`))

return new Response(bodyString, {
status: 200,
headers: {
"content-type": "application/json; charset=UTF-8",
"X-Robots-Tag": "noindex",
"cache-control": "public, max-age=86400"
}
})
}
55 changes: 0 additions & 55 deletions functions/api/getOgpFromAsin/[asin].ts

This file was deleted.

Loading
Loading