Skip to content

Commit

Permalink
fix: Reduce tearing issues (#1113)
Browse files Browse the repository at this point in the history
* fix: 'Outdated docs' banner flash & simplify guide check

* refactor: Swap in `useRoute` where possible

* refactor: Swap out fake suspense impl
  • Loading branch information
rschristian committed Apr 9, 2024
1 parent a1485d4 commit 1c1a08f
Show file tree
Hide file tree
Showing 23 changed files with 38 additions and 80 deletions.
1 change: 0 additions & 1 deletion content/de/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
layout: home
title: Preact
toc: false
---


Expand Down
1 change: 0 additions & 1 deletion content/en/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
layout: home
title: Preact
toc: false
description: 'Fast 3kB alternative to React with the same modern API.'
---

Expand Down
1 change: 0 additions & 1 deletion content/es/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
layout: home
title: Preact
toc: false
---


Expand Down
1 change: 0 additions & 1 deletion content/fr/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
layout: home
title: Preact
toc: false
---

<jumbotron>
Expand Down
1 change: 0 additions & 1 deletion content/it/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
layout: home
title: Preact
toc: false
---


Expand Down
1 change: 0 additions & 1 deletion content/ja/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
layout: home
title: Preact
toc: false
description: 'Preact is a fast 3kB alternative to React with the same modern API'
---

Expand Down
1 change: 0 additions & 1 deletion content/kr/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
layout: home
title: Preact
toc: false
description: 'Fast 3kB alternative to React with the same modern API.'
---

Expand Down
1 change: 0 additions & 1 deletion content/pt-br/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
layout: home
title: Preact
toc: false
---


Expand Down
1 change: 0 additions & 1 deletion content/ru/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
layout: home
title: Preact
toc: false
description: 'Быстрая 3КБ-альтернатива React с тем же современным API.'
---

Expand Down
1 change: 0 additions & 1 deletion content/tr/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
layout: home
title: Preact
toc: false
---


Expand Down
1 change: 0 additions & 1 deletion content/zh/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
layout: home
title: Preact
toc: false
description: 'React 的 3kb 轻量级替代方案,拥有相同的现代 API。'
---

Expand Down
7 changes: 4 additions & 3 deletions src/components/code-block/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMemo } from 'preact/hooks';
import { Suspense } from 'preact/compat';
import { wrap } from 'comlink';
import { FakeSuspense, useResource } from '../../lib/use-resource';
import { useResource } from '../../lib/use-resource';

let prismWorker;

Expand Down Expand Up @@ -75,9 +76,9 @@ function HighlightedCodeBlock({ code, lang }) {
return (
<div class="highlight-container">
<pre class="highlight">
<FakeSuspense fallback={fallback}>
<Suspense fallback={fallback}>
<HighlightedCode code={code} lang={lang} />
</FakeSuspense>
</Suspense>
</pre>
{repl && (
<a class="repl-link" href={`/repl?code=${encodeURIComponent(source)}`}>
Expand Down
4 changes: 2 additions & 2 deletions src/components/controllers/blog-page.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRoute, useLocation } from 'preact-iso';
import { useRoute } from 'preact-iso';
import { useContent } from '../../lib/use-resource';
import { useTitle, useDescription } from './utils';
import { NotFound } from './not-found';
Expand All @@ -20,7 +20,7 @@ export default function BlogPage() {
}

function BlogLayout() {
const { path } = useLocation();
const { path } = useRoute();
const [lang] = useLanguage();

const { html, meta } = useContent([lang, path === '/' ? 'index' : path]);
Expand Down
21 changes: 9 additions & 12 deletions src/components/controllers/doc-page.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { useRoute, useLocation } from 'preact-iso';
import { useRoute } from 'preact-iso';
import { useContent } from '../../lib/use-resource';
import { useTitle, useDescription } from './utils';
import config from '../../config.json';
import { NotFound } from './not-found';
import cx from '../../lib/cx';
import { LATEST_MAJOR, isDocPage } from '../../lib/docs';
import { useLanguage } from '../../lib/i18n';
import { MarkdownRegion } from './markdown-region';
import Sidebar from '../sidebar';
import Footer from '../footer/index';
import { docRoutes } from '../../lib/route-utils';
import { LATEST_MAJOR } from '../doc-version';
import style from './style.module.css';

export function DocPage() {
Expand All @@ -20,29 +20,27 @@ export function DocPage() {
return <NotFound />;
}

return <DocLayout />;
return <DocLayout isGuide />;
}

export function DocLayout() {
const { path } = useLocation();
export function DocLayout({ isGuide = false }) {
const { path } = useRoute();
const [lang] = useLanguage();

const { html, meta } = useContent([lang, path === '/' ? 'index' : path]);
useTitle(meta.title);
useDescription(meta.description);

const hasSidebar = meta.toc !== false && isDocPage(path);

return (
<div class={cx(style.page, hasSidebar && style.withSidebar)}>
<div class={cx(style.page, isGuide && style.withSidebar)}>
<div class={style.outer}>
{hasSidebar && (
{isGuide && (
<div class={style.sidebarWrap}>
<Sidebar />
</div>
)}
<div class={style.inner}>
<OldDocsWarning />
{isGuide && <OldDocsWarning />}
<MarkdownRegion html={html} meta={meta} />
<Footer />
</div>
Expand All @@ -53,9 +51,8 @@ export function DocLayout() {

function OldDocsWarning() {
const { name, version } = useRoute().params;
const { path } = useLocation();

if (!isDocPage(path) || version === LATEST_MAJOR) {
if (version === LATEST_MAJOR) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/controllers/markdown-region.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useLocation } from 'preact-iso';
import { useRoute } from 'preact-iso';
import EditThisPage from '../edit-button';
import ContentRegion from '../content-region';
import BlogMeta from '../blog-meta';
Expand All @@ -10,7 +10,7 @@ import BlogMeta from '../blog-meta';
* @propery {any} [components]
*/
export function MarkdownRegion({ html, meta, components }) {
const { path } = useLocation();
const { path } = useRoute();

const canEdit = path !== '/' && !path.startsWith('/tutorial');
const isBlogArticle = path.startsWith('/blog/');
Expand Down
5 changes: 2 additions & 3 deletions src/components/controllers/tutorial-page.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRoute, useLocation } from 'preact-iso';
import { useRoute } from 'preact-iso';
import { SolutionProvider } from './tutorial/contexts';
import { useContent } from '../../lib/use-resource';
import { useTitle, useDescription } from './utils';
Expand All @@ -19,8 +19,7 @@ export default function TutorialPage() {
}

function TutorialLayout() {
const { path } = useLocation();
const { params } = useRoute();
const { path, params } = useRoute();
const [lang] = useLanguage();

const { html, meta } = useContent([lang, !params.step ? 'tutorial/index' : path]);
Expand Down
12 changes: 6 additions & 6 deletions src/components/controllers/tutorial/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
useMemo,
useCallback
} from 'preact/hooks';
import { useLocation } from 'preact-iso';
import { useRoute } from 'preact-iso';
import linkState from 'linkstate';
import { TutorialContext, SolutionContext } from './contexts';
import cx from '../../../lib/cx';
Expand Down Expand Up @@ -168,7 +168,7 @@ function TutorialView({

const [showCodeOverride, toggleCode] = useReducer(s => !s, true);

const { url } = useLocation();
const { path } = useRoute();
const [lang] = useLanguage();
const { solved } = useContext(SolutionContext);

Expand All @@ -183,7 +183,7 @@ function TutorialView({
if (!loading && !initialLoad) {
content.current.scrollTo(0, 0);
}
}, [url, loading, initialLoad]);
}, [path, loading, initialLoad]);

const reRun = useCallback(() => {
let code = tutorial.state.code;
Expand Down Expand Up @@ -211,7 +211,7 @@ function TutorialView({
<div class={style.output} key="output">
{!initialLoad && (
<Runner
key={url}
key={path}
ref={tutorial.runner}
onSuccess={tutorial.onSuccess}
onRealm={tutorial.onRealm}
Expand All @@ -226,7 +226,7 @@ function TutorialView({
close
</button>
<ErrorOverlay
key={'e:' + url}
key={'e:' + path}
class={style.error}
name={error.name}
message={error.message}
Expand Down Expand Up @@ -298,7 +298,7 @@ function TutorialView({
</Splitter>

<InjectPrerenderData
name={url}
name={path}
data={{
html,
meta
Expand Down
10 changes: 6 additions & 4 deletions src/components/doc-version/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { useLocation, useRoute } from 'preact-iso';
import config from '../../config.json';
import style from './style.module.css';

export const AVAILABLE_DOCS = [10, 8];
export const LATEST_MAJOR = 'v10';
export const AVAILABLE_DOCS = ['10', '8'];

/**
* Select box to switch the currently displayed docs version
*/
export default function DocVersion() {
const { path, route } = useLocation();
const { version, name } = useRoute().params;
const { route } = useLocation();
const { params, path } = useRoute();
const { version, name } = params;

const onChange = useCallback(
e => {
Expand All @@ -28,7 +30,7 @@ export default function DocVersion() {
Version:{' '}
<select value={version} class={style.select} onChange={onChange}>
{AVAILABLE_DOCS.map(v => {
const suffix = v === 10 ? ' (current)' : '';
const suffix = LATEST_MAJOR.slice(1) == v ? ' (current)' : '';
return (
<option key={v} value={`v${v}`}>
{v}.x{suffix}
Expand Down
4 changes: 2 additions & 2 deletions src/components/edit-button/index.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useLocation } from 'preact-iso';
import { useRoute } from 'preact-iso';
import { useLanguage } from '../../lib/i18n';
import style from './style.module.css';

export default function EditThisPage({ isFallback }) {
let { path } = useLocation();
let { path } = useRoute();
const [lang] = useLanguage();

path = !isFallback ? path + '.md' : '';
Expand Down
6 changes: 3 additions & 3 deletions src/components/footer/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import config from '../../config.json';
import { useState, useEffect, useCallback } from 'preact/hooks';
import { useLocation } from 'preact-iso';
import { useRoute } from 'preact-iso';
import style from './style.module.css';
import { useLanguage } from '../../lib/i18n';

Expand Down Expand Up @@ -42,8 +42,8 @@ function useContributors(deps) {
}

export default function Footer() {
const { url } = useLocation();
const contrib = useContributors([url]);
const { path } = useRoute();
const contrib = useContributors([path]);
const [lang, setLang] = useLanguage();

const onSelect = useCallback(e => setLang(e.target.value), [setLang]);
Expand Down
4 changes: 2 additions & 2 deletions src/components/sidebar/sidebar-nav.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useLocation } from 'preact-iso';
import { useRoute } from 'preact-iso';
import cx from '../../lib/cx';
import style from './sidebar-nav.module.css';

Expand All @@ -13,7 +13,7 @@ import style from './sidebar-nav.module.css';
* @param {SidebarNavProps} props
*/
export default function SidebarNav({ items, onClick }) {
const { path } = useLocation();
const { path } = useRoute();

return (
<nav
Expand Down
21 changes: 0 additions & 21 deletions src/lib/docs.js

This file was deleted.

9 changes: 0 additions & 9 deletions src/lib/use-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,3 @@ export function useResource(fn, deps) {
else if (state.status === 'error') throw state.result;
throw state.promise;
}

export function FakeSuspense(props) {
this.__c = childDidSuspend;
return this.state && this.state.suspended ? props.fallback : props.children;
}

function childDidSuspend(err) {
err.then(() => this.forceUpdate());
}

0 comments on commit 1c1a08f

Please sign in to comment.