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

fix: edge case when rsa depends on another rsa #785

Merged
merged 19 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 16 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
21 changes: 21 additions & 0 deletions e2e/fixtures/rsc-basic/modules/ai/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "ai",
"type": "module",
"version": "1.0.0",
"description": "Vercel AI mockup",
"exports": {
"./rsc": {
"types": "./src/index.d.ts",
"react-server": "./src/server.js",
"import": "./src/client.js"
}
},
"devDependencies": {
"react-dom": "^18",
"react-server-dom-webpack": "18.3.0-canary-eb33bd747-20240312"
},
"peerDependencies": {
"react": "^18 || ^19"
},
"private": true
}
8 changes: 8 additions & 0 deletions e2e/fixtures/rsc-basic/modules/ai/src/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use client';
import { useActions } from './shared.js';

export { useActions };

export function createAI() {
throw new Error('You should not call createAI in the client side');
}
7 changes: 7 additions & 0 deletions e2e/fixtures/rsc-basic/modules/ai/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { ReactNode } from 'react';

declare function createAI(
actions: Record<string, (...args: any[]) => any>,
): (props: { children: ReactNode }) => ReactNode;

declare function useActions(): Record<string, any>;
25 changes: 25 additions & 0 deletions e2e/fixtures/rsc-basic/modules/ai/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use server';
import { InternalProvider } from './shared.js';
import { jsx } from 'react/jsx-runtime';

async function innerAction({ action }, state, ...args) {
'use server';
return action(...args);
}

function wrapAction(action, options) {
return innerAction.bind(null, { action, options });
}

export function createAI(actions) {
const wrappedActions = {};
for (const name in actions) {
wrappedActions[name] = wrapAction(actions[name]);
}
return function AI(props) {
return jsx(InternalProvider, {
actions: wrappedActions,
children: props.children,
});
};
}
19 changes: 19 additions & 0 deletions e2e/fixtures/rsc-basic/modules/ai/src/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use client';
import { createContext, useContext } from 'react';
import { jsx } from 'react/jsx-runtime';

const ActionContext = createContext(null);

export function useActions() {
return useContext(ActionContext);
}

export function InternalProvider(props) {
return jsx('div', {
'data-testid': 'ai-internal-provider',
children: jsx(ActionContext.Provider, {
value: props.actions,
children: props.children,
}),
});
}
1 change: 1 addition & 0 deletions e2e/fixtures/rsc-basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"start": "waku start"
},
"dependencies": {
"ai": "link:./modules/ai",
"react": "19.0.0-rc-3da26163a3-20240704",
"react-dom": "19.0.0-rc-3da26163a3-20240704",
"react-server-dom-webpack": "19.0.0-rc-3da26163a3-20240704",
Expand Down
5 changes: 5 additions & 0 deletions e2e/fixtures/rsc-basic/src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import { ClientCounter } from './ClientCounter.js';
import { ServerPing } from './ServerPing/index.js';
import { ServerBox } from './Box.js';
import { ServerProvider } from './ServerAction/Server.js';
import { ClientActionsConsumer } from './ServerAction/Client.js';

const App = ({ name }: { name: string }) => {
return (
Expand All @@ -12,6 +14,9 @@ const App = ({ name }: { name: string }) => {
<p data-testid="app-name">{name}</p>
<ClientCounter />
<ServerPing />
<ServerProvider>
<ClientActionsConsumer />
</ServerProvider>
</ServerBox>
);
};
Expand Down
14 changes: 14 additions & 0 deletions e2e/fixtures/rsc-basic/src/components/ServerAction/Client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use client';

import { useActions } from 'ai/rsc';
import { useEffect } from 'react';

export const ClientActionsConsumer = () => {
const actions = useActions();
useEffect(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
globalThis.actions = actions;
}, [actions]);
return <div>globalThis.actions: {JSON.stringify(Object.keys(actions))}</div>;
};
13 changes: 13 additions & 0 deletions e2e/fixtures/rsc-basic/src/components/ServerAction/Server.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { ReactNode } from 'react';
import { createAI } from 'ai/rsc';

const AI = createAI({
foo: async () => {
'use server';
return 0;
},
});

export function ServerProvider({ children }: { children: ReactNode }) {
return <AI>{children}</AI>;
}
13 changes: 13 additions & 0 deletions e2e/rsc-basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,18 @@ for (const { build, command } of commands) {
page.getByTestId('server-ping').getByTestId('counter'),
).toHaveText('2');
});

test('server action', async ({ page }) => {
await page.goto(`http://localhost:${port}/`);
await expect(page.getByTestId('app-name')).toHaveText('Waku');
await expect(page.getByTestId('ai-internal-provider')).toHaveText(
'globalThis.actions: ["foo"]',
);
const result = await page.evaluate(() => {
// @ts-expect-error no types
return globalThis.actions.foo();
});
expect(result).toBe(0);
});
});
}
1 change: 1 addition & 0 deletions packages/waku/src/lib/hono/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const runner = (options: MiddlewareOptions): MiddlewareHandler => {
.map(async (middleware) => (await middleware).default(options)),
),
);

return async (c, next) => {
const ctx: HandlerContext = {
req: {
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/lib/plugins/vite-plugin-rsc-rsdw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ globalThis.__WAKU_${type}_CHUNK_LOAD__ ||= (
if (!globalThis.__WAKU_${type}_MODULE_LOADING__.has(id)) {
globalThis.__WAKU_${type}_MODULE_LOADING__.set(
id,
customImport(id).then((m) => {
(customImport ? customImport(id) : import(id)).then((m) => {
Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

I think this won't work because node condition is not including react-server

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

that's why you need a vite server to load the module

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

and import() doesn't support .ts(x) file in dev side

Copy link
Owner

Choose a reason for hiding this comment

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

Okay, but it should work on PRD, right?

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

I think so, but ID is not correct; it is now like /path/to/file which is presented as HTTP resource

globalThis.__WAKU_${type}_MODULE_CACHE__.set(id, m);
})
);
Expand Down
4 changes: 2 additions & 2 deletions packages/waku/src/lib/renderers/rsc-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ export async function renderRsc(
) {
// XXX This doesn't support streaming unlike busboy
const formData = parseFormData(bodyStr, contentType);
args = await decodeReply(formData);
args = await decodeReply(formData, bundlerConfig);
Copy link
Owner

Choose a reason for hiding this comment

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

I actually wonder why this works. This is basically resolving client entry. Can it be used for server entry?

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

I think it's also resolving the server entry, yes

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

Copy link
Owner

Choose a reason for hiding this comment

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

In bundlerConfig, we use resolveClientEntry, so even if it works, it's confusing.

} else if (bodyStr) {
args = await decodeReply(bodyStr);
args = await decodeReply(bodyStr, bundlerConfig);
}
const [fileId, name] = rsfId.split('#') as [string, string];
let mod: any;
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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