Skip to content

Commit 00cd58d

Browse files
committed
Add info about handling status codes on server
1 parent 7dc9cd8 commit 00cd58d

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

versioned_docs/version-5.x/configuring-links.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,17 @@ For example:
202202
Settings: 'settings',
203203
},
204204
},
205-
404: '*',
205+
NotFound: '*',
206206
}
207207
```
208208

209-
Here, we have defined a route named `404` and set it to match `*` aka everything. If the path didn't match `user/:id` or `settings`, it'll be matched by this route.
209+
Here, we have defined a route named `NotFound` and set it to match `*` aka everything. If the path didn't match `user/:id` or `settings`, it'll be matched by this route.
210210

211211
So, a path like `/library` or `/settings/notification` will resolve to the following state object:
212212

213213
```js
214214
const state = {
215-
routes: [{ name: '404' }],
215+
routes: [{ name: 'NotFound' }],
216216
};
217217
```
218218

@@ -232,7 +232,7 @@ You can even go more specific, for example, say if you want to show a different
232232
},
233233
},
234234
},
235-
404: '*',
235+
NotFound: '*',
236236
}
237237
```
238238

@@ -260,6 +260,8 @@ const state = {
260260
};
261261
```
262262

263+
When doing server rendering, you'd also want to return correct status code for 404 errors. See [server rendering docs](server-rendering.md#handling-404-or-other-status-codes) for a guide on how to handle it.
264+
263265
## Rendering an initial route
264266

265267
Sometimes you want to ensure that a certain screen will always be present as the first screen in the navigator's state. You can use the `initialRouteName` property to specify the screen to use for the initial screen.

versioned_docs/version-5.x/server-rendering.md

+57
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,63 @@ Make sure that you have specified a `title` option in your screens:
144144
/>
145145
```
146146
147+
## Handling 404 or other status codes
148+
149+
When [rendering a screen for an invalid URL](configuring-links.md#handling-unmatched-routes-or-404), we should also return a `404` status code from the server.
150+
151+
First, we need to create a context where we'll attach the status code. To do this, place the following code in a separate file that we will be importing on both the server and client:
152+
153+
```js
154+
import * as React from 'react';
155+
156+
const StatusCodeContext = React.createContext();
157+
158+
export default StatusCodeContext;
159+
```
160+
161+
Then, we need to use the context in our `NotFound` screen. Here, we add a `code` property with the value of `404` to signal that the screen was not found:
162+
163+
```js
164+
function NotFound() {
165+
const status = React.useContext(StatusCodeContext);
166+
167+
if (status) {
168+
staus.code = 404;
169+
}
170+
171+
return (
172+
<View>
173+
<Text>Oops! This URL doesn't exist.</Text>
174+
</View>
175+
);
176+
}
177+
```
178+
179+
You could also attach additional information in this object if you need to.
180+
181+
Next, we need to create a status object to pass in the context on our server. By default, we'll set the `code` to `200`. Then pass the object in `StatusCodeContext.Provider` which should wrap the element with `ServerContainer`:
182+
183+
```js
184+
// Create a status object
185+
const status = { code: 200 };
186+
187+
const html = ReactDOMServer.renderToString(
188+
// Pass the status object via context
189+
<StatusCodeContext.Provider value={status}>
190+
<ServerContainer ref={ref} location={location}>
191+
{element}
192+
</ServerContainer>
193+
</StatusCodeContext.Provider>
194+
);
195+
196+
// After rendering, get the status code and use it for server's response
197+
ctx.status = status.code;
198+
```
199+
200+
After we render the app with `ReactDOMServer.renderToString`, the `code` property of the `status` object will be updated to be `404` if the `NotFound` screen was rendered.
201+
202+
You can follow a similar approach for other status codes too.
203+
147204
## Summary
148205

149206
- Use the `location` prop on `ServerContainer` to render correct screens based on the incoming request.

0 commit comments

Comments
 (0)