Skip to content

Commit 0017375

Browse files
authored
fix(gatsby): pass serverData to Head (#37500)
* fix(gatsby): pass serverData to Head * update public HeadProps type definition * add test to e2e/dev * add test to e2e/prod * update unit tests
1 parent e7e5cb4 commit 0017375

File tree

9 files changed

+42
-7
lines changed

9 files changed

+42
-7
lines changed

e2e-tests/development-runtime/cypress/integration/head-function-export/html-insertion.js

+3
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,8 @@ describe(`Head function export html insertion`, () => {
9797
cy.getTestElement(`link`)
9898
.invoke(`attr`, `href`)
9999
.should(`equal`, data.ssr.link)
100+
cy.getTestElement(`server-data`)
101+
.invoke(`attr`, `content`)
102+
.should(`equal`, JSON.stringify(data.ssr.serverData))
100103
})
101104
})

e2e-tests/development-runtime/shared-data/head-function-export.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const data = {
5050
noscript: `You may be a puzzle, but I like the way the parts fit`,
5151
style: `green`,
5252
link: `/used-by-head-function-export-ssr.css`,
53+
serverData: { hello: `world` },
5354
},
5455
invalidElements: {
5556
title: `I should actually be inserted, unlike the others`,

e2e-tests/development-runtime/src/pages/head-function-export/ssr.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ export default function HeadFunctionExportSSR() {
1111

1212
export async function getServerData() {
1313
return {
14-
hello: `world`,
14+
props: data.ssr.serverData,
1515
}
1616
}
1717

18-
export function Head() {
18+
export function Head({ serverData }) {
1919
const { base, title, meta, noscript, style, link } = data.ssr
2020

2121
return (
@@ -32,6 +32,11 @@ export function Head() {
3232
`}
3333
</style>
3434
<link data-testid="link" href={link} rel="stylesheet" />
35+
<meta
36+
data-testid="server-data"
37+
name="server-data"
38+
content={JSON.stringify(serverData)}
39+
/>
3540
</>
3641
)
3742
}

e2e-tests/production-runtime/cypress/integration/head-function-export/html-insertion.js

+3
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,8 @@ describe(`Head function export html insertion`, () => {
108108
cy.getTestElement(`link`)
109109
.invoke(`attr`, `href`)
110110
.should(`equal`, data.ssr.link)
111+
cy.getTestElement(`server-data`)
112+
.invoke(`attr`, `content`)
113+
.should(`equal`, JSON.stringify(data.ssr.serverData))
111114
})
112115
})

e2e-tests/production-runtime/shared-data/head-function-export.js

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const data = {
5151
noscript: `You may be a puzzle, but I like the way the parts fit`,
5252
style: `green`,
5353
link: `/used-by-head-function-export-ssr.css`,
54+
serverData: { hello: `world` },
5455
},
5556
invalidElements: {
5657
title: `I should actually be inserted, unlike the others`,

e2e-tests/production-runtime/src/pages/head-function-export/ssr.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ export default function HeadFunctionExportSSR() {
1111

1212
export async function getServerData() {
1313
return {
14-
hello: `world`,
14+
props: data.ssr.serverData,
1515
}
1616
}
1717

18-
export function Head() {
18+
export function Head({ serverData }) {
1919
const { base, title, meta, noscript, style, link } = data.ssr
2020

2121
return (
@@ -32,6 +32,11 @@ export function Head() {
3232
`}
3333
</style>
3434
<link data-testid="link" href={link} rel="stylesheet" />
35+
<meta
36+
data-testid="server-data"
37+
name="server-data"
38+
content={JSON.stringify(serverData)}
39+
/>
3540
</>
3641
)
3742
}

packages/gatsby/cache-dir/__tests__/head/utils.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ const fullPropsExample = {
2727
name: `john`,
2828
},
2929
},
30-
serverData: null,
30+
serverData: {
31+
hello: `world`,
32+
},
3133
},
3234
page: {
3335
componentChunkName: `component---src-pages-person-name-tsx`,
@@ -53,7 +55,9 @@ const fullPropsExample = {
5355
name: `john`,
5456
},
5557
},
56-
serverData: null,
58+
serverData: {
59+
hello: `world`,
60+
},
5761
params: {
5862
name: `john`,
5963
},
@@ -114,6 +118,9 @@ describe(`head utils`, () => {
114118
params: {
115119
name: `john`,
116120
},
121+
serverData: {
122+
hello: `world`,
123+
},
117124
data: {
118125
site: {
119126
siteMetadata: {
@@ -137,6 +144,7 @@ describe(`head utils`, () => {
137144
pathname: `/john/`,
138145
},
139146
params: {},
147+
serverData: null,
140148
data: {},
141149
pageContext: {},
142150
})

packages/gatsby/cache-dir/head/utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function filterHeadProps(input) {
1111
},
1212
params: input.params,
1313
data: input.data || {},
14+
serverData: input.serverData,
1415
pageContext: input.pageContext,
1516
}
1617
}

packages/gatsby/index.d.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ export type PageProps<
156156
/**
157157
* A props object passed into the Head function for [Gatsby Head API](https://gatsby.dev/gatsby-head).
158158
*/
159-
export type HeadProps<DataType = object, PageContextType = object> = {
159+
export type HeadProps<
160+
DataType = object,
161+
PageContextType = object,
162+
ServerDataType = object
163+
> = {
160164
location: {
161165
/**
162166
* Returns the Location object's URL's path.
@@ -173,6 +177,10 @@ export type HeadProps<DataType = object, PageContextType = object> = {
173177
* A context object which is passed in during the creation of the page.
174178
*/
175179
pageContext: PageContextType
180+
/**
181+
* Data passed into the page via the [getServerData](https://www.gatsbyjs.com/docs/reference/rendering-options/server-side-rendering/) SSR function.
182+
*/
183+
serverData: ServerDataType
176184
}
177185

178186
/**

0 commit comments

Comments
 (0)