Skip to content

Commit

Permalink
fix: crypto.randomUUID is not a function in some browser
Browse files Browse the repository at this point in the history
edge browser case Appliction error: a client-side exception.
  • Loading branch information
AprilNEA committed Dec 27, 2023
1 parent 1dfe27d commit d0a5418
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
"sass": "^1.59.2",
"shared": "workspace:*",
"swr": "^2.2.4",
"uuid": "^9.0.1",
"zustand": "^4.3.6"
},
"devDependencies": {
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@types/react-katex": "^3.0.0",
"@types/uuid": "^9.0.7",
"autoprefixer": "^10.4.16",
"postcss": "^8.4.31",
"tailwindcss": "^3.3.5",
Expand Down
4 changes: 3 additions & 1 deletion packages/frontend/src/app/(user-end)/chat/new/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
移除将会导致在客户端未刷新的情况下 randomUUID 永远相同 */
import { RedirectType, redirect } from 'next/navigation';

import { randomUUID } from '@/utils/client-utils';

export default function NewChatPage() {
redirect(`/chat/${crypto.randomUUID()}?new=true`, RedirectType.replace);
redirect(`/chat/${randomUUID()}?new=true`, RedirectType.replace);
}
10 changes: 10 additions & 0 deletions packages/frontend/src/utils/client-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { v4 as uuidv4 } from 'uuid';

import { showToast } from '@/components/ui-lib';
import Locale from '@/locales';

export const randomUUID = () => {
if ('crypto' in window && 'randomUUID' in crypto) {
return crypto.randomUUID();
} else {
return uuidv4();
}
};

export function trimTopic(topic: string) {
return topic.replace(/[,。!?、,.!?]*$/, '');
}
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d0a5418

Please sign in to comment.