Repository navigation
Application Optimization
- What is application optimization?
- Why would you optimize your application?
- Sass
- Gulp
- Parcel
- Grunt
- Fonts Optimization
To deliver a good web application for your customer, you want the customer to be able to go through the website quickly without waiting for a long time. There are several useful tools for this. This is called production optimization. There are also a few basic things that you can apply such as:
-
Minification of resources refers to the process of removing unnecessary, redundant data from your HTML, CSS, and JavaScript that are not required to load. This includes eliminating code comments and formatting, white space characters, unused codes, newline characters, and so on.
-
Prevent broken links to files that don’t exist.
-
Remove Unnecessary Custom Fonts
-
Script type module in JS (type=”module”)
-
Code splitting (JS, CSS, and HTML can be split into smaller parts)
Depending on your device and it's specs, you can make your application quicker for different devices. Usually if you have an older device for example, with worse specs, you can feel the difference when a application is optimized and when it's not. If it's optimized it will most likely be quicker than when it's not.
Your application can also make a difference in bandwidth. This is more of an issue for financial purposes. If you have a larger bandwidth use, then your application will most likely cost more money, because you want users to be able to use your application, so you would need to pay more for more bandwidth.
In our application we are going to apply lazy loading. this means that the content of the site is loaded first and then the image. this makes the application slightly faster. By giving styling to the html tag the img has a fixed size and width. This prevents the content from jumping when it is loaded We want to apply the sass tool. In the following headings we are going to explain what it is and why we are going to use it.
Sass ('Syntactically awesome style sheets) is a CSS preprocessor, which adds special features such as variables, nested rules and mixins into regular CSS.
A CSS preprocessor is a scripting language that extends CSS by allowing developers to write code in one language and then compile it into CSS. Sass is now one of the most popular preprocessor and also user friendly.
SCSS (Sassy CSS): Uses the .scss file extension and is fully compliant with CSS syntax. All you need to do is instead of putting .css put .scss
Just like other programming languages, Sass allows the use of variables that can store information you can use throughout your style sheet. For example, you can store a colour value in a variable at the top of the file, and then use this variable when setting the colour of your elements. In css variables are written as var(--name) and in sass you only need to put the dollar sign in front of it and add the name "$name"
Written by: Dave Dankwah
Gulp is a task runner that uses node.js as platform. With Gulp you can build system automated tasks for example for: HTML, CSS, JS and even Sass, you can compile them and minify them. With Gulp you can do simple tasks, such as move files, copy them and rename them. It let's you define tasks by name and run tasks by name.
Tasks such as compiling Sass files to CSS, combining multiple javascript files, minifying javascript & css files, can automatically run when a file change has been detected. This makes Gulp flexible to use and enhances your workflow. Gulp also has different plug-ins's to make your life easier.
You need to add three folders
- First you'll have to make a configure file for Gulp, called
gulpfile.jsin yourrootworking branch. In this file you will write all the tasks Gulp will have to run for you. - You'll also have to make an app folder. In this folder you will put all your javascript files (script.js) and your SASS files (style.scss). `You can change the files in this folder.
- You'll also have to make a dist folder. This folder will store the compiled files, after running the code that's written in
gulpfile.js. You cannot change the files in this folder.
To use the modules you installed, such as gulp, sass, cssnano, concat and uglify, you will have to require these modules at the top of your gulpfile.js.
After this you can finally add code for tasks you want to run in the gulpfile.js
To run Gulp, you simply have to type in gulp in your terminal. This will run the code you have written in the configure file called gulpfile.js.
The way you add a task in Gulp is as following:
gulp.task('[Function Name]', function(){
// Do stuff here
}
There are several main functions in Gulp, with each their own purpose, such as:
-
.task()= creates a task -
.src()= Indetifies which files you're looking for -
.pipe()= Adds a function to the Node stream -
.dest()= Writes the results of the file into a specific location -
.watch()= Detects changes within files and updates them automatically
I copied this code from: https://www.freecodecamp.org/news/super-simple-gulp-tutorial-for-beginners-45141974bfe8/. This is to give you an example of how it would look like when you write the code:
gulp.task('sass', function(){
return gulp.src('app/style.scss')
.pipe(sass())
.pipe(cssnano())
.pipe(gulp.dest('dist/css'));
});
The way I implemented gulp was in a branch called gulp-implementation. This was because I wasn't able to do this within the first deadline of this project. Other people also had to work on things like grunt and parcel, so we decided to these optimization things in our own branches to have to least amount of hinder from each other.
On the following website: https://gulpjs.com/plugins, you can find all sorts of plugins for gulp. I went through this list and looked for plugins that sounded familair to me, such as: gulp-sass (because we're using sass in our project), gulp-uglify (to minify javascript files), gulp-concat (bundle or concatenating multiple javascript files) and gulp-watch (watches your files for changes and adjust them for you automatically). I pretty much decided to try it out with just a few in this list instead of making it too complicated, because I'm the type of person that bites of more than I can chew and then I realize I've been wasting a lot of time on something I can't even finish sooo... I decided to implement the followwing plugins for this reason:
- gulp-sass = compiling sccs files into css files
- gulp-concat = to bundle the compiled css files into one file
The main goal of using Gulp was mainly for practice for myself to see how Gulp works and how I need to use it.
I want to talk here about what I did different than when you're normally using gulp. The thing I just really want to adress is that I used my static folder for all the files. I didn't wanted to change the name of this folder because to me this project is already big. Everything works the way it's supposed to and I didn't wanted to break anything and go through the entire thing, wasting my precious time on renaming file names. This is also the reason why I don't have a dist file. In future project I would however implement it the way it is documented, because I do understand that if other people would look at my code and they see a dest folder.
Sources: https://gulpjs.com/ https://www.freecodecamp.org/news/super-simple-gulp-tutorial-for-beginners-45141974bfe8/ https://gulpjs.com/docs/en/getting-started/quick-start/
Written by: Roshnie de Groen
What is Parcel?
Parcel is a webapllication bundler it's a basically a buildtool that can bundle files. Bundle takes different parts of your code like CSS or HTML code and converts it into different bundles. Examples of bundles are development bundles or production bundles. A development bundle is a bundle that you can work on and see your progress in the browser and a production bundle is a bundle that takes your files of your website into something that the browser can understand (ES6 or SASS files. Your files are also smaller and minified if you use bundles and it's going to allow you to use modern features. This way you can compare it to an application or buildtool, but more importantly you can compare it to a webpack, like grunt without configuration.
Advantages of Parcel:
- ZERO configuration needed.
- Fast bundle times
- Minifying files / optimization
- Small bundles
- Module replacement
- Friendly error login
- Automatic transforms
And many more!
Features of Parcel:
- Development
- Code splitting
- Resolution
- Targets
- Production
- Bundle inlining
- Plugins
- CLI
- Node emulation
- Parcel API
- Scope hoisting
Languages and Frameworks
- HTMl
- CSS
- JavaScript
- TypeScript
- React
- SVG
- Images
- Vue
- Sass
- Pug
- Stylus
- Less
- SugarSS
- Elm
- JSON
- TOML
- GraphQL
- YAML
- GLSL
- MDX
- XML
How to use Parcel?
For this project I did not use Parcel. I only did research. But how can you use Parcel according to my research?
-
Make a dist folder. * A dist folder is a folder with al yout minified files (bundles) and is used on production sites. We used node.js in this project. To make a dist folder with node.js you have to run the following commands in your terminal first:
-
Choose the language of your files you want to bundle.
-
Follow their instruction for each language you chose.
-
Move your (minified files) bundles into the dist folder.
Sources : https://www.youtube.com/watch?v=ILHn43wsDv0, https://parceljs.org/docs/, https://stackoverflow.com/questions/23730882/what-is-the-role-of-src-and-dist-folders, https://www.youtube.com/watch?v=ILHn43wsDv0&t=165s
Written by: Vanessa Choe
Grunt.js is a JavaScript task runner. It can be used to automate various tasks for your application with minimum effort which increases the developer's productivity.
When you are doing a lot of manual work everytime, you can easily automate this with grunt, the configuration takes some time to set up, needs some project and asset planning but it will save you a lot of time in the long-run. If you work in a team environment that means you can work with a set of unified commands and everyone will share a common workflow.
- Install Node.js and Grunt.
- Create package.json and list dependencies (Grunt and plugins).
- Install NPM modules.
- Create Gruntfile.js.
- Configure tasks you need to run.
- Run those tasks in the command line while you work.
Sources : https://semaphoreci.com/community/tutorials/getting-started-with-grunt-js
Written by: Ruben Finnerud
Nowadays so many websites use web fonts and all of these fonts are different sources the user needs to acquire when they're visiting your website, which means that it is possible that the web fonts you're using for your website can slow down your machine. To tackle this issue, there's a very simple solution and that's when fonts optimization comes into play. Fonts optimization means that your fonts are optimized by the way they're loaded into the website. This improves the user experiences.
There are two types of basic web fonts:
- Web-safe fonts = Standard & pre-installed on different devices
- Webfonts = Fonts that are not standard & pre-installed, so they must be downloaded by the browser
When you want to use a webfont instad of a web-safe font, it needs to be downloaded or "stored" somewhere on your device and this is called "hosting". There are two options to "host" web fonts:
- Self-hosting = Saving the resources of the font on your webserver with your website files
- Third-party hosting = Using a font-service like Google (will fail when there's an outage of their server)
Like I said before, webfonts can cause your application to slow down and I'll explain two ways in which that is possible:
**1. FOUT = Flash of unstyled ** **2. FOIT = Flash of insvisible text **
-
FOUT stands for "flash of unstyled text" which basically means that once you load the page, you'll first get to see unstyled text (the fallback font) and then later on when the font has been loaded or hosted on the website or application, you'll see the styled font.
-
FOIT stands for "flash of invisible text" which means that once you load the page, you'll first get to see empty spots on the web page and once the font has been loaded or hoster, it will show the text with the font.
You could be asking yourself "why is this an issue"? Well, if different people have different devices, different connections. If you as a user already has slow connection and the application itself is also slow then that just really sucks for the user and probably move onto a different website or application.
Yep, that's right. Font optimization!
Before I want to talk about the solution, let's look at how we normally can import fonts into our projects. We have a few different methods to import fonts into our projects:
- Import the font files into your project and then using
@font-facein your CSS file - By using the
linktag in your header of your HTML document
The issue with font-face is that it takes time to load the font when it loads the styling in your CSS file, however if you put the font in the link tag that's inside the header of your HTML, it forces to load your font together with your document, which means that you don't have to wait for the text after the page has appeared.
To improve the optimization of your fonts, you can use attributes like:
-
rel=preloadwithcrossorigin= to remove FOUT (if you don't putcrossorigin, it will ignore the preload). -
as="font" type="font/woff2"= this helps the browser to prioritize the download of this resource -
aswill prioritize the preload too. -
Preloadwill download and cache a resource, so the browser won't need to keep on dowloading the fonts. - You can use
prefetchif you don't need a font immediately. - You can use
preconnectwhen you need a resource soon, but the full link/url is stil unknown (for older browser it'sdns-preconnect). -
Prerenderfor a webpage that you know a lot of people will navigate too and you want to increase the render speed. -
Modulepreloadis used for when you need a ES module script soon. This is also exactly what I used for in this project.
If you want to check the speed of your text being loaded in, you can load your application and run lighthouse. This will tell you the improvements you can make to your website or application regarding optimization.
Sources: https://wp-rocket.me/blog/guide-web-font-optimization/#section-3 https://dev.to/masakudamatsu/loading-google-fonts-and-any-other-web-fonts-as-fast-as-possible-in-early-2021-4f5o https://www.youtube.com/watch?v=vTf9HRTWKtM https://web.dev/codelab-preload-web-fonts/ https://3perf.com/blog/link-rels/
Written by: Roshnie de Groen