Skip to content

Commit

Permalink
feat(gatsby): let serve use getServerData headers (#33159)
Browse files Browse the repository at this point in the history
* feat(gatsby): let serve use getServerData headers

* fix types
  • Loading branch information
pieh committed Sep 13, 2021
1 parent fc66250 commit fe2f09b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
13 changes: 12 additions & 1 deletion packages/gatsby/cache-dir/page-ssr/index.d.ts
Expand Up @@ -27,7 +27,12 @@ interface IQueryResult {
// https://codemix.com/opaque-types-in-javascript/
type Opaque<K, T> = T & { __TYPE__: K }
// redacted details as this is meant to be opaque internal type that shouldn't be relied on by integrators (can change any time)
type ISSRData = Opaque<"ISSRData", {}>
type ISSRData = Opaque<
"ISSRData",
{
serverDataHeaders?: Record<string, string>
}
>

type PageContext = Record<string, any>

Expand All @@ -52,6 +57,12 @@ export function getData(args: {
runQuery(query: string, context: Record<string, any>): Promise<IQueryResult>
findPageByPath(pathName: string): IGatsbyPage | undefined
}
req: Partial<{
query: Record<string, unknown>
method: string
url: string
headers: Record<string, string>
}>
}): Promise<ISSRData>

export function renderPageData(args: {
Expand Down
18 changes: 16 additions & 2 deletions packages/gatsby/src/commands/serve.ts
Expand Up @@ -232,12 +232,12 @@ module.exports = async (program: IServeProgram): Promise<void> => {
program.directory,
`.cache`,
`query-engine`
))
)) as typeof import("../schema/graphql-engine/entry")
const { getData, renderPageData, renderHTML } = require(path.join(
program.directory,
`.cache`,
`page-ssr`
))
)) as typeof import("../utils/page-ssr-module/entry")
const graphqlEngine = new GraphQLEngine({
dbPath: path.join(program.directory, `.cache`, `data`, `datastore`),
})
Expand All @@ -260,6 +260,13 @@ module.exports = async (program: IServeProgram): Promise<void> => {
req,
})
const results = await renderPageData({ data })
if (page.mode === `SSR` && data.serverDataHeaders) {
for (const [name, value] of Object.entries(
data.serverDataHeaders
)) {
res.setHeader(name, value)
}
}
return void res.send(results)
}

Expand All @@ -279,6 +286,13 @@ module.exports = async (program: IServeProgram): Promise<void> => {
req,
})
const results = await renderHTML({ data })
if (page.mode === `SSR` && data.serverDataHeaders) {
for (const [name, value] of Object.entries(
data.serverDataHeaders
)) {
res.setHeader(name, value)
}
}
return res.send(results)
}

Expand Down
13 changes: 10 additions & 3 deletions packages/gatsby/src/utils/page-ssr-module/entry.ts
Expand Up @@ -16,7 +16,7 @@ import {
} from "../page-data-helpers"
// @ts-ignore render-page import will become valid later on (it's marked as external)
import htmlComponentRenderer from "./render-page"
import { getServerData } from "../get-server-data"
import { getServerData, IServerData } from "../get-server-data"

export interface ITemplateDetails {
query: string
Expand All @@ -28,6 +28,7 @@ export interface ISSRData {
page: IGatsbyPage
templateDetails: ITemplateDetails
potentialPagePath: string
serverDataHeaders?: Record<string, string>
}

const pageTemplateDetailsMap: Record<
Expand Down Expand Up @@ -71,7 +72,7 @@ export async function getData({
// 3. Execute query
// query-runner handles case when query is not there - so maybe we should consider using that somehow
let results: IExecutionResult = {}
let serverData: any = undefined
let serverData: IServerData | undefined
if (templateDetails.query) {
executionPromises.push(
graphqlEngine
Expand Down Expand Up @@ -104,7 +105,13 @@ export async function getData({
}
results.pageContext = page.context

return { results, page, templateDetails, potentialPagePath }
return {
results,
page,
templateDetails,
potentialPagePath,
serverDataHeaders: serverData?.headers,
}
}

export async function renderPageData({
Expand Down

0 comments on commit fe2f09b

Please sign in to comment.