Skip to content

Commit c8b31c6

Browse files
committed
Fix docs route and hash-driven MDX regressions
1 parent df0f694 commit c8b31c6

File tree

4 files changed

+54
-13
lines changed

4 files changed

+54
-13
lines changed

src/app/[[...markdownPath]]/page.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
getDocsPageData,
1212
getDocsStaticParams,
1313
MissingMarkdownContentError,
14+
shouldServeDocsRoute,
1415
} from 'utils/content';
1516

1617
export async function generateStaticParams() {
@@ -23,6 +24,11 @@ export default async function MarkdownPage({
2324
params: Promise<{markdownPath?: string[]}>;
2425
}) {
2526
const {markdownPath} = await params;
27+
28+
if (!shouldServeDocsRoute(markdownPath)) {
29+
notFound();
30+
}
31+
2632
let pageData;
2733

2834
try {

src/components/MDX/Challenges/Challenges.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,11 @@ export function Challenges({
125125
const scrollAnchorRef = useRef<HTMLDivElement>(null);
126126
const queuedScrollRef = useRef<undefined | QueuedScroll>(QueuedScroll.INIT);
127127
const [selectedIndex, setSelectedIndex] = useState(0);
128+
const [hasInteracted, setHasInteracted] = useState(false);
128129
const hash = useLocationHash();
129130
const hashIndex = challenges.findIndex((challenge) => challenge.id === hash);
130-
const activeIndex = hashIndex === -1 ? selectedIndex : hashIndex;
131+
const activeIndex =
132+
!hasInteracted && hashIndex !== -1 ? hashIndex : selectedIndex;
131133
const currentChallenge = challenges[activeIndex];
132134

133135
useEffect(() => {
@@ -152,6 +154,7 @@ export function Challenges({
152154
}
153155

154156
const handleChallengeChange = (index: number) => {
157+
setHasInteracted(true);
155158
setSelectedIndex(index);
156159
};
157160

@@ -191,7 +194,8 @@ export function Challenges({
191194
totalChallenges={totalChallenges}
192195
hasNextChallenge={activeIndex < totalChallenges - 1}
193196
handleClickNextChallenge={() => {
194-
setSelectedIndex((index) => index + 1);
197+
setHasInteracted(true);
198+
setSelectedIndex(activeIndex + 1);
195199
queuedScrollRef.current = QueuedScroll.NEXT;
196200
}}
197201
/>

src/components/MDX/ExpandableExample.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ function ExpandableExample({children, excerpt, type}: ExpandableExampleProps) {
3939
const id = children[0].props.id;
4040
const hash = useLocationHash();
4141
const [isExpanded, setIsExpanded] = useState(false);
42-
const autoExpanded = hash === id;
42+
const [isAutoExpandedDismissed, setIsAutoExpandedDismissed] = useState(false);
43+
const autoExpanded = hash === id && !isAutoExpandedDismissed;
44+
const isOpen = isExpanded || autoExpanded;
4345

4446
return (
4547
<details
46-
open={isExpanded || autoExpanded}
48+
open={isOpen}
4749
onToggle={(e: any) => {
48-
if (!autoExpanded) {
49-
setIsExpanded(e.currentTarget!.open);
50-
}
50+
setIsExpanded(e.currentTarget!.open);
5151
}}
5252
className={cn(
5353
'my-12 rounded-2xl shadow-inner-border dark:shadow-inner-border-dark relative',
@@ -100,13 +100,19 @@ function ExpandableExample({children, excerpt, type}: ExpandableExampleProps) {
100100
'bg-yellow-50 border-yellow-50 hover:bg-yellow-40 focus:bg-yellow-50 active:bg-yellow-50':
101101
isExample,
102102
})}
103-
onClick={() => setIsExpanded((current) => !current)}>
103+
onClick={() => {
104+
if (autoExpanded) {
105+
setIsAutoExpandedDismissed(true);
106+
setIsExpanded(false);
107+
return;
108+
}
109+
110+
setIsExpanded((current) => !current);
111+
}}>
104112
<span className="me-1">
105-
<IconChevron
106-
displayDirection={isExpanded || autoExpanded ? 'up' : 'down'}
107-
/>
113+
<IconChevron displayDirection={isOpen ? 'up' : 'down'} />
108114
</span>
109-
{isExpanded || autoExpanded ? 'Hide Details' : 'Show Details'}
115+
{isOpen ? 'Hide Details' : 'Show Details'}
110116
</Button>
111117
</summary>
112118
<div

src/utils/content.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,39 @@ export async function getDocsPageData(
104104
return getDocsPageDataCached(normalizedPath, pathname);
105105
}
106106

107+
export function shouldServeDocsRoute(markdownPath: string[] | undefined) {
108+
const normalizedPath = normalizeMarkdownPath(markdownPath);
109+
110+
if (normalizedPath === 'index') {
111+
return markdownPath == null || markdownPath.length === 0;
112+
}
113+
114+
if (normalizedPath.endsWith('/index')) {
115+
return false;
116+
}
117+
118+
if (
119+
process.env.NODE_ENV === 'production' &&
120+
DEV_ONLY_PAGES.has(normalizedPath)
121+
) {
122+
return false;
123+
}
124+
125+
return true;
126+
}
127+
107128
async function getDocsPageDataCached(
108129
normalizedPath: string,
109130
pathname: string
110131
): Promise<DocsPageData> {
111132
'use cache';
112133

113134
cacheTag(DEV_CONTENT_CACHE_TAG);
114-
cacheLife(pathname.endsWith('/translations') ? 'hours' : 'max');
135+
if (pathname.endsWith('/translations')) {
136+
cacheLife('hours');
137+
} else {
138+
cacheLife('max');
139+
}
115140
return getDocsPageDataUncached(normalizedPath, pathname);
116141
}
117142

0 commit comments

Comments
 (0)