Neatly Packaged Magic

    TypeScript icon, indicating that this package has built-in type declarations

    1.0.12 • Public • Published

    Eleventy serverless preview mode

    Render a single 11ty page using data from your Wordpress API endpoint.

    If you have content in Wordpress for your Eleventy (11ty) site, you can create a Function as a Service (FaaS) function that will render Wordpress content without having to save it anywhere (serverless).


    • Single-page 11ty rendering of content retrieved from your Wordpress API data source.
    • Digest page for all pages that match a specific Wordpress tag ID.
    • Easy Azure FaaS integration

    User experience

    1. Content editor edits and saves a Wordpress post.
    2. Content editor navigates to the preview mode endpoint.
    3. Content editor selects a post from the list of preview ready posts.
    4. Content editor views fully rendered content.

    Sample navigation

    • https://[my-function-url]/ - Digest page. Display a list of the most recently updates posts (up to 100). Can also be set to filter for a specific tag (ex preview).
    • https://[my-function-url]/myfile.jpg - Resource request. Will download and then make available by proxy content from the main site (https://[real-url]/myfile.jpg). This allows for CSS and other content can be sent to the browser as relative links.
    • https://[my-function-url]/my-slug - Render requests. Will render the page using 11ty with Wordpress content from post that matches the slug.


    • End users are using Wordpress to edit content.
    • Your project is using wordpress-to-github (Coming soon) or similiar tool for deploying Wordpress content to an 11ty project.

    Eleventy setup

    Use your existing 11ty build to provide all the template work required to render your preview.

    This package requires functionality available in Eleventy v1.0.0 -

    Preview mode page template

    Define a page in your 11ty input templates to customize how your pages are rendered.

    Add this to your 11ty input folder (ex. pages) with the .njk extention (ex. previewModePage.njk).



    Connecting to the 11ty configuration

    Connect the 11ty build to the handler service. At build time, an auto generated folder called preview-mode-auto-generated will be created.


    const { addPreviewModeToEleventy } = require("@cagov/11ty-serverless-preview-mode");
     * @type {import('@cagov/11ty-serverless-preview-mode').WordpressSettingFunction}
    const itemSetterCallback = (item, jsonData) => {
      let featuredMedia = jsonData._embedded["wp:featuredmedia"];
      //Customize for your templates = 'page.njk'; = ['news']; = false; = jsonData.title.rendered; ='T')[0]; //new Date(jsonData.modified_gmt) = jsonData.excerpt.rendered; = jsonData.excerpt.rendered; = jsonData.excerpt.rendered; =[0].name; = featuredMedia ? featuredMedia[0].source_url : "img/thumb/APIs-Blog-Postman-Screenshot-1.jpg";
      item.template.frontMatter.content += jsonData.content.rendered;
    module.exports = function(eleventyConfig) {
      addPreviewModeToEleventy(eleventyConfig, itemSetterCallback);

    Git ignore

    When your run your 11ty build locally, you don't want to save the generated output (preview-mode-auto-generated) to your repo.


    # 11ty serverless generated folder

    Setting up with Azure Function as a Service (FaaS)

    Using Azure FaaS, the service can render posts from remote content, while redirecting all other resource requests (.css, .png, etc) back to the real web server. Any request without ?postid= will be considered a forwarded resource request. Requests without any path will render a digest page showing all the available preview pages that match a tag id.

    Azure function source

    The package has a complete handler for Azure FaaS - azureFunctionHandler. Include the url for your live web site to allow resource request forwarding.


    const { azureFunctionHandler } = require("@cagov/11ty-serverless-preview-mode");
    /** @type {import('@cagov/11ty-serverless-preview-mode').WordpressSettings} */
    const wordpressSettings = {
      wordPressSite: "", //Wordpress endpoint
      resourceUrl: "", //host for resource content redirects
      previewWordPressTagSlug: 'preview-mode' // optional filter for digest list of preview in Wordpress
    module.exports = async function (context) {
      await azureFunctionHandler(context, wordpressSettings);


    Trap ALL routes for your functions to support resource forwarding. Set route like this...

      "bindings": [
          "authLevel": "anonymous",
          "type": "httpTrigger",
          "direction": "in",
          "name": "req",
          "methods": [
          "route": "{*routes}"
          "type": "http",
          "direction": "out",
          "name": "res"


    Set routePrefix to blank in the host.json file in your Azure function project root.

      "extensions": {
          "http": {
              "routePrefix": ""


    This package is available on NPM at


    npm i @cagov/11ty-serverless-preview-mode

    DownloadsWeekly Downloads






    Unpacked Size

    20.1 kB

    Total Files


    Last publish


    • zakiyak
    • timothyshambra
    • xjensen
    • carter.medlin
    • aaronhans
    • chacha-california
    • kkoryaka