Single Page Application Server Script

The SPA server script file is the script file associated with the script record. It acts as a plug-in with an API that can modify the response (for example, add scripts or perform validations).

You can load most server-side SuiteScript modules from the SPA server script. For examples, see SPA Server Script Basic Examples using SuiteScript modules.

The SPA server script has the following structure:

  1. It includes the two JSDoc tags, @NApiVersion and @NScriptType, declaring the appropriate version (2.1) and script type (SpaServerScript).

                  /**
     * @NApiVersion 2.1
     * @NScriptType SpaServerScript
     */ 
    
                
  2. It implements and exports an initializeSpa(context) entry point function. This function receives a context parameter, an object that contains the context.addStyleSheet(options) method.

                  /**
     * @NApiVersion 2.1
     * @NScriptType SpaServerScript
     */
    export const initializeSpa = (context) => {}; 
    
                
  3. The context.addStyleSheet(options) method adds a style sheet to the SPA. It returns void.

    This method has the following parameters:

    Parameter

    Type

    Required/Optional

    Description

    relativePath

    string

    At least one is required (and not null).

    The path to the CSS file relative to the assets folder (for example, "/main.css").

    url

    string

    At least one is required (and not null).

    The URL where the CSS file is located (for example, "http://sampleweb.com/css/main.css").

    The URL member overrides any value provided in the relativePath member.

    The following example shows how to use the context.addStyleSheet(options) method with both parameters specified. The url gets precedence over the relativePath.

                  /**
     * @NApiVersion 2.1
     * @NScriptType SpaServerScript
     */
    import log from 'N/log';
     
    export const initializeSpa = (context) => {
          context.addStyleSheet({
              relativePath: "/main.css",
              url: "http://sampleweb.com/css/main.css"});
          log.debug({
              title: 'Debug log server script',
              details: 'Server script added stylesheet'
          });
    }; 
    
                

Related Topics:

Single Page Application Creation And Development
Developing Single Page Applications
Single Page Application Client Script

General Notices