From 90c27e0d397e12fc08c551512402f71fa05241e2 Mon Sep 17 00:00:00 2001 From: Aryan J Date: Wed, 8 May 2019 13:43:33 -0400 Subject: [PATCH] fix(docz-theme-default): allow use of link component in ssr (#854) check environment before using global `location` variable in link component fixes #832 --- .../src/components/ui/Link.tsx | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/core/docz-theme-default/src/components/ui/Link.tsx b/core/docz-theme-default/src/components/ui/Link.tsx index 8a71c8994..1eb3536f9 100644 --- a/core/docz-theme-default/src/components/ui/Link.tsx +++ b/core/docz-theme-default/src/components/ui/Link.tsx @@ -24,18 +24,21 @@ type LinkProps = React.AnchorHTMLAttributes export const Link: SFC = ({ href, ...props }) => { const { separator, linkComponent: Link } = useConfig() const docs = useDocs() - const toCheck = useMemo( - () => - [ - location.pathname - .split(separator) - .slice(0, -1) - .join(separator) - .slice(1), - (href || '').replace(/^(?:\.\/)+/gi, ''), - ].join('/'), - [separator] - ) + const toCheck = + typeof window === 'undefined' + ? null + : useMemo( + () => + [ + location.pathname + .split(separator) + .slice(0, -1) + .join(separator) + .slice(1), + (href || '').replace(/^(?:\.\/)+/gi, ''), + ].join('/'), + [separator] + ) const matched = docs && docs.find(doc => doc.filepath === toCheck) const nHref = matched ? matched.route : href