@@ -8,37 +8,6 @@ import { links } from "../config/links";
88import " ../styles/global.css" ;
99
1010const lang: Lang = " en" ;
11-
12- // Fetch npm package data at build time
13- let version = " " ;
14- let monthlyDownloads = " " ;
15- let lastPublished = " " ;
16-
17- try {
18- const [pkgRes, dlRes] = await Promise .all ([
19- fetch (" https://registry.npmjs.org/noshift.js" ),
20- fetch (" https://api.npmjs.org/downloads/point/last-month/noshift.js" ),
21- ]);
22- if (pkgRes .ok ) {
23- const pkg = await pkgRes .json ();
24- version = pkg [" dist-tags" ]?.latest ?? " " ;
25- const timeStr = pkg .time ?.[version ] ?? " " ;
26- if (timeStr ) {
27- lastPublished = new Date (timeStr ).toLocaleDateString (" en-US" , {
28- year: " numeric" ,
29- month: " short" ,
30- day: " numeric" ,
31- });
32- }
33- }
34- if (dlRes .ok ) {
35- const dl = await dlRes .json ();
36- const count = dl .downloads ?? 0 ;
37- monthlyDownloads = count .toLocaleString (" en-US" );
38- }
39- } catch (_ ) {
40- /* fallback: leave empty */
41- }
4211---
4312
4413<BaseLayout lang ={ lang } title =" NoShift.js — Documentation" >
@@ -91,44 +60,32 @@ try {
9160 </div >
9261
9362 { /* Package Stats */ }
94- {
95- version && (
96- <div class = " pkg-stats" >
97- <div class = " stat-item" >
98- <Icon name = " material-symbols:tag" class = " stat-icon" />
99- <span class = " stat-label" id = " stat-version-label" >
100- { t (" en" , " home.version" )}
101- </span >
102- <span class = " stat-value" >{ version } </span >
103- </div >
104- <div class = " stat-item" >
105- <Icon name = " material-symbols:download" class = " stat-icon" />
106- <span class = " stat-label" id = " stat-dl-label" >
107- { t (" en" , " home.monthlyDownloads" )}
108- </span >
109- <span class = " stat-value" >{ monthlyDownloads } </span >
110- </div >
111- { lastPublished && (
112- <div class = " stat-item" >
113- <Icon
114- name = " material-symbols:calendar-month-outline"
115- class = " stat-icon"
116- />
117- <span class = " stat-label" id = " stat-pub-label" >
118- { t (" en" , " home.lastPublished" )}
119- </span >
120- <span
121- class = " stat-value"
122- id = " stat-pub-value"
123- data-raw = { lastPublished }
124- >
125- { lastPublished }
126- </span >
127- </div >
128- )}
129- </div >
130- )
131- }
63+ <div class =" pkg-stats" id =" pkg-stats" >
64+ <div class =" stat-item" >
65+ <Icon name =" material-symbols:tag" class =" stat-icon" />
66+ <span class =" stat-label" id =" stat-version-label" >
67+ { t (" en" , " home.version" )}
68+ </span >
69+ <span class =" stat-value" id =" stat-version-value" >—</span >
70+ </div >
71+ <div class =" stat-item" >
72+ <Icon name =" material-symbols:download" class =" stat-icon" />
73+ <span class =" stat-label" id =" stat-dl-label" >
74+ { t (" en" , " home.monthlyDownloads" )}
75+ </span >
76+ <span class =" stat-value" id =" stat-dl-value" >—</span >
77+ </div >
78+ <div class =" stat-item" >
79+ <Icon
80+ name =" material-symbols:calendar-month-outline"
81+ class =" stat-icon"
82+ />
83+ <span class =" stat-label" id =" stat-pub-label" >
84+ { t (" en" , " home.lastPublished" )}
85+ </span >
86+ <span class =" stat-value" id =" stat-pub-value" >—</span >
87+ </div >
88+ </div >
13289
13390 { /* Install command */ }
13491 <div class =" install-bar" >
@@ -370,8 +327,49 @@ console.log("Hello from " + name + "!");`}
370327 });
371328
372329 // Auto-detect browser language on load
373- if (navigator.language.startsWith("ja")) {
330+ const currentLang = navigator.language.startsWith("ja") ? "ja" : "en";
331+ if (currentLang === "ja") {
374332 applyLang("ja");
375333 }
334+
335+ // Fetch npm package stats dynamically
336+ (async () => {
337+ try {
338+ const [pkgRes, dlRes] = await Promise.all([
339+ fetch("https://registry.npmjs.org/noshift.js"),
340+ fetch("https://api.npmjs.org/downloads/point/last-month/noshift.js"),
341+ ]);
342+
343+ if (pkgRes.ok) {
344+ const pkg = await pkgRes.json();
345+ const ver = pkg["dist-tags"]?.latest ?? "";
346+ if (ver) {
347+ const versionEl = document.getElementById("stat-version-value");
348+ if (versionEl) versionEl.textContent = ver;
349+ }
350+ const timeStr = pkg.time?.[ver] ?? "";
351+ if (timeStr) {
352+ const pubEl = document.getElementById("stat-pub-value");
353+ if (pubEl) {
354+ pubEl.dataset.raw = timeStr;
355+ const lang = document.documentElement.lang || "en";
356+ pubEl.textContent = new Date(timeStr).toLocaleDateString(
357+ lang === "ja" ? "ja-JP" : "en-US",
358+ { year: "numeric", month: "short", day: "numeric" },
359+ );
360+ }
361+ }
362+ }
363+
364+ if (dlRes.ok) {
365+ const dl = await dlRes.json();
366+ const count = dl.downloads ?? 0;
367+ const dlEl = document.getElementById("stat-dl-value");
368+ if (dlEl) dlEl.textContent = count.toLocaleString("en-US");
369+ }
370+ } catch (_) {
371+ /* silently fail */
372+ }
373+ })();
376374 </script >
377375</BaseLayout >
0 commit comments