Modules, DOM and Performance (Week 9) Flashcards
What does a SPA usually consist of?
JS and HTML templates.
Critically, the ‘preferred form’ of working with SPAs is not a good format for downloading what?
Many small files
Data that is not needed at runtime (ie, good function names)
What is bundling?
process of taking the preferred form of Web code/content and putting it into the preferred form for a browser to consume
Analogous to compiling a C or Java program
In terms of bundling, what does a browser/user prefer?
✔️ Fewer network requests (fewer files)
✔️ Smaller network requests
✔️ Parallel network requests
✔️ No network requests at all (caching)
Explain the ‘fewer files’ aspect of simple bundling
Concatenate all required *.js files to one big file
Do the same for *.css files
Tools like Gulp and Grunt follow this basic pattern
Sweep up a bunch of files without knowing about file content
Perform some transform on them to generate (fewer) output files
Explain the ‘smaller files’ aspect of simple bundling
Browsers don’t care about JS function names (much)
We do care about data sent over the network though
Enter UglifyJS
What does UglifyJS do to code?
Code is name mangled, some optimizations are applied
Mangling can only be applied when we are sure that all instances of the name are renamed
What is UglifyJS and its purpose/benefits?
✨✨✨
UglifyJS is a JavaScript <strong>parser/compressor/beautifier toolkit </strong>.
It can be used to combine and minify JavaScript assets so that they require <strong>less HTTP requests</strong> and make your <strong>site load faster</strong>.
In terms of UglifyJS, what is used to aid in debugging?
sourcemaps can be created
When using UglifyJS on code to reduce the size of files, what does the browser load?
mangled code + sourcemaps and the dev tools presents the code as it originally was –> Invaluable for debugging production issues
What are some examples of things that cannot be mangled in UglifyJS?
String constants (const SOME_VALUE = “Your string”;),
object keys
What is Webpack and why use it?
✨✨✨
<strong>Webpack is a tool that lets you compile JavaScript modules</strong>, also known as module bundler.
Given a large number of files, it <strong>generates a single file (or a few files) that run your app</strong>.
It can perform many operations: helps you bundle your resources.
Allows you to use require() for CSS files.
What are the current best practice to bundle code?
Webpack (rollup/parcel) and other bundlers
What is Webpack configured with?
1) Where to find input files
2) Where to start from (app entry point)
What will Webpack recursively follow? Explain.
Webpack will recursively follow <strong>imports</strong>
- Import -> import foo from “module_name”
- Export -> export foo = 42;
- Only the files that are <strong>used get pulled into the final output</strong>
- <strong>Unused (dead) code can be removed</strong>
What will Webpack outputs be like?
something like main.9789bf1740765e50e459.js
This file is ~10Mb of JS
What is the solution to not using the whole app and not wanting to download 10Mb of scripts to use your app?
Lazy loading
Explain the technique of lazy loading
- Break your SPA into logical chunks, eg
- -> Login (likely the main chunk)
- -> Settings
- Only download the chunks you need
When is lazy loading triggered?
on router changes e.g. /login, /settings
What must agree and on what for lazy loading?
The framework and the bundler need to agree on how to split the import graph, usually done with bundler plugins.
Using Gzip compression, what does 10Mb go to?
1.47Mb –> 6.6. x smaller
How does the browser signal that it can handle compressed content?
with a request header:
accept-encoding: gzip, deflate, br
How does the server respond to the request header of ‘accept-encoding: gzip, deflate, br’?
✨✨✨
Informs the browser in the response with another header:
content-encoding: gzip
Give an example of the caching header for content that expires on a specific date
expires: Wed, 08 May 2019 04:54:20 GMT
Give an example of the caching header for content that expires periodically
cache-control:max-age=7200
Give an example of the caching header for content cannot be cached
cache-control:no-cache