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_codeor a name that covers it, such asadmin: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,linkornoscripttags your provider gives you, and the origins the code loads from or sends data to, such ashttps://plausible.io.
Add an entry
- In the admin sidebar, under Runtime, click Pages.
- Click Head code in the header. The window lists the site's entries under Your entries and, under From extensions, what extensions add.
- Click Add entry. The form is titled New entry.
- Choose the Site, and enter a Name for the people who keep the entry, such as
Plausible. - Paste the tags in Code.
- 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
/pricingor/blog/:slug. A path matches the page's address or its stored pattern exactly. - Click + Add origin under Allowed origins and enter each origin the code reaches, such as
https://plausible.io. - Switch Published on to serve the entry. A draft is saved and kept out of the pages until you publish it.
- 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,linkandnoscripttags stay. Anoscriptkeeps only itsmetaandlinktags. - An attribute named
on…,nonceorhttp-equivis refused, and so is asrcorhrefthat is not anhttporhttpsaddress or a path on the site. The message names the attribute and the tag. Put an inline handler's code in ascripttag, and leave thenonceout, because the page gives each script its own. - A
scriptwith nosrcand no code, and alinkwith nohref, 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
- Open a public page of the site and view its source. The script tag sits in the
headand carries anonce. - 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.
- 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
- Open Pages, click Head code and click Add entry.
- Set Name to
Plausible. - Paste this in Code:
html
<script defer data-domain="example.com" src="https://plausible.io/js/script.js"></script> - Leave Where on All pages. Add
https://plausible.iounder Allowed origins. - Switch Published on and click Create.
- Open the site's home page and check its source for the script tag.
Next steps
- Add custom CSS, JS hooks and components: code that belongs to your own pages rather than the head.
- Set a page's SEO and share card: the title, description and link-preview tags of one page.
- Plugin events:
runtime:page:headlets an extension add head tags too. - Sites, masters and global records: why a master writes this code.