Skip to main content
Warning This document has not been updated for a while now. It may be out of date.
Last updated: 8 Feb 2022

slimmer: what-slimmer-does

Slimmer is a piece of Rack Middleware that is inserted near the top of the middleware stack. Note that Slimmer is used in frontend apps such as finder-frontend, rather than publishing apps.

It takes a response from the Rails app, and a template from Static, and combines these into a single response. This allows us to do things like add 'Sign in' / 'Sign out' links to the header (or, more accurately, remove whichever link is not applicable) prior to rendering the page. This can't be done natively in Static because it would mean we can't cache responses from Static, which is a hosted service (i.e. templates are downloaded over HTTP). Slimmer is local to the app, so just a means of sharing the code that interfaces with Static, and can cache the fetched templates, locales and components.

How Slimmer works

Templates

There are a few different templates in Static so that apps can render consistent error pages, core layout pages, pages without footer links and so on.

Apps can choose to look for templates somewhere other than Static by specifying the asset_host.

Processors

TitleInserter

Takes the <title> content from the Rails response and copies it into the template.

TagMover

Copies <script>, <link>, and <meta> tags from the Rails response into the template's <head>.

For <script> and <link> tags it only copies tags with a src and href attribute respectively, and only if a tag with a matching attribute doesn't already exist in the template.

For <meta> tags, it only copies tags with a name, and content attribute, and only if a tag with matching attributes (and a matching http-eqiv attribute) doesn't already exist.

ConditionalCommentMover

Takes any conditional comments from the Rails response, and appends them to the template's <head>

BodyInserter

Takes the entirety of the element #wrapper (by default), and replaces the corresponding #wrapper element in the template with it. Both the source and destination selector can be configured.

BodyClassCopier

Takes any classes applied to the <body> element in the Rails response, and adds them to the <body> element in the template. This will add to any classes already existing in the template.

HeaderContextInserter

Takes an element in the Rails response with a selector of .header-context (by default), and replaces the corresponding element in the template with it. Does nothing unless the selector exists in both.

The .header-context element typically wraps the 'breadcrumb' trail on the site.

SearchPathSetter

If there is an X-Slimmer-Search-Path header in the Rails response, it finds the search form (form#search) in the template, and replaces the action attribute with the value of the header.