Skip to content
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

feat(gatsby): let serve use getServerData headers #33159

Merged
merged 2 commits into from
Sep 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/gatsby/cache-dir/page-ssr/index.d.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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