From 411c08d9e8856327ce6c9c52a49f9a1546e3c143 Mon Sep 17 00:00:00 2001 From: Satoshi Nakamoto Date: Sun, 15 Oct 2023 13:45:21 -0400 Subject: [PATCH] Dynamic protocol, host for share URLs so they work on localhost and in prod --- components/share.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/components/share.js b/components/share.js index ab32a431..42575e58 100644 --- a/components/share.js +++ b/components/share.js @@ -3,13 +3,22 @@ import ShareIcon from '../svgs/share-fill.svg' import copy from 'clipboard-copy' import { useMe } from './me' import { useToast } from './toast' +import { SSR } from '../lib/constants' + +const getShareUrl = (item, me) => { + const path = `/items/${item?.id}${me ? `/r/${me.name}` : ''}` + if (!SSR) { + return `${window.location.protocol}//${window.location.host}${path}` + } + return `https://stacker.news${path}` +} export default function Share ({ item }) { const me = useMe() const toaster = useToast() - const url = `https://stacker.news/items/${item.id}${me ? `/r/${me.name}` : ''}` + const url = getShareUrl(item, me) - return typeof window !== 'undefined' && navigator?.share + return !SSR && navigator?.share ? (
{