feat: IndexNow #332
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build Check | |
| on: | |
| push: | |
| branches: [main] | |
| pull_request: | |
| workflow_dispatch: | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| env: | |
| NEXT_TELEMETRY_DISABLED: "1" | |
| CI: "true" | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: pnpm/action-setup@v4 | |
| with: | |
| version: 10 | |
| - uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| cache: pnpm | |
| - run: pnpm install --frozen-lockfile | |
| - run: pnpm run lint | |
| - run: pnpm run lint:images | |
| - run: pnpm run typecheck | |
| # === IndexNow 提交(仅在 main 分支执行) === | |
| - name: Install jq (for JSON build) | |
| if: github.ref == 'refs/heads/main' | |
| run: sudo apt-get update && sudo apt-get install -y jq | |
| - name: Submit IndexNow (changed URLs) | |
| if: github.ref == 'refs/heads/main' | |
| env: | |
| SITE_ORIGIN: https://involutionhell.vercel.app | |
| INDEXNOW_API: https://involutionhell.vercel.app/api/indexnow | |
| INDEXNOW_API_TOKEN: ${{ secrets.INDEXNOW_API_TOKEN }} # 若未启用鉴权可删 | |
| run: | | |
| set -euo pipefail | |
| # 获取本次代码变更 | |
| git fetch --depth=2 origin ${{ github.ref }} || true | |
| CHANGED="$(git diff --name-only HEAD~1 HEAD || true)" | |
| # 过滤属于文档或页面的变更(按需调整正则) | |
| CHANGED_DOCS="$(echo "$CHANGED" | grep -E '^(app/docs/|content/docs/).*\.(mdx?|tsx?)$' || true)" | |
| URLS=() | |
| # 规则 1:app/docs/**/page.(md/tsx) → /docs/** | |
| while IFS= read -r f; do | |
| [ -z "$f" ] && continue | |
| if [[ "$f" =~ ^app/docs/(.*)/page\.(mdx|md|tsx|ts|jsx|js)$ ]]; then | |
| slug="${BASH_REMATCH[1]}" | |
| URLS+=("$SITE_ORIGIN/docs/$slug") | |
| fi | |
| done <<< "$CHANGED_DOCS" | |
| # 规则 2:content/docs/**.(md|mdx) → /docs/**(若不用 MDX 直出可删掉此段) | |
| while IFS= read -r f; do | |
| [ -z "$f" ] && continue | |
| if [[ "$f" =~ ^content/docs/(.*)\.(md|mdx)$ ]]; then | |
| slug="${BASH_REMATCH[1]}" | |
| slug="${slug%/index}" | |
| URLS+=("$SITE_ORIGIN/docs/$slug") | |
| fi | |
| done <<< "$CHANGED_DOCS" | |
| # 去重 | |
| mapfile -t URLS < <(printf "%s\n" "${URLS[@]}" | awk 'NF' | sort -u) | |
| # 兜底:没有匹配就推首页 | |
| if [ "${#URLS[@]}" -eq 0 ]; then | |
| URLS=("$SITE_ORIGIN/") | |
| fi | |
| echo "✅ Submitting URLs to IndexNow:" | |
| printf ' - %s\n' "${URLS[@]}" | |
| # 组 JSON | |
| JSON="$(jq -n --argjson arr "$(printf '%s\n' "${URLS[@]}" | jq -R . | jq -s .)" '{urlList: $arr}')" | |
| # 可选鉴权 | |
| AUTH_HEADER=() | |
| if [ -n "${INDEXNOW_API_TOKEN:-}" ]; then | |
| AUTH_HEADER=(-H "Authorization: Bearer ${INDEXNOW_API_TOKEN}") | |
| fi | |
| # 调用你的 /api/indexnow | |
| curl -sS -X POST "$INDEXNOW_API" \ | |
| -H "Content-Type: application/json" \ | |
| "${AUTH_HEADER[@]}" \ | |
| -d "$JSON" |