@lomray/vite-ssr-boost 2.2.1 → 2.3.0-beta.1
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.
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ComponentType, ReactNode } from 'react';
|
|
2
|
+
interface IOnlyClient<T> {
|
|
3
|
+
load: () => Promise<{
|
|
4
|
+
default: ComponentType<T>;
|
|
5
|
+
} | ComponentType<T>>;
|
|
6
|
+
children: (Component: ComponentType<T>) => ReactNode;
|
|
7
|
+
fallback?: ReactNode;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Render component only on client side
|
|
11
|
+
* @constructor
|
|
12
|
+
*/
|
|
13
|
+
declare function OnlyClient<T>({ load, children, fallback }: IOnlyClient<T>): ReactNode;
|
|
14
|
+
export { OnlyClient as default };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import e,{useState as l,useEffect as t,startTransition as n,lazy as a}from"react";function o({load:o,children:c,fallback:d}){const[r,i]=l(null);return t((()=>{n((()=>{const l=a((()=>o().then((e=>({default:()=>c("default"in e?e.default:e)}))).catch((l=>(console.error("Client side component loading failed:",l),{default:()=>e.createElement("p",null,"Failed to load client side component.")})))));i(l)}))}),[]),r?e.createElement(r,null):d}export{o as default};
|
|
2
|
+
//# sourceMappingURL=only-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"only-client.js","sources":["../../src/components/only-client.tsx"],"sourcesContent":["import type { ComponentType, ReactNode } from 'react';\nimport React, { lazy, startTransition, useEffect, useState } from 'react';\n\ninterface IOnlyClient<T> {\n load: () => Promise<{ default: ComponentType<T> } | ComponentType<T>>;\n children: (Component: ComponentType<T>) => ReactNode;\n fallback?: ReactNode;\n}\n\n/**\n * Render component only on client side\n * @constructor\n */\nfunction OnlyClient<T>({ load, children, fallback }: IOnlyClient<T>): ReactNode {\n const [Component, setComponent] = useState<ComponentType<unknown> | null>(null);\n\n useEffect(() => {\n startTransition(() => {\n const LoadedComponent = lazy(() =>\n load()\n .then((Loaded) => ({\n default: () => children('default' in Loaded ? Loaded.default : Loaded),\n }))\n .catch((error: Error) => {\n console.error('Client side component loading failed:', error);\n\n return { default: () => <p>Failed to load client side component.</p> };\n }),\n );\n\n setComponent(LoadedComponent);\n });\n }, []);\n\n return Component ? <Component /> : fallback;\n}\n\nexport default OnlyClient;\n"],"names":["OnlyClient","load","children","fallback","Component","setComponent","useState","useEffect","startTransition","LoadedComponent","lazy","then","Loaded","default","catch","error","console","React","createElement"],"mappings":"kFAaA,SAASA,GAAcC,KAAEA,EAAIC,SAAEA,EAAQC,SAAEA,IACvC,MAAOC,EAAWC,GAAgBC,EAAwC,MAoB1E,OAlBAC,GAAU,KACRC,GAAgB,KACd,MAAMC,EAAkBC,GAAK,IAC3BT,IACGU,MAAMC,IAAY,CACjBC,QAAS,IAAMX,EAAS,YAAaU,EAASA,EAAOC,QAAUD,OAEhEE,OAAOC,IACNC,QAAQD,MAAM,wCAAyCA,GAEhD,CAAEF,QAAS,IAAMI,EAAAC,cAAA,IAAA,KAAA,+CAI9Bb,EAAaI,EAAgB,GAC7B,GACD,IAEIL,EAAYa,EAAAC,cAACd,EAAS,MAAMD,CACrC"}
|