Skip to content

Commit 4d763dc

Browse files
sbardianpieh
authored andcommitted
docs: Update browser and ssr api docs to include pluginOptions parameter (#12547)
## Description Was using `wrapRootElement` api and had need of `pluginOptions` in my `gatsby-plugin-breadcrumb` plugin. Documentation didn't show `pluginOptions` as a parameter, but reviewing the code it seems all `Browser` and `SSR` api's do get `pluginOptions` sent as a parameter, from what I can see. I was able to use them successfully with `wrapRootElement` so thought adding it to the docs would be helpful.
1 parent a8252aa commit 4d763dc

File tree

6 files changed

+71
-17
lines changed

6 files changed

+71
-17
lines changed

packages/gatsby/cache-dir/api-ssr-docs.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
/**
2+
* Object containing options defined in `gatsby-config.js`
3+
* @typedef {object} pluginOptions
4+
*/
5+
16
/**
27
* Replace the default server renderer. This is useful for integration with
38
* Redux, css-in-js libraries, etc. that need custom setups for server
49
* rendering.
5-
* @param {Object} $0
10+
* @param {object} $0
611
* @param {string} $0.pathname The pathname of the page currently being rendered.
712
* @param {function} $0.replaceBodyHTMLString Call this with the HTML string
813
* you render. **WARNING** if multiple plugins implement this API it's the
@@ -22,7 +27,7 @@
2227
* to the `html.js` component.
2328
* @param {function} $0.setBodyProps Takes an object of data which
2429
* is merged with other body props and passed to `html.js` as `bodyProps`.
25-
* @param {Object} pluginOptions
30+
* @param {pluginOptions} pluginOptions
2631
* @example
2732
* // From gatsby-plugin-glamor
2833
* const { renderToString } = require("react-dom/server")
@@ -55,7 +60,7 @@ exports.replaceRenderer = true
5560
* over server rendering. However, if your plugin requires taking over server
5661
* rendering then that's the one to
5762
* use
58-
* @param {Object} $0
63+
* @param {object} $0
5964
* @param {string} $0.pathname The pathname of the page currently being rendered.
6065
* @param {function} $0.setHeadComponents Takes an array of components as its
6166
* first argument which are added to the `headComponents` array which is passed
@@ -72,7 +77,7 @@ exports.replaceRenderer = true
7277
* to the `html.js` component.
7378
* @param {function} $0.setBodyProps Takes an object of data which
7479
* is merged with other body props and passed to `html.js` as `bodyProps`.
75-
* @param {Object} pluginOptions
80+
* @param {pluginOptions} pluginOptions
7681
* @example
7782
* const { Helmet } = require("react-helmet")
7883
*
@@ -99,7 +104,7 @@ exports.onRenderBody = true
99104
* Called after every page Gatsby server renders while building HTML so you can
100105
* replace head components to be rendered in your `html.js`. This is useful if
101106
* you need to reorder scripts or styles added by other plugins.
102-
* @param {Object} $0
107+
* @param {object} $0
103108
* @param {string} $0.pathname The pathname of the page currently being rendered.
104109
* @param {Array<ReactNode>} $0.getHeadComponents Returns the current `headComponents` array.
105110
* @param {function} $0.replaceHeadComponents Takes an array of components as its
@@ -116,7 +121,7 @@ exports.onRenderBody = true
116121
* first argument which replace the `postBodyComponents` array which is passed
117122
* to the `html.js` component. **WARNING** if multiple plugins implement this
118123
* API it's the last plugin that "wins".
119-
* @param {Object} pluginOptions
124+
* @param {pluginOptions} pluginOptions
120125
* @example
121126
* // Move Typography.js styles to the top of the head section so they're loaded first.
122127
* exports.onPreRenderHTML = ({ getHeadComponents, replaceHeadComponents }) => {
@@ -144,6 +149,8 @@ exports.onPreRenderHTML = true
144149
* @param {object} $0
145150
* @param {ReactNode} $0.element The "Page" React Element built by Gatsby.
146151
* @param {object} $0.props Props object used by page.
152+
* @param {pluginOptions} pluginOptions
153+
* @returns {ReactNode} Wrapped element
147154
* @example
148155
* import React from "react"
149156
* import Layout from "./src/components/layout"
@@ -165,6 +172,8 @@ exports.wrapPageElement = true
165172
* _Note:_ [There is equivalent hook in Browser API](/docs/browser-apis/#wrapRootElement)
166173
* @param {object} $0
167174
* @param {ReactNode} $0.element The "Root" React Element built by Gatsby.
175+
* @param {pluginOptions} pluginOptions
176+
* @returns {ReactNode} Wrapped element
168177
* @example
169178
* import React from "react"
170179
* import { Provider } from "react-redux"

packages/gatsby/src/utils/api-browser-docs.js

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1+
/**
2+
* This argument is empty. This is for consisntency so `pluginOptions` is always second argument.
3+
* @typedef {undefined} emptyArg
4+
*/
5+
6+
/**
7+
* Object containing options defined in `gatsby-config.js`
8+
* @typedef {object} pluginOptions
9+
*/
10+
111
/**
212
* Called when the Gatsby browser runtime first starts.
13+
* @param {emptyArg} _
14+
* @param {pluginOptions} pluginOptions
315
* @example
416
* exports.onClientEntry = () => {
517
* console.log("We've started!")
@@ -10,6 +22,8 @@ exports.onClientEntry = true
1022

1123
/**
1224
* Called when the initial (but not subsequent) render of Gatsby App is done on the client.
25+
* @param {emptyArg} _
26+
* @param {pluginOptions} pluginOptions
1327
* @example
1428
* exports.onInitialClientRender = () => {
1529
* console.log("ReactDOM.render has executed")
@@ -22,6 +36,7 @@ exports.onInitialClientRender = true
2236
* @param {object} $0
2337
* @param {object} $0.location A location object
2438
* @param {object|null} $0.prevLocation The previous location object
39+
* @param {pluginOptions} pluginOptions
2540
* @example
2641
* exports.onPreRouteUpdate = ({ location, prevLocation }) => {
2742
* console.log("Gatsby started to change location to", location.pathname)
@@ -35,6 +50,7 @@ exports.onPreRouteUpdate = true
3550
* @param {object} $0
3651
* @param {object} $0.location A location object
3752
* @param {object} $0.action The "action" that caused the route change
53+
* @param {pluginOptions} pluginOptions
3854
* @example
3955
* exports.onRouteUpdateDelayed = () => {
4056
* console.log("We can show loading indicator now")
@@ -47,6 +63,7 @@ exports.onRouteUpdateDelayed = true
4763
* @param {object} $0
4864
* @param {object} $0.location A location object
4965
* @param {object|null} $0.prevLocation The previous location object
66+
* @param {pluginOptions} pluginOptions
5067
* @example
5168
* exports.onRouteUpdate = ({ location, prevLocation }) => {
5269
* console.log('new pathname', location.pathname)
@@ -78,6 +95,7 @@ exports.onRouteUpdate = true
7895
* coordinates to scroll to, a string of the `id` or `name` of an element to
7996
* scroll to, `false` to not update the scroll position, or `true` for the
8097
* default behavior.
98+
* @param {pluginOptions} pluginOptions
8199
* @example
82100
* exports.shouldUpdateScroll = ({
83101
* routerProps: { location },
@@ -95,17 +113,22 @@ exports.shouldUpdateScroll = true
95113

96114
/**
97115
* Allow a plugin to register a Service Worker. Should be a function that returns true.
116+
* @param {emptyArg} _
117+
* @param {pluginOptions} pluginOptions
118+
* @returns {boolean} Should Gatsby register `/sw.js` service worker
98119
* @example
99120
* exports.registerServiceWorker = () => true
100121
*/
101122
exports.registerServiceWorker = true
102123

103124
/**
104-
* Allow a plugin to replace the page component renderer. This api runner can be used to
105-
* implement page transitions. See https://github.com/gatsbyjs/gatsby/tree/master/examples/using-page-transitions for an example of this.
125+
* Allow a plugin to replace the page component renderer.
126+
* @deprecated Use [wrapPageElement](#wrapPageElement) to decorate page element.
106127
* @param {object} $0
107128
* @param {object} $0.props The props of the page.
108129
* @param {object} $0.loader The gatsby loader.
130+
* @param {pluginOptions} pluginOptions
131+
* @returns {ReactNode} Replaced default page renderer
109132
*/
110133
exports.replaceComponentRenderer = true
111134

@@ -119,6 +142,8 @@ exports.replaceComponentRenderer = true
119142
* @param {object} $0
120143
* @param {ReactNode} $0.element The "Page" React Element built by Gatsby.
121144
* @param {object} $0.props Props object used by page.
145+
* @param {pluginOptions} pluginOptions
146+
* @returns {ReactNode} Wrapped element
122147
* @example
123148
* import React from "react"
124149
* import Layout from "./src/components/layout"
@@ -140,6 +165,8 @@ exports.wrapPageElement = true
140165
* _Note:_ [There is equivalent hook in SSR API](/docs/ssr-apis/#wrapRootElement)
141166
* @param {object} $0
142167
* @param {ReactNode} $0.element The "Root" React Element built by Gatsby.
168+
* @param {pluginOptions} pluginOptions
169+
* @returns {ReactNode} Wrapped element
143170
* @example
144171
* import React from "react"
145172
* import { Provider } from "react-redux"
@@ -163,6 +190,7 @@ exports.wrapRootElement = true
163190
* @param {object} $0
164191
* @param {string} $0.pathname The pathname whose resources should now be prefetched
165192
* @param {function} $0.getResourcesForPathname Function for fetching resources related to pathname
193+
* @param {pluginOptions} pluginOptions
166194
*/
167195
exports.onPrefetchPathname = true
168196

@@ -172,21 +200,28 @@ exports.onPrefetchPathname = true
172200
* @param {object} $0
173201
* @param {string} $0.pathname The pathname whose resources have now been prefetched
174202
* @param {function} $0.getResourceURLsForPathname Function for fetching URLs for resources related to the pathname
203+
* @param {pluginOptions} pluginOptions
175204
*/
176205
exports.onPostPrefetchPathname = true
177206

178207
/**
179208
* Plugins can take over prefetching logic. If they do, they should call this
180209
* to disable the now duplicate core prefetching logic.
210+
* @param {emptyArg} _
211+
* @param {pluginOptions} pluginOptions
212+
* @returns {boolean} Should disable core prefetching
181213
* @example
182214
* exports.disableCorePrefetching = () => true
183215
*/
184216
exports.disableCorePrefetching = true
185217

186218
/**
187-
* Allow a plugin to replace the ReactDOM.render function call by a custom renderer.
188-
* This method takes no param and should return a function with same signature as ReactDOM.render()
189-
* Note it's very important to call the callback after rendering, otherwise Gatsby will not be able to call `onInitialClientRender`
219+
* Allow a plugin to replace the `ReactDOM.render`/`ReactDOM.hydrate` function call by a custom renderer.
220+
* @param {emptyArg} _
221+
* @param {pluginOptions} pluginOptions
222+
* @returns {Function} This method should return a function with same signature as `ReactDOM.render()`
223+
*
224+
* _Note:_ it's very important to call the `callback` after rendering, otherwise Gatsby will not be able to call `onInitialClientRender`
190225
* @example
191226
* exports.replaceHydrateFunction = () => {
192227
* return (element, container, callback) => {
@@ -201,13 +236,15 @@ exports.replaceHydrateFunction = true
201236
* Inform plugins when a service worker has been installed.
202237
* @param {object} $0
203238
* @param {object} $0.serviceWorker The service worker instance.
239+
* @param {pluginOptions} pluginOptions
204240
*/
205241
exports.onServiceWorkerInstalled = true
206242

207243
/**
208244
* Inform plugins of when a service worker has an update available.
209245
* @param {object} $0
210246
* @param {object} $0.serviceWorker The service worker instance.
247+
* @param {pluginOptions} pluginOptions
211248
*/
212249
exports.onServiceWorkerUpdateFound = true
213250

@@ -216,19 +253,22 @@ exports.onServiceWorkerUpdateFound = true
216253
* and the page is ready to reload to apply changes.
217254
* @param {object} $0
218255
* @param {object} $0.serviceWorker The service worker instance.
256+
* @param {pluginOptions} pluginOptions
219257
*/
220258
exports.onServiceWorkerUpdateReady = true
221259

222260
/**
223261
* Inform plugins when a service worker has become active.
224262
* @param {object} $0
225263
* @param {object} $0.serviceWorker The service worker instance.
264+
* @param {pluginOptions} pluginOptions
226265
*/
227266
exports.onServiceWorkerActive = true
228267

229268
/**
230269
* Inform plugins when a service worker is redundant.
231270
* @param {object} $0
232271
* @param {object} $0.serviceWorker The service worker instance.
272+
* @param {pluginOptions} pluginOptions
233273
*/
234274
exports.onServiceWorkerRedundant = true

www/src/components/api-reference/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export const pageQuery = graphql`
4646
}
4747
4848
fragment DocumentationFragment on DocumentationJs {
49+
kind
4950
...DocumentationDescriptionFragment
5051
...DocumentationExampleFragment
5152
...DocumentationParamsFragment

www/src/components/api-reference/signature.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const FunctionSignature = ({ definition, block, ignoreParams }) => {
104104
.map((param, index) => (
105105
<React.Fragment key={param.name}>
106106
{index > 0 && <Punctuation>, </Punctuation>}
107-
{param.name}
107+
{param.name === `$0` ? `apiCallbackContext` : param.name}
108108
{param.type && (
109109
<React.Fragment>
110110
<Punctuation>{param.optional && `?`}:</Punctuation>
@@ -125,7 +125,7 @@ const FunctionSignature = ({ definition, block, ignoreParams }) => {
125125
{definition.returns && definition.returns.length ? (
126126
<TypeExpression type={definition.returns[0].type} />
127127
) : (
128-
<TypeComponent>null</TypeComponent>
128+
<TypeComponent>undefined</TypeComponent>
129129
)}
130130
</Wrapper>
131131
)

www/src/pages/docs/browser-apis.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import { itemListDocs } from "../../utils/sidebar/item-list"
1212
class BrowserAPIDocs extends React.Component {
1313
render() {
1414
const funcs = sortBy(
15-
this.props.data.file.childrenDocumentationJs,
15+
this.props.data.file.childrenDocumentationJs.filter(
16+
doc => doc.kind !== `typedef`
17+
),
1618
func => func.name
1719
)
1820

@@ -43,7 +45,7 @@ class BrowserAPIDocs extends React.Component {
4345
<br />
4446
<hr />
4547
<h2>Reference</h2>
46-
<APIReference docs={funcs} />
48+
<APIReference docs={funcs} showTopLevelSignatures={true} />
4749
</Container>
4850
</Layout>
4951
)

www/src/pages/docs/ssr-apis.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import { itemListDocs } from "../../utils/sidebar/item-list"
1212
class SSRAPIs extends React.Component {
1313
render() {
1414
const funcs = sortBy(
15-
this.props.data.file.childrenDocumentationJs,
15+
this.props.data.file.childrenDocumentationJs.filter(
16+
doc => doc.kind !== `typedef`
17+
),
1618
func => func.name
1719
)
1820
return (
@@ -42,7 +44,7 @@ class SSRAPIs extends React.Component {
4244
<br />
4345
<hr />
4446
<h2>Reference</h2>
45-
<APIReference docs={funcs} />
47+
<APIReference docs={funcs} showTopLevelSignatures={true} />
4648
</Container>
4749
</Layout>
4850
)

0 commit comments

Comments
 (0)