-
-
Notifications
You must be signed in to change notification settings - Fork 71
impr: React prerendering test app #2597
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
Open
cieplypolar
wants to merge
10
commits into
fix/unplugin-babel-exports
Choose a base branch
from
impr/waku-app
base: fix/unplugin-babel-exports
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
92b05b4
waku experiment
cieplypolar 31d0cc1
rename
cieplypolar 5e25cf8
pnpm lock fix
cieplypolar 5180fc1
oxfmt
cieplypolar c73cb0e
reproduce nextjs prerender
cieplypolar 76ca08a
no ssr
cieplypolar 68f9ae2
rewrite to nextjs
cieplypolar 2886554
next_public prefix
cieplypolar cb2fbf3
cleanup
cieplypolar 940daa0
review changes
cieplypolar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| .next | ||
| next-env.d.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # React Prerendering Test | ||
|
|
||
| Minimal Next.js app for testing how `@typegpu/react` behaves during prerendering. | ||
|
|
||
| ## Usage | ||
|
|
||
| `pnpm check-ssr` runs build with both SSR enabled and disabled | ||
|
|
||
| ## Dev | ||
|
|
||
| SSR is disabled with the `NEXT_PUBLIC_DISABLE_SSR` env var. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import type { ReactNode } from 'react'; | ||
| import type { Metadata } from 'next'; | ||
|
|
||
| export const metadata: Metadata = { | ||
| title: 'React Prerendering Test', | ||
| }; | ||
|
|
||
| export default function RootLayout({ children }: { children: ReactNode }) { | ||
| return ( | ||
| <html lang="en"> | ||
| <body | ||
| style={{ | ||
| margin: 0, | ||
| minHeight: '100vh', | ||
| display: 'grid', | ||
| placeItems: 'center', | ||
| background: '#171724', | ||
| }} | ||
| > | ||
| {children} | ||
| </body> | ||
| </html> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| 'use client'; | ||
|
|
||
| import dynamic from 'next/dynamic'; | ||
| import Shader from '../components/Shader.tsx'; | ||
|
|
||
| const ShaderNoSSR = dynamic(() => import('../components/Shader.tsx'), { | ||
| ssr: false, | ||
| }); | ||
|
|
||
| export default function Page() { | ||
| return process.env.NEXT_PUBLIC_DISABLE_SSR ? <ShaderNoSSR /> : <Shader />; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| module.exports = (api) => { | ||
| api.cache(true); | ||
| return { | ||
| presets: ['next/babel'], | ||
| plugins: ['./node_modules/unplugin-typegpu/dist/babel.js'], // cannot parse .ts file | ||
| }; | ||
|
cieplypolar marked this conversation as resolved.
|
||
| }; | ||
|
cieplypolar marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { spawnSync } from 'node:child_process'; | ||
|
|
||
| type Env = Record<string, string>; | ||
|
|
||
| function build(env: Env) { | ||
| const { status, stdout, stderr } = spawnSync('pnpm', ['next', 'build'], { | ||
| cwd: import.meta.dirname, | ||
| env: { ...process.env, ...env }, | ||
| encoding: 'utf8', | ||
| }); | ||
| return { failed: status !== 0, output: `stdout:\n${stdout}\nstderr:\n${stderr}` }; | ||
| } | ||
|
|
||
| console.log('Building with SSR...'); | ||
| const ssr = build({}); | ||
| console.log(ssr.failed ? '\n' + ssr.output : 'OK'); | ||
|
|
||
| console.log('========================================\n'); | ||
|
|
||
| console.log('Building with SSR disabled...'); | ||
| const noSsr = build({ NEXT_PUBLIC_DISABLE_SSR: '1' }); | ||
| console.log(noSsr.failed ? noSsr.output : 'OK'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| 'use client'; | ||
|
|
||
| import { useConfigureContext, useFrame, useRoot, useUniform } from '@typegpu/react'; | ||
| import { useMemo } from 'react'; | ||
| import tgpu, { common, d, std } from 'typegpu'; | ||
| import { perlin2d } from '@typegpu/noise'; | ||
| import { hexToOklab, oklabToRgb } from '@typegpu/color'; | ||
|
|
||
| export default function Shader() { | ||
| const root = useRoot(); | ||
| const time = useUniform(d.f32); | ||
|
|
||
| function noise(v: d.v2f) { | ||
|
cieplypolar marked this conversation as resolved.
Outdated
|
||
| 'use gpu'; | ||
| return perlin2d.sample(v); | ||
| } | ||
|
|
||
| const octavesAccessor = tgpu.accessor(d.u32); | ||
| const standardizeNoiseAccessor = tgpu.accessor(d.bool); | ||
| const getMaxValue = tgpu.comptime((octaves: number) => 1 - 2 ** -octaves); | ||
| const rotation = d.mat2x2f(0.8, 0.6, -0.6, 0.8); | ||
|
|
||
| function fbm(v: d.v2f) { | ||
| 'use gpu'; | ||
| let u = d.vec2f(v); | ||
| let f = d.f32(); | ||
|
|
||
| // first octave | ||
| { | ||
| let sample = noise(u + time.$); | ||
| if (standardizeNoiseAccessor.$) sample = sample * 0.5 + 0.5; | ||
| f += 0.5 * sample; | ||
| u = rotation * u * 2.01; | ||
| } | ||
|
|
||
| for (const i of tgpu.unroll(std.range(2, octavesAccessor.$))) { | ||
| let sample = noise(u); | ||
| if (standardizeNoiseAccessor.$) sample = sample * 0.5 + 0.5; | ||
| f += 0.5 ** i * sample; | ||
| u = rotation * u * (2 + i / 100); | ||
| } | ||
|
|
||
| // last octave | ||
| { | ||
| let sample = noise(u + std.sin(time.$)); | ||
| if (standardizeNoiseAccessor.$) sample = sample * 0.5 + 0.5; | ||
| f += 0.5 ** d.f32(octavesAccessor.$) * sample; | ||
| u = rotation * u * 2.01; | ||
| } | ||
|
|
||
| return f / getMaxValue(octavesAccessor.$); | ||
| } | ||
|
|
||
| const fbm4 = tgpu.fn(fbm).with(octavesAccessor, 4).with(standardizeNoiseAccessor, false); | ||
| const fbm6 = tgpu.fn(fbm).with(octavesAccessor, 6).with(standardizeNoiseAccessor, true); | ||
|
|
||
| function domainWarp(v: d.v2f) { | ||
| 'use gpu'; | ||
| return fbm6(v + fbm4(v + fbm4(v))); | ||
| } | ||
|
|
||
| function palette(t: number) { | ||
| 'use gpu'; | ||
| const purple = hexToOklab('#c04bf2'); | ||
| const blue = hexToOklab('#4e65f6'); | ||
| const dark = hexToOklab('#0f092b'); | ||
|
|
||
| const factor1 = std.smoothstep(0.25, 0.5, t); | ||
| const factor2 = std.smoothstep(0.5, 0.7, t); | ||
|
|
||
| const mixed = std.mix(std.mix(dark, blue, factor1), purple, factor2); | ||
| return oklabToRgb(mixed); | ||
| } | ||
|
|
||
| const renderPipeline = useMemo( | ||
| () => | ||
| root.createRenderPipeline({ | ||
| vertex: common.fullScreenTriangle, | ||
| fragment: ({ uv }) => { | ||
| 'use gpu'; | ||
| const centeredUV = (2 * uv - 1) * 4; | ||
|
|
||
| const sample = domainWarp(centeredUV); | ||
| const sampleRight = domainWarp(centeredUV + d.vec2f(0.04, 0)); | ||
| const sampleDown = domainWarp(centeredUV + d.vec2f(0, 0.04)); | ||
|
|
||
| const dx = d.vec3f(0.04, 0, (sampleRight - sample) * 0.4); | ||
| const dy = d.vec3f(0, 0.04, (sampleDown - sample) * 0.4); | ||
| const normal = std.normalize(std.cross(dx, dy)); | ||
|
|
||
| const lightDir = std.normalize(d.vec3f(0.0, -1.0, 1.0)); | ||
| const diffuse = std.max(0.0, std.dot(normal, lightDir)); | ||
|
|
||
| const baseColor = palette(sample); | ||
|
|
||
| const litColor = baseColor * diffuse; | ||
|
|
||
| return d.vec4f(litColor, 1); | ||
| }, | ||
| }), | ||
| [root, time], | ||
| ); | ||
|
|
||
| const { ref, ctxRef } = useConfigureContext({ autoResize: true, alphaMode: 'premultiplied' }); | ||
|
|
||
| useFrame(({ elapsedSeconds }) => { | ||
| if (!ctxRef.current) return; | ||
|
|
||
| time.write(elapsedSeconds * 0.5); | ||
| renderPipeline.withColorAttachment({ view: ctxRef.current }).draw(3); | ||
| }); | ||
|
|
||
| return <canvas ref={ref} style={{ display: 'block', width: '80vmin', height: '80vmin' }} />; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import type { NextConfig } from 'next'; | ||
|
|
||
| const nextConfig: NextConfig = { | ||
| output: 'export', | ||
| distDir: './dist', | ||
| }; | ||
|
|
||
| export default nextConfig; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| { | ||
| "name": "react-prerendering-test", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "scripts": { | ||
| "dev": "next dev", | ||
| "build": "next build", | ||
| "start": "next start", | ||
| "dev:no-ssr": "NEXT_PUBLIC_DISABLE_SSR=1 next dev", | ||
| "build:no-ssr": "NEXT_PUBLIC_DISABLE_SSR=1 next build", | ||
| "check-ssr": "node checkSsr.mts" | ||
|
cieplypolar marked this conversation as resolved.
|
||
| }, | ||
| "dependencies": { | ||
| "@typegpu/color": "workspace:*", | ||
| "@typegpu/noise": "workspace:*", | ||
| "@typegpu/react": "workspace:*", | ||
| "next": "^16.2.7", | ||
| "react": "catalog:", | ||
| "react-dom": "catalog:", | ||
| "typegpu": "workspace:*" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "catalog:types", | ||
| "@types/react": "^19.2.15", | ||
| "@types/react-dom": "^19.2.3", | ||
| "@webgpu/types": "catalog:types", | ||
| "typescript": "catalog:types", | ||
| "unplugin-typegpu": "workspace:*" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ESNext", | ||
| "useDefineForClassFields": true, | ||
| "lib": ["ESNext", "DOM", "DOM.Iterable"], | ||
| "module": "ESNext", | ||
| "esModuleInterop": true, | ||
| "skipLibCheck": true, | ||
| "moduleResolution": "bundler", | ||
| "moduleDetection": "force", | ||
| "allowImportingTsExtensions": true, | ||
| "resolveJsonModule": true, | ||
| "isolatedModules": true, | ||
| "noEmit": true, | ||
| "jsx": "react-jsx", | ||
| "strict": true, | ||
| "allowJs": true, | ||
| "incremental": true, | ||
| "plugins": [ | ||
| { | ||
| "name": "next" | ||
| } | ||
| ], | ||
| "types": ["@webgpu/types", "@types/react", "@types/react-dom", "@types/node"] | ||
| }, | ||
| "include": [ | ||
| "next-env.d.ts", | ||
| "**/*.ts", | ||
| "**/*.tsx", | ||
| ".next/types/**/*.ts", | ||
| ".next/dev/types/**/*.ts", | ||
| "**/*.mts", | ||
| "dist/types/**/*.ts", | ||
| "dist/dev/types/**/*.ts" | ||
| ], | ||
| "exclude": ["node_modules"] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.