Created
August 16, 2024 10:28
-
-
Save eallion/5bfe56c23d72daaba0b4602abdfc3d70 to your computer and use it in GitHub Desktop.
Cloudflare Worker 获取最新的 Hugo 版本号
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
| addEventListener('fetch', event => { | |
| event.respondWith(handleRequest(event.request)) | |
| }) | |
| async function handleRequest(request) { | |
| const url = 'https://api.github.com/repos/gohugoio/hugo/releases/latest' | |
| try { | |
| const response = await fetch(url, { | |
| headers: { | |
| 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36' | |
| } | |
| }) | |
| if (!response.ok) { | |
| throw new Error(`HTTP error! status: ${response.status}`) | |
| } | |
| const data = await response.json() | |
| const tagName = data.tag_name | |
| return new Response(tagName, { | |
| headers: { 'Content-Type': 'text/plain' } | |
| }) | |
| } catch (error) { | |
| return new Response(`Error: ${error.message}`, { | |
| status: 500, | |
| headers: { 'Content-Type': 'text/plain' } | |
| }) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://hugo.eallion.workers.dev