@@ -13,13 +13,13 @@ that includes high-level APIs to do networking, sandboxed code execution and
1313disk access. It runs on almost every platform, behaves similarly everywhere, and
1414is always kept backwards compatible.
1515
16- An important part of this machinery is networking. In the browser, there's 8 (or
16+ An important part of this machinery is networking. In the browser, there are 8 (or
1717so) different ways to access the network:
1818
1919- By navigating to a new page
20- - Through ` <script> ` , ` <link> ` , & other tags.
20+ - Through ` <script> ` , ` <link> ` , and other tags.
2121- Using ` preload ` headers.
22- - Using ` new XMLHTTPRequest() ` , ` window.fetch() ` , & ` navigator.sendBeacon() ` .
22+ - Using ` new XMLHTTPRequest() ` , ` window.fetch() ` , and ` navigator.sendBeacon() ` .
2323- Using (dynamic) ` import() ` .
2424- Using the Server Sent Events API.
2525- Using the WebSocket API.
@@ -45,31 +45,31 @@ The browser has several ways of performing HTTP requests.
4545- __ Navigating to a new page:__ performs an HTTP request whenever you type in a
4646 url in your browser. The server then replies with some ` index.html ` file which
4747 contains more links to assets and other pages.
48- - __ Through ` <script> ` , ` <link> ` & other tags:__ When an ` index.html ` page is
48+ - __ Through ` <script> ` , ` <link> ` and other tags:__ When an ` index.html ` page is
4949 loaded, the browser will read out all ` <link> ` and ` <script> ` tags in the
5050 document's head. This will trigger requests for more resources, which in turn
5151 can request even more resources. There's also the ` <a> ` and ` <img> ` tags,
5252 which act similarly.
5353- __ Using ` preload ` headers.__ When a browser page is loaded, the server can
5454 set headers for additional resources that should be loaded. The browser then
5555 proceeds to request those.
56- - __ Using ` new XMLHTTPRequest() ` & ` window.fetch() ` :__ In order to make dynamic
56+ - __ Using ` new XMLHTTPRequest() ` and ` window.fetch() ` :__ In order to make dynamic
5757 requests to say, a JSON API, you can use these APIs. ` XMLHTTPRequest ` is the
5858 classic way of performing requests, but these days there's the more modern
5959 ` window.fetch() ` API. Regardless of which API you use, they produce similar
6060 results.
6161- __ Using ` navigator.sendBeacon() ` :__ Sometimes you want to perform an HTTP
62- Request, but want to allow more important requests to be prioritized first.
63- For example with analytics data. The ` sendBeacon() ` API, allows for exactly
62+ Request, but want to allow more important requests to be prioritized first,
63+ such as with analytics data. The ` sendBeacon() ` API allows for exactly
6464 this: it allows you to create an HTTP POST request, but schedules it to occur
65- in the background. Even if the page is closed before the request had a chance
65+ in the background, even if the page is closed before the request had a chance
6666 to complete.
6767- __ Using (dynamic) ` import ` :__ Scripts can request other scripts, by using the
6868 ` import ` syntax. This can either be dynamic or static - but in both cases it
6969 makes an HTTP request to require JavaScript.
7070
7171### Fetch
72- There's many ways of creating an HTTP request, but the most common one these
72+ There are many ways to create an HTTP request, but the most common one these
7373days is using ` fetch() ` . In Choo you might want to trigger a request based on
7474some other event coming through the EventEmitter. Let's look at a brief example
7575of how to trigger a request based on a Choo event:
@@ -103,10 +103,10 @@ app.store((state, emitter) => {
103103 endpoint. The url is dynamically created based on the username that's passed.
1041043 . We know the response will be JSON, so we try and convert the binary blob to
105105 a valid JavaScript Object.
106- 5 . Now that we have an object, we add the new tweets to our existing list of
106+ 4 . Now that we have an object, we add the new tweets to our existing list of
107107 tweets. Once the new data is set, we emit the ` 'render' ` event to trigger a
108108 DOM update.
109- 6 . If any of the steps above failed for any reason, we emit the ` 'error' ` event.
109+ 5 . If any of the steps above failed for any reason, we emit the ` 'error' ` event.
110110 If this was a real-world project, this is where we'd also make sure we had a
111111 good user-facing error, and would report the error to our analytics server.
112112 Good error handling is a very imporant aspect of production applications.
@@ -194,7 +194,7 @@ app.store((state, emitter) => {
1941946 . If for some reason a parsing error occurs, we should emit an ` 'sse:error' `
195195 event.
1961967 . If everything has gone well, we can expose the event to the rest of our app
197- using the ` sse:message ` event.
197+ using the ` ' sse:message' ` event.
1981988 . Connection errors can occur. The connection can be closed from the server,
199199 it can be reconnecting or some other unknown error might have occured.
2002009 . If the connection was closed cleanly, we emit the ` 'sse:closed' ` connection,
@@ -277,7 +277,7 @@ app.store((state, emitter) => {
277277 ` 'ws:open' ` .
2782786 . We can now tell the system that our websocket is ready to be used, so we
279279 update our state before that.
280- 7 . Now that we're reading to start listening for events, we can emit ` 'ws:open ` .
280+ 7 . Now that we're reading to start listening for events, we can emit ` 'ws:open' ` .
2812818 . Whenever we receive a ` 'message' ` event, we emit the ` 'ws:message' ` event.
2822829 . Whenever the connection closes, we set ` state.websocket.open ` to ` false ` and
283283 we emit the ` 'ws:close' ` event.
@@ -287,5 +287,5 @@ app.store((state, emitter) => {
287287And that's it! We hope you've now read enough to get started with network
288288protocols in the browser! There's much more to explore, and as the web evolves
289289so will the protocols. But we're confident that interfacing Choo with the
290- browser's networking protocols will always remain straight forward and
290+ browser's networking protocols will always remain straightforward and
291291convenient. Happy (network) hacking!
0 commit comments