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

removed scope from config; added pageClientStarted #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,5 @@ dist
# End of https://www.toptal.com/developers/gitignore/api/node,linux,macos

dist

.idea
6 changes: 6 additions & 0 deletions pages/example/@id/+Page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { useUnit } from "effector-react";
import { clientOnly } from "vike-react/clientOnly";
import { Link } from "~/shared/routing";

import { $id } from "./model";

const Component = clientOnly(() => import("./ClientComponent"));

export function Page() {
const id = useUnit($id);

return (
<div>
<h1>Example</h1>
<p>Read parameter from route: {id}</p>
<p>
Client id: <Component fallback="loading..." />
</p>
<Link href="/">Go home</Link>
</div>
);
Expand Down
3 changes: 3 additions & 0 deletions pages/example/@id/+pageClientStarted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createEvent } from "effector";

export const pageClientStarted = createEvent();
12 changes: 12 additions & 0 deletions pages/example/@id/ClientComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useUnit } from "effector-react";

import { $clientId } from "./model";

// Since $clientId changes during client rendering to avoid hydration error we need to make consumer components only client
const ClientComponent = () => {
const clientId = useUnit($clientId);

return <>{clientId}</>;
};

export default ClientComponent;
8 changes: 8 additions & 0 deletions pages/example/@id/model.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { createStore, sample } from "effector";
import { redirectTo } from "~/shared/routing";

import { pageClientStarted } from "./+pageClientStarted";
import { pageStarted } from "./+pageStarted";

export const $id = createStore("");
export const $clientId = createStore(0);

const dataInitialized = sample({
clock: pageStarted,
Expand All @@ -21,3 +23,9 @@ sample({
fn: ({ sampleData: { id } }) => id,
target: $id,
});

sample({
clock: pageClientStarted,
fn: () => 1,
target: $clientId,
});
3 changes: 3 additions & 0 deletions renderer/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export default {
pageStarted: {
env: { client: true, server: true },
},
pageClientStarted: {
env: { client: true, server: false },
},
// https://effector.dev/en/api/effector/scope/
scope: {
env: { client: true, server: true },
Expand Down
1 change: 0 additions & 1 deletion renderer/+onBeforeRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export const onBeforeRender: OnBeforeRenderAsync = async (pageContext) => {

return {
pageContext: {
scope,
// https://effector.dev/en/api/effector/serialize
scopeValues: serialize(scope),
},
Expand Down
20 changes: 11 additions & 9 deletions renderer/+onBeforeRenderClient.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { fork } from "effector";
import { allSettled, fork, serialize } from "effector";

// https://vike.dev/onBeforeRenderClient
export function onBeforeRenderClient(pageContext: Vike.PageContext) {
export async function onBeforeRenderClient(pageContext: Vike.PageContext) {
// https://vike.dev/pageContext
if (!("scope" in pageContext)) {
return {
pageContext: {
// https://effector.dev/en/api/effector/fork/
scope: fork({ values: pageContext.scopeValues }),
},
};

const scope = fork({ values: pageContext.scopeValues });

const pageClientStarted = pageContext.config.pageClientStarted;

if (pageClientStarted) {
await allSettled(pageClientStarted, { scope });
}

pageContext.scopeValues = serialize(scope);
}
4 changes: 1 addition & 3 deletions renderer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ declare global {
interface PageContext {
config: {
pageStarted?: EventCallable<{ params: Record<string, string>; data: unknown }>;
pageClientStarted?: EventCallable<void>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think to make universal interface with pageStarted, pageClientStarted, and possible createServerStarted?

Suggested change
pageClientStarted?: EventCallable<void>;
pageClientStarted?: EventCallable<{ params: Record<string, string>; data: unknown }>;

};

// https://effector.dev/en/api/effector/scope/
scope?: Scope;
scopeValues?: Record<string, unknown>;
}
}
Expand Down