configure(options)

Sets global options for Scrivito.

Before you can start using any of the Scrivito API calls, you must configure the connected Scrivito CMS instance. The tenant ID can be found on your dashboard at my.scrivito.com. Use the SCRIVITO_TENANT value from the “Settings” tab and pass it to Scrivito.configure:

src/config/scrivito.js
Copy
Scrivito.configure({ tenant: "0123456789abcdef0123456789abcdef" });

You can also configure the mapping between browser URLs and pages in the CMS. If your homepage is not the default root page, you can specify a different page using a callback. For further details regarding the routing, see Customizing the Routing.

Copy
Scrivito.configure({
  homepage: () => Scrivito.Obj.getByPath("/en"),
  routingBasePath: "/js",
  tenant: "0123456789abcdef0123456789abcdef"
});

Options

  • tenant (String) – the ID of the tenant to use. This configuration key must be provided. The tenant ID is shown on your dashboard at my.scrivito.com. Use the value of SCRIVITO_TENANT from the “Settings” tab. To run your application without connecting it to your tenant and thus the content of your website, you can enable the in-memory tenant by specifying inMemory as the tenant ID. Please see In-memory tenant below for more details.
  • homepage (Function) – a callback that returns a CMS Obj representing the page at the root URI /. Default: Scrivito.Obj.root()
  • origin (String) – the origin of Scrivito.urlFor(target, options). This configuration is optional. The default is the origin of the current window location. The origin needs to be a valid domain with a schema, a host and, optionally, a port. Providing a path is not allowed. See below for an example.
  • routingBasePath (String) – prefix for all path-based routes handled by Scrivito. This configuration is optional.
  • visitorAuthentication (Boolean) – allow access to restricted content. Specify true if the visitor is known to be signed in, and a token will be made available (see setVisitorIdToken). See also: Authenticating Visitors and Displaying Restricted Content. Default: false
  • constraintsValidation (Function) – defines a callback for having an attribute validated by a third-party library, based on constraints. The underlying function receives a constraints object and returns an attribute validation callback. This configuration is optional.

Remarks

With visitorAuthentication on, Scrivito.preload ignores the dump data.

Routing modes

  • Path-based routing

    If path-based routing is active, the „routing path“ is stored in the URI path. These URIs look like typical web server URIs.

    www.mysite.com/
    www.mysite.com/myPermalink
    www.mysite.com/someSlug-2cd1ca3e2080c7f0
  • Path-based routing with base path

    The routingBasePath is optional. It allows you to „mount” the Scrivito application onto a sub-path. If, for example, routingBasePath is set to /docs/guides, the URIs would look like this:

    www.mysite.com/docs/guides/
    www.mysite.com/docs/guides/myPermalink
    www.mysite.com/docs/guides/someSlug-2cd1ca3e2080c7f0

Setting the origin of Scrivito.urlFor() URLs

Here is an example of how to specify the origin to be used for URLs returned by Scrivito.urlFor(target, options).

Copy
Scrivito.configure({
  tenant: '0123456789abcdef0123456789abcdef',
  origin: 'https://example.com',
});

In-memory tenant

In order to run your application without any connection to the content of your website, you can enable the in-memory tenant by setting the value of tenant to inMemory (string). This is handy if you are running tests for your code that are reading and writing Scrivito content. In this case you don't want the tests to affect the content displayed on your actual website. Furthermore, in most cases you don't want the test code to communicate via the network at all.

The in-memory tenant allows exactly that:

  • You can change Scrivito content, without the changes being propagated to your production environment.
  • The changes are stored in memory, so making them doesn't require a network connection.
  • Once the process died, all changed content disappears.
Copy
Scrivito.configure({
  tenant: "inMemory"
});

However, there are some limitations when using the in-memory tenant:

  • An in-memory tenant is empty by default, so each time you start a process using the inMemory tenant, your tests would have to set up content first.
  • The search APIs are not available.
  • Everything related to binaries is not available.
  • Everything related to navigation and routing is not available.

Constraints validation callback

In order to have a third-party library validate an attribute based on constraints, a constraintsValidation callback can be provided. The underlying function receives a constraints object and returns an attribute validation callback. Here's an example for using Validate.js:

Copy
import { validate } from "validate.js";

Scrivito.configure({
  tenant: "123456789123456789",

  constraintsValidation: constraints => {
    return (name, { value }) => {
      return validate(
        { [name]: value },
        { [name]: constraints },
        { format: "flat" }
      );
    };
  }
});