Skip to content
Open
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
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"node": ">=18.6.0"
},
"dependencies": {
"@effector/next": "^0.7.0",
Copy link
Member

Choose a reason for hiding this comment

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

Why @effector/next is required for vike?

Copy link
Author

Choose a reason for hiding this comment

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

this is the same situation as in nextjs
we have to have 1 scope for an app and provide partial hydration of values

"@effector/reflect": "^9.2.0",
"@fastify/accepts": "^4.3.0",
"@fastify/compress": "^7.0.3",
Expand Down
33 changes: 29 additions & 4 deletions pages/+Wrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
import type React from "react";
import { useEffect, useRef } from "react";

import { fork } from "effector";
import { Provider } from "effector-react";
import { EffectorNext } from "@effector/next";
import { createEvent } from "effector";
import { useUnit } from "effector-react";
import { usePageContext } from "vike-react/usePageContext";

const noop = createEvent();

const Inner = () => {
const { config } = usePageContext();
const clientStartedRef = useRef(false);
const onClientStarted = useUnit(config.pageClientStarted ?? noop);

useEffect(() => {
if (!clientStartedRef.current && "pageClientStarted" in config) {
onClientStarted();
clientStartedRef.current = true;
}
}, []);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}, []);
}, [onClientStarted]);

It is required, when we can switch from page without .pageClientStarted to another with


return <></>;
};

export default function WrapperEffector({ children }: { children: React.ReactNode }) {
const { scopeValues } = usePageContext();
const pageContext = usePageContext();
const { scopeValues } = pageContext;

return <Provider value={fork({ values: scopeValues })}>{children}</Provider>;
return (
<EffectorNext values={scopeValues}>
<Inner />
{children}
</EffectorNext>
);
}
9 changes: 8 additions & 1 deletion pages/example/@id/+Page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { useUnit } from "effector-react";
import { Link } from "~/shared/routing";

import { $id } from "./model";
import { $random } from "../../index/model";
import { $clientId, $id } from "./model";

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

return (
<div>
<h1>Example</h1>
<p>Read parameter from route: {id}</p>
<p>
Client id: {clientId}
random: {random}
</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();
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,
});
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions renderer/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ export default {
pageStarted: {
env: { client: true, server: true },
},
// https://effector.dev/en/api/effector/scope/
scope: {
env: { client: true, server: true },
pageClientStarted: {
env: { client: true, server: false },
},
},

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
19 changes: 10 additions & 9 deletions renderer/+onBeforeRenderClient.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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 && !pageContext.isHydration) {
await allSettled(pageClientStarted, { scope });
pageContext.scopeValues = serialize(scope);
}
}
6 changes: 2 additions & 4 deletions renderer/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import type { EventCallable, Scope } from "effector";
import type { EventCallable } from "effector";

// https://vike.dev/pageContext#typescript
declare global {
namespace Vike {
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