Skip to content

Latest commit

 

History

History
175 lines (134 loc) · 8.4 KB

setup.md

File metadata and controls

175 lines (134 loc) · 8.4 KB

Setting up the player

Which setup

An H5P package is JavaScript. The player plays every package as data by default, none of its own code running; trusted, for packages you made, runs that code on the origin that serves the frame.

Your site Setup
Needs the element's API (results to your backend, resume="host", cmi5, a file picker) and plays packages you made Install
Plays packages you did not make Install, leaving trusted off, or Embed
Has signed-in users Embed, from a separate domain you run, or Install with trusted on no package
Cannot host a file (page builder, hosted CMS, LMS page) Embed

Installed, you get the whole API. A package plays as data, none of its own code running and its content checked, as an H5P plugin checks an author's upload, unless you set trusted, which runs its code as your site: Packages you did not make. Embedded, the player page that @missing-elements/h5p-embed writes is framed from a domain that holds nothing else, so a package never sees your site's cookies, storage or page, and you get its results as relayed xAPI.

Requirements

  • The page on https://, or localhost while developing: the player needs a Service Worker.
  • CORS headers on the package host, so the page can read the .h5p. Range support is optional: without it the archive is downloaded once and played from the cache. With an ETag or a Last-Modified header, the player can tell an archive republished at the same URL from the old one, and a reload after the browser stopped its worker reads nothing of the archive but one byte. A page reads Last-Modified from any origin, and an ETag from another origin only once the host adds Access-Control-Expose-Headers: ETag.
  • A browser with Service Workers, the Cache API, IndexedDB, Web Locks and DecompressionStream; Compatibility says which are tested.

Install with a bundler

Vite or webpack 5:

npm install @missing-elements/h5p-offline-player @missing-elements/h5p-runtime
import '@missing-elements/h5p-offline-player'
import { runtime } from '@missing-elements/h5p-runtime'

document.querySelector('h5p-player').runtime = runtime

The bundler emits the Service Worker, the runtime and its fonts as assets; nothing to copy. The runtime is a separate package because it is GPL-3.0 (see the README's Licence). A bundler that does not emit new URL(…, import.meta.url) assets, esbuild and Rollup among them, leaves the worker and runtime files behind: serve both packages' dist/ as under Install without a build step, and set sw and assets-base. Rollup with @web/rollup-plugin-import-meta-assets stops at the element's default frame-assets/ URL.

For the library pack served from your own site rather than jsDelivr, install @missing-elements/h5p-libraries too and hand over its pack export, which the bundler emits as one asset per library:

import { pack } from '@missing-elements/h5p-libraries'

document.querySelector('h5p-player').pack = pack

On an AI site builder, one prompt can make this install. Lovable did, from Embed an H5P file using https://github.com/missing-elements/h5p-offline-player and the .h5p uploaded in the chat (the example it built): it bundled the element, served the runtime from public/h5p-runtime/ with assets-base, and set libraries="pack". Its hosting answers Range, so the package is read in place. Other builders have not been tried.

Install without a build step

Copy the files onto your own site, so that nothing a learner's browser loads comes from anyone else's server, the library pack included once you copy it below; npm is only how you get them. public/ stands for the folder your site is served from:

npm install @missing-elements/h5p-offline-player @missing-elements/h5p-runtime
cp -r node_modules/@missing-elements/h5p-offline-player/dist public/h5p-player
cp -r node_modules/@missing-elements/h5p-runtime/dist public/h5p-player/frame-assets
<script type="module" src="/h5p-player/h5p-player.js"></script>
<h5p-player src="https://host.example/course.h5p"></h5p-player>

The element finds the Service Worker and the runtime beside its own script, so it needs no other attribute. A package's libraries come from the pack, which loads from jsDelivr. To serve it from your site, copy it too, with the list of its libraries' licences, and name its folder with pack-base="/h5p-libraries/":

npm install @missing-elements/h5p-libraries
cp -r node_modules/@missing-elements/h5p-libraries/parts public/h5p-libraries
cp node_modules/@missing-elements/h5p-libraries/libraries.txt public/h5p-libraries/

To upgrade, delete the folders and copy them again. Serve them with Cache-Control: no-cache, or with no caching header at all: the file names stay the same from one version to the next, so a copy a browser kept would run against the new Service Worker (Upgrading).

From jsDelivr

Or load the element and the runtime from jsDelivr, and copy only the Service Worker:

<script type="module"
  src="https://cdn.jsdelivr.net/npm/@missing-elements/h5p-offline-player@2.0.0/dist/h5p-player.js"></script>
<h5p-player src="https://host.example/course.h5p" sw="/h5p-sw.js"
            assets-base="https://cdn.jsdelivr.net/npm/@missing-elements/h5p-runtime@1.0.4/dist/"></h5p-player>

Copy dist/h5p-sw.js of the same version onto your own site: browsers register a Service Worker only from the page's origin. Its scope is /h5p/, so an existing site worker is left alone. Pin the version, and copy the worker again whenever you change it; the element warns when the two differ. Every learner's browser then fetches the player from jsDelivr, as it does the pack's libraries unless pack-base names a copy of your own. Do not serve packages from jsDelivr: it cuts ranges from a compressed copy, and a range-read package breaks.

Embed

@missing-elements/h5p-embed writes the player page as a static site:

npx @missing-elements/h5p-embed@latest h5p-player --packages https://host.example

It needs Node 22 or later. Write @latest: without it, npm on an older Node quietly installs the last version that supported it, which writes a player from before 1.0.

Every package plays untrusted, none of its own code running, with the site's own libraries. --trusted https://host.example names the origins whose packages play with their own code, for packages you made.

Deploy it to a separate registrable domain that holds nothing else (a subdomain would receive your site's cookies) and frame it:

<iframe src="https://h5p-player.example.net/?src=https://host.example/course.h5p&xapi=https://your-site.example"
        allow="fullscreen; microphone; camera" style="width: 100%; border: 0"></iframe>
<script src="https://h5p-player.example.net/resizer.js"></script>

microphone is for Audio Recorder and Speak the Words. camera is for content that films: no content type of the library pack that the player runs does (AR Scavenger does not run, Troubleshooting), so leave it out unless a content type of your own needs it. A frame from another site gets neither unless the page allows it. The embed package's README has the page's query parameters, the policy files it writes for each kind of host, and the SCORM package it can write instead.

A page in the demo's look

The player ships the demo's page styles as @missing-elements/h5p-offline-player/page.css: its colours for light and dark, the bar, buttons, the status strip and the player's box while it loads; dist/mark.svg is the mark. h5p-verify --preview and the H5P Preview extension draw their pages with it. A page of your own can, through a bundler that takes CSS (Vite, webpack 5 with a CSS loader):

import '@missing-elements/h5p-offline-player/page.css'

Without a bundler, copy dist/page.css beside your page, as the element's own files are, and link it. It names no other file.

When something goes wrong

Troubleshooting lists the symptoms, causes and fixes. A site that allows one Service Worker, or must work offline, mounts the player in its own worker: Single-worker hosts and upgrades.