From 07e3efbd6640434a1ade8fbab9f051f1e2f283cd Mon Sep 17 00:00:00 2001 From: liwei Date: Sun, 30 Jul 2023 16:42:53 +0800 Subject: [PATCH] feat: github release cdn --- components/HeroSection.js | 47 ++++++++++++++++++++++---------- components/Translate.js | 2 +- public/locales/en-US/common.json | 4 +-- public/locales/zh-CN/common.json | 2 +- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/components/HeroSection.js b/components/HeroSection.js index 8690a4f..9671711 100644 --- a/components/HeroSection.js +++ b/components/HeroSection.js @@ -1,16 +1,27 @@ import { useTranslation } from 'next-i18next' import Image from 'next/image' -import Container from './Container' import Link from 'next/link' -import UAParser from 'ua-parser-js' -import androidSvg from '../public/images/platform/android.svg' -import linuxSvg from '../public/images/platform/linux.svg' -import macosSvg from '../public/images/platform/macos.svg' -import windowsSvg from '../public/images/platform/windows.svg' +import { useRouter } from 'next/router' import { useEffect, useState } from 'react' +import UAParser from 'ua-parser-js' +import Container from './Container' const HeroSection = ({ release }) => { + const router = useRouter() + const isCn = router.locale == 'zh-CN' + const [githubAccessible, setGithubAccessible] = useState(true) const [downloadUrl, setDownloadUrl] = useState('') + + useEffect(() => { + if (!isCn) { + setGithubAccessible(true) + return + } + fetch('https://api.github.com').catch(() => { + setGithubAccessible(false) + }) + }, [isCn]) + useEffect(() => { const parser = new UAParser() const os = parser.getOS().name @@ -30,20 +41,28 @@ const HeroSection = ({ release }) => { ua.os = 'android' } + const getDownloadCdnUrl = (rawUrl) => { + if (!githubAccessible) { + return `https://ghproxy.com/${rawUrl.replace('//', '/')}` + } else { + return rawUrl + } + } + const osAssert = release.assert[ua.os] // first match os and arch if (osAssert?.[ua.arch]) { - setDownloadUrl(osAssert[ua.arch]) + setDownloadUrl(getDownloadCdnUrl(osAssert[ua.arch])) return } // second match os if (osAssert) { - setDownloadUrl(osAssert[Object.keys(osAssert)[0]]) + setDownloadUrl(getDownloadCdnUrl(osAssert[Object.keys(osAssert)[0]])) return } // no match, fallback to github latest release setDownloadUrl('https://github.com/GopeedLab/gopeed/releases/latest') - }, [release.assert]) + }, [githubAccessible, release.assert]) const { t } = useTranslation('common') return ( @@ -70,7 +89,7 @@ const HeroSection = ({ release }) => { {t('downloadMore')} @@ -83,16 +102,16 @@ const HeroSection = ({ release }) => { -
-
+
+
{t('home.feat1.title')}

{t('home.feat1.desc')}

-
+
{t('home.feat2.title')}

{t('home.feat2.desc')}

-
+
{t('home.feat3.title')}

{t('home.feat3.desc')}

diff --git a/components/Translate.js b/components/Translate.js index f5d52c8..c6cec08 100644 --- a/components/Translate.js +++ b/components/Translate.js @@ -55,7 +55,7 @@ const Translate = (props) => { } else { return (