aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2021-12-21 19:32:59 +0100
committerArmand Philippot <git@armandphilippot.com>2021-12-21 19:32:59 +0100
commit6917a572011489aafe62c9d2479615cb2928094f (patch)
tree836b339563e8bf92f7704459fcd29a085bb6497d
parenta52dd5d7acd06d92e27c8a96fe010963ec5b8275 (diff)
fix: use state to define hostname in sharing component
The window is not available during build, so an error occured. By using React hooks to set the hostname once in the browser, it works as expected.
-rw-r--r--src/components/Sharing/Sharing.tsx5
1 files changed, 3 insertions, 2 deletions
diff --git a/src/components/Sharing/Sharing.tsx b/src/components/Sharing/Sharing.tsx
index 9f8383b..d4d2dff 100644
--- a/src/components/Sharing/Sharing.tsx
+++ b/src/components/Sharing/Sharing.tsx
@@ -21,6 +21,7 @@ type Website = {
const Sharing = ({ excerpt, title }: { excerpt: string; title: string }) => {
const [pageExcerpt, setPageExcerpt] = useState('');
const [pageUrl, setPageUrl] = useState('');
+ const [hostname, setHostname] = useState('');
const router = useRouter();
useEffect(() => {
@@ -36,6 +37,7 @@ const Sharing = ({ excerpt, title }: { excerpt: string; title: string }) => {
router.asPath
}`;
+ setHostname(hostname);
setPageUrl(fullUrl);
}, [router.asPath]);
@@ -59,8 +61,7 @@ const Sharing = ({ excerpt, title }: { excerpt: string; title: string }) => {
}
break;
case 'title':
- const prefix =
- id === 'email' ? t`Seen on ${window.location.hostname}:` : '';
+ const prefix = id === 'email' ? t`Seen on ${hostname}:` : '';
sharingUrl += encodeURI(`${prefix} ${title}`);
break;
case 'url':