Skip to content

Commit

Permalink
✨ feat: speed up in china
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhUyU1997 committed Apr 9, 2023
1 parent 5baf08c commit 88819a8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/components/Menu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CheckIcon, DotFilledIcon } from '@radix-ui/react-icons'

import classes from './styles.module.css'
import { BodyEditor } from '../../editor'
import i18n from '../../i18n'
import i18n, { IsChina } from '../../i18n'
import { Helper } from '../../environments/online/helper'
import { uploadImage } from '../../utils/transfer'
import { getCurrentTime } from '../../utils/time'
Expand All @@ -13,6 +13,7 @@ import classNames from 'classnames'
import { useLanguageSelect } from '../../hooks'
import { ShowContextMenu } from '../ContextMenu'
import { ShowDialog } from '../Dialog'
import { SetCDNBase } from '../../utils/detect'

const {
MenubarRoot,
Expand Down Expand Up @@ -96,6 +97,17 @@ const MenubarDemo: React.FC<{
>
{i18n.t('Detect From Image')}
</Menubar.Item>
{IsChina() ? (
<Menubar.Item
className={MenubarItem}
onSelect={() => {
SetCDNBase(false)
helper.DetectFromImage(onChangeBackground)
}}
>
{i18n.t('Detect From Image') + ' [中国]'}
</Menubar.Item>
) : undefined}
<Menubar.Item
className={MenubarItem}
onSelect={() => helper.SetRandomPose()}
Expand Down
16 changes: 15 additions & 1 deletion src/utils/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,27 @@ const MyPose = import.meta.env.DEV
: ((window as any).Pose as Class<Pose, [PoseConfig]>)
console.log('MyPose', MyPose)

const AliyuncsBase =
'https://openpose-editor.oss-cn-beijing.aliyuncs.com/%40mediapipe/pose'
const JsdelivrBase = 'https://cdn.jsdelivr.net/npm/@mediapipe/pose'

let UseJsdelivrBase = true
function GetCDNBase() {
if (UseJsdelivrBase) return JsdelivrBase
else return AliyuncsBase
}

export function SetCDNBase(isJsdelivrBase: boolean) {
UseJsdelivrBase = isJsdelivrBase
}

const pose = new MyPose({
locateFile: (file) => {
if (file in assets) {
console.log('local', file)
return (assets as any)[file]
}
const url = `https://cdn.jsdelivr.net/npm/@mediapipe/pose/${file}`
const url = `${GetCDNBase()}/${file}`

console.log('load pose model', url)
return url
Expand Down
17 changes: 16 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ const config: UserConfigFn = ({ command, mode, ssrBuild }) => {
},
},
},
{
urlPattern:
/^https:\/\/openpose-editor.oss-cn-beijing\.aliyuncs\.com\/.*/i,
handler: 'CacheFirst',
options: {
cacheName: 'aliyuncs',
expiration: {
maxEntries: 10,
maxAgeSeconds: 60 * 60 * 24 * 365, // <== 365 days
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
],
},
includeAssets: [
Expand Down Expand Up @@ -94,7 +109,7 @@ const config: UserConfigFn = ({ command, mode, ssrBuild }) => {
base: mode === 'singlefile' ? './' : '/open-pose-editor/',
define: {
global: {},
__APP_VERSION__: JSON.stringify('0.1.13'),
__APP_VERSION__: JSON.stringify('0.1.14'),
__APP_BUILD_TIME__: Date.now(),
},
build: {
Expand Down

0 comments on commit 88819a8

Please sign in to comment.