Year #2, Week #13 đ» đ
New stuff we learned this week: đ€
Cookies đȘ
- The HTTP protocal is inherently stateless â which means there is no built-in connection between requests. Itâs like every web server in the world has amnesia.
- But lots of things on the web need state - for stuff like keeping track if youâre logged in, holding game scores, remembering whatâs in your shopping cart, etc.
- One of the main ways that weâve worked around the stateless nature of HTTP is using cookies. Cookies are little bits of text that your browser can store, to be used later.
- A web server can instruct a browser to set a cookie by using the
Set-Cookie
header, like so:
HTTP/2.0 200 OK
Content-Type: text/html
Set-Cookie: goat=banjorodeo
- the above HTTP response from a server instructs the client (the browser) to
set a cookie named
goat
to the value of ofbanjorodeo
. - If the browser chooses to store the cookie (which it might not, based on user preferences), then it will send back the same cookie with each subsequent request to that domain, like this:
GET /sample_page.html HTTP/2.0
Host: www.radsite.com
Cookie: goat=banjorodeo
- this back-and-forth interplay between a server instructing a cookie to be set, and the client passing it along in subsequent requests, allows us to overcome the stateless nature of HTTP. The most common example is for a site where you need to log in. In that case, once the server verifies your username and password, it can give you a special secret token (usually a random string of letters and numbers). From then on, the client will keep sending back that special string, even though it is meaningless to the client. On the server side though, the server is able to match up the special token to know who owns the token, and the token then proves that they previously logged in successfully. This allows you to keep doing things without having to re-login in every single request. Cool!
- Javascript can also READ and CREATE cookies (with a few limitations, for security reasons). The API for doing this is absurdly awful:
// add a cookie called `herp` whose value is `derp`
document.cookie = "herp=derp";
// strangely, this doesn't' overwrite, it sets a 2ND cookie
document.cookie = "beep=boop";
// just accessing `document.cookie` reads all the cookies
console.log(document.cookie);
// -> "herp=derp; beep=boop"
- Cookies can be set to expire after a given amount of time. This is usually done for security reasons, and is the reason why you often have to re-login to sites every couple weeks. Itâs because your cookie expires, so you need to get a new one. No one wants an old cookie! To do this in javascript you would write:
document.cookie = "herp=derp; Expires=Thu, 21 Oct 2021 07:28:00 GMT";
- Note: if you donât set an expiration for a cookie, it will only last for the current âsessionâ, which means when the tab or browser is closed, the cookie will go away.
- Cookies can also be set to only be valid for a specific path or portion of a website:
// this cookie will only be good for paths like:
// example.site/docs
// example.site/docs/red
// example.site/docs/red/green
document.cookie = "herp=derp; path=/docs";
- To delete a cookie you actually have to re-set it with itâs expiration date in the past, lol.
// this will delete a cookie ÂŻ\_(ă)_/ÂŻ
document.cookie = "herp=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
Icon Fonts
- Icons are small graphics, usually used to quickly convey meaning to someone scanning a webpage or trying to use some functionality. Theyâre all over the web.
- Icons can be implemented in lots of ways, including just using plain old html images, or css background images, or svgs.
- One technique that has become very popular is to have a special font that consists totally of ICONS instead of regular letters. There used to be a common system font called âZapf Dingbatsâ that sort of worked like this.
- There are now a number of free and paid (or mixed) fonts out there that work like this, allowing you to easily embed icons in your web pages.
- one really nice thing about icons AS FONTS is that you can change their color and style them with CSS, giving you way more flexibility than just a static PNG or JPG image.
- But, instead of making you insert weird characters (like a âZâ to get a picture of a person) that make no sense to Google or assistive technologies like screen readers, most icon fonts instead rely on CSS pseudo-selectors, creating classes that map to icons, like this:
.dancing-lady::after {
content: "z"; /* đ */
}
- Then, you can just create some empty element and add a class to it, like
<i class="dancing-lady">
. The actual CSS is a bit more complicated than that, but it really boils down to just setting up a character as a pseudo-element. - Font Awesome is a very popular and well-done
icon font that supplies many of itâs icons for Free. Weâll be using Font
Awesome for some of our projects going forward. To embed an icon with font
awesome, you usually just need to link to some CSS or JS in the
<head>
of your page, and then, you use a few classes on empty elements (usuallyi
), like so:
<i class="fa fa-cloud-download"></i>
Useful Links:
- Color Keywords
- TouchType.co
- How To Type
- GitLab
- Execute Program
- Font Awesome
- Tic-Tac-Toe videos: #1Â Â #2Â Â #3
Homework Plan
- 1 days review all flashcards (in your app)
- 1 day Akron Snowmen assignment
- 1 day cookies (flashcards app) Homework đȘ
- 1 day icons Homework
- 1 day watch Tic-Tac-Toe videos #1Â Â #2Â Â #3
- 1 days touch typing practice
- 4 days Execute Program homework
Akron Snowmen Assignment
- Check your MR for feedback I left â Iâve requested at least one change from each of you. Make the requested change.
- Slowly and carefully review the Interactive Rebase portion of week 30 new stuff.
- squash your commits into one or more meaningful commits with GOOD COMMIT MESSAGES.
- push your branch back up. It should fail. Do you understand why?
- change your git command so that the push works.
- slack the MR url again so I can merge.
Icons / Flashcard App Assignment
- Slowly and carefully review the Icon Fonts section of âNew Stuffâ above ^^^.
- If your name is not Tabitha, get your parent to unblock these three domains:
https://fontawesome.com
andhttps://ka-f.fontawesome.com
andhttps://kit.fontawesome.com
. - check if you have an unmerged MR from the last time you worked on your flashcards app. If so, merge it. Then, connect to the HTC machine with vscode, switch over to master, pull from origin, and delete the old branch.
- make sure you are on master and have merged any unmerged work and have pulled before moving one.
- create a new branch.
- add this script tag to the
<head>
portion of./index.html
:
<script
src="https://kit.fontawesome.com/2f8568b58b.js"
crossorigin="anonymous"
></script>
- next, decide two different (at least, you can do more) places where you would like to incorporate an icon into your flashcard app.
- browse font awesome for a bit (but donât get carried away, OK?) and find the icons you want to use.
- incorporate the icons, and style them to nicely fit your app.
- commit your work.
- finally, add 20 more new cards from your old stack of non-app flashcards, and commit that work separately.
- build, submit a MR, and slack both urls, as usual.
Cookies Homework đȘ
- slowly and carefully read the âCookiesâ section of âNew Stuffâ above ^^^.
- connect with vscode to your tic-tac-toe or connect-4 project.
- if you had any last-minute tweaks, bugfixes, or styling improvements you wanted to make, go ahead and do those, but donât get too lost in the details.
- push up any new work you added from the last step.
- review your own diffs on your MR webpage â do you see anything you want to
fix before you merge? Are there millions of
console.log()
statements strewn about? Or silly names youâd like to fix before moving on? Do so. If necessary, do an interactive rebase. - merge your MR.
- switch to master, pull your merged changes, and delete the old branch.
- make a new branch.
- add a new feature the game should keep track of how many games each side has won, using a cookie.
- hints/requirements:
- It should display the current score of games somewhere (like âX has won 3 games, O has won 1 gameâ).
- if nobody has played or won any games, they both should say zero.
- you should not need to introduce any new state for this feature
- make sure the cookie lasts beyond quitting the browser, to do this, youâll
need to set an expiration. Hereâs a valid-formed future date you can use:
Thu, 07 Feb 2030 12:00:00 GMT
- you could set two cookies, one for each player, or you could design your cookie so that one cookie holds both sidesâ scores, itâs up to you to decide.
- the trickiest part of this might be figuring out how to set or modify the
cookie only once when a game is won, and not multiple times every time react
re-renders. Hint you might need to use
useEffect()
and itâs dependency array second argument to get this right (Iâm not actually sure, I havenât fully thought it throughâŠđ€ but you guys are smart, you can figure it out.)
- once youâve got it working, pop the hood on the browser and go to the
Application
tab of the dev tools. Experiment with manually creating, modifying and deleting the cookie. - Extra Credit: âš Add another cookie based feature â have the browser
save the game state so that you can quit the browser and re-open it and
the board and turn will be exactly as it was when you left it.
Hints/requirements for Extra credit:
- youâll need to do some thinking about HOW to save the game state into the cookie string. Do you want to JSON encode it? Or change it in/out of some other simple string representation?
- to boot up the app with the right saved state, youâll need to leverage check
the cookie (or cookies) when setting the default values passed to
useState()
- Kiah Credit: âš add a button somewhere that allows the user to reset all of the data stored in cookies, wiping the running tally of won games and the game state. Should this feature require some new state or not?
- Optional Extra Task â if youâve finished the other parts of this task, and it sounds fun, add one or more icons to your game app.
- build your game, submit a MR, slack both urls.