-
Notifications
You must be signed in to change notification settings - Fork 308
cdn-swap-npm #263
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
base: main
Are you sure you want to change the base?
cdn-swap-npm #263
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ export type { | |
| } from "./types.js"; | ||
|
|
||
| import { init } from "./core/index.js"; | ||
| import { fetchLatestVersion } from "./utils/fetch-latest-version.js"; | ||
| import type { Plugin, ReactGrabAPI } from "./types.js"; | ||
|
|
||
| declare global { | ||
|
|
@@ -114,4 +115,21 @@ if (typeof window !== "undefined" && !window.__REACT_GRAB_DISABLED__) { | |
| window.dispatchEvent( | ||
| new CustomEvent("react-grab:init", { detail: globalApi }), | ||
| ); | ||
|
|
||
| if (process.env.DISTRIBUTION === "npm" && globalApi) { | ||
| void fetchLatestVersion().then((latestVersion) => { | ||
| if (!latestVersion) return; | ||
|
|
||
| globalApi?.dispose(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: The CDN swap calls Prompt for AI agents |
||
| setGlobalApi(null); | ||
|
|
||
| const script = document.createElement("script"); | ||
| script.src = `https://unpkg.com/react-grab@${latestVersion}/dist/index.global.js`; | ||
| script.onerror = () => { | ||
| script.remove(); | ||
| setGlobalApi(init()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: The Prompt for AI agents |
||
| }; | ||
| document.head.appendChild(script); | ||
| }); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CDN swap permanently loses all registered pluginsHigh Severity The CDN swap disposes Additional Locations (1)There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Swap invalidates API for once-only event listenersHigh Severity The |
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,43 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { isExtensionContext } from "./is-extension-context.js"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isNewerSemver = (latest: string, current: string): boolean => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const latestParts = latest.split(".").map(Number); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const currentParts = current.split(".").map(Number); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (let index = 0; index < 3; index++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const latestPart = latestParts[index] ?? 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const currentPart = currentParts[index] ?? 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (latestPart > currentPart) return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (latestPart < currentPart) return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+4
to
+11
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The isNewerSemver function improperly parses semantic versions with pre-release identifiers, metadata, or non-standard formats, causing incorrect version comparisons |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const fetchLatestVersion = async (): Promise<string | null> => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const currentVersion = process.env.VERSION; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!currentVersion || !navigator.onLine || isExtensionContext()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const response = await fetch( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| `https://www.react-grab.com/api/version?source=browser&t=${Date.now()}`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| referrerPolicy: "origin", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| keepalive: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| priority: "low", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cache: "no-store", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } as RequestInit, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const latestVersion = await response.text(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !latestVersion || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| latestVersion === currentVersion || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !isNewerSemver(latestVersion, currentVersion) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return latestVersion; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing handler for successful script load in NPM distribution version update flow causes stale globalApi state and no event notification when newer version loads from unpkg