clay.js
  1. ClayJS
  2. Reference
  3. Plugins
  4. upgrade

upgrade

The upgrade plugin will let a document created from a Hyperclay template receive updates made to that template.

The upgrade plugin offers a document made from a Hyperclay template the newer version of that template, once per load, to whoever can edit it. Nothing is replaced until that person accepts.

Loading#

A copy whose hyper-source is on https://hyperclay.com/templates/ gets the plugin without asking, for anyone who can edit, so copies made before it existed upgrade too. A template served from anywhere else names it: ?plugins=upgrade. exclude=upgrade turns it off. It loads after first paint and checks once clay.ready resolves.

Who is asked#

The plugin asks canEdit, and it does not ask a draft: a visitor cannot act on an offer, and a draft is not the document the source would replace. On a page that is neither, nothing is fetched at all.

It is also silent when the page carries no template metas, when the network or the fetch fails, when the published HTML does not match the digest its manifest names, and when the published version is not newer.

The offer works in view as well as in edit, because it asks a capability and not a mode. Opening the menu or choosing Edit is never an upgrade: the upgrade runs only from a click on its own menu item, or on the Upgrade action of its notice.

The two metas#

A copy made from a template says where it came from:

HTML
<meta name="hyper-source" content="https://hyperclay.com/templates/kanban/owned/latest.html">
<meta name="hyper-version" content="1.0.0">

hyper-source is the published template’s HTML, and its manifest is the same URL with .json in place of .html, which holds { id, variant, version, html, sha256 }. hyper-version is the version this copy was made from. A page whose hyper-source points at itself is the source rather than a copy of one, and is offered nothing.

A copy carries a third meta, clay-template, holding the template’s id and the variant the file was made for, journal hosted in the source and journal owned in a copy the ship script publishes. The three metas travel together, right under <meta charset="utf-8">. ClayJS reads clay-template when it prepares an upgrade, and a manifest whose id and variant are not that meta’s two words stops the run with This upgrade is for a different document.

The upgrade carries what the data-rules-name="api" tag names. It reads this copy’s saved fields through that tag and the new version’s through its own, refuses with lost and names the field when a saved value would not survive, and counts a rules name the new version no longer has as discarded values, which is refused the same way. A field the new version adds starts as the new version has it, except inside a list: a list item is the owner’s own, so a field or a list the new version adds inside one starts empty there, and never takes the value of the new version’s sample item. A field the tag does not name is never read, so a copy should name every field it saves in its api tag. A block editor document is the exception: its article[data-blockeditor] and the live stores beside it are carried whole into the new version, since no rules tag can name them, and a document that declares no data at all is refused with This document does not say which parts are its data, so it cannot be upgraded safely. Nothing was changed.

The check#

The manifest is read at most once every 24 hours. The answer is cached in local storage under the manifest’s URL and the version this copy has, and a cached answer younger than 24 hours is used as it stands.

The plugin fetches the HTML the manifest names and hashes it with SHA-256. A digest that is not the manifest’s sha256 ends the check with nothing offered: bytes that do not hash to the published version are not that version. So does a version that is equal to this copy’s, or older.

The offer#

With the menu loaded, the offer is a menu item reading Upgrade to version followed by the published version, with a dot on claygoo. Without a menu it is a persistent notice with Upgrade and Not now. Not now remembers that version, and that version is not offered again.

Either one dispatches clay:upgrade-requested on the document with detail: { version, html, sha256, latestUrl }: the published version, the absolute URL of its HTML, that HTML’s digest, and the manifest’s URL. The click lands there and nothing has been written yet.

The run#

The click draws a modal. It says the page’s layout and code are replaced and that the current version stays in the document’s history, and it offers Upgrade and Cancel. A host that keeps no version history also gets Download a copy first, and Upgrade stays disabled until that copy is downloaded. In edit mode the copy is the page as it would save; in view mode, which has nothing to save, it is the stored file itself, fetched fresh from the host.

An upgrade is one document operation, so a mode switch cannot start its save barrier in the middle of one and two upgrades cannot interleave. The run then:

  1. Flushes the saves when the page is in edit, and stops if a save could not be settled.
  2. Fetches the manifest again, and checks that it is for this template and variant, that its version is newer, and that its HTML is served from the template host.
  3. Fetches the HTML, and checks its SHA-256 against the manifest once more.
  4. Runs the transformations the template declares over the page as it stands.
  5. Refuses with lost when a saved value would not survive the new version, and names it. Nothing is written in that case.
  6. Saves the new HTML through the document’s own replace, with the ETag it last saw.
  7. Reloads the page.

A refused upgrade leaves the document as it was, and the modal says why.