Support SSR

This commit is contained in:
Ariful Alam 2022-03-28 18:49:35 +06:00
parent 48f4920c1d
commit 7d9b1d9606
3 changed files with 10 additions and 6 deletions

View File

@ -121,6 +121,7 @@ const Blog = ({ loading, blog, googleAnalytics }) => {
console.error(error);
}
typeof window !== 'undefined' &&
window.open(article.link, '_blank');
}}
>

View File

@ -72,7 +72,7 @@ const Project = ({ repo, loading, github, googleAnalytics }) => {
console.error(error);
}
window.open(item.html_url, '_blank');
typeof window !== 'undefined' && window.open(item.html_url, '_blank');
}}
>
<div className="flex justify-between flex-col p-8 h-full w-full">

View File

@ -17,7 +17,8 @@ export const getInitialTheme = (themeConfig) => {
}
if (themeConfig.respectPrefersColorScheme && !themeConfig.disableSwitch) {
return window.matchMedia('(prefers-color-scheme: dark)').matches
return typeof window !== 'undefined' &&
window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: themeConfig.defaultTheme;
}
@ -57,8 +58,10 @@ export const ga = {
// initialize google analytic
initialize: (id) => {
try {
if (typeof window !== 'undefined') {
window.gtag('js', new Date());
window.gtag('config', id);
}
} catch (error) {
console.error(error);
}
@ -66,7 +69,7 @@ export const ga = {
// log specific events happening
event: ({ action, params }) => {
try {
window.gtag('event', action, params);
typeof window !== 'undefined' && window.gtag('event', action, params);
} catch (error) {
console.error(error);
}