In this video, you moved Sam’s starter recipes out of main.js into a data file, since they’re data, not code, and Sam can now fix a step without touching main.js. You loaded them with fetch(), and saw why the code has to check the server’s answer before it reads the file.
Key Points
- A page asks its server for a data file. The page came from Live Preview’s server, not from your disk, so it can’t read the file directly.
fetch("data/recipes.json")asks the server for it, as the page does for every file it loads. The path starts from the page’s folder, so the file goes in adatafolder next toindex.html. awaitwaits for the answer. At the top level of a module,awaitin front offetch()makesmain.jswait there until the server answers, and the rest ofmain.jswaits too. The page doesn’t freeze while it does. Why that works comes later.- First the answer, then the body.
fetch()gives back a Response, the server’s answer, not the data yet. Itsstatusis a number,200when the server found the file. Itsokistruefor any success status,200to299, andfalseotherwise.oksays nothing about what’s in the file, so it’strueeven when the file’s JSON is broken. The answer can arrive before all of its body, so reading the body takes a secondawait:response.json()reads it and parses it, asJSON.parse()does. - The Network panel shows the request. With the Fetch/XHR filter, which hides the page’s other files, a reload shows one row for
recipes.jsonwith status200. Click it, and its Response tab shows the body, which is your file’s text. The page doesn’t draw the recipes’ steps yet. If your box has saved recipes, the page shows those instead of the starters; open it in a private window to see the four starters. - A 404 doesn’t make
fetch()throw. With the path one letter off,data/recipe.json, the server answers with a 404, and an answer is still a Response, withokset tofalse. The log line afterfetch()still ran and showed404 false. The red line in the Console is the browser reporting the failed request, not an error in your code.fetch()throws when the request can’t finish, for example when there’s no server. - The error that does come blames the JSON. Reading the body threw
SyntaxError: Unexpected token '<'…is not valid JSON, because the body was Live Preview’s File not found page, in HTML, and the<is where that page starts. Acatcharound these lines would get the same misleading error. The real problem is the file name. - The rule:
fetch()hands you the server’s answer, even when that answer is no. Checkresponse.ok, then read the body. Whenokisfalse, throw anew Error()whose message names the file and the status.new Error()makes an error object like the ones acatchreceives, andthrowstops the code with it. - The error names the real problem. With the typo, the Console then says
Uncaught Error: Couldn't load data/recipe.json: status 404. The box still stops. The form is there, but the list stays empty and nothing works, and only the Console says why. You’ll show the problem on the status line later in the course.
The Code
Open Sam’s recipe box through Live Preview at http://127.0.0.1:3000/recipes/index.html. Make a folder called data inside recipes, next to index.html, and save this as recipes/data/recipes.json: