Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use prerender data API #1192

Merged
merged 6 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, drive-by issue that I ran into whilst testing:

While our usage example on the home page included the prop, the repl-after here was missing it. This was problematic as it'd attempt to fetch https://github.com/undefined if the user tried to open the example in the repl. Must've gotten missed when we added the repl-before & after stuff.

</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';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a relic of the Preact-CLI setup, InjectPrerenderData was linked to a getPrerenderData function that hasn't been in use since we switched over. Injecting therefore did nothing.

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting/setting from/to localstorage here seems unnecessary? I don't see any obvious benefit to doing so.

};
export default function GitHubStars({ user, repo, simple, children }) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converted to a function as it simplified consuming the context

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