Skip to content

Commit 0fce6db

Browse files
committed
refactor: update distribution handling and version fetching in react-grab
- Modified tsup configuration to support both "cdn" and "npm" distributions. - Enhanced version fetching logic in index.ts to dynamically load the latest version from unpkg for npm distribution. - Updated log-intro.ts to utilize fetchLatestVersion for version checks, improving consistency in version management.
1 parent d252ab0 commit 0fce6db

File tree

4 files changed

+75
-20
lines changed

4 files changed

+75
-20
lines changed

packages/react-grab/src/core/log-intro.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LOGO_SVG } from "./logo-svg.js";
2-
import { isExtensionContext } from "../utils/is-extension-context.js";
2+
import { fetchLatestVersion } from "../utils/fetch-latest-version.js";
33

44
export const logIntro = () => {
55
try {
@@ -10,25 +10,14 @@ export const logIntro = () => {
1010
`background: #330039; color: #ffffff; border: 1px solid #d75fcb; padding: 4px 4px 4px 24px; border-radius: 4px; background-image: url("${logoDataUri}"); background-size: 16px 16px; background-repeat: no-repeat; background-position: 4px center; display: inline-block; margin-bottom: 4px;`,
1111
"",
1212
);
13-
if (navigator.onLine && version && !isExtensionContext()) {
14-
fetch(
15-
`https://www.react-grab.com/api/version?source=browser&t=${Date.now()}`,
16-
{
17-
referrerPolicy: "origin",
18-
keepalive: true,
19-
priority: "low",
20-
cache: "no-store",
21-
} as RequestInit,
22-
)
23-
.then((response) => response.text())
24-
.then((latestVersion) => {
25-
if (latestVersion && latestVersion !== version) {
26-
console.warn(
27-
`[React Grab] v${version} is outdated (latest: v${latestVersion})`,
28-
);
29-
}
30-
})
31-
.catch(() => null);
13+
if (process.env.DISTRIBUTION !== "npm") {
14+
void fetchLatestVersion().then((latestVersion) => {
15+
if (latestVersion) {
16+
console.warn(
17+
`[React Grab] v${version} is outdated (latest: v${latestVersion})`,
18+
);
19+
}
20+
});
3221
}
3322
// HACK: Entire intro log is best-effort; never block initialization
3423
} catch {}

packages/react-grab/src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export type {
4242
} from "./types.js";
4343

4444
import { init } from "./core/index.js";
45+
import { fetchLatestVersion } from "./utils/fetch-latest-version.js";
4546
import type { Plugin, ReactGrabAPI } from "./types.js";
4647

4748
declare global {
@@ -114,4 +115,21 @@ if (typeof window !== "undefined" && !window.__REACT_GRAB_DISABLED__) {
114115
window.dispatchEvent(
115116
new CustomEvent("react-grab:init", { detail: globalApi }),
116117
);
118+
119+
if (process.env.DISTRIBUTION === "npm" && globalApi) {
120+
void fetchLatestVersion().then((latestVersion) => {
121+
if (!latestVersion) return;
122+
123+
globalApi?.dispose();
124+
setGlobalApi(null);
125+
126+
const script = document.createElement("script");
127+
script.src = `https://unpkg.com/react-grab@${latestVersion}/dist/index.global.js`;
128+
script.onerror = () => {
129+
script.remove();
130+
setGlobalApi(init());
131+
};
132+
document.head.appendChild(script);
133+
});
134+
}
117135
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { isExtensionContext } from "./is-extension-context.js";
2+
3+
const isNewerSemver = (latest: string, current: string): boolean => {
4+
const latestParts = latest.split(".").map(Number);
5+
const currentParts = current.split(".").map(Number);
6+
for (let index = 0; index < 3; index++) {
7+
const latestPart = latestParts[index] ?? 0;
8+
const currentPart = currentParts[index] ?? 0;
9+
if (latestPart > currentPart) return true;
10+
if (latestPart < currentPart) return false;
11+
}
12+
return false;
13+
};
14+
15+
export const fetchLatestVersion = async (): Promise<string | null> => {
16+
const currentVersion = process.env.VERSION;
17+
if (!currentVersion || !navigator.onLine || isExtensionContext()) {
18+
return null;
19+
}
20+
21+
try {
22+
const response = await fetch(
23+
`https://www.react-grab.com/api/version?source=browser&t=${Date.now()}`,
24+
{
25+
referrerPolicy: "origin",
26+
keepalive: true,
27+
priority: "low",
28+
cache: "no-store",
29+
} as RequestInit,
30+
);
31+
const latestVersion = await response.text();
32+
if (
33+
!latestVersion ||
34+
latestVersion === currentVersion ||
35+
!isNewerSemver(latestVersion, currentVersion)
36+
) {
37+
return null;
38+
}
39+
return latestVersion;
40+
} catch {
41+
return null;
42+
}
43+
};

packages/react-grab/tsup.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const browserBuildConfig: Options = {
6565
env: {
6666
...DEFAULT_OPTIONS.env,
6767
VERSION: version,
68+
DISTRIBUTION: "cdn",
6869
},
6970
format: ["iife"],
7071
globalName: "globalThis.__REACT_GRAB_MODULE__",
@@ -91,6 +92,10 @@ const libraryBuildConfig: Options = {
9192
...DEFAULT_OPTIONS,
9293
clean: false,
9394
entry: ["./src/index.ts", "./src/core/index.tsx", "./src/primitives.ts"],
95+
env: {
96+
...DEFAULT_OPTIONS.env,
97+
DISTRIBUTION: "npm",
98+
},
9499
format: ["cjs", "esm"],
95100
loader: {
96101
".css": "text",

0 commit comments

Comments
 (0)