Add a Dynamic Guestbook to a Static Eleventy Site

A static Eleventy guestbook can use Netlify Forms to store entries, a webhook function to trigger a rebuild, and a data file that fetches submissions from the Netlify API. A 5-second delay and retry logic prevent new entries from being missed.

By
Brennan Kenneth Brown
Published
Reading time
3 minutes
Level
Intermediate
You need
  • An Eleventy site on Netlify
  • Basic JavaScript
Title card reading 'Bring Back the 90's Guestbook with JAMstack' with a lucky cat drawing
OpenGraph Image generated by generate-og-images.js

A static site cannot accept user submissions without a backend of some sort. This guide builds a guestbook for an Eleventy site on Netlify using Netlify Forms, one serverless function, and one Eleventy data file. I chose it over hosted guestbook services because some, such as 123guestbook, have shut down. You can see the result on my guestbook page.

How does a static site guestbook work?

The guestbook has three components:

1. The Form (Frontend)

An HTML form with the Netlify Forms attribute:

<form name="guestbook" method="POST" data-netlify="true" action="/guestbook-success" class="guestbook-form">
  <div class="form-group">
    <label for="name">Name *</label>
    <input type="text" id="name" name="name" required>
  </div>
  
  <div class="form-group">
    <label for="message">Message *</label>
    <textarea id="message" name="message" rows="4" required></textarea>
  </div>
  
  <button type="submit" class="btn">Sign Guestbook</button>
</form>

data-netlify="true" tells Netlify to intercept form submissions and store them, so no backend is needed. The action points to a success page you create yourself. The data file below also reads an optional website field, which this form does not include.

2. The Webhook (Serverless Function)

When someone submits the form, Netlify calls this function, which triggers a rebuild so the new entry appears. The function needs node-fetch in your dependencies. Setting up Netlify to send form submissions to the function is not covered in the code below.

// netlify/functions/guestbook-webhook.js
const fetch = require('node-fetch');

exports.handler = async function(event, context) {
  if (event.httpMethod !== 'POST') {
    return { statusCode: 405, body: 'Method Not Allowed' };
  }

  try {
    const payload = JSON.parse(event.body);
    
    if (payload.type === 'submission' && payload.data?.name === 'guestbook') {
      console.log('New guestbook submission received:', payload.data);
      
      // Wait a bit before triggering rebuild
      console.log('Waiting 5 seconds before triggering rebuild...');
      await new Promise(resolve => setTimeout(resolve, 5000));
      
      const buildHookUrl = process.env.NETLIFY_BUILD_HOOK_URL;
      
      if (buildHookUrl) {
        const response = await fetch(buildHookUrl, {
          method: 'POST',
          body: JSON.stringify({ trigger: 'guestbook_submission' }),
          headers: { 'Content-Type': 'application/json' }
        });
        
        if (response.ok) {
          console.log('Build triggered successfully');
        }
      }
    }
    
    return {
      statusCode: 200,
      body: JSON.stringify({ received: true })
    };
  } catch (error) {
    console.error('Webhook error:', error);
    return {
      statusCode: 500,
      body: JSON.stringify({ error: 'Internal Server Error' })
    };
  }
};

3. The Data Fetcher (11ty Data File)

At build time, 11ty fetches all submissions from the Netlify API. getSampleEntries() is a fallback you must define yourself for local builds without credentials:

// src/_data/guestbook.js
const fetch = require("node-fetch");

module.exports = async function() {
  const siteId = process.env.NETLIFY_SITE_ID;
  const token = process.env.NETLIFY_FORMS_ACCESS_TOKEN;
  
  if (!token || !siteId) {
    console.warn("No Netlify API credentials found. Using sample data.");
    return getSampleEntries();
  }
  
  // Get form ID first
  const formsUrl = `https://api.netlify.com/api/v1/sites/${siteId}/forms`;
  const formsResponse = await fetch(formsUrl, {
    headers: {
      "Authorization": `Bearer ${token}`,
      "User-Agent": "curl/7.79.1"
    }
  });
  
  const forms = await formsResponse.json();
  const guestbookForm = forms.find(form => form.name === 'guestbook');
  
  // Fetch submissions with retry logic
  const url = `https://api.netlify.com/api/v1/sites/${siteId}/forms/${guestbookForm.id}/submissions`;
  
  let response;
  let retries = 3;
  let delay = 2000;
  
  while (retries > 0) {
    const submissionsResponse = await fetch(url, {
      headers: {
        "Authorization": `Bearer ${token}`,
        "User-Agent": "curl/7.79.1"
      }
    });
    
    if (submissionsResponse.ok) {
      response = await submissionsResponse.json();
      break;
    } else if (retries > 1) {
      console.log(`Retrying in ${delay}ms... (${retries} attempts left)`);
      await new Promise(resolve => setTimeout(resolve, delay));
      delay *= 2;
      retries--;
    }
  }
  
  // Transform and return entries
  return response.map(submission => ({
    name: submission.data.name,
    message: submission.data.message,
    website: submission.data.website || "",
    date: new Date(submission.created_at),
    id: submission.id
  }));
};

Why do new guestbook entries sometimes not appear?

The guestbook worked from my laptop. When my partner signed it from her phone, a loading spinner ran for a long time, then she landed on the success page, but no new entry appeared. The message showed up in Netlify's form dashboard but never on the live site.

I first suspected a mobile browser issue, a network problem, or a CSS bug hiding the messages. The cause was eventual consistency: the Netlify API did not return the new submission immediately. This is what happened:

  1. User submits form → Netlify stores it immediately
  2. Webhook triggers instantly → Site rebuild starts
  3. Site queries Netlify API for submissions → Too soon!
  4. API returns old data (submission not yet indexed)
  5. Site rebuilds without the new entry

The submission existed, but Netlify's API needed a moment to index it. On desktop, the timing usually worked. On mobile networks with variable latency, the race condition showed up.

I fixed it in two ways:

  1. Delay the webhook: Wait 5 seconds before triggering the rebuild
  2. Add retry logic: If the API fails, retry with exponential backoff

Both changes are already in the code above. Together they made the guestbook reliable on every device I tried. One caution: the 5-second wait runs inside the function, so check your Netlify Functions timeout limit.

What did I learn from building it?

All guestbook data lives in my Netlify account, so I can export it and I am not tied to a third-party comment platform.

  1. Serverless functions let a static site have dynamic features.
  2. Distributed systems are not instantaneous, so consider race conditions.
  3. Test on real mobile networks, not only desktop WiFi.
  4. Three small files replace a backend.

What is the full submission flow?

  1. User fills form → Netlify stores submission
  2. Netlify sends webhook → Serverless function receives it
  3. Function waits 5 seconds → Triggers build hook
  4. 11ty builds site → Fetches submissions with retry logic
  5. Site deploys → New message appears automatically

What do I need to set up the guestbook?

You need:

  1. A Netlify site with Forms enabled
  2. A build hook URL (Site settings > Build & deploy > Build hooks)
  3. A Netlify Personal Access Token with forms:read permission
  4. The three code files above

Set these environment variables in Netlify:

  • NETLIFY_FORMS_ACCESS_TOKEN
  • NETLIFY_SITE_ID
  • NETLIFY_BUILD_HOOK_URL

No database or server is involved.