Update with-webassembly example to fix hydration error (#62807)

## What?

This example hasn't been touched since I first added it, updates it to
use App Router and leverage server components, much simpler
implementation 😌

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-2679
This commit is contained in:
Tim Neutkens 2024-03-04 14:16:35 +01:00 committed by GitHub
parent dd98e7740d
commit 04bda302e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 42 additions and 48 deletions

View file

@ -0,0 +1 @@
export function add_one(number: Number): Number;

View file

@ -1,16 +1,16 @@
import type { AddModuleExports } from "../../wasm";
import type * as addWasmModule from "../../../add.wasm";
// @ts-ignore
import addWasm from "../../add.wasm?module";
const module$ = WebAssembly.instantiate(addWasm);
export default async function handler() {
export async function GET() {
const instance = (await module$) as any;
const exports = instance.exports as AddModuleExports;
const exports = instance.exports as typeof addWasmModule;
const { add_one: addOne } = exports;
const number = addOne(10);
return new Response(`got: ${number}`);
}
export const config = { runtime: "edge" };
export const runtime = "edge";

View file

@ -0,0 +1,11 @@
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html>
<body>{children}</body>
</html>
);
}

View file

@ -0,0 +1,18 @@
import Link from "next/link";
import { RustServerComponent } from "../components/RustComponent";
export default function Page({
searchParams,
}: {
searchParams: { [key: string]: string };
}) {
const number = parseInt(searchParams.number || "30");
return (
<div>
<RustServerComponent number={number} />
<div>
<Link href={`/?number=${number + 1}`}>+</Link>
</div>
</div>
);
}

View file

@ -1,24 +1,6 @@
import type { AddModuleExports } from "../wasm";
import dynamic from "next/dynamic";
export async function RustServerComponent({ number }: { number: number }) {
const exports = await import("../add.wasm");
const { add_one: addOne } = exports;
interface RustComponentProps {
number: Number;
return <>{addOne(number)}</>;
}
const RustComponent = dynamic({
loader: async () => {
// Import the wasm module
// @ts-ignore
const exports = (await import("../add.wasm")) as AddModuleExports;
const { add_one: addOne } = exports;
// Return a React component that calls the add_one method on the wasm module
return ({ number }: RustComponentProps) => (
<div>
<>{addOne(number)}</>
</div>
);
},
});
export default RustComponent;

View file

@ -12,9 +12,9 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "^18.11.10",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.9",
"typescript": "^4.9.3"
"@types/node": "^20.11.24",
"@types/react": "^18.2.61",
"@types/react-dom": "^18.2.19",
"typescript": "^5.3.3"
}
}

View file

@ -1,15 +0,0 @@
import { useRouter } from "next/router";
import Link from "next/link";
import RustComponent from "../components/RustComponent";
export default function Page() {
const { query } = useRouter();
const number = parseInt(query.number as string) || 30;
return (
<div>
<RustComponent number={number} />
<Link href={`/?number=${number + 1}`}>+</Link>
</div>
);
}

View file

@ -1,3 +0,0 @@
export interface AddModuleExports {
add_one(number: Number): Number;
}