Skip to content

Commit 9a32590

Browse files
sidharthachatterjeegatsbybotLekoArtsmoonmeister
authored
fix(gatsby): Ensure status returned by getServerData is respected (#33914)
* Fix documentation and starter for use of status in getServerData * Use status from getServerData in develop and serve * Add missing handling for HTML in serve * chore: format * Update server-side-rendering.md * Update docs/docs/reference/rendering-options/server-side-rendering.md Co-authored-by: Alex Moon <[email protected]> Co-authored-by: gatsbybot <[email protected]> Co-authored-by: Lennart <[email protected]> Co-authored-by: Alex Moon <[email protected]>
1 parent 2ca29ec commit 9a32590

File tree

7 files changed

+27
-13
lines changed

7 files changed

+27
-13
lines changed

docs/docs/how-to/rendering-options/using-server-side-rendering.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ export async function getServerData() {
6565
}
6666
} catch (error) {
6767
return {
68-
headers: {
69-
status: 500,
70-
},
68+
status: 500,
69+
headers: {},
7170
props: {}
7271
}
7372
}
@@ -104,9 +103,8 @@ export async function getServerData() {
104103
}
105104
} catch (error) {
106105
return {
107-
headers: {
108-
status: 500,
109-
},
106+
status: 500,
107+
headers: {},
110108
props: {}
111109
}
112110
}

docs/docs/reference/rendering-options/server-side-rendering.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ The `context` parameter is an object with the following keys:
3434
- `query`: An object representing the query string
3535
- `params`: If you use [File System Route API](/docs/reference/routing/file-system-route-api/) the URL path gets passed in as `params`. For example, if your page is at `src/pages/{Product.name}.js` the `params` will be an object like `{ name: 'value' }`.
3636

37-
`getServerData` can return an object with two keys:
37+
`getServerData` can return an object with several possible keys:
3838

39+
- `status` (optional): The HTTP status code that should be returned. Should be a [valid HTTP status](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) code.
3940
- `props` (optional): Object containing the data passed to `serverData` page prop. Should be a serializable object.
4041
- `headers` (optional): Object containing `headers` that are sent to the browser and caching proxies/CDNs (e.g., cache headers).
4142

packages/gatsby/src/commands/serve.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,11 @@ module.exports = async (program: IServeProgram): Promise<void> => {
267267
}
268268
}
269269

270-
return void res.send(results)
270+
if (page.mode === `SSR` && data.serverDataStatus) {
271+
return void res.status(data.serverDataStatus).send(results)
272+
} else {
273+
return void res.send(results)
274+
}
271275
} catch (e) {
272276
report.error(
273277
`Generating page-data for "${requestedPagePath}" / "${potentialPagePath}" failed.`,
@@ -313,7 +317,11 @@ module.exports = async (program: IServeProgram): Promise<void> => {
313317
}
314318
}
315319

316-
return res.send(results)
320+
if (page.mode === `SSR` && data.serverDataStatus) {
321+
return void res.status(data.serverDataStatus).send(results)
322+
} else {
323+
return void res.send(results)
324+
}
317325
} catch (e) {
318326
report.error(
319327
`Rendering html for "${potentialPagePath}" failed.`,

packages/gatsby/src/utils/get-server-data.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { match } from "@gatsbyjs/reach-router/lib/utils"
66
export interface IServerData {
77
headers?: Record<string, string>
88
props?: Record<string, unknown>
9+
status?: number
910
}
1011

1112
interface IModuleWithServerData {

packages/gatsby/src/utils/page-ssr-module/entry.ts

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface ISSRData {
3737
templateDetails: ITemplateDetails
3838
potentialPagePath: string
3939
serverDataHeaders?: Record<string, string>
40+
serverDataStatus?: number
4041
searchString: string
4142
}
4243

@@ -222,6 +223,7 @@ export async function getData({
222223
templateDetails,
223224
potentialPagePath,
224225
serverDataHeaders: serverData?.headers,
226+
serverDataStatus: serverData?.status,
225227
searchString,
226228
}
227229
} finally {

packages/gatsby/src/utils/start-server.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ export async function startServer(
340340
)
341341
}
342342

343+
let statusCode = 200
343344
if (pageMode === `SSR`) {
344345
try {
345346
const result = await serverDataPromise
@@ -354,6 +355,10 @@ export async function startServer(
354355
}
355356
}
356357

358+
if (result.status) {
359+
statusCode = result.status
360+
}
361+
357362
pageData.result.serverData = result.props
358363
pageData.getServerDataError = null
359364
} catch (error) {
@@ -378,7 +383,7 @@ export async function startServer(
378383
pageData.result.serverData = null
379384
}
380385

381-
res.status(200).send(pageData)
386+
res.status(statusCode).send(pageData)
382387
return
383388
} catch (e) {
384389
report.error(

starters/default/src/pages/using-ssr.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ export async function getServerData() {
4040
}
4141
} catch (error) {
4242
return {
43-
headers: {
44-
status: 500,
45-
},
43+
status: 500,
44+
headers: {},
4645
props: {},
4746
}
4847
}

0 commit comments

Comments
 (0)