11ty Logo

Eleventy Edge: Critical CSS

← Return to the Full list of Eleventy Edge Demos

Critical CSS rendered on the edge for empty-cache visits and further optimized for repeat views with a full cache.

Welcome back!

You are a full-cache repeat visitor.

For testing: Reload this page. Reset the cookie.

Check the network tab on your browser’s devtools (make sure the Disable cache preference is not checked). You received a highest priority <link rel="stylesheet" href="/static/style.css"> on a primed browser cache.


Learn more on 11ty.dev: Eleventy Edge Plugin documentation

Source Code

Loader code in _includes/layout.liquid

<head>
<!-- … -->
{%- capture criticalCss %}
{% include "css/style.css" %}
{% include "css/theme.css" %}
{% include "css/form-autosubmit.css" %}
{% endcapture %}

{% edge "liquid" criticalCss %}
{% if eleventy.edge.cookies.repeat %}
<link rel="stylesheet" href="/static/style.css">
{% else %}
<style>{{ _ }}</style>
<link rel="stylesheet" href="/static/style.css" media="print" onload="this.media='all'">
{% endif %}
{% endedge %}
<!-- … -->
</head>

Improvements:

Edge Function

Sets the cookie to track repeat views and the ?repeat-reset query param to delete the cookie.

./netlify/edge-functions/repeat.js