Skip to content

Customize MaterializeCSS Before Downloading in the Browser – Part 2

Posted in Javascript

We are still discussing how to Customize MaterializeCSS Before Downloading in the browser. The application gave birth to this and the part one articles

Looking for Part 1?

In the previous part, we  considered up until the point of opening up what the buildSass() does. The buildSass() takes an destination argument. Where from it?

  1. User visits the homepage. Checks a bunch of boxes in a form, then submits
  2. The Server receives the form post
  3. The Server looks through the form post object, then appends the incoming data to a .scss file.
  4. The Server runs a compiling spree on the .scss file.
  5. The output is pushed as a response to the browser in the form of attachment.
  6. The User is happy.

The stroked through bullet points were discussed in the previous article of this two-part article

The Actual Issue

Multiple users are going to request to build a custom version of Materialize in real-time with their personal configurations which will differ entirely from one another.

Article difficulty somewhere Intermediate Level

Because of this, it will in our best interest, as a developer, to generate on the fly, new temporal .scss files per each user’s immediate configuration, use the file to build our .css and then clean up the file, as in delete it.

This create-use-delete process will happen for every request that hits the server. With that in mind, it is important to generate really random numbers hard to repeat, to ensure it almost never happens a file I generated before I could use to build, is overridden by another user’s settings because their file name happened to be same as mine.

Should that happen, it’ll be bad for business, and might lead to confusion. We don’t want that, so I brought in a little bit of UUID. You can read more on that, but the first time I heard of it was with using MongoDB.

UUIDs are meant to be random, but random in a way that means they’ll never, ever, ever be the same, in a perfect world.



For instance, MongoDBs UUID is generated using your computer’s timestamp, along with certain unique details from your machine. And so, I doubt probably using details like your computer’s hostname, model, brand name, timestamp (date inclusive), and pure random numbers will ever generate two strings the same.

I am no genius with UUIDs, but they are intentionally designed in a way to achieve extreme, kinda universal uniqueness.

Although the use case for UUIDs are overkill for our use case here beside each file after use is deleted, at least you’ve heard about UUIDs.


Here is our complete

The newSass() function is responsible for generating a new file and passes in the generated file to the buildSass() as destination for use in building our custom CSS.

I believe the whole picture is beginning to form at this point, how the pieces fit together?

5) newSassFile()

Ooh! This is getting exciting. The newSassFile() has a couple of tricks up its sleeves. First, we generate the unique filename in creating a destination reference to a file using the uuid.v1() function from the node-uuid package.

We, therefore, employ the services of NodeJS’s File System, however through fs-extra which blesses us with a clean way of copying files from one point to another. In this case, we copy from our barebone.scss file into the just generated file reference within the destination variable.

Race Conditions

Savvy readers will by now be asking, ‘Why am I waiting for file copying to finish before doing anything else, like invoking buildSass()?’

Well, I learned it the hard way: Race conditions. Nodejs is single threaded, for the most part. However, when it comes to I/O processing, it doesn’t always happen the way it should. I am no guru to delve deeper into the whys and hows, as they are already nicely deliberated in this Stack Overflow question:

Can Node JS Code result in Race Condition?

In the instance above, the answer to the question is a big fat YES. Because, without tucking the build process code within AFTER the copying is complete, on my HDD, almost 100 out of 100 times, the build process begins without the copying complete.




Now, we are talking of milliseconds, however, in that milliseconds, the copying operation is slow, resulting in parts of the code running in memory to outrun the hard drive operation.

I learned this the hard way! Although your code is written to be synchronous and arranged in a synchronous manner, with regards to I/O, there isn’t a 100% guarantee they’ll follow the order you wish them to follow.

In the above scenario, I saved myself the troubles of guess works by making sure they all kick into action ONLY AFTER the copying is done. That solved everything.

Maybe there’s a better way to achieve the same effect without having to tuck the other code within the finished copying part, but hey, it works for now.

3) & 4) doLoop()? What for?

For something beautiful, like this:

The doLoop(); is the guy responsible for taking the form data submitted, stripping off the name parts, and feeding them to a function called doReplace() which takes an oldString and newString parameters to replace an existing string with a new one.

The toFile refers to the file on which the string replacements should happen. And so since we’re looking at a for loop, the string replacements will keep happening until we’ve run out of keys in our req.body object.

The doReplace() is nothing but a mere replace function from the replace node package. That’s all. Nothing fancy happening there.

Everything together

Below is everything we needed for the route to function properly and return our .css as a result, free of comments and console logs:


This article, although went straight to the points without explaining much, I believe you found it useful, and in a position to go out there and keep customizing your CSS in browser easily.

Remember, the principles used within this article applies to any .scss out there. So if your framework of choice has a way of cherry picking what modules of the framework to use via a Sass approach or even another approach, you should be able to apply this principle in getting something up and running easily.

I’ll be more than happy to see what you build with concepts from this article. Leave your comments, feedback, questions and suggestions in the comments section below and I’ll be happy to read and respond to them.

Thanks for joining me, and hope to see you in the next one