what

Preview what you are writing

Draw a page's last save with the public page's own component, before it is published.

Allow 15–20 minutes. This is an editorial estimate. Continue in the same my-site folder, signed in as the editor, with the draft from lessons 15 and 16.

Read

Start at L16. You can create and edit drafts, but you cannot yet see their saved content through the public page's component. This lesson adds that preview.

The preview is an admin page that reads a page's working copy, which is your last save, and draws its article with the same component the public page uses, components/page-content.html. The preview keeps the admin header and footer, but the article is drawn by that one component, so a change to the component changes both pages.

The preview shows what was saved. Text typed in the edit form and not saved yet is not included: save first, then preview. Saving still does not change the public site.

The preview lives in the admin folder, so only a signed-in editor can open it. The server tells the browser not to keep a copy, and a static build never writes 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-L17.

2. Add the preview page. Create site/admin/pages/preview/[id].html, with the square brackets in the file name: /admin/pages/preview/9 opens it with params.id set to 9. copy = "working" reads the last save instead of the published copy. The note uses page._published, which the server fills in for editors: it is true when the page has a published copy.

site/admin/pages/preview/[id].html
<what>
title = "Preview | Admin | My website"
description = "A page's last save, drawn the way visitors will see it."
fetch.page.url = "local:pages"
fetch.page.copy = "working"
fetch.page.filter = "id=#params.id#"
fetch.page.single = true
</what>

<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.<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#"/>
<p><a href="/admin/pages/edit/#page.id#">Back to editing</a></p>

3. Link the edit page to it. Replace site/admin/pages/edit/[id].html. Only the paragraph with the preview link is new.

site/admin/pages/edit/[id].html
<what>
title = "Edit page | Admin | My website"
description = "Edit a page's working copy."
fetch.page.url = "local:pages"
fetch.page.copy = "working"
fetch.page.filter = "id=#params.id#"
fetch.page.single = true
</what>

<h1>Edit page</h1>
<p>You are editing the working copy of <strong>#page.title#</strong> (revision #page._revision#). Saving changes the working copy only: visitors keep seeing the published copy until the page is published.</p>
<p><a href="/admin/pages/preview/#page.id#">Preview your last save</a>: it draws this page as you last saved it with the public page's component. Anything typed below and not saved yet is not included.</p>
<if flash.error>
<div class="form-error" role="alert">
  <p>#flash.error#</p>
  <ul>
    <if errors.title><li>Title: #errors.title#</li></if>
    <if errors.summary><li>Summary: #errors.summary#</li></if>
    <if errors.body><li>Body: #errors.body#</li></if>
  </ul>
</div>
</if>
<!-- After a refused save, the form shows what was typed and the revision it was opened with. -->
<if old>
<form class="sign-in" method="post" action="/w-action/pages/#page.id#?w-redirect=/admin">
  <input type="hidden" name="_revision" value="#old._revision#">
  <p>Address: <strong>/pages/#page.slug#</strong> (fixed when the page was created)</p>
  <div class="field">
    <label for="title">Title</label>
    <input id="title" name="title" type="text" maxlength="120" value="#old.title#" aria-describedby="title-error">
    <span id="title-error" class="field-error">#errors.title#</span>
  </div>
  <div class="field">
    <label for="summary">Summary</label>
    <input id="summary" name="summary" type="text" maxlength="280" value="#old.summary#" aria-describedby="summary-error">
    <span id="summary-error" class="field-error">#errors.summary#</span>
  </div>
  <div class="field">
    <label for="body">Body</label>
    <textarea id="body" name="body" rows="8" maxlength="20000" aria-describedby="body-error">#old.body#</textarea>
    <span id="body-error" class="field-error">#errors.body#</span>
  </div>
  <button type="submit">Save changes</button>
</form>
<else/>
<form class="sign-in" method="post" action="/w-action/pages/#page.id#?w-redirect=/admin">
  <input type="hidden" name="_revision" value="#page._revision#">
  <p>Address: <strong>/pages/#page.slug#</strong> (fixed when the page was created)</p>
  <div class="field">
    <label for="title">Title</label>
    <input id="title" name="title" type="text" maxlength="120" value="#page.title#" aria-describedby="title-error">
    <span id="title-error" class="field-error">#errors.title#</span>
  </div>
  <div class="field">
    <label for="summary">Summary</label>
    <input id="summary" name="summary" type="text" maxlength="280" value="#page.summary#" aria-describedby="summary-error">
    <span id="summary-error" class="field-error">#errors.summary#</span>
  </div>
  <div class="field">
    <label for="body">Body</label>
    <textarea id="body" name="body" rows="8" maxlength="20000" aria-describedby="body-error">#page.body#</textarea>
    <span id="body-error" class="field-error">#errors.body#</span>
  </div>
  <button type="submit">Save changes</button>
</form>
</if>

4. Style the note. Replace static/styles.css. Only the last rule is new.

static/styles.css
/* 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; }

5. Start the server again with run-what dev. Nothing is published on this start.

Check

  1. Signed in, open /admin, follow your draft's title in All pages, then Preview your last save. The note shows the revision and says “This page is not published, so visitors cannot see it.” The title, summary and body appear as they will on the public page, with quotes and angle brackets shown as text.
  2. Follow Back to editing, change the summary without saving, and follow the preview link: the preview still shows the saved summary. Go back, change the summary again, save, and preview: the new summary appears and the revision has grown by one. /pages still does not list the draft.
  3. Open one of the eight published pages from All pages, change its title, save, and preview it. The preview shows the new title and says “Visitors see the published copy.” Its public address still shows the old title.
  4. Sign out and open the preview address again: you are sent to the sign-in page.

Exercise

Add the word “Page:” before #heading# in components/page-content.html and save. Open a public page and a preview side by side, and explain in two sentences why both changed. Then remove the word again.

If something goes wrong

  • The preview shows the old text. The preview shows your last save only. Save the form first, and check that the revision in the note has grown.
  • The preview shows the published title, not your save. The fetch is missing copy = "working"; check step 2.
  • The preview is “Page not found”. The id in the address does not match a page; open the preview from the edit page.
  • You are sent to the sign-in page. You are signed out. Sign in as the editor and open the preview again.
  • “You don't have permission”. You are signed in with an account that is not an editor.
  • Start again from a known state. Download the L17 checkpoint into a new, empty folder; never unpack it over your project. It has no database and no account: run run-what admin add editor in the new folder, then run-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, then preview it.

Next: L18: publish deliberately.