Mappings

Contents

In this chapter, you’ll make your site answer at a URL that has no content behind it, by mapping the URL to a function in your app.

This chapter requires the result of chapter 9, as described in Advanced features.

Mappings

Every page you have rendered so far belongs to a content item: the site service finds the content at the requested path, and renders its page. Some URLs are different. Files such as robots.txt, sitemap.xml, and llms.txt have a fixed address at the root of a site, and there is no content item to render.

A mapping binds a URL pattern to a function in your app. Mappings are listed in the site descriptor, and the site service checks them before it starts rendering a page. When a pattern matches, your function handles the request, and no page is rendered.

The example here is llms.txt, a proposed convention for telling AI tools what a site contains, written as Markdown.

Task: Serve llms.txt

  1. Create the function - Create the file below with the following content:

    src/main/resources/cms/mappings/llms-txt.ts
    import type { Content, Response } from '@enonic-types/core';
    
    import { getChildren } from '/lib/xp/content';
    import { getSite, pageUrl } from '/lib/xp/portal';
    
    type Country = { description?: string, wikipedia?: string };
    
    export function GET(): Response {
      const site = getSite()!;
    
      const children = getChildren<Content<Country>>({
        key: site._path,
        count: 100,
      });
    
      const countries = children.hits
        .filter((item) => item.type === `${app.name}:country`)
        .map((item) => {
          const url = pageUrl({ path: item._path, type: 'absolute' });
          const description = item.data.description ? `: ${item.data.description}` : '';
    
          return `- [${item.displayName}](${url})${description}`;
        });
    
      return {
        contentType: 'text/plain; charset=utf-8',
        body: [`# ${site.displayName}`, '', '## Countries', '', ...countries, ''].join('\n'),
      };
    }

    The function works like the implementation of a part: it exports GET, and returns a response. getSite() returns the site the request belongs to, getChildren() fetches the content below it, and pageUrl() creates the address of each country. Instead of rendering a view, the function returns the text directly.

  2. Add the mapping - In the site descriptor, add these lines between kind and apis:

    src/main/resources/cms/site.yaml
    mappings:
      - controller: "/cms/mappings/llms-txt.js"
        pattern: '/llms\.txt'

    The pattern is a regular expression, matched against the part of the URL that follows the site. The backslash makes the dot match a dot only, and the single quotes keep YAML from interpreting the backslash.

    The controller property points to the compiled file, so it ends with .js, not .ts.
  3. Verify the build - The terminal running enonic dev prints BUILD SUCCESSFUL.

  4. Verify the result - Open http://localhost:8080/site/siteframework/master/hello-world/llms.txt in your browser. It lists the countries of your site:

    # Hello World
    
    ## Countries
    
    - [Norway](http://localhost:8080/site/siteframework/master/hello-world/norway): Home of the midnight sun
    - [Sweden](http://localhost:8080/site/siteframework/master/hello-world/sweden): Home of the midsummer celebration
    - [Colombia](http://localhost:8080/site/siteframework/master/hello-world/colombia): Home of El Dorado
    - [USA](http://localhost:8080/site/siteframework/master/hello-world/usa): The United States of America - home of the brave

    The file is made from the content each time it is requested. Publish a new country, and it shows up in the list.

When to use a mapping

A mapping is the right tool when the URL is decided for you, as it is for llms.txt. When you can choose the URL yourself, an API is usually a better fit.

Mappings can do more than this chapter shows. A mapping can match content instead of a URL, for instance all content of one type, and it can point to a filter that runs before and after the rendering instead of replacing it.

Summary

You’ve created a function that returns a text file, and mapped it to a URL at the root of your site.

For more details, visit the mappings documentation.

Contents

Contents