Skip to content
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

New prop serialization step #1094

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 16 additions & 27 deletions packages/start/islands/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Component, ComponentProps, lazy, sharedConfig } from "solid-js";
import { Component, ComponentProps, createUniqueId, lazy, sharedConfig } from "solid-js";
import { Hydration, NoHydration } from "solid-js/web";
import { useRequest } from "../server/ServerContext";
import { IslandManifest } from "../server/types";
import { splitProps } from "./utils";
export { default as clientOnly } from "./clientOnly";

declare module "solid-js" {
namespace JSX {
interface IntrinsicElements {
"solid-island": {
"data-props": string;
"data-component": string;
"data-island": string;
"data-when": "idle" | "load";
Expand Down Expand Up @@ -45,11 +43,9 @@ export function island<T extends Component<any>>(
);
}

return ((compProps: ComponentProps<T>) => {
return ((props: ComponentProps<T>) => {
if (import.meta.env.SSR) {
const context = useRequest();
const [, props] = splitProps(compProps, ["children"] as any);
const [, spreadProps] = splitProps(compProps, [] as any);

let fpath: string;
let styles: string[] = [];
Expand All @@ -64,42 +60,35 @@ export function island<T extends Component<any>>(
fpath = path;
}

const serialize = (props: ComponentProps<T>) => {
let offset = 0;
let el = JSON.stringify(props, (key, value) => {
if (value && value.t) {
offset++;
return undefined;
}
return value;
});

return {
"data-props": el,
"data-offset": offset
};
};

// @ts-expect-error
if (!sharedConfig.context?.noHydrate) {
return <Component {...compProps} />;
return <Component {...props} />;
}

// FIXME Introduce a new ID generation scheme
// for islands
const id = createUniqueId();

sharedConfig.context.serialize(
id,
props,
);


return (
<Hydration>
<solid-island
data-id={id}
data-component={fpath!}
data-island={path}
data-when={(props as any)["client:idle"] ? "idle" : "load"}
data-css={JSON.stringify(styles)}
{...serialize(props)}
>
<IslandComponent {...spreadProps} />
<IslandComponent {...props} />
</solid-island>
</Hydration>
);
} else {
return <Component {...compProps} />;
}
return <Component {...props} />;
}) as T;
}
4 changes: 2 additions & 2 deletions packages/start/islands/mount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function hydrateServerRouter() {
if (!Component || !el.dataset.hk) return;

let hk = el.dataset.hk;
_$DEBUG("hydrating island", el.dataset.island, hk.slice(0, hk.length - 1) + `1-`, el);
// _$DEBUG("hydrating island", el.dataset.island, hk.slice(0, hk.length - 1) + `1-`, el);

let props = createStore({
...JSON.parse(el.dataset.props!),
Expand All @@ -38,7 +38,7 @@ export function hydrateServerRouter() {
map.set(el, props);

hydrate(() => createComponent(Component, props[0]), el, {
renderId: hk.slice(0, hk.length - 1) + `${1 + Number(el.dataset.offset)}-`,
renderId: el.dataset.id,
owner: lookupOwner(el)
});

Expand Down
Loading