Add head code to public pages

v0.0.3

Some tags belong in the head of every public page: an analytics script, a search engine's verification tag, a font link. Mishka CMS keeps them as head code entries of a site and draws them on that site's public pages. This guide adds an analytics script and checks that it loads under the site's Content Security Policy.

Before you begin

  • Sign in as a master. The code runs for every visitor of the site, so only a master writes it, and the master needs admin:dashboard:runtime:head_code or a name that covers it, such as admin:dashboard:runtime:*. A site account does not see the Head code button, whatever it holds. See Permission names.
  • Have the tags ready: the whole script, meta, link or noscript tags your provider gives you, and the origins the code loads from or sends data to, such as https://plausible.io.

Add an entry

  1. In the admin sidebar, under Runtime, click Pages.
  2. Click Head code in the header. The window lists the site's entries under Your entries and, under From extensions, what extensions add.
  3. Click Add entry. The form is titled New entry.
  4. Choose the Site, and enter a Name for the people who keep the entry, such as Plausible.
  5. Paste the tags in Code.
  6. Choose Where the entry goes: All pages, Only these pages or All pages except these. For the last two, click + Add page under Pages and enter each page's address or path pattern, such as /pricing or /blog/:slug. A path matches the page's address or its stored pattern exactly.
  7. Click + Add origin under Allowed origins and enter each origin the code reaches, such as https://plausible.io.
  8. Switch Published on to serve the entry. A draft is saved and kept out of the pages until you publish it.
  9. Click Create. The page answers Head code entry created. When you edit an entry, the button is Save Changes and the page answers Head code entry updated.

The list shows each entry with its name, where it goes, its status (Draft or Published v1, with the number of its versions) and, for a master, its site. To see what reaches one page, click View on its row in the Pages list and open the Configuration tab. Under Head code it lists each entry that reaches the page, with its scope, or says No head code entry reaches this page. Click an entry to edit it in the Head code window. Every save is a version in the activity log.

What the CMS keeps

The CMS parses the code and never trusts it as HTML:

  • Only script, meta, link and noscript tags stay. A noscript keeps only its meta and link tags.
  • An attribute named on…, nonce or http-equiv is refused, and so is a src or href that is not an http or https address or a path on the site. The message names the attribute and the tag. Put an inline handler's code in a script tag, and leave the nonce out, because the page gives each script its own.
  • A script with no src and no code, and a link with no href, are dropped. Code that keeps no tag at all is refused with Nothing in this code can go in a page's head. Use script, meta, link or noscript tags.
  • The code is at most 20 KB, and a site holds at most 20 entries.
  • Each origin is https:// and a host, with an optional port, and nothing after it. An entry that is not for all pages needs at least one page path that starts with /.

Every script is drawn with the response's nonce, so it runs whatever its src. The allowed origins are added to script-src and connect-src of the Content Security Policy on that site's public pages, which lets the script load and send its data.

Check that it works

  1. Open a public page of the site and view its source. The script tag sits in the head and carries a nonce.
  2. Open the browser console. The script runs, and the console shows no Content Security Policy violation. A violation means an origin is missing from Allowed origins.
  3. Open an admin page or the visual editor. They never get head code.

When a visitor moves to another page without a reload, the CMS adds the head code of the new page and removes the code that no longer applies. A script that was removed has already run and stays loaded until the next full page load.

Edit, switch off or remove an entry

  • Click Edit on an entry. Its form unfolds in the row. Change it and save. To stop serving it, switch Published off.
  • Click Delete and confirm to archive an entry. An archived entry is not served and cannot be edited until you click Restore. Delete permanently removes it and its versions.

A change reaches every server at once, and no query runs for it on a visit.

Example: an analytics script on every page

  1. Open Pages, click Head code and click Add entry.
  2. Set Name to Plausible.
  3. Paste this in Code:
    html
    <script defer data-domain="example.com" src="https://plausible.io/js/script.js"></script>
  4. Leave Where on All pages. Add https://plausible.io under Allowed origins.
  5. Switch Published on and click Create.
  6. Open the site's home page and check its source for the script tag.

Next steps