Skip to content

Commit

Permalink
refactor: Use prerender data API (#1192)
Browse files Browse the repository at this point in the history
* refactor: Use prerender data API

* refactor: Switch to proper prerender data provider

* docs: Fix home page github stars repl example
  • Loading branch information
rschristian authored Nov 18, 2024
1 parent a0bc595 commit 1b7b7da
Show file tree
Hide file tree
Showing 21 changed files with 155 additions and 134 deletions.
2 changes: 1 addition & 1 deletion content/de/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export default class Stars extends Component {
}
}
// --repl-after
render(<Stars />, document.getElementById("app"));
render(<Stars repo="preactjs/preact" />, document.getElementById("app"));
</code></pre>
</div>
<div>
Expand Down
2 changes: 1 addition & 1 deletion content/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export default class Stars extends Component {
}
}
// --repl-after
render(&lt;Stars /&gt;, document.getElementById("app"));
render(&lt;Stars repo="preactjs/preact" /&gt;, document.getElementById("app"));
</code></pre>
</div>
<div>
Expand Down
2 changes: 1 addition & 1 deletion content/es/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export default class Stars extends Component {
}
}
// --repl-after
render(&lt;Stars /&gt;, document.getElementById("app"));
render(&lt;Stars repo="preactjs/preact" /&gt;, document.getElementById("app"));
</code></pre>
</div>
<div>
Expand Down
2 changes: 1 addition & 1 deletion content/fr/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export default class Stars extends Component {
}
}
// --repl-after
render(&lt;Stars /&gt;, document.getElementById("app"));
render(&lt;Stars repo="preactjs/preact" /&gt;, document.getElementById("app"));
</code></pre>
</div>
<div>
Expand Down
2 changes: 1 addition & 1 deletion content/it/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export default class Stars extends Component {
}
}
// --repl-after
render(&lt;Stars /&gt;, document.getElementById("app"));
render(&lt;Stars repo="preactjs/preact" /&gt;, document.getElementById("app"));
</code></pre>
</div>
<div>
Expand Down
2 changes: 1 addition & 1 deletion content/ja/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export default class Stars extends Component {
}
}
// --repl-after
render(&lt;Stars /&gt;, document.getElementById("app"));
render(&lt;Stars repo="preactjs/preact" /&gt;, document.getElementById("app"));
</code></pre>
</div>
<div>
Expand Down
2 changes: 1 addition & 1 deletion content/kr/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export default class Stars extends Component {
}
}
// --repl-after
render(&lt;Stars /&gt;, document.getElementById("app"));
render(&lt;Stars repo="preactjs/preact" /&gt;, document.getElementById("app"));
</code></pre>
</div>
<div>
Expand Down
2 changes: 1 addition & 1 deletion content/pt-br/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export default class Stars extends Component {
}
}
// --repl-after
render(&lt;Stars /&gt;, document.getElementById("app"));
render(&lt;Stars repo="preactjs/preact" /&gt;, document.getElementById("app"));
</code></pre>
</div>
<div>
Expand Down
2 changes: 1 addition & 1 deletion content/ru/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export default class Stars extends Component {
}
}
// --repl-after
render(&lt;Stars /&gt;, document.getElementById("app"));
render(&lt;Stars repo="preactjs/preact" /&gt;, document.getElementById("app"));
</code></pre>
</div>
<div>
Expand Down
2 changes: 1 addition & 1 deletion content/tr/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export default class Stars extends Component {
}
}
// --repl-after
render(&lt;Stars /&gt;, document.getElementById("app"));
render(&lt;Stars repo="preactjs/preact" /&gt;, document.getElementById("app"));
</code></pre>
</div>
<div>
Expand Down
2 changes: 1 addition & 1 deletion content/zh/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export default class Stars extends Component {
}
}
// --repl-after
render(&lt;Stars /&gt;, document.getElementById("app"));
render(&lt;Stars repo="preactjs/preact" /&gt;, document.getElementById("app"));
</code></pre>
</div>
<div>
Expand Down
24 changes: 15 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"@codemirror/state": "^6.4.1",
"@codemirror/view": "^6.28.1",
"@docsearch/react": "^3.6.0",
"@preact/preset-vite": "^2.8.3",
"@preact/preset-vite": "^2.9.1",
"@preact/signals": "^1.1.3",
"@preact/signals-core": "^1.2.3",
"@rollup/browser": "^3.18.0",
Expand Down
12 changes: 9 additions & 3 deletions src/components/app.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { LocationProvider, ErrorBoundary } from 'preact-iso';
import { LanguageProvider } from '../lib/i18n';
import { PrerenderDataProvider } from '../lib/prerender-data.jsx';
import Header from './header';
import Routes from './routes';

export default function App() {
/**
* @param {{ prerenderData?: import('../types.d.ts').PrerenderData }} props
*/
export default function App({ prerenderData }) {
return (
<ErrorBoundary>
<LocationProvider>
<LanguageProvider>
<Header />
<Routes />
<PrerenderDataProvider value={prerenderData}>
<Header />
<Routes />
</PrerenderDataProvider>
</LanguageProvider>
</LocationProvider>
</ErrorBoundary>
Expand Down
8 changes: 1 addition & 7 deletions src/components/controllers/tutorial/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import {
useMemo,
useCallback
} from 'preact/hooks';
import { useLocation, useRoute } from 'preact-iso';
import { useLocation } from 'preact-iso';
import { TutorialContext, SolutionContext } from './contexts';
import { ErrorOverlay } from '../repl/error-overlay';
import { parseStackTrace } from '../repl/errors';
import cx from '../../../lib/cx';
import { InjectPrerenderData } from '../../../lib/prerender-data';
import { useResource } from '../../../lib/use-resource';
import { useLanguage } from '../../../lib/i18n';
import { Splitter } from '../../splitter';
Expand Down Expand Up @@ -220,11 +219,6 @@ export function Tutorial({ html, meta }) {
<ButtonContainer meta={meta} showCode={showCode} help={help} />
</div>
</Splitter>

<InjectPrerenderData
name={url}
data={{ html, meta }}
/>
</div>
</TutorialContext.Provider>
);
Expand Down
83 changes: 36 additions & 47 deletions src/components/github-stars.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from 'preact';
import { localStorageGet, localStorageSet } from '../lib/localstorage';
import { fallbackData, repoInfo } from '../lib/github';
import { useEffect, useState } from 'preact/hooks';
import { repoInfo } from '../lib/github';
import { usePrerenderData } from '../lib/prerender-data.jsx';

const githubStars = repo =>
repoInfo(repo).then(d => d.stargazers_count);
Expand All @@ -10,53 +10,42 @@ const formatNumber = num => (num + '').replace(/(\d{3})$/g, ',$1');
// make available to homepage REPL demo
if (typeof window !== 'undefined') window.githubStars = githubStars;

export default class GithubStars extends Component {
state = {
stars: localStorageGet('_stars') || fallbackData.preactStargazers
};
export default function GitHubStars({ user, repo, simple, children }) {
const { preactStargazers } = usePrerenderData();
const [stars, setStars] = useState(preactStargazers);

setStars = stars => {
if (stars && stars != this.state.stars) {
localStorageSet('_stars', stars);
this.setState({ stars });
}
};

componentDidMount() {
let { user, repo } = this.props;
githubStars(user + '/' + repo).then(this.setStars);
}

render({ user, repo, simple, children }, { stars }) {
let url = `https://github.com/${user}/${repo}/`;
if (simple) {
return (
<a href={url} class="stars" target="_blank" rel="noopener noreferrer">
⭐️ {stars} Stars
</a>
);
}
useEffect(() => {
githubStars(`${user}/${repo}`).then(setStars);
}, []);

const url = `https://github.com/${user}/${repo}/`;
if (simple) {
return (
<span class="github-btn">
<a
class="gh-btn"
href={url}
target="_blank"
rel="noopener noreferrer"
aria-label="Star on GitHub"
>
<span class="gh-ico" /> Star
</a>
<a
class="gh-count"
href={url}
target="_blank"
rel="noopener noreferrer"
>
{stars ? formatNumber(Math.round(stars)) : children || '..'}
</a>
</span>
<a href={url} class="stars" target="_blank" rel="noopener noreferrer">
⭐️ {stars} Stars
</a>
);
}

return (
<span class="github-btn">
<a
class="gh-btn"
href={url}
target="_blank"
rel="noopener noreferrer"
aria-label="Star on GitHub"
>
<span class="gh-ico" /> Star
</a>
<a
class="gh-count"
href={url}
target="_blank"
rel="noopener noreferrer"
>
{stars ? formatNumber(Math.round(stars)) : children || '..'}
</a>
</span>
);
}
8 changes: 5 additions & 3 deletions src/components/header/gh-version.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useEffect, useState } from 'preact/hooks';
import { fallbackData, fetchRelease } from '../../lib/github';
import { fetchRelease } from '../../lib/github';
import { usePrerenderData } from '../../lib/prerender-data.jsx';
import config from '../../config.json';

export default function ReleaseLink({ ...props }) {
const { preactReleaseURL, preactVersion } = usePrerenderData();
const [release, setRelease] = useState({
url: globalThis.prerenderPreactReleaseUrl || fallbackData.preactReleaseUrl,
version: globalThis.prerenderPreactVersion || fallbackData.preactVersion
url: preactReleaseURL,
version: preactVersion
});

useEffect(() => {
Expand Down
Loading

0 comments on commit 1b7b7da

Please sign in to comment.