Skip to content

Instantly share code, notes, and snippets.

@eallion
Created August 16, 2024 10:28
Show Gist options
  • Select an option

  • Save eallion/5bfe56c23d72daaba0b4602abdfc3d70 to your computer and use it in GitHub Desktop.

Select an option

Save eallion/5bfe56c23d72daaba0b4602abdfc3d70 to your computer and use it in GitHub Desktop.
Cloudflare Worker 获取最新的 Hugo 版本号
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' }
})
}
}
@eallion
Copy link
Author

eallion commented Aug 16, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment