@rpcbase/client 0.279.0 → 0.281.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/client",
3
- "version": "0.279.0",
3
+ "version": "0.281.0",
4
4
  "type": "module",
5
5
  "main": "./src/index.ts",
6
6
  "scripts": {
@@ -0,0 +1,15 @@
1
+ import posthog from "posthog-js"
2
+ import { PostHogProvider } from "posthog-js/react"
3
+
4
+ import { useApplyScroll } from "../utils/useApplyScroll"
5
+
6
+
7
+ export const RootProvider = ({children}) => {
8
+ useApplyScroll()
9
+
10
+ return (
11
+ <PostHogProvider client={posthog}>
12
+ {children}
13
+ </PostHogProvider>
14
+ )
15
+ }
@@ -1,3 +1,2 @@
1
1
  export * from "./useThrottledMeasure"
2
- export * from "./useApplyScroll"
3
2
  export * from "./useMediaQuery"
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./apiClient"
2
- export * from "./initClient"
2
+ export * from "./initWithRoutes"
3
3
  export * from "./types"
4
4
  export * from "./getFeatureFlag"
5
+ export * from "./RootProvider"
@@ -0,0 +1,115 @@
1
+ import { StrictMode } from "react"
2
+ import posthog from "posthog-js"
3
+ import {createBrowserRouter, createRoutesFromElements, RouterProvider} from "@rpcbase/router"
4
+ import { hydrateRoot } from "react-dom/client"
5
+
6
+ import { initApiClient } from "./apiClient"
7
+ import {cleanupURL} from "./cleanupURL"
8
+
9
+
10
+ const isProduction = import.meta.env.MODE === "production"
11
+
12
+ type ReactErrorInfo = { componentStack?: string }
13
+
14
+
15
+ /* eslint-disable @typescript-eslint/no-explicit-any */
16
+ const showErrorOverlay = (err: { title: string, message: string, reason: string, plugin: string }) => {
17
+ const ErrorOverlay = customElements.get("vite-error-overlay")
18
+ // don't open outside vite environment
19
+ if (!ErrorOverlay) {return}
20
+ console.log(err)
21
+ const overlay = new ErrorOverlay(err)
22
+ document.body.appendChild(overlay)
23
+ }
24
+
25
+
26
+ const handleServerErrors = () => {
27
+ if ((window as any).__staticRouterHydrationData?.errors) {
28
+
29
+ const {errors} = (window as any).__staticRouterHydrationData
30
+
31
+ Object.values(errors).forEach((error: any) => {
32
+ showErrorOverlay({
33
+ plugin: "ssr-router",
34
+ ...error.reason
35
+ })
36
+ })
37
+ }
38
+ }
39
+
40
+
41
+ // TODO: what type is this
42
+ export const initWithRoutes = async (routesElement, opts) => {
43
+
44
+ await initApiClient()
45
+
46
+ cleanupURL()
47
+
48
+ handleServerErrors()
49
+
50
+
51
+ const routes = createRoutesFromElements(routesElement)
52
+ const router = createBrowserRouter(routes)
53
+
54
+
55
+ const toError = (error: unknown): Error =>
56
+ error instanceof Error ? error : new Error(String(error))
57
+
58
+ const mentionsHydration = (value: unknown, depth = 0): boolean => {
59
+ if (depth > 5) {
60
+ return false
61
+ }
62
+ if (typeof value === "string") {
63
+ return value.toLowerCase().includes("hydrat")
64
+ }
65
+ if (value instanceof Error) {
66
+ const digest = (value as { digest?: unknown }).digest
67
+ const cause = (value as { cause?: unknown }).cause
68
+ return (
69
+ mentionsHydration(value.message, depth + 1) ||
70
+ mentionsHydration(digest, depth + 1) ||
71
+ mentionsHydration(cause, depth + 1)
72
+ )
73
+ }
74
+ return false
75
+ }
76
+
77
+ const phReactHandler =
78
+ (react_context: "uncaught" | "caught" | "recoverable") =>
79
+ (error: unknown, errorInfo?: ReactErrorInfo) => {
80
+ const err = toError(error)
81
+ // send as exception (counts toward error tracking)
82
+ posthog.captureException(err, {
83
+ react_context,
84
+ component_stack: errorInfo?.componentStack
85
+ })
86
+ if (react_context === "uncaught") {
87
+ console.warn("Uncaught error", err, errorInfo?.componentStack)
88
+ }
89
+ }
90
+
91
+ const hydrationOptions: Parameters<typeof hydrateRoot>[2] | undefined = isProduction ? {
92
+ onUncaughtError: phReactHandler("uncaught"),
93
+ onCaughtError: phReactHandler("caught"),
94
+ onRecoverableError: phReactHandler("recoverable")
95
+ } : (opts?.devThrowsOnHydrationErrors ? {
96
+ onRecoverableError(error, errorInfo) {
97
+ const err = toError(error)
98
+ if (mentionsHydration(err) || mentionsHydration(errorInfo?.componentStack)) {
99
+ throw err
100
+ }
101
+ // For non-hydration recoverable errors in dev, keep a visible signal.
102
+ console.error(err, errorInfo?.componentStack)
103
+ }
104
+ } : undefined)
105
+
106
+
107
+ hydrateRoot(
108
+ document.getElementById("root") as HTMLElement,
109
+ <StrictMode>
110
+ <RouterProvider router={router} />
111
+ </StrictMode>,
112
+ hydrationOptions
113
+ )
114
+
115
+ }
package/src/initClient.ts DELETED
@@ -1,35 +0,0 @@
1
- import { initApiClient } from "./apiClient"
2
- import {cleanupURL} from "./cleanupURL"
3
-
4
- /* eslint-disable @typescript-eslint/no-explicit-any */
5
- const showErrorOverlay = (err: { title: string, message: string, reason: string, plugin: string }) => {
6
- const ErrorOverlay = customElements.get("vite-error-overlay")
7
- // don't open outside vite environment
8
- if (!ErrorOverlay) {return}
9
- console.log(err)
10
- const overlay = new ErrorOverlay(err)
11
- document.body.appendChild(overlay)
12
- }
13
-
14
-
15
- const handleServerErrors = () => {
16
- if ((window as any).__staticRouterHydrationData?.errors) {
17
-
18
- const {errors} = (window as any).__staticRouterHydrationData
19
-
20
- Object.values(errors).forEach((error: any) => {
21
- showErrorOverlay({
22
- plugin: "ssr-router",
23
- ...error.reason
24
- })
25
- })
26
- }
27
- }
28
-
29
- export const initClient = () => {
30
- initApiClient()
31
-
32
- cleanupURL()
33
-
34
- handleServerErrors()
35
- }
File without changes