Skip to content

An AI-powered search engine with a generative UI

fix: handle SSR in cookie utilities

Yoshiki Miura6cd01aac49eb
diff --git a/lib/utils/cookies.ts b/lib/utils/cookies.ts
index a3cfa634..9ac1a44c 100644
--- a/lib/utils/cookies.ts
+++ b/lib/utils/cookies.ts
@@ -1,4 +1,6 @@
export function setCookie(name: string, value: string, days = 30) {
+ if (typeof document === 'undefined') return
+
const date = new Date()
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000)
const expires = `expires=${date.toUTCString()}`
@@ -6,6 +8,8 @@ export function setCookie(name: string, value: string, days = 30) {
}
export function getCookie(name: string): string | null {
+ if (typeof document === 'undefined') return null
+
const cookies = document.cookie.split(';')
for (const cookie of cookies) {
const [cookieName, cookieValue] = cookie.trim().split('=')
@@ -17,5 +21,7 @@ export function getCookie(name: string): string | null {
}
export function deleteCookie(name: string) {
+ if (typeof document === 'undefined') return
+
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`
}