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(react-server): fix ?import query dual package when client module is used at both boundary and non-boundary #315

Merged
merged 7 commits into from
May 14, 2024
Merged
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
28 changes: 28 additions & 0 deletions packages/react-server/examples/basic/e2e/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,34 @@ test("server compnoent > fixture", async ({ page }) => {
await page.getByText("TestDepServerComponent").click();
});

test("client module used at boundary and non-boundary basic", async ({
page,
}) => {
await page.goto("/test/deps");
await page.getByText("Client2Context [ok]").click();
});

test("client module used at boundary and non-boundary hmr @dev", async ({
page,
}) => {
await page.goto("/test/deps");
await page.getByText("Client2Context [ok]").click();

await waitForHydration(page);

await editFile("./src/routes/test/deps/_client2.tsx", (s) =>
s.replace(`value="ok"`, `value="okok"`),
);
await page.getByText("Client2Context [okok]").click();

// TODO: still dual package after HMR due to
// `<id>?t=...` imported by client component
// `<id>` imported by server component (as client reference)
await page.reload();
await waitForHydration(page);
await page.getByText("Client2Context [not-ok]").click();
});

test("RouteProps.request", async ({ page }) => {
await page.goto("/test/other");
await waitForHydration(page);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use client";

import React from "react";
import { Client2Context } from "./_client2";

export function Client1() {
const context = React.useContext(Client2Context);
return <>Client2Context [{context}]</>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use client";

import React from "react";

// test case to verify this module won't get "dual-package"-ed when
// - imported by server component and
// - imported by client component

// imported by _client1.tsx
export const Client2Context = React.createContext("not-ok");

// imported by page.tsx
export function Client2Provider(props: React.PropsWithChildren) {
return (
<Client2Context.Provider value="ok">
{props.children}
</Client2Context.Provider>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
default as BalancerDefault,
Balancer as BalancerNamed,
} from "react-wrap-balancer";
import { Client1 } from "./_client1";
import { Client2Provider } from "./_client2";

export default function Page() {
return (
Expand All @@ -19,6 +21,11 @@ export default function Page() {
<div>
<TestDepServerComponent />
</div>
<div>
<Client2Provider>
<Client1 />
</Client2Provider>
</div>
<div className="border p-2">
<a
className="text-lg font-bold antd-link"
Expand Down
4 changes: 2 additions & 2 deletions packages/react-server/src/features/use-client/browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { memoize, tinyassert } from "@hiogawa/utils";

async function importWrapper(id: string) {
if (import.meta.env.DEV) {
// transformed to "?import"
return import(/* @vite-ignore */ id);
// `__raw_import` injected via inline script
return (globalThis as any).__raw_import(id);
} else {
const clientReferences = await import(
"virtual:client-references" as string
Expand Down
6 changes: 6 additions & 0 deletions packages/react-server/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ export function vitePluginReactServer(options?: {
tinyassert(match && 1 in match);
let head = match[1];

// expose raw dynamic `import` which doesn't go through vite's transform
// since it would inject `<id>?import` and cause dual packages when
// client code is both imported at the boundary (as `<id>?import`)
// and not at the boundary (as `<id>`).
head += `<script>globalThis.__raw_import = (id) => import(id)</script>\n`;

// serve dev css as ?direct so that ssr html won't get too huge.
// also remove style on first hot update.
head += `\
Expand Down