This is the early access documentation preview for Custom Views. This documentation might not be in sync with our official documentation.
Extensions
Extensions let you connect with external APIs, perform mutations, and render dynamic pages.
Extensions are JavaScript/TypeScript functions that run on the extension runner inside the API hub. commercetools Frontend supports the following types of extensions:
- Data sources: provide data to be displayed on a page through Frontend components.
- Actions: let you perform API requests using HTTP endpoints.
- Dynamic page handler: lets you resolve the page folder URLs dynamically and render content dynamically based on the path.
We currently support Node.js version 16.x for extensions.
Define extensions
The API hub extension runner expects a single JavaScript file to run including all dependencies. You can find a pre-configured file at this path packages/<project-name>/backend/index.ts
in your GitHub customer repository.
In the JavaScript file, default export is used to export a standardized object that configures functions to run as extensions. The object keys define the extension type and an identifier for the extension. The following example defines:
- Two data source extensions:
content/blog-list
andcontent/blog-body
- Four action extensions:
cart/add
,cart/remove
,account/login
, andaccount/logout
. - The
dynamic-page-handler
extension
export default {'data-sources': {'content/blog-list': function () {/* ... */},'content/blog-body': function () {/* ... */},},actions: {cart: {add: function () {/* ... */},remove: function () {/* ... */},},account: {login: function () {/* ... */},logout: function () {/* ... */},},},'dynamic-page-handler': function () {/* ... */},};
Develop extensions
Before developing extensions, ensure you have set up your project. The extension development process works as follows:
- You edit an extension source file locally. This can be the
backend/index.ts
or any file imported inside of it. - The webpack file watcher detects the change and rebuilds the project.
- If the build is successful, it is synchronized to the extension runner with your developer API token.
- The API hub extension runner detects the change and runs the latest code on the next execution.
To start the extension development, run the following command on your command-line tool in the root directory of your project repository.
frontastic run
This command starts the build processes and opens the CLI dashboard from where you can monitor the extension logs as follows:
Press the
b
key to see the build log of your extensions.Press the
e
key to see the execution logs of your extensions running on the extension runner. The logs include run time errors,console.log
calls from the extensions during execution, information about the upload of the extension build after recompile, and information about the execution of extensions.
For more information about developing using the CLI, see CLI.
Extension build process
A pre-configured webpack build process automatically starts when you run frontastic run
from the CLI. The extensions run on the extension runner and are synchronized automatically after file save during development.