Publish deliberately
Let the editor publish the revision on screen, and have the server refuse any publish or unpublish whose revision or publication version is out of date.
Allow 25–35 minutes. This is an editorial estimate. Continue in the same my-site folder, signed in as the editor, with the draft from lessons 15 to 17.
Read
Start at L17. The editor can save and preview, but visitors still read only the eight pages published when the database was first filled. This lesson adds Publish and Unpublish.
Publish copies the page's last save, its public fields (title, slug, summary and body), into the published copy that visitors read. Saving again changes only the working copy: visitors keep the published copy until you publish again. Unpublish removes the published copy, so the public address answers “Page not found” and the page leaves the listing and search. The working copy stays.
The buttons are on the preview, so you publish what you are looking at. Each form sends two hidden numbers: _revision, the revision on screen, and _live_version, which counts every publish and unpublish of the page. If either has changed since the preview was opened (you saved in another tab, or published from another preview), the server refuses with “This page changed since you opened it.” and that request changes nothing.
After a refusal the preview shows the latest save but leaves out the buttons. Read the page again, then follow Reload preview before trying again: the reloaded preview's buttons carry the current numbers. The server cannot tell whether you read the page; what it guarantees is that a request with an out-of-date revision or publication version is refused.
Only signed-in editors can publish: the publish rule names the role, and nobody can publish without it.
Change
1. Keep a copy. Stop the dev server with Ctrl+C. Copy the whole my-site folder to a new folder such as my-site-before-L18.
2. Let editors publish. Replace what.toml. Only the publish line and its comment are new.
# What Project Configuration
[server]
port = 8085
host = "127.0.0.1"
[session]
enabled = true
cookie_name = "w_session"
max_age = 604800
database = "data/sessions.db"
# The database owns the pages now. On the first start after this change the
# server copies data/pages.json into the database once, keeps every id, and
# records that it did. It never reads the file again.
[import]
pages = "data/pages.json"
# Editors may create and change pages; deleting them comes later. No two
# pages may share a slug: the database itself refuses the second one.
[collections.pages]
create = "editor"
update = "editor"
delete = "none"
unique = ["slug"]
# Visitors read published copies of these fields only. A page created now
# stays private until it is published.
public = ["title", "slug", "summary", "body"]
# Editors read the working copies, drafts included, from the admin area.
working_read = "editor"
# Editors publish. Publish copies a page's last save (its public fields) into
# the published copy visitors read; Unpublish removes that copy. Nobody can
# publish until this line exists, and only signed-in accounts can hold it.
publish = "editor"
# The server checks every new or changed page itself, whatever form sent it.
fields.writable = ["title", "slug", "summary", "body"]
fields.required = ["title", "slug"]
fields.max_length = { title = 120, summary = 280, body = 20000 }
fields.slug = ["slug"]
fields.immutable = ["slug"]
# Sign-in. The site keeps its own editor accounts in data/app.db. You add one
# from the terminal with `run-what admin add`: there is no built-in account and
# no password in any file of the project.
[auth]
enabled = true
provider = "local"
login_path = "/login"
after_login = "/"
3. Add the buttons to the preview. Replace site/admin/pages/preview/[id].html. The alert at the top appears only after a refused publish or unpublish, and then the forms are left out. & in the form addresses is how HTML writes a plain & inside an attribute.
<what>
title = "Preview | Admin | My website"
description = "A page's last save, drawn with the public page's component, ready to publish."
fetch.page.url = "local:pages"
fetch.page.copy = "working"
fetch.page.filter = "id=#params.id#"
fetch.page.single = true
</what>
<if flash.error>
<div class="form-error" role="alert">
<p>#flash.error#</p>
<p>This request did not publish or unpublish the page. The copy below is the latest save, which may not be the one you previewed: read it again first.</p>
<p><a href="/admin/pages/preview/#page.id#">Reload preview before trying again</a></p>
</div>
</if>
<p class="preview-note">Preview of your last save, revision #page._revision#. Saving does not update the public site. <if page._published>Visitors see the published copy, revision #page._published_revision#.<else/>This page is not published, so visitors cannot see it.</if></p>
<what-page-content heading="#page.title#" summary="#page.summary#" body="#page.body#"/>
<!-- Both forms send the revision on screen and the live version it was
opened with. After a refusal they are left out: reload first. -->
<unless flash.error>
<div class="publish-controls">
<form class="sign-in" method="post" action="/w-action/pages/#page.id#?w-action=publish&w-redirect=/admin/pages/preview/#page.id#">
<input type="hidden" name="_revision" value="#page._revision#">
<input type="hidden" name="_live_version" value="#page._live_version#">
<button type="submit">Publish revision #page._revision#</button>
</form>
<if page._published>
<form class="sign-in unpublish" method="post" action="/w-action/pages/#page.id#?w-action=unpublish&w-redirect=/admin/pages/preview/#page.id#">
<input type="hidden" name="_revision" value="#page._revision#">
<input type="hidden" name="_live_version" value="#page._live_version#">
<button type="submit">Unpublish</button>
</form>
</if>
</div>
</unless>
<p><a href="/admin/pages/edit/#page.id#">Back to editing</a></p>
4. Show what is published. Replace site/admin/index.html. All pages gains a Published column.
<what>
title = "Admin | My website"
description = "The editing area: every page, published or draft."
fetch.pages = "local:pages"
fetch.drafts.url = "local:pages"
fetch.drafts.copy = "working"
</what>
<h1>Admin</h1>
<p>Only a signed-in editor can open this page. <a href="/admin/pages/new">Create a new page</a>: it starts as a draft that only editors see.</p>
<if pages>
<div class="table-wrap">
<table class="inventory">
<caption>Published pages</caption>
<thead>
<tr><th scope="col">Id</th><th scope="col">Title</th><th scope="col">Slug</th></tr>
</thead>
<tbody>
<loop data="#pages#" as="page">
<tr><td>#page.id#</td><td><a href="/pages/#page.slug|urlencode#">#page.title#</a></td><td><code>#page.slug#</code></td></tr>
</loop>
</tbody>
</table>
</div>
<else/>
<p class="empty-state">No pages yet.</p>
</if>
<h2>All pages</h2>
<p>The working copies, drafts included. Visitors never see this list. Follow a title to edit that page; its preview publishes it.</p>
<if drafts>
<div class="table-wrap">
<table class="inventory">
<caption>All pages (working copies)</caption>
<thead>
<tr><th scope="col">Id</th><th scope="col">Title</th><th scope="col">Slug</th><th scope="col">Revision</th><th scope="col">Published</th></tr>
</thead>
<tbody>
<loop data="#drafts#" as="page">
<tr><td>#page.id#</td><td><a href="/admin/pages/edit/#page.id#">#page.title#</a></td><td><code>#page.slug#</code></td><td>#page._revision#</td><td><if page._published>revision #page._published_revision#<else/>not published</if></td></tr>
</loop>
</tbody>
</table>
</div>
</if>
5. Place the buttons. Replace static/styles.css. Only the last two rules are new: they put Publish and Unpublish side by side and make Unpublish the quieter button.
/* Framework styles are supplied automatically. These rules style our site. */
.site-body {
margin: 0;
color: #26372c;
background: #faf8f2;
font-family: system-ui, sans-serif;
line-height: 1.65;
}
.site-header, .site-main, .site-footer {
width: min(100% - 2rem, 48rem);
margin-inline: auto;
}
.site-header {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 1rem;
padding-block: 1.5rem;
border-bottom: 1px solid #c9cec4;
}
.site-name { font-size: 1.2rem; font-weight: 750; }
.site-nav { display: flex; flex-wrap: wrap; gap: 1rem; }
.site-body a { color: #315f40; text-underline-offset: 0.2em; }
.site-body a:focus-visible {
outline: 3px solid #825414;
outline-offset: 4px;
}
.site-main { padding-block: 2.5rem; }
.site-main h1 { font-size: clamp(2rem, 7vw, 3rem); line-height: 1.15; }
.site-main p { max-width: 65ch; }
.site-main figure { margin: 2rem 0; }
.site-image { display: block; max-width: 100%; height: auto; border-radius: 1rem; }
.site-main figcaption { margin-top: 0.6rem; color: #4b594e; }
.site-footer { border-top: 1px solid #c9cec4; padding-block: 1.5rem; }
.skip-link {
position: absolute;
top: -10rem;
left: 1rem;
padding: 0.6rem 1rem;
background: #fff;
z-index: 10;
}
.skip-link:focus { top: 1rem; }
.card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr)); gap: 1.25rem; }
.content-card { padding: 1.5rem; border: 1px solid #c9cec4; border-radius: 1rem; background: #fffdf8; }
.content-card h2 { margin-top: 0; font-size: 1.4rem; }
.card-note { font-size: 0.9rem; color: #4b594e; }
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}
.empty-state { padding: 1rem 1.25rem; border: 1px dashed #c9cec4; border-radius: 1rem; }
.table-wrap { overflow-x: auto; }
.inventory { border-collapse: collapse; width: 100%; min-width: 28rem; }
.inventory caption { text-align: left; padding-bottom: 0.6rem; color: #4b594e; }
.inventory th, .inventory td { text-align: left; padding: 0.6rem 0.8rem; border-bottom: 1px solid #c9cec4; vertical-align: top; }
.inventory thead th { border-bottom-width: 2px; }
.listing-controls { display: flex; flex-wrap: wrap; align-items: end; gap: 0.75rem 1rem; margin-bottom: 1rem; }
.listing-controls .field { display: grid; gap: 0.25rem; }
.listing-controls input, .listing-controls select, .listing-controls button { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.7rem; border: 1px solid #8a9586; border-radius: 0.5rem; background: #fffdf8; color: inherit; }
.listing-controls input { min-width: min(100%, 16rem); }
.listing-controls button { background: #315f40; border-color: #315f40; color: #fff; cursor: pointer; }
.listing-controls :focus-visible { outline: 3px solid #825414; outline-offset: 2px; }
.result-count { color: #4b594e; }
.pager { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 1rem; margin-top: 1.5rem; }
.pager [aria-current] { font-weight: 650; }
/* L13: the account corner and the sign-in form */
.site-account { display: flex; flex-wrap: wrap; align-items: center; gap: 0.75rem; }
.site-account form { margin: 0; }
.site-account button, .sign-in button { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.9rem; border: 1px solid #315f40; border-radius: 0.5rem; background: #315f40; color: #fff; cursor: pointer; }
.sign-in { display: grid; gap: 1rem; max-width: 24rem; }
.sign-in .field { display: grid; gap: 0.25rem; }
.sign-in input { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.7rem; border: 1px solid #8a9586; border-radius: 0.5rem; background: #fffdf8; color: inherit; }
.form-error { padding: 0.75rem 1rem; border-left: 4px solid #a33a2e; background: #fbeeec; }
/* L17: the note above a preview */
.preview-note { padding: 0.75rem 1rem; border-left: 4px solid #315f40; background: #edf3ec; }
/* L18: Publish and Unpublish side by side; Unpublish is the quieter button */
.publish-controls { display: flex; flex-wrap: wrap; gap: 0.75rem; margin-block: 1.5rem; }
.publish-controls .unpublish button { background: #fffdf8; color: #315f40; }
6. Start the server again with run-what dev. Nothing is published on this start.
Check
- Open /pages in a private window: this is your visitor. Signed in as the editor, preview your draft and choose Publish revision N. The preview now says “Visitors see the published copy, revision N.” Reload the visitor's window: the page is listed, its address opens, and Matching pages counts one more.
- Edit the page and save. Reload the visitor's page: it still shows the published text. Preview the page and publish the new revision: the visitor sees the change after a reload.
- Open the preview in two tabs. Publish from the first. Publish from the second: it says “This page changed since you opened it.” and “This request did not publish or unpublish the page.”, and there are no buttons. Follow Reload preview before trying again: the buttons come back.
- Choose Unpublish. The visitor's page answers “Page not found”, and Matching pages counts one fewer. All pages still lists the page as “not published”.
- Stop and start the server: every page keeps its state, and nothing is published on the start.
Exercise
Publish a page, then edit and save it twice without publishing. Explain what visitors see, what the preview shows, and which revision the Published column and the preview note name.
If something goes wrong
- “You don't have permission to publish that item.” The
publishline is missing fromwhat.toml, the server was not restarted after you added it, or you are not signed in as the editor. - “This page changed since you opened it.” A save, a publish or an unpublish happened after this preview was opened. Read the page again, then follow Reload preview before trying again.
- “Reload the page and try again.” A hidden
_revisionor_live_versionfield is missing; check step 3. - The public page still shows the old text. Saving does not publish. Preview the page and choose Publish.
- Start again from a known state. Download the L18 checkpoint into a new, empty folder; never unpack it over your project. It has no database and no account: run
run-what admin add editorin the new folder, thenrun-what dev. The first start imports the eight pages and publishes them once; it has no drafts, so create one with the form from lesson 15, preview it, then publish it.