Learn how to fetch data into your React modules through a variety of methods, including GraphQL and HubL.
getServerSideProps
function (Professional and Enterprise only)props.hublParameters
at the React component level.
In the template, add parameters for each data property in the HubL module tag, then use HubL tokens to fetch the data.
props.hublParameters
to pass the data to the React module.
hublDataTemplate
API instead.
hublDataTemplate
. In the string, include a HubL statement to set the hublData
variable.hublData
. Use props.hublData
to access the returned data.getServerSideProps
function from React modules. This function must return an object with a serverSideProps
property and a cacheConfig
property, which configures caching of the module.
In the React component, the information returned in serverSideProps
can be accessed via props.serverSideProps
. For fetching specific data based on dependencies such as URLs, query parameters, or the contact object, HubSpot provides the following utility functions:
These helper functions wrap your data fetching functions and automatically inject relevant dependencies (with TypeScript types), ensuring that your module has the necessary context to fetch and process the data.
For example, when you use withUrlPath
, the wrapped function will receive a URL without query parameters, making it easy to fetch data based on the path alone. Similarly, withContact
ensures that your function has access to the URL, query, and contact information, allowing for more complex data-fetching scenarios.
fieldValues
, hublData
, and dataQueryResult
etc. is available.
withModuleProps
, but also includes the page URL (without query parameters). Caching at the module level is partly based on the props and dependencies used in data fetching. By omitting the query parameters, you can optimize this caching, as it won’t create new cache records for every query parameter variation.
To include query parameters, use withUrlAndQuery
instead.
withUrlPath
and provides module properties like withModuleProps
, but also includes the page URL with query parameters. A new cache record will be created for each permutation of the URL with query parameters for this module.
withUrlAndQuery
, this function provides the module properties, along with a URL with query parameters, but also includes a contact object.
Cache-control: max-age=10
) for data fetching modules.
Cache keys are based on the following:
fieldValues
, hublData
, dataQueryResult
)withUrlPath
will result in a new cache key being created each time the module is rendered on a page at a new URL path).getServerSideProps
function includes a caching
property which you can use to control caching. This property only contains the cacheControl
property, which represents the Cache-Control header, and it can include any of the standard directives.
In the example below, the module will be cached for 60 seconds, and any request after that will trigger a re-cache.
serverless.json
configuration, but always follows a common structure: https://<domain>/hs/serverless/<path>
.
https://<domain>/_hcms/api/<path>
.