Skip to content

Commit

Permalink
lazy load dev overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansolid committed Oct 18, 2024
1 parent ab007e0 commit 5ef4f75
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-tables-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": patch
---

lazy load dev overlay
17 changes: 14 additions & 3 deletions packages/start/src/shared/clientOnly.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @refresh skip
import type { Component, ComponentProps, JSX } from "solid-js";
import type { Component, ComponentProps, JSX, Setter } from "solid-js";
import { createMemo, createSignal, onMount, sharedConfig, splitProps, untrack } from "solid-js";
import { isServer } from "solid-js/web";

Expand All @@ -11,16 +11,18 @@ import { isServer } from "solid-js/web";
export default function clientOnly<T extends Component<any>>(
fn: () => Promise<{
default: T;
}>
}>,
options: { lazy?: boolean } = {}
) {
if (isServer) return (props: ComponentProps<T> & { fallback?: JSX.Element }) => props.fallback;

const [comp, setComp] = createSignal<T>();
fn().then(m => setComp(() => m.default));
!options.lazy && load(fn, setComp);
return (props: ComponentProps<T>) => {
let Comp: T | undefined;
let m: boolean;
const [, rest] = splitProps(props, ["fallback"]);
options.lazy && load(fn, setComp);
if ((Comp = comp()) && !sharedConfig.context) return Comp(rest);
const [mounted, setMounted] = createSignal(!sharedConfig.context);
onMount(() => setMounted(true));
Expand All @@ -31,3 +33,12 @@ export default function clientOnly<T extends Component<any>>(
);
};
}

function load<T>(
fn: () => Promise<{
default: T;
}>,
setComp: Setter<T>
) {
fn().then(m => setComp(() => m.default));
}
2 changes: 1 addition & 1 deletion packages/start/src/shared/dev-overlay/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const DevOverlayDialog =
? () => {
return <></>;
}
: /* #__PURE__ */ clientOnly(() => import("./DevOverlayDialog"));
: /* #__PURE__ */ clientOnly(() => import("./DevOverlayDialog"), { lazy: true });

export function DevOverlay(props: DevOverlayProps): JSX.Element {
const [errors, setErrors] = createSignal<unknown[]>([]);
Expand Down

0 comments on commit 5ef4f75

Please sign in to comment.