Eliom 13, Ocsigen Start 9 and Ocsigen Toolkit 5
Eliom 13, Ocsigen Start 9 and Ocsigen Toolkit 5 are out. They are released together, as one modernized stack: hierarchical module names everywhere, a shared build system, new project templates, persistent data stored as JSON instead of Marshal, proper HTTP status codes, and a long list of fixes in sessions, forms and the client. This is a major release with breaking changes, so please read the Upgrading section before you move an application.
Eliom is the multi-tier framework of Ocsigen: one OCaml program describes both the server and the client parts of a web or mobile application, and links, forms and services are checked by the type system. Ocsigen Toolkit is a set of client-server widgets built on it, and Ocsigen Start is a library and a project template with users, sessions and notifications ready to use.
New module names
The three libraries now expose hierarchical module names. Eliom_service becomes Eliom.Service, Ot_popup becomes Ot.Popup, Os_session becomes Os.Session, and so on:
(* Eliom 12 *)
let main_service =
Eliom_service.create ~path:(Eliom_service.Path [])
~meth:(Eliom_service.Get Eliom_parameter.unit) ()
(* Eliom 13 *)
let main_service =
Eliom.Service.create ~path:(Eliom.Service.Path [])
~meth:(Eliom.Service.Get Eliom.Parameter.unit) ()The same change happened in the recent Ocsigen Server 8, which Eliom 13 now requires. The tools that generate Eliom code, ocsigen-i18n 6 and ocsigen-ppx-rpc 2, now emit the new names.
You do not have to rename everything at once. The new eliom-compat, ocsigen-toolkit-compat and ocsigen-start-compat packages keep the old names as aliases of the new modules.
Build and templates
The templates now build Eliom applications and libraries with plain dune rules, generated by the new ocsigen-dune-rules tool. Ocsigen Toolkit and Ocsigen Start use it too: the library of Ocsigen Start is no longer built with Makefiles, and Eliom drops its ocamlbuild plugin and its topkg files.
The templates of eliom-distillery were rewritten:
app, the template of Eliom, gives a project that runs either as its own executable (dune exec myapp), with Ocsigen Server linked as a library, or as a module loaded byocsigenserverwith a configuration file;os, the template of Ocsigen Start, puts the code of the application in a library, with a small executable that links the server. The WebAssembly client is on by default, and one variable,USE_WASM, controls it for both the configuration file and the executable. The database schema is now a file that can be replayed:make db-updatecreates the tables on a new database, and brings an existing one up to date.
Persistent data as JSON
Eliom used to store persistent data (Eliom references, session data, persistent cookies, session groups) with OCaml's Marshal. This format depends on the OCaml version and on the exact types of the program, and reading a value with the wrong type can crash the server instead of giving an error. Eliom 13 stores all these values as JSON, with Deriving_Json. The format is stable and human-readable, and the API is now typed: you give the codec of the value.
type prefs = {lang : string; dark_mode : bool} [@@deriving json]
let prefs =
Eliom.Reference.eref ~scope:Eliom.Common.default_group_scope
~persistent:("prefs", [%json: prefs])
{lang = "en"; dark_mode = false}A stored value that cannot be decoded is now treated as absent, and logged, instead of failing the request.
Important: the new data live in new tables, and the old Marshal tables are never read. After the upgrade, all the persistent data of Eliom 12 are reset to their default values. Persistent cookies are dropped, so users are logged out. This affects more than sessions: a persistent reference with the group scope often holds per-user data. For example, Ocsigen Start keeps there the tips that a user has already seen, so the tips will show again. If you cannot lose such data, export them with your Eliom 12 application before upgrading, and import them again with the new API. The old tables are kept, not deleted.
HTTP status codes
A service handler can now choose the status of its answer, and still return a normal page:
let%server article_handler id () =
let open Lwt.Syntax in
let* article = Db.find_article id in
match article with
| Some article -> Lwt.return (article_page article)
| None ->
Eliom.Registration.set_status `Not_found;
Lwt.return not_found_pageEliom.Registration.set_exn_handler can also answer with a page of the application. An unknown URL then gets a 404 page, and the client-side program displays it without leaving the application. The os template does this.
In Ocsigen Start, the error pages of Os.Page are now sent with a status that matches the error: 403 when the user may not see the page, 404 when the thing does not exist, and 500 for other errors. They were all sent with 200.
Sessions, forms and the browser
Session groups got a deep review:
- The limits on the number of sessions without a group, and on anonymous coservices, applied separately to each client subnetwork. Behind a reverse proxy, or with IPv6, this protected nothing. They now apply to the whole site.
- Services registered with the session-group scope now work: Eliom never looked them up before. In Ocsigen Start, the service session of a user joins the group of the user, so these services are shared by all the sessions of the user, and
Os.Session.disconnect_allcloses them. - Persistent session groups are now safe when several requests of the same user arrive at once in one server process, for example two logins.
On the client side, Eliom now requires a browser at least as recent as Internet Explorer 10 (2012), which removed a lot of old fallback code. A form submitted through XHR sends the button that really submitted it. A button with a formaction attribute now sends the form where it says: the browser submits it, without XHR. PUT and DELETE services called from the client send PUT and DELETE requests. Ocsigen Toolkit fixes several bugs of the WebAssembly client, for example a crash when a popup was opened from a scrolled page.
And many fixes
Eliom 13 has more than sixty bug fixes. To name a few: stateless Comet channels built from React events no longer stop after a garbage collection, discarding the state of a session group really closes the group, and client functions return rejected promises instead of raising exceptions synchronously. The syntax extension now leaves plain .ml files alone, which removes a common cause of the confusing errors about client values not linked on the client, and it gives clearer messages. The full lists are in the CHANGES files of Eliom, Ocsigen Start and Ocsigen Toolkit.
Upgrading
A short checklist for an existing application:
- export the persistent data you need to keep, before upgrading (see above);
- to keep the old module names for now, add the
-compatlibraries to your dune files, for exampleeliom-compat.serverandeliom-compat.client, andocsigenserver-compatfor the old names of Ocsigen Server 8; - update your HTML code for TyXML 5, which changes some element and attribute types;
- annotate the types of your persistent values with
[@@deriving json], and give their codec toeref ~persistentandState.create_persistent_table; - replace
Eliom.runbyEliom.App.run; the maximum numbers of sessions given to it are now plain integers; - a statically linked executable now refuses to start when a service with a path is left unregistered, as
ocsigenserveralready did; - update ocsigen-i18n to 6 and ocsigen-ppx-rpc to 2;
- the configuration options and the
Statefunctions about subnetworks were renamed or removed, and an old option is now an error; Unsafe.databuilt on the client is now parsed as HTML, as on the server, so it must never contain user input that was not sanitized.
Documentation
All the documentation is now on ocaml.org, as well as on ocsigen.org. The manuals were rewritten as odoc pages, so the manual and the API of each library are the documentation of its opam package: Eliom, Ocsigen Start and Ocsigen Toolkit.
The tutorial, which goes from a first application to advanced features, has its own opam package, ocsigen. Its documentation on ocaml.org is the tutorial, and installing it installs the whole stack that the tutorial uses. The tutorial is also on ocsigen.org.
Getting it
Eliom 13 needs OCaml 4.14 or later, and Ocsigen Start 9 needs OCaml 5.1 or later. The simplest way to get the whole stack is the ocsigen package:
$ opam update
$ opam install ocsigenYou can also install only what you need, for example opam install eliom.
To start a new project, generate it, then install its dependencies:
$ eliom-distillery -name myapp -template app # Eliom only
$ eliom-distillery -name mysite -template os # with Ocsigen Start
$ cd myapp && opam install --deps-only .The README of the generated project explains the next steps.