Skip to content

Commit e0cdb1a

Browse files
authored
refactor: Extract content paths util (#1265)
1 parent cd49b0b commit e0cdb1a

File tree

5 files changed

+25
-16
lines changed

5 files changed

+25
-16
lines changed

src/components/content-region/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const COMPONENTS = {
1515
const url = new URL(props.href, location.origin);
1616

1717
const prefetchAndPreload = () => {
18-
if (props.href.startsWith('/repl?code')) {
18+
if (props.href.startsWith('/repl')) {
1919
ReplPage.preload();
2020
preloadRepl();
2121
} else if (props.href.startsWith('/tutorial')) {

src/components/controllers/page.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function Page() {
2020

2121
export function PageLayout() {
2222
const { path } = useRoute();
23-
const { html, meta } = useContent(path === '/' ? 'index' : path);
23+
const { html, meta } = useContent(path);
2424

2525
return (
2626
<div class={style.page}>

src/components/controllers/tutorial-page.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export default function TutorialPage() {
2020
}
2121

2222
function TutorialLayout() {
23-
const { path, params } = useRoute();
24-
const { html, meta } = useContent(!params.step ? '/tutorial/index' : path);
23+
const { path } = useRoute();
24+
const { html, meta } = useContent(path);
2525

2626
// Preload the next chapter
2727
useEffect(() => {

src/components/header/index.jsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,25 +213,20 @@ const NavLink = ({ to, isOpen, route, ...props }) => {
213213
}
214214

215215
const href = to.href || to.path;
216-
const prefetchHref = href == '/tutorial'
217-
? '/tutorial/index'
218-
: href == '/'
219-
? '/index'
220-
: href;
221216
const homeProps = to.href == '/' || to.path == '/'
222217
? { onContextMenu: BrandingRedirect, 'aria-label': 'Home' }
223218
: {};
224219

225220
const prefetchAndPreload = () => {
226-
if (prefetchHref.startsWith('/repl')) {
221+
if (href.startsWith('/repl')) {
227222
ReplPage.preload();
228223
preloadRepl();
229-
} else if (prefetchHref.startsWith('/tutorial')) {
224+
} else if (href.startsWith('/tutorial')) {
230225
TutorialPage.preload();
231226
preloadRepl();
232227
}
233228

234-
prefetchContent(prefetchHref);
229+
prefetchContent(href);
235230
};
236231

237232
return (

src/lib/use-content.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,29 @@ import {
1010
CACHE
1111
} from './use-resource.js';
1212

13+
/**
14+
* Correct the few site paths that differ from the markdown file name/structure
15+
*
16+
* @param {string} path
17+
* @returns {string}
18+
*/
19+
function getContentPath(path) {
20+
if (path == '/') return '/index';
21+
if (path == '/tutorial') return '/tutorial/index';
22+
return path;
23+
}
24+
1325
/**
1426
* @param {string} path
1527
* @returns {import('./../types.d.ts').ContentData}
1628
*/
1729
export function useContent(path) {
1830
const [lang] = useLanguage();
31+
const contentPath = getContentPath(path);
1932
/** @type {import('./../types.d.ts').ContentData} */
20-
const { html, meta } = useResource(() => getContent([lang, path]), [
33+
const { html, meta } = useResource(() => getContent([lang, contentPath]), [
2134
lang,
22-
path
35+
contentPath
2336
]);
2437
useTitle(meta.title);
2538
useDescription(meta.description || '');
@@ -32,9 +45,10 @@ export function useContent(path) {
3245
*/
3346
export function prefetchContent(path) {
3447
const lang = document.documentElement.lang;
35-
const fetch = () => getContent([lang, path]);
48+
const contentPath = getContentPath(path);
49+
const fetch = () => getContent([lang, contentPath]);
3650

37-
const cacheKey = createCacheKey(fetch, [lang, path]);
51+
const cacheKey = createCacheKey(fetch, [lang, contentPath]);
3852
if (CACHE.has(cacheKey)) return;
3953

4054
setupCacheEntry(fetch, cacheKey);

0 commit comments

Comments
 (0)