Your first Pagelove application

Scope and prerequisites: this is an incremental study of the client helpers, not a complete deployment or save-recovery checklist. Have a disposable hosted site and its authoring connection ready. For complete downloadable files, checked permissions and validation, conditional saves, conflicts, and cleanup, start with Build a shared note. Preserve the learning steps below; use the deployment checklist before sharing your own application.

In this tutorial we'll build a working shopping list — a page where you tick items off as you buy them, add new items, and delete the ones you no longer need. Each edit requests a server save. An accepted write survives a page reload; changing the local page alone does not confirm that it was saved.

Along the way you'll meet the heart of Pagelove: writing fragments of a page straight back to the server with PUT, POST, and DELETE, and granting permission for those writes with an authorization rule. You'll write plain HTML, CSS, and a little JavaScript — there's no database to set up and no server code to write.

Let's start with a single index.html file and build it up together.

First, let's put some scaffolding in place: an HTML page with a heading and a simple list, giving each item a checkbox so we can tick it off once we've bought it.

<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Simple Todo List</title>
    </head>
    <body>
        <h1>Shopping List</h1>
        <ul id="todo-list">
            <li><input type="checkbox">Get Milk</li>
            <li><input type="checkbox">Buy Eggs</li>
            <li><input type="checkbox">Make pancakes</li>
        </ul>
    </body>
</html>

Open index.html in your browser and you'll see the list: three items, each with a checkbox. It works, but it's not very exciting yet. Let's make it so we can visibly cross items off.

First we'll add a little CSS inside the body. It looks for any list item whose checkbox is checked and strikes the text through:

<style>
    li:has(input:checked) {
        text-decoration: line-through;
    }
</style>

Now, at the bottom of the body, we'll add a small script. When a checkbox changes, it toggles the checked attribute on the input — not just the live property. That distinction matters, and we'll see why in a moment:

<script type="module">
    document
        .querySelectorAll("input[type=checkbox]")
        .forEach((listItem) =>
            listItem.addEventListener("change", (event) => {
                event.target.toggleAttribute("checked");
            }),
        );
</script>

Try it: check a box and the whole item gets a line through it. Now reload the page. Notice that your ticks are gone — the page has reset to its original three unchecked items. For a shopping list that's no good; we need the changes to stick.

This is where Pagelove comes in.

First, let's load the Pagelove client library by adding this script inside the <head> at the top of the page:

<script type="module" src="https://pagelove.github.io/beta-js/pagelove.mjs"></script>

Now let's modify the script we just wrote so that it requests a save of the updated list item every time we change it:

<script type="module">
    document
        .querySelectorAll("input[type=checkbox]")
        .forEach((listItem) =>
            listItem.addEventListener("change", (event) => {
                event.target.toggleAttribute("checked");
                const li = event.target.closest("li");
                li.PUT();
            }),
        );
</script>

These first snippets show the successful path and omit response handling. A checked box is still a local draft until the write is accepted. Continue with save and failure recovery for an explicit conditional save that retains drafts and uncertain requests.

Limit what visitors can save

This tutorial makes a shared demonstration list. Its actor="*" rules will let any visitor change the list, including visitors who are not signed in. Anyone allowed to write can send their own HTTP requests without using our page's JavaScript. Before enabling those writes, add this hidden block to the body of index.html, outside the list:

<div hidden itemscope itemtype="https://pagelove.org/ShapeConstraint">
    <meta itemprop="resource" content="/index.html" />
    <meta itemprop="selector" content="ul#todo-list" />
    <code itemprop="permit">li</code>
    <code itemprop="permit">input[type="checkbox"]</code>
    <code itemprop="permit">input[type="checkbox"][checked]</code>
    <code itemprop="permit">button[class="delete"]</code>
</div>

This closed shape permits list items, checkbox inputs, and the delete buttons we'll add later. It allows the checkbox's type and optional checked attributes, and the delete button's class; text inside the elements is allowed. A public write that would leave an unpermitted element or attribute in the list, such as an image, script, or onclick, is rejected with 422 before it is stored. The shape applies to both replacements and appends, so the checks hold even when a caller bypasses our JavaScript.

This restricts the markup visitors can save; it does not make the list private or stop visitors changing each other's items. A personal list needs authorization rules for its owner or intended group. Store the shape using your authoring access. WebDAV deliberately bypasses shape validation, so trusted authoring credentials remain separate from visitor permissions.

Now grant permission for the public write. Pagelove won't write through the public interface unless a rule says it may. We grant that permission with an AuthorizationRule — a small block of HTML microdata we can put anywhere in the document, or even in another file. For now, let's add it to the body of index.html. This rule says: allow anyone to PUT changes to a list item inside #todo-list.

<div hidden itemscope itemtype="https://pagelove.org/AuthorizationRule">
    <meta itemprop="actor" content="*" />
    <meta itemprop="resource" content="/index.html" />
    <meta itemprop="method" content="PUT" />
    <meta itemprop="selector" content="#todo-list li" />
    <meta itemprop="action" content="allow" />
</div>

That's quite a bit of stuff! Just to make sure we're together, your index.html should now look something like this:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Simple Todo List</title>
        <script type="module" src="https://pagelove.github.io/beta-js/pagelove.mjs"></script>
    </head>
    <body>
        <style>
            li:has(input:checked) {
                text-decoration: line-through;
            }
        </style>
        <h1>Shopping List</h1>
        <ul id="todo-list">
            <li><input type="checkbox" />Get Milk</li>
            <li><input type="checkbox" />Buy Eggs</li>
            <li><input type="checkbox" />Make pancakes</li>
        </ul>
        <script type="module">
            document
                .querySelectorAll("input[type=checkbox]")
                .forEach((listItem) =>
                    listItem.addEventListener("change", (event) => {
                        event.target.toggleAttribute("checked");
                        const li = event.target.closest("li");
                        li.PUT();
                    }),
                );
        </script>
        <div hidden itemscope itemtype="https://pagelove.org/AuthorizationRule">
            <meta itemprop="actor" content="*" />
            <meta itemprop="resource" content="/index.html" />
            <meta itemprop="method" content="PUT" />
            <meta itemprop="selector" content="#todo-list li" />
            <meta itemprop="action" content="allow" />
        </div>
        <div hidden itemscope itemtype="https://pagelove.org/ShapeConstraint">
            <meta itemprop="resource" content="/index.html" />
            <meta itemprop="selector" content="ul#todo-list" />
            <code itemprop="permit">li</code>
            <code itemprop="permit">input[type="checkbox"]</code>
            <code itemprop="permit">input[type="checkbox"][checked]</code>
            <code itemprop="permit">button[class="delete"]</code>
        </div>
    </body>
</html>

Now try it again: check an item, wait for its write to be accepted, and reload the page. A successful PUT() stores the updated fragment. Authorization permits an operation; acceptance also depends on validation, concurrency, and the request completing. The short example does not display that result yet.

Adding items

Tracking what we've bought is useful, but a shopping list also needs a way to add things. Let's put a small form underneath the list:

<div>
    <label for="new-item">Add item</label>
    <input id="new-item" type="text">
    <button command="--add-item" commandfor="todo-list">Add</button>
</div>

Notice that the button uses the Invoker Commands API — a recent browser feature that lets a button declare which element it acts on (commandfor) and what it does (command). To respond to it, we'll add a little code beneath our previous event listener:

document
    .querySelector("#todo-list")
    .addEventListener("command", (event) => {
        if (event.command == "--add-item") {
            const text = document.querySelector("#new-item").value;
            const item = document.createElement("li");
            item.textContent = text.trim();
            const checkbox = document.createElement("input");
            checkbox.type = "checkbox";
            item.prepend(checkbox);
            event.target.POST(item.outerHTML);
        }
    });

The entered value goes through textContent, so text such as <img> stays literal text rather than becoming an HTML element. The checkbox is created separately. We serialize the finished item with outerHTML and pass that markup to POST; the client adds the returned item after the request succeeds. Keep the item detached until then.

Until now we've only changed items that were already on the list, so a PUT was the right tool. Adding a brand-new item is a different operation — a POST — so we need a second rule granting permission for it:

<div hidden itemscope itemtype="https://pagelove.org/AuthorizationRule">
    <meta itemprop="actor" content="*" />
    <meta itemprop="resource" content="/index.html" />
    <meta itemprop="method" content="POST" />
    <meta itemprop="selector" content="#todo-list" />
    <meta itemprop="action" content="allow" />
</div>

Reload the page and type something into the box. When the POST succeeds, the client appends the returned item and the change is stored. A refused request or lost response needs explicit handling before deciding what was saved or whether to retry.

Deleting items

Some weeks you'll want different things, so let's add the last piece: removing an item from the list with a DELETE.

First, add a delete button inside every <li>, and a little CSS in the <style> tag to style it:

<button class="delete">&#x1F5D1;</button>
button.delete {
    border: none;
    cursor: pointer;
    background-color: transparent;
}

Now add another event listener to the script, so clicking a delete button removes its list item:

document.querySelectorAll("li button.delete").forEach((button) => {
    button.addEventListener("click", (event) => {
        button.closest("li").DELETE();
    });
});

We also want newly-added items to come with a delete button of their own. Create the button as an element too, keeping the entered text in textContent, and replace the add-item handler with this version:

document
    .querySelector("#todo-list")
    .addEventListener("command", (event) => {
        if (event.command == "--add-item") {
            const text = document.querySelector("#new-item").value;
            const item = document.createElement("li");
            item.textContent = text.trim();
            const checkbox = document.createElement("input");
            checkbox.type = "checkbox";
            item.prepend(checkbox);
            const deleteButton = document.createElement("button");
            deleteButton.className = "delete";
            deleteButton.textContent = "\u{1F5D1}";
            item.append(" ", deleteButton);
            event.target.POST(item.outerHTML);
        }
    });

As before, Pagelove needs permission to make this change. This time, though, we don't need a brand-new rule: a DELETE targets the same #todo-list li selector as our original PUT, so we can simply add DELETE as a second method on that existing rule:

<div hidden itemscope itemtype="https://pagelove.org/AuthorizationRule">
    <meta itemprop="actor" content="*" />
    <meta itemprop="resource" content="/index.html" />
    <meta itemprop="method" content="PUT" />
    <meta itemprop="method" content="DELETE" />
    <meta itemprop="selector" content="#todo-list li" />
    <meta itemprop="action" content="allow" />
</div>

Our full application should now look like this:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Simple Todo List</title>
        <script type="module" src="https://pagelove.github.io/beta-js/pagelove.mjs"></script>
    </head>
    <body>
        <style>
            li:has(input:checked) {
                text-decoration: line-through;
            }

            button.delete {
                border: none;
                cursor: pointer;
                background-color: transparent;
            }
        </style>
        <h1>Shopping List</h1>
        <ul id="todo-list">
            <li>
                <input type="checkbox" />Get Milk
                <button class="delete">&#x1F5D1;</button>
            </li>
            <li>
                <input type="checkbox" />Buy Eggs
                <button class="delete">&#x1F5D1;</button>
            </li>
            <li>
                <input type="checkbox" />Make pancakes
                <button class="delete">&#x1F5D1;</button>
            </li>
        </ul>
        <div>
            <label for="new-item">Add item</label>
            <input id="new-item" type="text" />
            <button command="--add-item" commandfor="todo-list">Add</button>
        </div>
        <script type="module">
            document
                .querySelectorAll("input[type=checkbox]")
                .forEach((listItem) =>
                    listItem.addEventListener("change", (event) => {
                        event.target.toggleAttribute("checked");
                        const li = event.target.closest("li");
                        li.PUT();
                    }),
                );
            document
                .querySelector("#todo-list")
                .addEventListener("command", (event) => {
                    if (event.command == "--add-item") {
                        const text = document.querySelector("#new-item").value;
                        const item = document.createElement("li");
                        item.textContent = text.trim();
                        const checkbox = document.createElement("input");
                        checkbox.type = "checkbox";
                        item.prepend(checkbox);
                        const deleteButton = document.createElement("button");
                        deleteButton.className = "delete";
                        deleteButton.textContent = "\u{1F5D1}";
                        item.append(" ", deleteButton);
                        event.target.POST(item.outerHTML);
                    }
                });
            document.querySelectorAll("li button.delete").forEach((button) => {
                button.addEventListener("click", (event) => {
                    button.closest("li").DELETE();
                });
            });
        </script>
        <div hidden itemscope itemtype="https://pagelove.org/AuthorizationRule">
            <meta itemprop="actor" content="*" />
            <meta itemprop="resource" content="/index.html" />
            <meta itemprop="method" content="PUT" />
            <meta itemprop="method" content="DELETE" />
            <meta itemprop="selector" content="#todo-list li" />
            <meta itemprop="action" content="allow" />
        </div>
        <div hidden itemscope itemtype="https://pagelove.org/AuthorizationRule">
            <meta itemprop="actor" content="*" />
            <meta itemprop="resource" content="/index.html" />
            <meta itemprop="method" content="POST" />
            <meta itemprop="selector" content="#todo-list" />
            <meta itemprop="action" content="allow" />
        </div>
        <div hidden itemscope itemtype="https://pagelove.org/ShapeConstraint">
            <meta itemprop="resource" content="/index.html" />
            <meta itemprop="selector" content="ul#todo-list" />
            <code itemprop="permit">li</code>
            <code itemprop="permit">input[type="checkbox"]</code>
            <code itemprop="permit">input[type="checkbox"][checked]</code>
            <code itemprop="permit">button[class="delete"]</code>
        </div>
    </body>
</html>

Making it dynamic

There's one rough edge left. We attach our event listeners once, when the page first loads — so they only bind to the items that exist at that moment. Add a new item and you'll notice its checkbox and delete button do nothing: no listener was ever attached to them.

To fix this properly, we'll bind listeners as elements appear, using the DOMSubscriber module instead of a one-off document.querySelectorAll. Update the <script> to look like this:

<script type="module">
    import { DOMSubscriber } from "https://cdn.pagelove.net/js/dom-subscriber/cde4007/index.mjs";
    DOMSubscriber.subscribe(
        document,
        "input[type=checkbox]",
        (listItem) => {
            listItem.addEventListener("change", (event) => {
                event.target.toggleAttribute("checked");
                const li = event.target.closest("li");
                li.PUT();
            });
        },
    );
    DOMSubscriber.subscribe(document, "li button.delete", (button) => {
        button.addEventListener("click", (event) => {
            button.closest("li").DELETE();
        });
    });
    document
        .querySelector("#todo-list")
        .addEventListener("command", (event) => {
            if (event.command == "--add-item") {
                const text = document.querySelector("#new-item").value;
                const item = document.createElement("li");
                item.textContent = text.trim();
                const checkbox = document.createElement("input");
                checkbox.type = "checkbox";
                item.prepend(checkbox);
                const deleteButton = document.createElement("button");
                deleteButton.className = "delete";
                deleteButton.textContent = "\u{1F5D1}";
                item.append(" ", deleteButton);
                event.target.POST(item.outerHTML);
            }
        });
</script>

DOMSubscriber watches the live DOM for elements matching each selector and binds the right listeners the moment they appear — including items you add later. Now every checkbox and delete button works, no matter when it was created.

You now have a shared shopping-list prototype using PUT, POST, and DELETE, with authorization and a closed shape on its application write path. The snippets above demonstrate the successful path; they do not await each response, retain a frozen pending request, or provide conflict and failure recovery. Those omissions matter before relying on the list.

Where to go next