This is part three of my series of posts describing how I made my quiz game o(m)g:image.
- Project Announcement
- Pt. I: Design Iterations
- Pt. II: As Little JS As Possible
- Pt. III: The HTML
o(m)g:image is presented like a quiz:
- You get one question at a time
- When you choose an answer, it shows you if you got it right (and, if you didnât, what the right answer is)
- You go to the next question
To do this, I generate an HTML page for every question and for every possible questionâs answer (correct or incorrect).
In my source code I have a file that outlines all the questions and their answers, e.g.
- id: 1
answerId: 2
answersById:
1: The Ultimate Guide to Developer Happiness
2: The Key to Building High-Performing Teams
3: Woman Wins $500,000 at Georgia/Alabama Football Game
4: A Guide to Sizing Wedding Rings
- id: 2
# more data here
I use Web Origami as my build tool to loop over all this data and output an HTML page for 1) each question, and 2) each answer for each question. The URLs for accessing these pages follow this pattern:
/questions/:question-id/answers/:answer-id
So, for example, question 1 has 4 possible answers and answer number 2 (in the source data) is the correct one. So for that question, I create an HTML page for:
/questions/1
- e.g. âHereâs the question and its possible answersâ
/questions/1/answers/1
,/questions/1/answers/3
, and/questions/1/answers/4
- e.g. âSorry, you got it wrong. Answer 2 was right.â
/questions/1/answers/2
- e.g. âYou got it right!â
Because the game is structured this way, I have a pretty good idea how people will flow through the game:
- Question 1, then
- Question 1: answer 1, 2, 3, or 4, then
- Question 2, then
- Question 2: answer 1, 2, 3, or 4, then
- Etc.
So when a user lands on a question page, e.g.
/questions/1
And they click on an answer, rather than go fetch that page with JavaScript and insert it into the current page, I go ahead and let the browserâs default take over which is to do a full page reload.
However, I optimize for this by preloading the HTML pages where the user will go next. So if theyâre on /questions/1
, I preload each answer page for that question:
<!-- /questions/1 -->
<link rel="preload" href="/questions/1/answers/1/" as="fetch">
<link rel="preload" href="/questions/1/answers/2/" as="fetch">
<link rel="preload" href="/questions/1/answers/3/" as="fetch">
<link rel="preload" href="/questions/1/answers/4/" as="fetch">
Then, when they click on an answer, the browser already has that HTML page. It loads fast! For example, hereâs a question page and you can see in the Network tab of the Developer Tools how it preloaded each answerâs HTML page.
Similarly, when someone lands on an answer page for a question â like /questions/1/answers/2
â I have a pretty good idea where theyâll go next (the next question!) so I preload that:
<!-- /questions/1/answers/2 -->
<link rel="preload" href="/questions/2/" as="fetch">
Once again, things load fast!
This is possible, in part, because every HTML page is small. Hereâs a screenshot of âView sourceâ on the first question:
Because these pages are so small in size, itâs totally negligible to just load four or five of them in anticipation of a click.
Side note: I considered inlining all my CSS as a <style>
tag for each HTML page. But then as I thought about how I wanted to preload answers, I realized using a <link>
to a single CSS stylesheet across all pages would be even better, because while itâs one extra request it gets cached for every subsequent HTML page â trade-offs! Thatâs, in part, how every HTML page can be so small.
What I love about all of this is that I donât have to write any JavaScript. Because the UI is so small, and I designed it so each element on screen doesnât shift in layout from one page to the next, it can feel like a SPA with in-place transitions but itâs not! It requires zero JavaScript to achieve.
It feels akin to those tricks they use in film, like forced perspective, where you can achieve the illusion of something without the cost of special effects (very useful on small budgets). For example, I love this explanation of how they made the actors for Gandalf and Frodo in The Lord of the Rings appear in the same scene and, even though theyâre both full-sized humans, one appeared much more hobbit-sized purely via some tricks of cameras, tracks, and props â zero computerized special effects required!
It feels a bit analogous to web development, where personal-sized projects donât want to pull out the big âspecial effectsâ JavaScript used by big studios. You can achieve a similar, perhaps even better, effect using a few little tricks.
Reply via: Email :: Mastodon :: Bluesky
Tagged in: #omgImg