Speed up widget development with Sitefinity MCP server. Learn more...

Sample: Next.js script widget

Overview

The Script widget allows you to embed JavaScript code or external script files on your pages. You can also use it to paste embed codes from third-party tools, such as Google Analytics, social media widgets, or other tracking solutions. 

You can choose from the following options:

  • Inline
    In this mode the Script widget renders the content of the text area in the widget designer on the place where the widget is dropped in the WYSIWYG editor
  • BodyTop
    These modes register scripts with the script injection system. The ScriptInjector components subsequently retrieve these scripts and position them at the start or end of the body element in the page template.
  • BodyBottom
    These modes register scripts with the script injection system. The ScriptInjector components subsequently retrieve these scripts and position them at the start or end of the body element in the page template.

PREREQUISITES: You must set up a Sitefinity renderer application and connect it to your Sitefinity CMS application. For more information, see Install Sitefinity in Next.js mode

Required setup

Two critical configurations are needed:

  • Include ScriptInjector components in the page template - Add ScriptInjectorBodyTop and ScriptInjectorBodyBottom components to your page template
  • Pass RequestContext to ScriptInjectors - The ScriptInjector components require the RequestContext to access the layout data

IMPORTANT: This custom template configuration is required for every page where the Script widget is placed with BodyTop or BodyBottom location.

Create the widget

he implementation consists of four main components:

  • Script Widget (script.tsx)
    Main widget component that renders inline scripts or returns null for BodyTop/BodyBottom scripts
    In edit mode, displays a preview of the script with location information
    Server-side component that processes the widget configuration

  • ScriptInjector (script-injector.tsx)
    Scans the page layout to find all Script widgets with BodyTop or BodyBottom location
    Renders the scripts at the appropriate positions in the page template
    Disabled in edit mode to prevent script execution during page editing

  • ScriptEntity (script.entity.ts)
    Designer metadata defining the widget properties
    Configures the widget editor interface in Sitefinity

  • ScriptLocation constant (location.ts)
    Select where the script should be placed (Inline, Body top, or Body bottom)
  • Navigate to '../widgets/script/'
  • Create a script.tsx file.
  • In the file, paste the following code and save your changes:

  • Navigate to '../widgets/script/'
  • Create a script-injector.tsx file.
  • In the file, paste the following code and save your changes:

  • Navigate to '../widgets/script/'
  • Create a script.entity.ts file.
  • In the file, paste the following code and save your changes:

  • Navigate to '../widgets/script/'
  • Create a location.ts file.
  • In the file, paste the following code and save your changes:

Integrate with page templates

 

Perform the following:

  1. Navigate to your page template file (e.g. src/app/templates/sitefinity-template.tsx)
  2. In the file, paste the following code and save your changes:

Register the widget

The next step is to register the component implementation and the designer metadata with the Next.js renderer. The Sitefinity Next.js SDK will automatically generate the needed designer metadata based on the defined entity. The registry is used to find the component function reference for the widget from the response of the Page Layout service. It is also used when generating metadata for the widget when it is used in the WYSIWYG page editor – labels, visuals etc.

The widget-registry.ts file file should include the following code:

 

Result

When you open your Renderer application and open the New editor, you will see the Script widget in the widget selector. When you add the widget on your page and edit it, you can choose where to place the script and you can enter the script in the text area.

Script

 Key Implementation Details

  • Server-Side Rendering: The Script widget is a server component that processes during page rendering
  • Edit Mode Preview: In edit mode, shows the first 3 lines of the script with a location hint
  • Layout Scanning: ScriptInjector components scan the entire widget tree to find Script widgets
  • Unique IDs: Each script gets a unique ID based on placeholder, location, and widget ID
  • HTML Injection: Scripts are injected using dangerouslySetInnerHTML for direct HTML rendering
NEW TO SITEFINITY?

Want to learn more?

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?