@@ -18,7 +18,88 @@ load and slow client hydration, potentially causing application stutters.
18
18
19
19
## NextJS SSR {#nextjs}
20
20
21
- We've optimized integration into NextJS with a custom [ Document] ( https://nextjs.org/docs/advanced-features/custom-document )
21
+ ### App Router
22
+
23
+ NextJS 14 includes a new way of routing in the '/app' directory. This allows further
24
+ performance improvements, as well as dynamic and nested routing.
25
+
26
+ #### Root Layout
27
+
28
+ Place [ DataProvider] ( https://dataclient.io/docs/api/DataProvider ) in your [ root layout] ( https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts#root-layout-required )
29
+
30
+ ``` tsx title="app/layout.tsx"
31
+ import { DataProvider } from ' @data-client/ssr/nextjs' ;
32
+ import { AsyncBoundary } from ' @data-client/react' ;
33
+
34
+ export default function RootLayout({ children }) {
35
+ return (
36
+ <html >
37
+ <body >
38
+ <DataProvider >
39
+ <header >Title</header >
40
+ <AsyncBoundary >{ children } </AsyncBoundary >
41
+ <footer ></footer >
42
+ </DataProvider >
43
+ </body >
44
+ </html >
45
+ );
46
+ }
47
+ ```
48
+
49
+ #### Client Components
50
+
51
+ To keep your data fresh and performant, you can use client components and [ useSuspense()] ( ../api/useSuspense.md )
52
+
53
+ ``` tsx title="app/todos/page.tsx"
54
+ ' use client' ;
55
+ import { useSuspense } from ' @data-client/react' ;
56
+ import { TodoResource } from ' ../../resources/Todo' ;
57
+
58
+ export default function InteractivePage() {
59
+ const todos = useSuspense (TodoResource .getList );
60
+ return <TodoList todos = { todos } />;
61
+ }
62
+ ```
63
+
64
+ #### Server Components
65
+
66
+ However, if your data never changes, you can slightly decrease the javascript bundle sent, by
67
+ using a server component. Simply ` await ` the endpoint:
68
+
69
+ ``` tsx title="app/todos/page.tsx"
70
+ import { TodoResource } from ' ../../resources/Todo' ;
71
+
72
+ export default async function StaticPage() {
73
+ const todos = await TodoResource .getList ();
74
+ return <TodoList todos = { todos } />;
75
+ }
76
+ ```
77
+
78
+ #### Demo
79
+
80
+ <StackBlitz app =" nextjs " file =" components/todo/TodoList.tsx,app/layout.tsx " view =" both " />
81
+
82
+ #### Class mangling and Entity.key
83
+
84
+ NextJS will rename classes for production builds. Due to this, it's critical to
85
+ define [ Entity.key] ( /rest/api/Entity#key ) as its default implementation is based on
86
+ the class name.
87
+
88
+ ``` ts
89
+ class User extends Entity {
90
+ id = ' ' ;
91
+ username = ' ' ;
92
+
93
+ pk() { return this .id }
94
+
95
+ // highlight-next-line
96
+ static key = ' User' ;
97
+ }
98
+ ```
99
+
100
+ ### Pages Router
101
+
102
+ With NextJS < ; 14, you might be using the pages router. For this we have [ Document] ( https://nextjs.org/docs/advanced-features/custom-document )
22
103
and NextJS specific wrapper for [ App] ( https://nextjs.org/docs/advanced-features/custom-app )
23
104
24
105
<PkgTabs pkgs =" @data-client/ssr @data-client/redux redux " />
@@ -59,11 +140,7 @@ export const getServerSideProps = () => ({ props: {} });
59
140
60
141
:::
61
142
62
- ### Demo
63
-
64
- <StackBlitz app =" nextjs " file =" components/todo/TodoList.tsx,pages/_app.tsx,pages/_document.tsx " view =" both " />
65
-
66
- ### Further customizing Document
143
+ #### Further customizing Document
67
144
68
145
To further customize Document, simply extend from the provided document.
69
146
@@ -107,7 +184,7 @@ export default class MyDocument extends DataClientDocument {
107
184
}
108
185
```
109
186
110
- ### CSP Nonce
187
+ #### CSP Nonce
111
188
112
189
Reactive Data Client Document serializes the store state in a script tag. In case you have
113
190
Content Security Policy restrictions that require use of a nonce, you can override
@@ -129,24 +206,6 @@ export default class MyDocument extends DataClientDocument {
129
206
}
130
207
```
131
208
132
- ### Class mangling and Entity.key
133
-
134
- NextJS will rename classes for production builds. Due to this, it's critical to
135
- define [ Entity.key] ( /rest/api/Entity#key ) as its default implementation is based on
136
- the class name.
137
-
138
- ``` ts
139
- class User extends Entity {
140
- id = ' ' ;
141
- username = ' ' ;
142
-
143
- pk() { return this .id }
144
-
145
- // highlight-next-line
146
- static key = ' User' ;
147
- }
148
- ```
149
-
150
209
## Express JS SSR
151
210
152
211
When implementing your own server using express.
0 commit comments