This class generates URLs for files based on the URLs set in the configuration.

It includes a basic cache buster that adds a parameter with the full version to core and library files (e.g. ?version=1.2.3). You can also implement other means of busting caches by implementing IUrlGenerator yourself. It would for example be possible to adding a generic cache buster string instead of adding the version. If you decide to do this, you must be aware of the fact that the JavaScript client generates URLs dynamically in two cases (at the time of writing), both in h5peditor.js:contentUpgrade. This function uses H5PIntegration.pluginCacheBuster, which can be customized by overriding H5PEditor.cacheBusterGenerator.

UrlGenerator requires these values to be set in config:

  • baseUrl
  • contentFilesUrlPlayerOverride (includes placeholder! See documentation of config for details!)
  • contentUserDataUrl
  • coreUrl
  • downloadUrl
  • editorLibraryUrl
  • h5pVersion
  • librariesUrl
  • paramsUrl
  • playUrl
  • setFinishedUrl
  • temporaryFilesUrl

The UrlGenerator can also be used to inject CSRF tokens into URLs for POST requests that are sent by the H5P editor core (Joubel's code) over which you don't have any control. You can then check the CSRF tokens in your middleware to authenticate requests.

Implements

Constructors

  • Parameters

    • config: IH5PConfig

      the config

    • OptionalcsrfProtection: {
          protectAjax: boolean;
          protectContentUserData: boolean;
          protectSetFinished: boolean;
          queryParamGenerator: (user: IUser) => { name: string; value: string };
      }

      (optional) If used, you must pass in a function that returns a CSRF query parameter for the user for who a URL is generated; the query parameter will be appended to URLs like this: "baseUrl/ajax/?name=value&action=..." You must specify which routes you want to be protected. If you don't pass in a csrfProtection object, no CSRF tokens will be added to URLs.

    Returns UrlGenerator

Methods

  • Generates a URL at which the resources of the content can be loaded. Should be undefined if the default content serving mechanism is used. You only have to return a URL here if you want to change the hostname of the route, e.g. if the content files can be requested directly from S3 or a CDN.

    Parameters

    • contentId: string

    Returns string

    the URL or undefined (Example: http://127.0.0.1:9000/s3bucket/123`)

  • Generates a URL to which the user data can be sent.

    Parameters

    • user: IUser

      the user who is currently accessing the h5p object

    • OptionalcontextId: string

      allows implementation to have multiple user data objects for one h5p content object

    • OptionalasUserId: string
    • Optionaloptions: { readonly?: boolean }

    Returns string

  • Generates a URL by which the content can be globally identified (or accessed). The URL is used when generating ids for object in xAPI statements, so it could also be a fake URL that is only used to uniquely identify the content. It could also simply be the contentId. (default behavior) If you store the generated xAPI statement in a neutral LRS and users will see or be able to click the URL, you should customize the URL by overriding this method.

    Parameters

    • contentId: string

    Returns string