From 3511fe9311585bc864452fe848f7365e0e753d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Charles?= Date: Mon, 27 Nov 2023 11:03:59 +0100 Subject: [PATCH] feat: ClientOnly component --- vike-solid/components/ClientOnly.tsx | 36 ++++++++++++++++++++++++++++ vike-solid/package.json | 18 ++++++++------ vike-solid/rollup.config.js | 2 ++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 vike-solid/components/ClientOnly.tsx diff --git a/vike-solid/components/ClientOnly.tsx b/vike-solid/components/ClientOnly.tsx new file mode 100644 index 0000000..31a5615 --- /dev/null +++ b/vike-solid/components/ClientOnly.tsx @@ -0,0 +1,36 @@ +import type { Component, JSX } from "solid-js"; +import { createEffect, createSignal, lazy, Suspense } from "solid-js"; +import { Dynamic } from "solid-js/web"; + +function ClientOnlyError() { + return

Error loading component.

+} + +export function ClientOnly(props: { + load: () => Promise<{ default: Component } | Component> + children: (Component: Component) => JSX.Element + fallback: JSX.Element + deps?: Parameters[1] +}) { + const [getComponent, setComponent] = createSignal | undefined>(undefined); + + createEffect(() => { + const loadComponent = () => { + const Component = lazy(() => + props.load() + .then((LoadedComponent) => { + return { default: () => props.children("default" in LoadedComponent ? LoadedComponent.default : LoadedComponent) }; + }) + .catch((error) => { + console.error("Component loading failed:", error); + return { default: ClientOnlyError }; + }) + ); + setComponent(() => Component); + }; + + loadComponent(); + }); + + return ; +} diff --git a/vike-solid/package.json b/vike-solid/package.json index ef5cd7b..54800d1 100644 --- a/vike-solid/package.json +++ b/vike-solid/package.json @@ -2,13 +2,6 @@ "name": "vike-solid", "version": "0.2.5", "type": "module", - "exports": { - ".": "./dist/+config.js", - "./vite": "./dist/vite-plugin-vike-solid.js", - "./usePageContext": "./dist/usePageContext.js", - "./renderer/onRenderHtml": "./dist/+onRenderHtml.js", - "./renderer/onRenderClient": "./dist/+onRenderClient.js" - }, "scripts": { "dev": "rollup -c rollup.config.js --watch", "build": "tsc --noEmit && rollup -c rollup.config.js", @@ -39,6 +32,14 @@ "vite": "^4.5.0", "vike": "^0.4.147" }, + "exports": { + ".": "./dist/+config.js", + "./vite": "./dist/vite-plugin-vike-solid.js", + "./usePageContext": "./dist/usePageContext.js", + "./ClientOnly": "./dist/ClientOnly.js", + "./renderer/onRenderHtml": "./dist/+onRenderHtml.js", + "./renderer/onRenderClient": "./dist/+onRenderClient.js" + }, "typesVersions": { "*": { ".": [ @@ -52,6 +53,9 @@ ], "usePageContext": [ "dist/components/usePageContext.d.ts" + ], + "ClientOnly": [ + "dist/components/ClientOnly.d.ts" ] } }, diff --git a/vike-solid/rollup.config.js b/vike-solid/rollup.config.js index 7aafd04..4ef6097 100644 --- a/vike-solid/rollup.config.js +++ b/vike-solid/rollup.config.js @@ -8,6 +8,7 @@ export default [ "./renderer/+onRenderHtml.tsx", "./renderer/+config.ts", "./components/usePageContext.tsx", + "./components/ClientOnly.tsx", "./cli/index.ts", ], ssr: true, @@ -26,6 +27,7 @@ export default [ input: [ "./renderer/+config.ts", "./components/usePageContext.tsx", + "./components/ClientOnly.tsx", "./vite-plugin-vike-solid.ts", ], output: [{ dir: "dist", format: "es", sanitizeFileName: false }],