Step 4: RETURN_CUSTOM_DATA Backend
The RETURN_CUSTOM_DATA
action is somewhat similar to USE_FUNCTION
except it skips the function invocation altogether and just returns some arbitrary JSON to the front-end. This means that we don’t have to worry about errors, timeouts, or asynchronous functions, since we aren’t actually executing any code.
Generally speaking, the JSON data that is returned in these rules will be some indicator that the front-end should perform some action, such as redirecting to another page or fetching from an API on the client-side.
Here we’ll use an example of fetching package tracking info. Here’s how we might set up the query rule to handle this client-side:
{
"rules": [
{
"criteria": {
"searchTermMatchesRegex": "\\d{5,}"
},
"actions": [
{
"actionType": "RETURN_CUSTOM_DATA",
"key": "trackingInfo",
"customData": {
"action": "FETCH_PACKAGE_DATA"
}
}
]
}
]
}
All this is doing is basically informing the front-end that it should fetch and display the package data, but it’s up to the front-end to actually implement the logic. Another common use case for RETURN_CUSTOM_DATA will be redirects. In this case, the back-end will simply return a URL to redirect to, and it’s up to the front-end to implement the logic to make the browser redirect.