This guide provides a comprehensive, step-by-step process to create your first custom widget using Nuxt.js and the TakeProfit Widget SDK (version 0.2.3). By following this guide, you’ll set up a Nuxt.js project, integrate the SDK, configure necessary security headers, and implement basic functionalities to interact with the TakeProfit platform. This example serves as a foundation for building more complex and feature-rich widgets tailored to your specific needs.
npm
or Yarn
None
(or your preferred choice)None
Axios
, PWA
)None
(optional)Universal (SSR / SSG)
my-tp-widget
with your desired project name.
http://localhost:3000
.
package.json
to ensure takeprofit-widget-sdk
is listed under dependencies
:
nuxt.config.js
Open nuxt.config.js
and add the following configuration to set the necessary headers:
server-middleware/headers.js
and add the following code:
X-Frame-Options
: Controls whether your site can be embedded in an iframe. Setting it to ALLOW-FROM https://takeprofit.com
allows the widget to be embedded only from the specified origin.Content-Security-Policy
: The frame-ancestors
directive specifies valid parents that may embed a page using <frame>
, <iframe>
, <object>
, <embed>
, or <applet>
. Setting it to 'self' https://takeprofit.com
allows framing from the same origin and the specified TakeProfit domain.plugins/tp-widget-sdk.js
and add the following code:
nuxt.config.js
and register the plugin:
mode: 'client'
: Ensures that the plugin is only loaded on the client-side, as the SDK interacts with the browser environment.inject
: Makes the SDK available throughout your application via this.$tpWidgetSDK
.types/tp-widget-sdk.d.ts
:
tsconfig.json
includes the types
directory:
pages/widget.vue
:
menuItemId
stores the ID of the added menu item.mounted
: Subscribes to widget and channel state changes.beforeDestroy
: Unsubscribes from widget and channel state changes to prevent memory leaks.handleWidgetChange
: Logs widget state updates.handleChannelChange
: Logs channel state updates.changeSecurity
: Requests a security change and handles the response.addMenuItem
: Adds a new menu item labeled “Refresh Data” and stores its ID.updateMenuItem
: Updates the existing menu item’s label to “Refresh Data (Updated)”.removeMenuItem
: Removes the existing menu item and resets menuItemId
.menuItemId
./widget
in your Nuxt.js app renders the widget.vue
page. You can test this by visiting http://localhost:3000/widget
in your browser.
widget.vue
component (already demonstrated in Step 5).
widget.vue
The menu item functions are already implemented in Step 5. Here’s a recap:
menuItemId
and enables the update and remove buttons.menuItemId
and disables the update and remove buttons.menuItemId
to prevent conflicts. In this example, we’re managing a single menu item, but you can extend this logic to handle multiple items as needed.
http://localhost:3000
.
F12
or Ctrl+Shift+I
to open the developer tools.
'http://localhost:3000/widget'
with your actual widget URL if different.imageUrl
for the widget icon.pages/widget.vue
, plugins/tp-widget-sdk.js
, server-middleware/headers.js
, etc.) are correctly set up and that your server configuration (server-middleware/headers.js
) is compatible with your hosting service.
vercel.json
configuration file.
vercel.json
src
: Matches all routes.headers
: Sets the required security headers.dest
: Serves the matched route.iframeUrl
in the TakeProfit platform’s local storage to point to your deployed widget’s URL:
requestId
and menuItemId
are unique to prevent conflicts.
takeprofit.com
).iframeUrl
and ensure it points to the correct widget URL.TPWidgetSDK.connect()
and TPWidgetSDK.hideLoader()
are called successfully.securityID
you are requesting is valid and recognized by the platform..catch
Blocks: Ensure that all asynchronous SDK methods have proper error handling using .catch
to manage rejected promises.