Skip to content

Commit 80cf4c1

Browse files
Fix Hydration Errors (#472)
* add extractPathWithoutFragment Co-authored-by: D V <[email protected]> * refactor regex to local variable Co-authored-by: D V <[email protected]> --------- Co-authored-by: D V <[email protected]>
1 parent b27e8c8 commit 80cf4c1

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

components/Layout.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useRouter } from 'next/router';
66
import { DocSearch } from '@docsearch/react';
77
import useStore from '~/store';
88
import { SectionContext } from '~/context';
9+
import extractPathWithoutFragment from '~/lib/extractPathWithoutFragment';
910

1011
type Props = {
1112
children: React.ReactNode;
@@ -117,7 +118,7 @@ const MainNavLink = ({
117118
className={classnames(
118119
className,
119120
'font-semibold p-2 md:p-4',
120-
`${router.asPath === uri ? 'text-primary hover:text-primary' : 'text-slate-600 hover:text-primary'}`,
121+
`${extractPathWithoutFragment(router.asPath) === uri ? 'text-primary hover:text-primary' : 'text-slate-600 hover:text-primary'}`,
121122
)}
122123
>
123124
{label}

components/Sidebar.tsx

+13-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Link from 'next/link';
55
import { HOST } from '~/lib/config';
66
import classnames from 'classnames';
77
import { SegmentHeadline } from './Layout';
8+
import extractPathWithoutFragment from '~/lib/extractPathWithoutFragment';
89
import CarbonAds from './CarbonsAds';
910

1011
const DocLink = ({
@@ -19,7 +20,7 @@ const DocLink = ({
1920
url.search = '';
2021
url.hash = '';
2122
const stringUrl = url.toString().substr(HOST.length, Infinity);
22-
const isActive = uri === stringUrl;
23+
const isActive = uri === extractPathWithoutFragment(stringUrl);
2324
return (
2425
<Link
2526
href={uri}
@@ -45,7 +46,7 @@ const DocLinkBlank = ({
4546
url.search = '';
4647
url.hash = '';
4748
const stringUrl = url.toString().substr(HOST.length, Infinity);
48-
const isActive = uri === stringUrl;
49+
const isActive = uri === extractPathWithoutFragment(stringUrl);
4950
return (
5051
<Link
5152
href={uri}
@@ -122,7 +123,7 @@ export const SidebarLayout = ({ children }: { children: React.ReactNode }) => {
122123
const [rotateChevron, setRotateChevron] = useState(false);
123124
const handleRotate = () => setRotateChevron(!rotateChevron);
124125
const rotate = rotateChevron ? 'rotate(180deg)' : 'rotate(0)';
125-
126+
const pathWtihoutFragment = extractPathWithoutFragment(router.asPath);
126127
return (
127128
<div className='max-w-[1400px] mx-auto flex flex-col items-center'>
128129
<section>
@@ -136,18 +137,18 @@ export const SidebarLayout = ({ children }: { children: React.ReactNode }) => {
136137
setOpen(!open);
137138
}}
138139
>
139-
{router.asPath === '/overview/what-is-jsonschema' && (
140+
{pathWtihoutFragment === '/overview/what-is-jsonschema' && (
140141
<h3 className='text-white ml-12'>Overview</h3>
141142
)}
142-
{getStartedPath.includes(router.asPath) && (
143+
{getStartedPath.includes(pathWtihoutFragment) && (
143144
<h3 className='text-white ml-12'>Getting Started</h3>
144145
)}
145146

146-
{getReferencePath.includes(router.asPath) && (
147+
{getReferencePath.includes(pathWtihoutFragment) && (
147148
<h3 className='text-white ml-12'>Reference</h3>
148149
)}
149150

150-
{getSpecificationPath.includes(router.asPath) && (
151+
{getSpecificationPath.includes(pathWtihoutFragment) && (
151152
<h3 className='text-white ml-12'>Specification</h3>
152153
)}
153154

@@ -210,13 +211,14 @@ export const DocsNav = () => {
210211
getSpecification: false,
211212
});
212213
useEffect(() => {
213-
if (getDocsPath.includes(router.asPath)) {
214+
const pathWtihoutFragment = extractPathWithoutFragment(router.asPath);
215+
if (getDocsPath.includes(pathWtihoutFragment)) {
214216
setActive({ ...active, getDocs: true });
215-
} else if (getStartedPath.includes(router.asPath)) {
217+
} else if (getStartedPath.includes(pathWtihoutFragment)) {
216218
setActive({ ...active, getStarted: true });
217-
} else if (getReferencePath.includes(router.asPath)) {
219+
} else if (getReferencePath.includes(pathWtihoutFragment)) {
218220
setActive({ ...active, getReference: true });
219-
} else if (getSpecificationPath.includes(router.asPath)) {
221+
} else if (getSpecificationPath.includes(pathWtihoutFragment)) {
220222
setActive({ ...active, getSpecification: true });
221223
}
222224
}, [router.asPath]);

lib/extractPathWithoutFragment.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function extractPathWithoutFragment(pathWithFragment: string) {
2+
const fragmentIndex = pathWithFragment.indexOf('#');
3+
if (fragmentIndex !== -1) {
4+
return pathWithFragment.substring(0, fragmentIndex);
5+
} else {
6+
return pathWithFragment;
7+
}
8+
}

lib/slugifyMarkdownHeadline.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import slugify from 'slugify';
22

3-
const FRAGMENT_REGEX = /\[#(?<slug>(\w|-|_)*)\]/g;
4-
53
export default function slugifyMarkdownHeadline(
64
markdownChildren: string | any[],
75
): string {
6+
const FRAGMENT_REGEX = /\[#(?<slug>(\w|-|_)*)\]/g;
87
if (!markdownChildren) return '';
98
if (typeof markdownChildren === 'string')
109
return slugify(markdownChildren, { lower: true, trim: true });

0 commit comments

Comments
 (0)