Skip to content

feat(gatsby-plugin-fastify): ssr status code #123

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

Merged
merged 3 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tricky-tips-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gatsby-plugin-fastify": minor
---

Support the HTTP response code from `getServerData` vvis returning `status` in the return object.
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"fastify-compress": "^3.6.1",
"fastify-plugin": "^3.0.0",
"fastify-static": "^4.5.0",
"gatsby": "^4.1.0",
"gatsby": "^4.2.0",
"gatsby-plugin-utils": "^2.1.0",
"jest": "^27.3.1"
},
Expand All @@ -62,7 +62,7 @@
"fastify-compress": "^3.6.0",
"fastify-plugin": "^3.0.0",
"fastify-static": "^4.2.0",
"gatsby": "^4.0.0"
"gatsby": "^4.2.0"
},
"engines": {
"node": ">=14.15.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,26 @@ describe(`Test Gatsby DSG/SSR Routes`, () => {

expect(response.statusCode).toEqual(404);
});

it(`Should throw returned status code from getServer Data for HTML`, async () => {
const fastify = await createFastifyInstance(serveGatsby);

const response = await fastify.inject({
url: "/ssr403",
method: "GET",
});

expect(response.statusCode).toEqual(403);
});

it(`Should throw returned status code from getServer Data for page-data.json`, async () => {
const fastify = await createFastifyInstance(serveGatsby);

const response = await fastify.inject({
url: "/page-data/ssr403/page-data.json",
method: "GET",
});

expect(response.statusCode).toEqual(403);
});
});
27 changes: 19 additions & 8 deletions packages/gatsby-plugin-fastify/src/plugins/serverRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,16 @@ export const handleServerRoutes: FastifyPluginAsync<{
graphqlEngine,
req,
});

const pageData = (await renderPageData({ data: pageQueryData })) as any;

if (page.mode === `SSR` && pageQueryData?.serverDataHeaders) {
for (const [name, value] of Object.entries(pageQueryData.serverDataHeaders)) {
reply.header(name, value);
if (page.mode === `SSR`) {
if (pageQueryData?.serverDataHeaders) {
reply.headers(pageQueryData.serverDataHeaders);
}

if (pageQueryData?.serverDataStatus) {
reply.code(pageQueryData.serverDataStatus);
}
}

Expand Down Expand Up @@ -99,15 +104,21 @@ export const handleServerRoutes: FastifyPluginAsync<{
reply.header("x-gatsby-fastify", `served-by: ${page?.mode || "dsg/ssr handler"}`);

try {
const data = await getData({
const pageQueryData = await getData({
pathName: potentialPagePath,
graphqlEngine,
req,
});
const results = await renderHTML({ data });
if (page.mode === `SSR` && data.serverDataHeaders) {
for (const [name, value] of Object.entries(data.serverDataHeaders)) {
reply.header(name, value);

const results = await renderHTML({ data: pageQueryData });

if (page.mode === `SSR`) {
if (pageQueryData?.serverDataHeaders) {
reply.headers(pageQueryData.serverDataHeaders);
}

if (pageQueryData?.serverDataStatus) {
reply.code(pageQueryData.serverDataStatus);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test-sites/fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"fastify-accepts": "^2.0.1",
"fastify-compress": "^3.6.1",
"fastify-static": "^4.4.2",
"gatsby": "^4.0.0",
"gatsby": "^4.2.0",
"gatsby-plugin-fastify": "*",
"gatsby-plugin-image": "^2.0.0",
"gatsby-plugin-manifest": "^4.0.0",
Expand Down
3 changes: 3 additions & 0 deletions test-sites/fastify/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ const IndexPage = () => {
<li style={{ ...listItemStyles }}>
<a href={withPrefix("/ssrBad")}>Bad SSR Page</a>
</li>
<li style={{ ...listItemStyles }}>
<a href={withPrefix("/ssr403")}>Unauthorized SSR Page</a>
</li>
</ul>
</main>
)
Expand Down
3 changes: 0 additions & 3 deletions test-sites/fastify/src/pages/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ export async function getServerData() {
}
} catch (error) {
return {
headers: {
status: 500,
},
props: {},
}
}
Expand Down
17 changes: 17 additions & 0 deletions test-sites/fastify/src/pages/ssr403.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from "react"

export default function Ssr403Example({ serverData }) {
return (
<main>
<h1>403 SSR Page</h1>
<pre>{JSON.stringify(serverData, null, 2)}</pre>
</main>
)
}

export async function getServerData({ url, query, method, params, headers }) {
return {
status: 403,
props: {},
}
}
Loading