-
-
Notifications
You must be signed in to change notification settings - Fork 247
doc: show GitHub stars and Discord members in the navbar #3677
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| <script setup> | ||
| /** | ||
| * GitHub stars and Discord members in the navbar, with the hand-drawn icons | ||
| * from moq.dev. Hidden below 768px; the stock socialLinks keep GitHub and | ||
| * Discord reachable in the mobile overlay. | ||
| * | ||
| * The counts are fetched in the browser because the site is static and only | ||
| * rebuilt on deploy. Both APIs allow anonymous CORS requests; GitHub's limit is | ||
| * 60/hour per IP, so the result is cached in localStorage for an hour rather | ||
| * than refetched on every page. | ||
| */ | ||
| import { onMounted, ref } from "vue"; | ||
|
|
||
| const GITHUB = "https://api.github.com/repos/moq-dev/moq"; | ||
| const DISCORD = "https://discord.com/api/v10/invites/FCYF3p99mr?with_counts=true"; | ||
|
|
||
| const CACHE_KEY = "moq.stats"; | ||
| const CACHE_TTL = 60 * 60 * 1000; | ||
|
|
||
| const stars = ref(); | ||
| const chatters = ref(); | ||
|
|
||
| function readCache() { | ||
| try { | ||
| const cached = JSON.parse(localStorage.getItem(CACHE_KEY)); | ||
| if (cached && Date.now() - cached.at <= CACHE_TTL) return cached; | ||
| } catch {} | ||
| } | ||
|
|
||
| function writeCache(stats) { | ||
| try { | ||
| localStorage.setItem(CACHE_KEY, JSON.stringify({ ...stats, at: Date.now() })); | ||
| } catch {} | ||
| } | ||
|
|
||
| async function fetchNumber(url, key) { | ||
| try { | ||
| // Discord echoes the requesting origin in Access-Control-Allow-Origin but | ||
| // marks the response cacheable without `Vary: Origin`, so a response cached | ||
| // for moq.dev fails the CORS check on doc.moq.dev. Skip the HTTP cache; | ||
| // localStorage above is the cache. | ||
| const res = await fetch(url, { cache: "no-store" }); | ||
| if (!res.ok) return; | ||
| const value = (await res.json())[key]; | ||
| return typeof value === "number" ? value : undefined; | ||
| } catch {} | ||
| } | ||
|
|
||
| onMounted(async () => { | ||
| let stats = readCache(); | ||
| if (!stats) { | ||
| const [s, c] = await Promise.all([ | ||
| fetchNumber(GITHUB, "stargazers_count"), | ||
| fetchNumber(DISCORD, "approximate_member_count"), | ||
| ]); | ||
| stats = { stars: s, chatters: c }; | ||
| if (s !== undefined && c !== undefined) writeCache(stats); | ||
| } | ||
| stars.value = stats.stars; | ||
| chatters.value = stats.chatters; | ||
| }); | ||
| </script> | ||
|
|
||
| <template> | ||
| <div class="moq-community"> | ||
| <a href="https://github.com/moq-dev/moq" title="GitHub"> | ||
| <img src="/emoji/github.svg" alt="GitHub" /> | ||
| <span v-if="stars !== undefined">{{ stars.toLocaleString() }}</span> | ||
| </a> | ||
| <a href="https://discord.moq.dev" title="Discord"> | ||
| <img src="/emoji/discord.svg" alt="Discord" /> | ||
| <span v-if="chatters !== undefined">{{ chatters.toLocaleString() }}</span> | ||
| </a> | ||
| </div> | ||
| </template> | ||
|
|
||
| <style scoped> | ||
| .moq-community { | ||
| display: none; | ||
| } | ||
|
|
||
| @media (min-width: 768px) { | ||
| .moq-community { | ||
| display: flex; | ||
| align-items: center; | ||
| gap: 1rem; | ||
| margin-left: 1rem; | ||
| } | ||
| } | ||
|
|
||
| .moq-community a { | ||
| display: flex; | ||
| align-items: center; | ||
| gap: 0.375rem; | ||
| font-size: 0.85rem; | ||
| font-weight: 700; | ||
| color: var(--vp-c-brand-1); | ||
| text-decoration: none; | ||
| } | ||
|
|
||
| .moq-community img { | ||
| height: 1.75rem; | ||
| width: auto; | ||
| transition: transform 0.25s; | ||
| } | ||
|
|
||
| .moq-community a:hover img { | ||
| transform: rotate(-6deg); | ||
| } | ||
| </style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,17 @@ | ||
| import { h } from "vue"; | ||
| import DefaultTheme from "vitepress/theme"; | ||
| import Banner from "./Banner.vue"; | ||
| import Community from "./Community.vue"; | ||
| import "./custom.css"; | ||
|
|
||
| export default { | ||
| extends: DefaultTheme, | ||
| Layout() { | ||
| // Render the site-wide notice above every page via the layout-top slot. | ||
| // Render the site-wide notice above every page via the layout-top slot, | ||
| // and the GitHub/Discord counts at the end of the navbar. | ||
| return h(DefaultTheme.Layout, null, { | ||
| "layout-top": () => h(Banner), | ||
| "nav-bar-content-after": () => h(Community), | ||
| }); | ||
| }, | ||
| }; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.