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
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>
);
}
11 changes: 6 additions & 5 deletions pages/example/@id/+Page.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +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"));
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: <Component fallback="loading..." />
Client id: {clientId}
random: {random}
</p>
<Link href="/">Go home</Link>
</div>
Expand Down
12 changes: 0 additions & 12 deletions pages/example/@id/ClientComponent.tsx

This file was deleted.

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.

4 changes: 0 additions & 4 deletions renderer/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ export default {
pageClientStarted: {
env: { client: true, server: false },
},
// https://effector.dev/en/api/effector/scope/
scope: {
env: { client: true, server: true },
},
},

// https://vike.dev/extends
Expand Down
5 changes: 2 additions & 3 deletions renderer/+onBeforeRenderClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ export async function onBeforeRenderClient(pageContext: Vike.PageContext) {

const pageClientStarted = pageContext.config.pageClientStarted;

if (pageClientStarted) {
if (pageClientStarted && !pageContext.isHydration) {
await allSettled(pageClientStarted, { scope });
pageContext.scopeValues = serialize(scope);
}

pageContext.scopeValues = serialize(scope);
}