Server-side in Sitevision

Here’s an example of how you can render the questions server-side so they automatically get the correct schema and HTML structure. This ensures that your Questions and Answers can be picked up by ChatGPT and similar services.

These features are still relatively new, so we’re happy to share tips and tricks with you. In that case, email us at support@rek.ai.

Add a script module in a template where you want the questions to appear. In the JavaScript section, add the following:

// =======================================
// SiteVision → rek.ai QnA Prediction Integration
// =======================================
 
// --- Required modules ---
var requester = require('Requester'); // Used for making HTTP requests (GET/POST) for plain text or HTML
var utils = require('Utils'); // SiteVision utility module
var pu = require('PropertyUtil'); // Utility for reading properties from nodes
 
// --- Site & Page Setup ---
var origin = 'YOUR DOMAIN HERE'; // Replace with your site domain (used in request headers)
var currentPage = portletContextUtil.currentPage; // The page this portlet is running on
var URI = pu.getString(currentPage, 'URI', ''); // Get the URI property of the current page (fallback to empty string)
URI = '^' + URI + '$'; // The ^ and $ ensure we only get questions for this exact page
 
// --- rek.ai Configuration ---
var projectID = '12341333'; // Your rek.ai project ID
var secret = 'ABCDEF'; // Your rek.ai secret key for authentication
 
// --- Build the rek.ai prediction request URL ---
var predictionurl = 'https://predict.rek.ai/';
predictionurl += 'predict?p=' + projectID;         // Add project ID
predictionurl += '&secret=' + secret;               // Add secret key
predictionurl += '&entitytype=rekai-qna';           // Specify that we're fetching QnA-type predictions
predictionurl += '&subtree=' + encodeURIComponent(URI);          // Limit predictions to the current page URI
predictionurl += '&unranked=true';                  // Return predictions in same order every time
predictionurl += '&format=html';                    // Request HTML response format
 
// =======================================
// Main execution function
// =======================================
function start() {
    try {
        // --- Setup request options ---
        var reqOptions = {
            dataType: 'text', // Expect plain text or HTML response
            headers: {
                'accept': 'text/html',              // Ask server for HTML
                'user-agent': 'sitevision-backend-proxy', // Identify request origin
                'origin': origin                    // Pass origin for CORS validation
            }
        };
 
        // --- Make GET request to rek.ai ---
        requester.get(predictionurl, reqOptions)
            .done(function (body, statusCode) {
                // ✅ Successfully received response
                try {
                    out.println(body); // Output the HTML returned from rek.ai directly to the portlet
                } catch (e) {
                    // Silent catch if printing fails
                }
            })
            .fail(function (message, status) {
                // Request failed
                out.println('rek.ai view failed: ' + status + ' - ' + message);
            });
 
    } catch (e) {
        // Catch any unexpected runtime errors
        out.println('rek.ai catch e: ' + e);
    }
}
 
// --- Run the integration ---
start();

In the Velocity part of the module, add the following:

<script>
    __rekai.ready(function() {
        var options = {};
        var questionOptions = window.__rekai.getQuestionsOptions();
        questionOptions.serversideRendering = true;
 
        window.__rekai.addCssStylesForQuestions(questionOptions);
        window.__rekai.addEventListenersForQuestions(options, questionOptions);
    });
</script>

Add a header above the questions

To add a header above the questions, you can modify the server-side rendering code to include a header element before outputting the questions. For example, you can insert an <h2> tag before the questions are rendered:

  predictionurl += '&headertext=' + encodeURIComponent(headertext);
  predictionurl += '&headerid=' + encodeURIComponent(headerid);
  predictionurl += '&headerfontclass=' + encodeURIComponent(headerfontclass);
  predictionurl += '&headerheadinglevel=' + encodeURIComponent(headerheadinglevel);

Set cache for the script module

You can set the cache time for the script module to something like 5 minutes. This way, you don’t hit the rek.ai API on every page load, but still get updated questions regularly. Sitevision Cache (opens in a new tab)