The first argument passed to each of Gatsby’s Node APIs is an object containing a set of helpers. Helpers shared by all Gatsby’s Node APIs are documented in Shared helpers section.
Common convention is to destructure helpers right in argument list:
The Creating a Source Plugin tutorial explains some of the Shared helpers in more detail.
NoteSome APIs provide additional helpers. For example createPages
provides graphql
function. Check documentation of specific APIs in Gatsby Node APIs for details.
Start building today on
Netlify!
Shared helpers Fieldsget
(key: string) => Promise<any>
Retrieve cached value
ParametersPromise<any>
Promise resolving to cached value
Exampleconst value = await cache.get(`unique-key`)
set
(key: string, value: any) => Promise<any>
Cache value
Parametersvalue
any
Value to be cached
Promise<any>
Promise resolving to cached value
Exampleawait cache.set(`unique-key`, value)
del
(key: string) => Promise<void>
Deletes cached value
ParametersPromise<void>
Promise resolving once key is deleted from cache
Exampleawait cache.del(`unique-key`)
info
(message: string) => void
message
string
Message to display
warn
(message: string) => void
message
string
Message to display
error
(message: string, error?: Error) => void
message
string
Message to display
error
Error
Optional error object
reporter.error(`text`, new Error('something'))
panic
(message: string, error?: Error) => void
message
string
Message to display
error
Error
Optional error object
reporter.panic(`text`, new Error('something'))
panicOnBuild
(message: string, error?: Error) => void
message
string
Message to display
error
Error
Optional error object
reporter.panicOnBuild(`text`, new Error('something'))
verbose
(message: string) => void
Note that this method only works if the —verbose option has been passed to the CLI
Parametersmessage
string
Message to display
activityTimer
(message: string) => ITimerReporter
Creates a new activity timer with the provided message. Check the full return type definition here.
Parametersmessage
string
Timer message to display
const activity = reporter.activityTimer(`Timer text`)
activity.start()
activity.setStatus(`status text`)
activity.end()
startSpan
(spanName: string) => Opentracing.Span
Start a tracing span. The span will be created as a child of the currently running API span. This is a convenience wrapper for
tracing.tracer.startSpan(`span-name`, { childOf: tracing.parentSpan}).
Parameters
spanName
string
name of the span
exports.sourceNodes = async ({ actions, tracing }) => {
const span = tracing.startSpan(`foo`)
span.setTag(`bar`, `baz`)
span.finish()
}
Collection of functions used to programmatically modify Gatsby’s internal state.
See actions
reference.
This is the same as pathPrefix
passed in gatsby-config.js
. It’s an empty string if you don’t pass pathPrefix
. When using assetPrefix, you can use this instead of pathPrefix to recieve the string you set in gatsby-config.js
. It won’t include the assetPrefix
.
Key-value store used to persist results of time/memory/cpu intensive tasks. All functions are async and return promises.
Fieldsget
(key: string) => Promise<any>
Retrieve cached value
ParametersPromise<any>
Promise resolving to cached value
Exampleconst value = await cache.get(`unique-key`)
set
(key: string, value: any) => Promise<any>
Cache value
Parametersvalue
any
Value to be cached
Promise<any>
Promise resolving to cached value
Exampleawait cache.set(`unique-key`, value)
del
(key: string) => Promise<void>
Deletes cached value
ParametersPromise<void>
Promise resolving once key is deleted from cache
Exampleawait cache.del(`unique-key`)
Create a stable content digest from a string or object, you can use the result of this function to set the internal.contentDigest
field on nodes. Gatsby uses the value of this field to invalidate stale data when your content changes.
const node = {
...nodeData,
internal: {
type: `TypeOfNode`,
contentDigest: createContentDigest(nodeData)
}
}
(input: string) => string
Utility function useful to generate globally unique and stable node IDs. It will generate different IDs for different plugins if they use same input.
Parametersconst node = {
id: createNodeId(`${backendData.type}${backendData.id}`),
...restOfNodeData
}
Internal event emitter / listener. Do not use, unless you absolutely must. Emitter is considered a private API and can change with any version.
(id: string) => GatsbyCacheGet cache instance by name - this should only be used by plugins that accept subplugins.
ParametersGatsbyCache
See cache
section for reference.
Get single node by given ID. Don’t use this in graphql resolvers - see getNodeAndSavePathDependency
.
ID
string
id of the node.
Node
Single node instance.
(ID: string, path: string) => NodeGet single node by given ID and create dependency for given path. This should be used instead of getNode
in graphql resolvers to enable tracking dependencies for query results. If it’s not used Gatsby will not rerun query if node changes leading to stale query results. See Page -> Node Dependency Tracking for more details.
ID
string
id of the node.
Node
Single node instance.
() => Node[]Get array of all nodes.
Exampleconst allNodes = getNodes()
(Type: string) => Node[]
Get array of nodes of given type.
Parametersconst markdownNodes = getNodesByType(`MarkdownRemark`)
(node: Node) => Promise<string>
Get content for a node from the plugin that created it.
Parametersmodule.exports = async function onCreateNode(
{ node, loadNodeContent, actions, createNodeId }
) {
if (node.internal.mediaType === 'text/markdown') {
const { createNode, createParentChildLink } = actions
const textContent = await loadNodeContent(node)
}
}
Use to prefix resources URLs. pathPrefix
will be either empty string or path that starts with slash and doesn’t end with slash. pathPrefix
also becomes <assetPrefix>/<pathPrefix>
when you pass both assetPrefix
and pathPrefix
in your gatsby-config.js
.
See Adding a Path Prefix page for details about path prefixing.
Set of utilities to output information to user
Fieldsinfo
(message: string) => void
message
string
Message to display
warn
(message: string) => void
message
string
Message to display
error
(message: string, error?: Error) => void
message
string
Message to display
error
Error
Optional error object
reporter.error(`text`, new Error('something'))
panic
(message: string, error?: Error) => void
message
string
Message to display
error
Error
Optional error object
reporter.panic(`text`, new Error('something'))
panicOnBuild
(message: string, error?: Error) => void
message
string
Message to display
error
Error
Optional error object
reporter.panicOnBuild(`text`, new Error('something'))
verbose
(message: string) => void
Note that this method only works if the —verbose option has been passed to the CLI
Parametersmessage
string
Message to display
activityTimer
(message: string) => ITimerReporter
Creates a new activity timer with the provided message. Check the full return type definition here.
Parametersmessage
string
Timer message to display
const activity = reporter.activityTimer(`Timer text`)
activity.start()
activity.setStatus(`status text`)
activity.end()
Internal redux state used for application state. Do not use, unless you absolutely must. Store is considered a private API and can change with any version.
Set of utilities that allow adding more detailed tracing for plugins. Check Performance tracing page for more details.
FieldsstartSpan
(spanName: string) => Opentracing.Span
Start a tracing span. The span will be created as a child of the currently running API span. This is a convenience wrapper for
tracing.tracer.startSpan(`span-name`, { childOf: tracing.parentSpan}).
Parameters
spanName
string
name of the span
exports.sourceNodes = async ({ actions, tracing }) => {
const span = tracing.startSpan(`foo`)
span.setTag(`bar`, `baz`)
span.finish()
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4