<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: SplitCSS.com</title>
    <description>The latest articles on Forem by SplitCSS.com (@splitcss).</description>
    <link>https://forem.com/splitcss</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F352946%2Fdaa1a018-f410-4580-9ecc-a37050234804.png</url>
      <title>Forem: SplitCSS.com</title>
      <link>https://forem.com/splitcss</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/splitcss"/>
    <language>en</language>
    <item>
      <title>Using SplitCSS to remove unused Tailwind CSS classes</title>
      <dc:creator>SplitCSS.com</dc:creator>
      <pubDate>Tue, 21 Apr 2020 19:53:16 +0000</pubDate>
      <link>https://forem.com/splitcss/using-splitcss-to-remove-unused-tailwind-css-classes-2fp1</link>
      <guid>https://forem.com/splitcss/using-splitcss-to-remove-unused-tailwind-css-classes-2fp1</guid>
      <description>&lt;p&gt;Utility CSS frameworks are a great way to style your web application nowadays. However, popular CSS frameworks like TailwindCSS can generate a lot of classes that are not used on majority of your web pages.&lt;/p&gt;

&lt;p&gt;Even after using some CLI tools, there will still be classes that most of your URLs don't need.&lt;/p&gt;

&lt;p&gt;Think about it, even if your web application has styled HTML tables on one URL, a CLI tool will let those classes for styling HTML tables in the entire stylesheet, for every URL. Same goes with borders, lists etc.&lt;/p&gt;

&lt;p&gt;If you define multiple colors in your TailwindCSS palette, there will be even more classes.&lt;/p&gt;

&lt;p&gt;As a result, the browser has no choice but to download the entire stylesheet, even if it needed only a small portion of it.&lt;/p&gt;

&lt;p&gt;You may think that unused CSS is not much of a big deal in the age of high-speed Internet connections, but in reality, most users use their mobile devices to surf the Internet.&lt;/p&gt;

&lt;p&gt;And unless connected to a Wi-Fi network, they don't have such a fast connection.&lt;/p&gt;

&lt;p&gt;Moreover, as engineers, we should always aim to optimize performance and never leave unnecessary stuff in the product.&lt;/p&gt;

&lt;p&gt;Just because a product is functional, does not mean that its developers are excused for leaving unused stuff in it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing unused Tailwind CSS styles with SplitCSS API
&lt;/h2&gt;

&lt;p&gt;Let's download  &lt;a href="https://tailwindcss.com/"&gt;Tailwind&lt;/a&gt;  and configure it.&lt;/p&gt;

&lt;p&gt;Here is our &lt;code&gt;tailwind.config.js&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
    theme: {
        colors: {
            white: '#fff',
            black: '#000',
            green: '#3FBD0C',
            gray: {
                light: '#dadada',
                dark: '#2D3449',
                darkest: '#222838'
            }
        },
        extend: {},
    },
    variants: {},
    plugins: [],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's configure Webpack to output our Tailwind-powered stylesheet in a file called tw.css&lt;/p&gt;

&lt;p&gt;So now, let's create an &lt;code&gt;index.php&lt;/code&gt; page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!doctype html&amp;gt;
&amp;lt;html lang="en"&amp;gt;

&amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8"&amp;gt;
    &amp;lt;title&amp;gt;Index Page&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="/tw.css"&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
    &amp;lt;h1 class="mt-8"&amp;gt;index.php&amp;lt;/h1&amp;gt;
    &amp;lt;p class="px-4 py-2"&amp;gt;This is my index page.&amp;lt;/p&amp;gt;
&amp;lt;/body&amp;gt;

&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have inserted our stylesheet in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; section.&lt;/p&gt;

&lt;p&gt;As you can see, our page uses only a few of Tailwind's classes. It does not use the utility classes created for our color palette.&lt;/p&gt;

&lt;p&gt;Most of what Tailwind came with is unused by our page, even if we had purged our files by scanning them.&lt;/p&gt;




&lt;p&gt;Now, in order to remove the unused Tailwind CSS, let's pull in SplitCSS API.&lt;/p&gt;

&lt;p&gt;In order to give the browser only the styles that are needed for the current URL, we will use the SplitCSS API.&lt;/p&gt;

&lt;p&gt;First, let's go ahead and  &lt;a href="https://splitcss.com/register"&gt;create a free SplitCSS account&lt;/a&gt;  and generate an API key.&lt;/p&gt;

&lt;p&gt;Now, let's create our proxy script, &lt;code&gt;styles.php&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
$apiToken = 'MY_API_TOKEN'; // replace with your real one
$htmlUrl = $_GET['html_url'];
$cssUrls = $_GET['css_urls'];
$output = 'css';
$splitCssUrl = "https://api.splitcss.com/clean-css?html_url={$htmlUrl}&amp;amp;css_urls[]={$cssUrls}&amp;amp;output={$output}&amp;amp;token={$apiToken}";
echo file_get_contents( $splitCssUrl );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's pass the &lt;code&gt;styles.php&lt;/code&gt; along with the URLs we want to optimize for:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!doctype html&amp;gt;
&amp;lt;html lang="en"&amp;gt;

&amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8"&amp;gt;
    &amp;lt;title&amp;gt;Index Page&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="/styles.php?html_url=https://example.com/index.php&amp;amp;css_urls[]=https://example.com/tw.css"&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
    &amp;lt;h1 class="mt-8"&amp;gt;index.php&amp;lt;/h1&amp;gt;
    &amp;lt;p class="px-4 py-2"&amp;gt;This is my index page.&amp;lt;/p&amp;gt;
&amp;lt;/body&amp;gt;

&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The CSS file that will be delivered to the browser, will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.mt-8 {
    margin-top: 2rem;
}

.px-4 {
    padding-left: 1rem;
    padding-right: 1rem;
}

.py-2 {
    padding-top: .5rem;
    padding-bottom: .5rem;
}

...
/* Plus all the responsive variants for these utilities */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what is most interesting is that none of the color-utilities based on the tailwind.config.js will appear in the output, because they are not used in this HTML document.&lt;/p&gt;

&lt;p&gt;This will drastically reduce the stylesheet filesize. The exact size will depend on how many Tailwind classes do you use in your HTML document.&lt;/p&gt;

&lt;p&gt;Of course, in a real application you will want to pass the HTML URL dynamically so you will always get only the Tailwind classes that your browser needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing unused CSS in Tailwind applications with conditionally loaded content
&lt;/h2&gt;

&lt;p&gt;In real-world scenarios, applications often load some content conditionally.&lt;/p&gt;

&lt;p&gt;For example, let's imagine we have a blog website. Some posts have comments, some don't.&lt;/p&gt;

&lt;p&gt;Because we cannot predict when the next comment will come, we have the following choice:&lt;/p&gt;

&lt;p&gt;1) always load the styles for the comments&lt;br&gt;
2) invalidate the SplitCSS cache when the first comment comes.&lt;/p&gt;

&lt;p&gt;In the first scenario, we can mark the comment styles so that SplitCSS ignores them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* uncss:ignore */
.tw-button {
@apply bg-green text-white px-4 py-2
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since SplitCSS is built on top of UnCSS, placing these comments before the actual classes will cause SplitCSS to include them in the resulting stylesheet.&lt;/p&gt;

&lt;p&gt;This can be relatively easily done for your custom utilities. For all the pre-built utilities that Tailwind comes with, it can be quite tedious.&lt;/p&gt;

&lt;p&gt;That's why it's better to take the second scenario.&lt;/p&gt;

&lt;p&gt;We can invalidate the SplitCSS cache when the first comment has been submitted.&lt;/p&gt;

&lt;p&gt;To achieve this, we can call the SplitCSS API upon submitting the first comment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$commentData = $request-&amp;gt;only(['email', 'title', 'body']);
$postId = $request-&amp;gt;get('post_id');
$post = $postRepository-&amp;gt;find( $postId );
if( $post-&amp;gt;comments()-&amp;gt;count() ) {
$apiKey = config('splitcss.api_key');
$postUrl = $post-&amp;gt;url();
$apiUrl = "https://api.splitcss.com/invalidate?token={apiKey}&amp;amp;html_url={$postUrl}";
file_get_contents( $apiUrl );
}
$comment = $commentRepository-&amp;gt;save($commentData);
$post-&amp;gt;comments()-&amp;gt;attach($comment);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above will &lt;a href="https://splitcss.com/documentation#invalidate-all"&gt;invalidate the SplitCSS cache&lt;/a&gt; when the first comment is submitted.&lt;/p&gt;

&lt;p&gt;By doing this, the user will see the comment styled properly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion - save bandwidth for every URL by removing unused CSS classes in Tailwind
&lt;/h2&gt;

&lt;p&gt;We are big fans of TailwindCSS and we think it's a great way to write stylesheets for modern websites.&lt;/p&gt;

&lt;p&gt;To speed up your website and improve the performance, you can use the SplitCSS API to serve the Tailwind classes that the browser needs in order to render the current URL.&lt;/p&gt;

&lt;p&gt;This can be in combination with CLI tools that scan your source files for classes.&lt;/p&gt;

&lt;p&gt;If you have further questions regarding using Tailwind in combination with SplitCSS, feel free to check out the &lt;a href="https://splitcss.com/faq"&gt;FAQ&lt;/a&gt; section.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>tailwindcss</category>
      <category>css</category>
    </item>
    <item>
      <title>SplitCSS: API for reducing unused CSS from your webpages. Your support is appreciated.</title>
      <dc:creator>SplitCSS.com</dc:creator>
      <pubDate>Thu, 19 Mar 2020 21:30:13 +0000</pubDate>
      <link>https://forem.com/splitcss/splitcss-api-for-reducing-unused-css-from-your-webpages-your-support-is-appreciated-1kf3</link>
      <guid>https://forem.com/splitcss/splitcss-api-for-reducing-unused-css-from-your-webpages-your-support-is-appreciated-1kf3</guid>
      <description>&lt;p&gt;Fellow developers, I am about to launch &lt;a href="https://splitcss.com"&gt;splitcss.com&lt;/a&gt;, an API for reducing dead stylesheets.&lt;/p&gt;

&lt;h2&gt;
  
  
  Easy stylesheet splitting for individual web pages
&lt;/h2&gt;

&lt;p&gt;Here's what happens very often: we have turned placing CSS into one file a habit.&lt;/p&gt;

&lt;p&gt;So the browser has no choice but to &lt;strong&gt;load an entire stylesheet&lt;/strong&gt; and &lt;strong&gt;apply only a handful of classes&lt;/strong&gt; to the page that the user is viewing.&lt;/p&gt;

&lt;p&gt;My aim is to deliver stylesheet splitting as a service.&lt;/p&gt;

&lt;p&gt;That would mean that the service would return the stylesheet rules that are actually needed by the browser.&lt;/p&gt;

&lt;p&gt;This would &lt;strong&gt;decrease file size and improve page speed&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Even tools like PageSpeed Insights started reporting the excessive amount of stylesheets:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JwjRYqQu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1584627588836/puwN9bjW1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JwjRYqQu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1584627588836/puwN9bjW1.png" alt="pagespeed-insights-remove-unused-css.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Some people might even see an opportunity here to have a positive impact on their SEO and rankings, since speed is a ranking factor.&lt;/p&gt;

&lt;p&gt;To be fair, this is &lt;strong&gt;not a silver bullet&lt;/strong&gt; for every possible web app out there. There are many different ways to include CSS in web pages nowadays.&lt;/p&gt;

&lt;p&gt;And also there are &lt;strong&gt;many CLI tools&lt;/strong&gt; that &lt;strong&gt;scan your project source files&lt;/strong&gt; for CSS selectors.&lt;/p&gt;

&lt;p&gt;Still, it would be very convenient if this could be done on URL basis. If a service would try to find the dead styles.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the difference compared to CLI tools?
&lt;/h2&gt;

&lt;p&gt;Well, what SplitCSS does is first &lt;strong&gt;runs Headless Chrome&lt;/strong&gt; to get the HTML and then starts analysing it for unused CSS selectors.&lt;/p&gt;

&lt;p&gt;Of course you can also do that from your terminal, but as a developer, I would love to have a service to take care of it. I would let it &lt;strong&gt;cache the cleaned stylesheet&lt;/strong&gt; for the URL and let the browser load that one.&lt;/p&gt;

&lt;h2&gt;
  
  
  What about performance?
&lt;/h2&gt;

&lt;p&gt;Many people think that consuming a service must be inevitably slow. Like you do an extra request, plus processing time plus delivering the response.&lt;/p&gt;

&lt;p&gt;That is true, but stylesheets are quite static resources and can be cached, both by the service and by the consumer.&lt;/p&gt;

&lt;p&gt;Also styles that appear after the user takes an action can be marked as necessary and the service will include them even they are not present in the initial DOM. &lt;br&gt;
Usually the number of such classes is low.&lt;/p&gt;

&lt;h2&gt;
  
  
  Your support is welcomed
&lt;/h2&gt;

&lt;p&gt;I have recently  &lt;a href="https://www.indiehackers.com/post/hey-fellow-developers-what-do-you-think-about-an-api-for-removing-unused-css-fcec3bb559"&gt;shared the idea&lt;/a&gt;  about the concept, with mixed feedback and a lot of concerns.&lt;br&gt;
Nevertheless there were some  &lt;a href="https://www.producthunt.com/posts/splitcss-com"&gt;positive reactions&lt;/a&gt;  and some people signed up for getting early access to the platform.&lt;/p&gt;

&lt;p&gt;This has meant a lot to me.&lt;/p&gt;

&lt;p&gt;So if anyone else finds the idea interesting, or at least intriguing, feel free to create an account - it doesn't cost anything.&lt;/p&gt;

&lt;p&gt;The service is &lt;strong&gt;not perfect&lt;/strong&gt; and probably it will never be, but I am willing to improve it.&lt;/p&gt;

&lt;p&gt;It can become good enough for many cases, I would be extremely happy.&lt;/p&gt;

&lt;p&gt;🙏 Thanks!&lt;/p&gt;

&lt;p&gt;Further resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;a href="https://splitcss.com/documentation"&gt;Documentation&lt;/a&gt; &lt;/li&gt;
&lt;li&gt; &lt;a href="https://splitcss.com/faq"&gt;FAQ&lt;/a&gt; &lt;/li&gt;
&lt;li&gt; &lt;a href="https://github.com/splitcss/splitcss"&gt;Github Package for Laravel&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>showdev</category>
      <category>startup</category>
      <category>api</category>
      <category>css</category>
    </item>
  </channel>
</rss>
