-
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add example of dynamic layout with static page (#922)
you can see the time change as navigating the site which shows that the layout is indeed dynamic The rest of the home page stays unchanged and can be cached Example for #885 --------- Co-authored-by: Tyler <[email protected]>
- Loading branch information
1 parent
c946a30
commit 5e183b8
Showing
13 changed files
with
338 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,23 @@ | ||
{ | ||
"name": "waku-example", | ||
"version": "0.1.0", | ||
"type": "module", | ||
"private": true, | ||
"scripts": { | ||
"dev": "waku dev", | ||
"build": "waku build", | ||
"start": "waku start" | ||
}, | ||
"dependencies": { | ||
"react": "19.0.0-rc-d6cb4e77-20240911", | ||
"react-dom": "19.0.0-rc-d6cb4e77-20240911", | ||
"react-server-dom-webpack": "19.0.0-rc-d6cb4e77-20240911", | ||
"waku": "0.21.2" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "18.3.5", | ||
"@types/react-dom": "18.3.0", | ||
"server-only": "0.0.1", | ||
"typescript": "5.6.2" | ||
} | ||
} |
This file contains 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,64 @@ | ||
import type { ReactNode } from 'react'; | ||
|
||
import { Link } from 'waku/router/client'; | ||
|
||
import '../styles.css'; | ||
|
||
const Pending = ({ isPending }: { isPending: boolean }) => ( | ||
<span | ||
style={{ | ||
marginLeft: 5, | ||
transition: 'opacity 75ms 100ms', | ||
opacity: isPending ? 1 : 0, | ||
}} | ||
> | ||
Pending... | ||
</span> | ||
); | ||
|
||
const BarLayout = ({ children }: { children: ReactNode }) => { | ||
return ( | ||
<html> | ||
<head> | ||
<title>Waku</title> | ||
</head> | ||
<body> | ||
<div> | ||
<p>This Layout is expected to be static</p> | ||
<ul> | ||
<li> | ||
<Link | ||
to="/" | ||
pending={<Pending isPending />} | ||
notPending={<Pending isPending={false} />} | ||
> | ||
Home | ||
</Link> | ||
</li> | ||
<li> | ||
<Link | ||
to="/foo" | ||
pending={<Pending isPending />} | ||
notPending={<Pending isPending={false} />} | ||
> | ||
Foo | ||
</Link> | ||
</li> | ||
<li> | ||
<Link | ||
to="/nested/bar" | ||
pending={<Pending isPending />} | ||
notPending={<Pending isPending={false} />} | ||
> | ||
Nested / Bar | ||
</Link> | ||
</li> | ||
</ul> | ||
{children} | ||
</div> | ||
</body> | ||
</html> | ||
); | ||
}; | ||
|
||
export default BarLayout; |
This file contains 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,10 @@ | ||
import { Counter } from './Counter'; | ||
|
||
const Bar = () => ( | ||
<div> | ||
<h2>Bar</h2> | ||
<Counter /> | ||
</div> | ||
); | ||
|
||
export default Bar; |
This file contains 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,19 @@ | ||
'use client'; | ||
|
||
import { useState } from 'react'; | ||
|
||
import { Link, useRouter_UNSTABLE as useRouter } from 'waku/router/client'; | ||
|
||
export const Counter = () => { | ||
const { path } = useRouter(); | ||
const [count, setCount] = useState(0); | ||
return ( | ||
<div style={{ border: '3px blue dashed', margin: '1em', padding: '1em' }}> | ||
<p>Count: {count}</p> | ||
<button onClick={() => setCount((c) => c + 1)}>Increment</button> | ||
<h3>This is a client component.</h3> | ||
<span>path: {path}</span> | ||
<Link to="/">Go to Home</Link> | ||
</div> | ||
); | ||
}; |
This file contains 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,16 @@ | ||
import type { ReactNode } from 'react'; | ||
|
||
const FooLayout = ({ children }: { children: ReactNode }) => { | ||
return ( | ||
<html> | ||
<body> | ||
<div> | ||
<h1>FOO layout</h1> | ||
{children} | ||
</div> | ||
</body> | ||
</html> | ||
); | ||
}; | ||
|
||
export default FooLayout; |
This file contains 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 @@ | ||
import 'server-only'; | ||
|
||
import { Counter } from './Counter'; | ||
|
||
const Foo = () => ( | ||
<div> | ||
<h2>Foo</h2> | ||
<Counter /> | ||
</div> | ||
); | ||
|
||
export default Foo; |
This file contains 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,68 @@ | ||
import type { ReactNode } from 'react'; | ||
|
||
import { Link } from 'waku/router/client'; | ||
|
||
import '../styles.css'; | ||
|
||
const Pending = ({ isPending }: { isPending: boolean }) => ( | ||
<span | ||
style={{ | ||
marginLeft: 5, | ||
transition: 'opacity 75ms 100ms', | ||
opacity: isPending ? 1 : 0, | ||
}} | ||
> | ||
Pending... | ||
</span> | ||
); | ||
|
||
const getCurrentTime = () => new Date(); | ||
|
||
const HomeLayout = ({ children }: { children: ReactNode }) => { | ||
const currentTime = getCurrentTime(); | ||
return ( | ||
<html> | ||
<head> | ||
<title>Waku</title> | ||
</head> | ||
<body> | ||
<div> | ||
<h1>Home layout</h1> | ||
<p>Last render time: {currentTime.toISOString()}</p> | ||
<ul> | ||
<li> | ||
<Link | ||
to="/" | ||
pending={<Pending isPending />} | ||
notPending={<Pending isPending={false} />} | ||
> | ||
Home | ||
</Link> | ||
</li> | ||
<li> | ||
<Link | ||
to="/foo" | ||
pending={<Pending isPending />} | ||
notPending={<Pending isPending={false} />} | ||
> | ||
Foo | ||
</Link> | ||
</li> | ||
<li> | ||
<Link | ||
to="/nested/bar" | ||
pending={<Pending isPending />} | ||
notPending={<Pending isPending={false} />} | ||
> | ||
Nested / Bar | ||
</Link> | ||
</li> | ||
</ul> | ||
{children} | ||
</div> | ||
</body> | ||
</html> | ||
); | ||
}; | ||
|
||
export default HomeLayout; |
This file contains 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 @@ | ||
const Home = () => ( | ||
<div> | ||
<h2>Home</h2> | ||
<p>This is the home page.</p> | ||
</div> | ||
); | ||
|
||
export default Home; |
This file contains 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,57 @@ | ||
import { lazy } from 'react'; | ||
import { createPages } from 'waku'; | ||
import type { PathsForPages } from 'waku/router'; | ||
import FooPage from './components/FooPage'; | ||
import BarPage from './components/BarPage'; | ||
import BarLayout from './components/BarLayout'; | ||
import FooLayout from './components/FooLayout'; | ||
|
||
// The use of `lazy` is optional and you can use import statements too. | ||
const HomeLayout = lazy(() => import('./components/HomeLayout')); | ||
const HomePage = lazy(() => import('./components/HomePage')); | ||
|
||
const pages = createPages(async ({ createPage, createLayout }) => [ | ||
createLayout({ | ||
render: 'static', | ||
path: '/', | ||
component: HomeLayout, | ||
}), | ||
|
||
createPage({ | ||
render: 'static', | ||
path: '/', | ||
component: HomePage, | ||
}), | ||
|
||
createLayout({ | ||
render: 'static', | ||
path: '/foo', | ||
component: FooLayout, | ||
}), | ||
|
||
createPage({ | ||
render: 'dynamic', | ||
path: '/foo', | ||
component: FooPage, | ||
}), | ||
|
||
createLayout({ | ||
render: 'dynamic', | ||
path: '/bar', | ||
component: BarLayout, | ||
}), | ||
|
||
createPage({ | ||
render: 'static', | ||
path: '/bar', | ||
component: BarPage, | ||
}), | ||
]); | ||
|
||
declare module 'waku/router' { | ||
interface RouteConfig { | ||
paths: PathsForPages<typeof pages>; | ||
} | ||
} | ||
|
||
export default pages; |
This file contains 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,15 @@ | ||
import { StrictMode } from 'react'; | ||
import { createRoot, hydrateRoot } from 'react-dom/client'; | ||
import { Router } from 'waku/router/client'; | ||
|
||
const rootElement = ( | ||
<StrictMode> | ||
<Router /> | ||
</StrictMode> | ||
); | ||
|
||
if ((globalThis as any).__WAKU_HYDRATE__) { | ||
hydrateRoot(document, rootElement); | ||
} else { | ||
createRoot(document as any).render(rootElement); | ||
} |
This file contains 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,3 @@ | ||
body { | ||
background-color: #fefefe; | ||
} |
This file contains 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,15 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"target": "esnext", | ||
"downlevelIteration": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "bundler", | ||
"skipLibCheck": true, | ||
"noUncheckedIndexedAccess": true, | ||
"exactOptionalPropertyTypes": true, | ||
"types": ["react/experimental"], | ||
"jsx": "react-jsx" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.