Skip to content

Application Optimization

rvdegroen edited this page Jun 30, 2022 · 26 revisions

Table of Contents

What is application 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:

  1. 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.

  2. Prevent broken links to files that don’t exist.

  3. Remove Unnecessary Custom Fonts

  4. Script type module in JS (type=”module”)

  5. Code splitting (JS, CSS, and HTML can be split into smaller parts)

Why would you optimize your application?

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

What is it?

Sass ('Syntactically awesome style sheets) is a CSS preprocessor, which adds special features such as variables, nested rules and mixins into regular CSS.

CSS preprocessor

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.

Why and how do you use it

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

What is Gulp?

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.

Why use Gulp?

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.

How do you use it?

You need to add three folders

  1. First you'll have to make a configure file for Gulp, called gulpfile.js in your root working branch. In this file you will write all the tasks Gulp will have to run for you.
  2. 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.
  3. 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:

  1. .task() = creates a task
  2. .src() = Indetifies which files you're looking for
  3. .pipe() = Adds a function to the Node stream
  4. .dest() = Writes the results of the file into a specific location
  5. .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')); 
});

Implementation

Implementation in a different branch

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.

Plugins

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:

  1. gulp-sass = compiling sccs files into css files
  2. 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.

Things I did different

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

Parcel

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:

  1. ZERO configuration needed.
  2. Fast bundle times
  3. Minifying files / optimization
  4. Small bundles
  5. Module replacement
  6. Friendly error login
  7. Automatic transforms

And many more!

Features of Parcel:

  1. Development
  2. Code splitting
  3. Resolution
  4. Targets
  5. Production
  6. Bundle inlining
  7. Plugins
  8. CLI
  9. Node emulation
  10. Parcel API
  11. 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?

  1. 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:

  2. Go to https://parceljs.org/docs/

  3. Choose the language of your files you want to bundle.

  4. Follow their instruction for each language you chose.

  5. 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

What is Grunt?

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.

Why would you use Grunt?

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.

How do you use Grunt?

  • 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

Fonts Optimization

What is fonts optimization?

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.

Web Fonts

There are two types of basic web fonts:

  1. Web-safe fonts = Standard & pre-installed on different devices
  2. Webfonts = Fonts that are not standard & pre-installed, so they must be downloaded by the browser

Hosting Webfonts

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:

  1. Self-hosting = Saving the resources of the font on your webserver with your website files
  2. Third-party hosting = Using a font-service like Google (will fail when there's an outage of their server)

Issues with Webfonts

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 **

  1. 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.

  2. 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.

How do you tackle this issue?

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:

  1. Import the font files into your project and then using @font-face in your CSS file
  2. By using the link tag 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:

  1. rel=preload with crossorigin = to remove FOUT (if you don't put crossorigin, it will ignore the preload).
  2. as="font" type="font/woff2" = this helps the browser to prioritize the download of this resource
  3. as will prioritize the preload too.
  4. Preload will download and cache a resource, so the browser won't need to keep on dowloading the fonts.
  5. You can use prefetch if you don't need a font immediately.
  6. You can use preconnect when you need a resource soon, but the full link/url is stil unknown (for older browser it's dns-preconnect).
  7. Prerender for a webpage that you know a lot of people will navigate too and you want to increase the render speed.
  8. Modulepreload is used for when you need a ES module script soon. This is also exactly what I used for in this project.

Checking your Font Optimization

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

Clone this wiki locally