5.4 Storing Data on the Client side Flashcards

1
Q

CLIENT-SIDE STORAGE?

A

term used to refer to several separate APIs. It lets you store data on the client-side (your user browser) instead of a database on a sever

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Client-side storage USED FOR ?

A
  • make an app work offiline

- improve an app’s performance by limiting the number of requests to the server

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Several options to store data on the client-side?

A
  • cookies, web storage, IndexedDB and caching
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

APPLICATION CACHING?!

A
  • stores your app’s interface and other data (images, sound files, icons, CSS files, JS files) directly in the browser. Every browser has a built-in cache so that asset files don’t need to be requested every time a user loads a new page on a website.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

OFFLINE STORAGE? (client-side storage)

A
  • saves user-related data.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

COOKIES?

A

HTTP cookies allow you to save a small amount of data. The server then sends the data to the client via HTTP, and vice versa. Every time the client makes a request to the server, cookies get sent along automatically, and you can extract from them data on both the client- and the server-side. This data is typically checked to see if requests are coming from the same client or user.
- they’re sent along with every HTTP request so create as few cookies as possible for ur apps to reduce request size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

PERSISTENT cookies, SESSION cookies, ZOMBIE cookies

A
    1. persistent - can store data until the cookie expires
    1. SESSION - doesn’t have an expiration date set and will be deleted when the browser is closed (more secure)
    1. ZOMBIE - automatically recreated right after deletion - done by storing the original cookie’s content outside of the web browser dedicated cookie storage (used for activities such as tracking the number of unique users a product has)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

USING COOKIES in JS

A

Cookies are saved as key-value pairs. You can read, write, and edit cookies in JavaScript using the document.cookie 🎉interface, and you can also define when to delete them. It makes sense to set an expiration date for a cookie for security reasons; for example, you may want users to log out automatically if they haven’t been online for two months. If the user comes back after this time, the cookie will have expired, and the user will have to log in again manually.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

HTML5 Web Storage

A
  • new version of cookies, except that your data isn’t transmitted to the server along with every HTTP or HTTPS call.
  • allows you to save data in the browser by storing key-value pairs. The data stored must be in string format, which means that if you wanted to store a JavaScript object, you’d need to convert it to a string.
  • provides two mechanisms for storing data in the browser: localStorage and sessionStorage
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

HTML5 localStorage

A
  • data placed in localStorage is “per origin” - means it can be accessed and stored by pages from the same origin. Npr data stored in localStorage on https://careerfoundry.com can only be accessed by other scripts that run on https://careerfoundry.com
  • this is what’s known as same-origin policy
  • localStorage is useful if you want to pre-populate your app with data when the user starts or opens your application. If, for example, you were building your chat application for the browser (as well as mobile), you could save users’ messages in localStorage. This way, when a user opens the app, they’d be able to see the last messages they sent or received without having to wait for the app to request data from the server.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

same-origin policy?

A

a security concept that allows a web page to access data in a second page only if it has the same origin. With this policy, it’s impossible for a script to read specific data when it doesn’t share the same origin. As such, https://myportfoliopage.com can’t access data stored in localStorage on https://careerfoundry.com, but it could read and use images, CSS, and JavaScript files not protected by the same-origin policy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

HTML5 sessionStorage

A
  • second mechanism provided by the Web Storage API for storing data locally
  • per-origin-per-tab, which means that the same-origin-policy applies and data is only available in one browser tab
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

To decide between localStorage or sessionStorage

A
  • think about what your data is for and how saving data benefits your user.
  • let’s say you have several forms on your website, each requiring a name and email address, and you’re expecting users to fill out and submit more than one form during their session. In this instance, once the user has entered their data into one form, you can save it in sessionStorage, then use it to pre-populate the other forms. As soon as the user finishes and closes their browser tab, sessionStorage will be cleared and their data will no longer be accessible to you (or any other person trying to read your app’s storage data illegally!).

Choosing a storage option can also be a matter of security: if you have to store personal data like email addresses or physical addresses, save them in sessionStorage so they aren’t persistent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When not to use Web Storage

A

f you want to save a big JavaScript object in localStorage you’d have to convert it into a string first. Then, when retrieving the data from storage, you’d have to convert it back into an object in your JavaScript code. Because of this, you can’t save structured data in web storage, and you can’t get a specific value from stored items because you can’t query them. Instead, you have to fetch the whole string, convert it into an object, then get the desired value from the object.

Now, imagine you have 1,000 objects, and of those objects, you only need those that have a color property set to ‘green’. To find the data you require, you’d have to iterate over every object individually = SRANJE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

IndexedDB?!

A

IndexedDB, has no storage limits. It’s a NoSQL database that saves your data as objects with key-value pairs, allowing you to create queries to fetch your desired data.

Like localStorage, your saved data won’t expire, and the same-origin policy is applied, so you can only fetch data from the same origin. What’s more, you can use IndexedDB in every modern browser.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

INDEXING?

A

Indexing in databases prioritizes search keys for querying. If you work with large databases that hold a lot of data, creating indexes on your columns or documents can drastically speed up the querying process.

17
Q

USE IndexedDB if …..

A

1) you need to make an application accessible and functional offline, and
2) you work with big data sets that you need to query locally.

18
Q

CACHE?

A

browser cache works is different—it stores the files and assets that your website or application needs in the user’s browser.

The browser cache isn’t an alternative to cookies, web storage, or IndexedDB. Every browser has and uses the cache automatically to make your website more performant and reduce the number of files that have to be downloaded from the server for each page request. As such, the browser cache keeps your user’s data consumption low and helps speed up your website or application.

19
Q

BROWSER CACHE!?

A
  • automatically caches your website’s static assets like images, fonts, CSS, and JavaScript files. The first time you load a website, the files are downloaded from the server. The browser then saves them locally so that the next time you visit the website, it can read the files from the cache instead of downloading them again. As a result, the website loads much faster.
20
Q

CACHE BUSTING?

A
  • you’ve updated your website’s homepage and your main CSS file. A user who’s visited your site before returns after the update. Their browser loads the new HTML file from your server, but it loads an old version of the CSS file that it’s already cached. This is because the browser doesn’t know that the CSS file’s content has changed (as the new and old file names are the same)
  • Cache busting is pretty low-tech—it basically involves giving your updated files new names, whereupon the browser will treat them like entirely new files.

There’s just one catch: renaming your files every time you make a change could quickly get frustrating. Imagine changing “app.js” to “final-app.js,” then “final-final-app.js,” then “hopefully-final-app.js,” and so on—you’ll soon run out of new names! The solution is semantic versioning (1.0.0.)

21
Q

SEMANTIC VERSIONING

A

Once your project is ready to be shared with others, give the release the version number “1.0.0.” After this, changes should be numbered as follows:

Bug fixes and minor changes (patch release): Increase the 3rd digit (1.0.1). These changes will not break any existing code (i.e., they are backwards compatible).

New features that allow backward compatibility (minor release): Increase the middle digit (1.1.0)

Changes that break backward compatibility (major release): Increase the first digit (2.0.0)
You can specify which kinds of updates your app can accept in your “package.json” file. So if you were starting with a 1.0.4 package, this is how you would specify the ranges:

Patch releases: 1.0 or 1.0.x or ~1.0.4
Minor releases: 1 or 1.x or ^1.0.4
Major releases: * or x

22
Q

asyncStorage

A

React Native own storage system - has been deprecated, so you’ll be using another package called @react-native-community/async-storage