Skip to main content
View on YouTube to comment and like!

Syntax Video

Summarize Hacker News with Hono + Cloudflare

Chapter 1 0:00

Intro

What's up? CJ here. In this video, I'm going to show you how I built this application, which shows us all of the articles that are on the front page of Hacker News and gives us summaries of those articles, including the full contents of the article as well. Now, for this app, I built it out with Hono and it's running on Cloudflare. It's storing all of the data in their key value store. We're using Cloudflare AI to talk to an AI model to actually do the summaries. And for our Hono app, we're building it all out with JSX. So, if we look at the source code of this page, it's just HTML. It's a beautiful thing. So, this video is going to be split into two parts. In the first part, I'm going to give you a quick overview of how all of this is hooked together.

I'm going to point to some of the more interesting things, show you some of my favorite pieces, and really just show you how easy it is to get up and going with serverside rendering of JSX inside of a Hono app that's running on a Cloudflare worker, which is really awesome and a joy to work with because you don't have to set up like a React app or a Nex.js app. It's so simple to just start serverside rendering JSX with Hono and so I'm pretty excited about that. Plus, I'll show you how I get article contents, summarize them, etc.

The second part of this video is going to be the full build. So, I'll take a step back. We'll start from the beginning and I'll show you how I built this thing step by step. If that sounds good to you, let's dive in. My name is CJ. Welcome to Syntax. [Music] Now, before we dive in, I do want to let you know that Hono is having a conference in October in Japan. And so on October 18th, Hono Conf 2025 is happening. This is the second annual conference that they're putting on and I've been invited to speak. So I'll be there along with a lot of these other talented speakers. And currently there is a call for proposals open. So if you'd like to give a talk at Honoc, definitely apply. And you don't necessarily have to be an expert. They have a user session where people can talk about how they're using Hono and maybe some tips and tricks that they've learned. So they're looking for speakers. So definitely apply if you haven't. And they're looking for sponsors as well. Also, if you want to sponsor Hon, you can uh reach out to the organizers. Century is sponsoring Hon as well as sending me to Hono to speak. So, that's going to be a good time. So,

Chapter 2 1:21

Hono Conf 2025

definitely check out Hono. I'm going to be there. I'd love to meet you in person. And also, if you have something interesting to talk about, submit a CFP. All right, let's get into it. So, here's the app I've built. First, if we take a look at the package.json, you can see we have a couple of scripts. We've got our dev script, which runs Wrangler. So this is a CLI tool provided by Cloudflare to actually run your workers locally. We got a deploy command and a command to generate types so we can actually use our KV bindings and AI bindings. Now in terms of dependencies, we're really just using hono. And then we have a few other dependencies that let us extract feeds from an RSS, extract the contents of a page. And in order to extract the contents of the page, we actually need a DOM. And so linkdom is very similar to JSDOM, but basically lets us use a DOM in a non-browser environment. And so this is everything we need to actually extract the contents of the pages and extract the contents of an RSS feed to ultimately send all of that stuff to an AI to be summarized. Now, one of the great things about Hono is when you generate an app with the CLI, it comes preconfigured to work with their JSX layer. You don't have to do anything

Chapter 3 2:13

Tour of the Code

extra. So all I've done is in my Wrangler.json C, this is your config file that describes how to run your app. I pointed it to a TSX file instead of a TS file which is the default. And then if we look at thattsx file, I can now start using JSX directly inside of the file. So the first thing I've done is set up a JSX renderer. This is a middleware that's provided by Hono. So you can basically create the shell of the page to then render other contents inside of. So you can see here that I'm setting this up on every single page because I'm building a HONO app where every single page is serverside rendered as HTML and everything is going to have this shell. So essentially we can define the the common CSS that's going to be loaded for every page. We could also add like meta tags or anything else we wanted to and then whatever we're rendering is going to get rendered into that exact spot right there. Now from there to actually use this renderer the context provides this render function and we can pass in some JSX. So you can see I've tapped into both the notfound handler and the on error handler to actually render out those pages instead of just returning the defaults. So, if

someone ever goes to a path that's not found in my app, we're going to render, which uses that JSX renderer middleware to create the body and styles and everything else and then just throw an H1 right into the body that says that that path wasn't found. And then if there ever was an error, instead of showing like the default error or the stack trace or everything else, we're actually going to render out a page and display that message. And so, right now, this is one of my favorite things about Hono. It's just completely nononsense, almost zero config to get up and going to start server rendering some pages. We don't have to worry about like a view engine or templates or anything else. We can literally just start writing JSX and everything else just works. It's fantastic. Now for the actual rendering of the homepage, this is where we go off and get the feed of all the items from the RSS. Once we have those items, then we can actually render them out. Now, I'll dive into that feed function and also these other functions that are called here. But this is one of the other very cool things about writing JSX inside of Hono. All of this code is running server side, right? So any JSX that we're writing here is not client side code. This is all stuff that's going to be running on the server. So we

can do things that you don't normally really even see inside of Nex.js, right? Like I'm actually awaiting inside of a call to render and doing multiple asynchronous requests. And basically each one of these functions is going to resolve with a JSX element that ultimately is just going to get rendered out in this page up here. So basically all of this stuff is running on the server and at the end of the day is just returning HTML. Now, it's important to note that Hono does technically have a client runtime if you do want to write client components, but for this app, there's no client side codes. Everything is rendered on the server and it's just returned as plain old HTML and CSS. Now, if we take a look at that get feed function, this is simply just calling this extract method with the link to the RSS feed. So, Hacker News provides an RSS feed. There are APIs available, but the RSS feed is perfect for my needs because it's literally just the front page with the 30 most upvoted articles.

And we can actually pass that RSS feed directly into this extract method from extractis feed extractor. So this is a really great library for working with RSS feeds of all types. You can pass in feeds that are JSON or Atom or basically any type of RSS feed. It can pull that in and then normalize it into some feed data. So that way we can actually get access to the items from that RSS feed.

I only had to do a little bit of extra work here because I wanted to include the link to the Hacker News comments. So that's what this method is doing here. But at the end of the day, we just return that array of entries, which is every article on the homepage of HackerNews. From there, I need to get the article contents and the summary for every single one of my articles. So I iterate over that array of items. And then for each one, I have this function called get article and summary. We're passing in our key value instance and our AI instance. Both of these are bound to our context here whenever this is running inside of a Cloudflare worker.

So we can actually call these AI methods and also put things and get things out of this key value store. So the result of this call is the actual contents of the page the art the full article itself and then the summary that we got back from the AI. So with all of that data, we render out a detailed summary card that has the title of the article, but then when you expand it, you can link off to the article itself or the comments and then below that show the summary and the article if we were able to retrieve it. Now let's take a look at this get article and summary method.

Essentially what we do is first check the key value store. So Cloudflare KV is basically just like a Reddus. It's a key value store where you can put things in that have a specific key and then get things out with that specific key. And so what I'm doing here is first checking to see, have we seen this article URL before? Because if we have and it's in that KV store, just return it immediately. And so that's how my page is able to load so fast is because it only has to do the work of requesting the article and requesting the summary once. After that, it's always just pulling the data directly from the KV store. And so once you've added this KV namespace into your app, you can use it.

And it also supports JSON data. So technically when you store the data you're just storing it as a string but when you pull it out the library can actually parse it for you and then you can get it back as a specific type. So this article summary type is just an object that has the article itself which which is a string and then the summary itself which is a string. So if it didn't exist in the cache we need to go get it now to request the article contents itself. I'm just using fetch.

But one of the nice things about using fetch inside of a Cloudflare worker is it has these CF options which can automatically cache those requests. So, this is another layer of of speeding things up where I'm basically just requesting the raw contents of that page, but I'm caching it for 24 hours. So, if any other code needed to request that same page, it can actually pull it from the cache first. Before, I've actually done this myself where I'm putting it into a KV store and then pulling it back out. But, it's really great that Cloudflare supports this by default. So, it's just caching those requests. And this way, I'm not bombarding these servers with hundreds and thousands of requests. Basically, my worker is only ever going to request their website once every 24 hours. And so that gets back the raw contents of the page. Now, we need to actually extract out the main article. So, we can pass that off to an AI. Now, parse HTML comes from this library called linkdom.

Like I mentioned earlier, it's very similar to JSDOM. Essentially, we can do DOM like things, but without a web browser. And inside of a cloudflare worker, you're not in a browser. So, after pulling back the page, we have to do a little bit of manual work. This isn't supported directly by linkdom just yet, but basically any image or link needs to have that URL as the base URL.

So we actually iterate through the document and replace any image URLs or links to basically have that URL as the root so they can load properly. But from there we can just pass that document into readability. So readability is a library that will actually given some web page extract out just the content. So just like the main article of a web page and this library is actually from Mozilla. It's built into Firefox. So, let me show you. For example, if I go directly to this article, if you use Firefox, this little button right here, when you click it, that extracts out the main contents and that's the exact same library that we're using. So, we just installed it from npm. It's called Mozilla Readability, and we're doing that exact thing here. So, pass that document in, extract out the contents.

Now, from there, we can take that actual content that was extracted from the page, and that's what we're going to send to the AI. Now, in my site, I'm actually rendering the full article underneath each one of these cards. And so, I didn't want to just add HTML from some random website. So, I'm using DOM purify. So, this is an industry standard library. It automatically sanitizes the page, removes any scripts or cross- site scripting attempts, and that gives us some clean HTML that we can actually append to our page. And that HTML is also the thing that I send off to the AI. So, that gets the just the contents of the page, summarizes it. When that comes back, we now have the full article and the summary. And from there, we put it into the KV store. So when you put things into the KV store, like I mentioned, they need to be strings. So we just stringify that object. But the next time this method is called, if we're calling it with the URL that we've already summarized, it's just going to pull that from the KV store. So we don't have to do this work all over again.

Now, this summarize function just wraps a call to Cloudflare workers AI, but actually can retry multiple times if it fails. because I found in some of my testing that the API will fail every now and then with like an O error or an unknown error, but if you retry a few times, eventually the actual request to the AI will go through. So my summarize function first truncates the article content itself because it's possible that the article is really really long and the model itself only has a certain context window. So this is just a very rudimentary way of limiting the number of tokens. is I'm basically just saying only grab the first 5,500 words or less because if there's less than 5,500 words, you're going to get the whole article. Uh, but this prevented some errors from the AI that basically said you're out of out of context. So that gives us the truncated content. And then I just have this function here which actually calls Cloudflare worker AI. So when you're using Cloudflare worker AI, you can pass in the model name here and then pass in all of your chat history. A

lot of models actually support a chat template. So you don't don't even need to pass in messages. you can just pass in the actual prompt and behind the scenes it it constructs it in the right way. But for my particular app, I did not want to use that built-in model for summarization. So when you're looking at the models page, this model here, Bartlarge CNN, is a specific model for summarizing articles. But when I was using it, it only gives you back maybe like one or two sentences summary. And I wanted a much more in-depth summary. So instead of using this model, we actually use a model that supports Laura or uh low rank something. Let's let's see what it exactly stands for. Yeah, low rank adaptation. Um and the nice thing about this is for certain models that Cloudflare supports, you can use a Laura adapter. So in their finetunes, if you look under public Laura adapters, you can see that there's this CF public CNN summarization adapter that can be applied to this specific Mistral model.

And the cool thing about this is you could create your own adapters. There's lots of public adapters out there and basically these adapters are fine-tuning a specific model because technically you could just write a prompt to this main mistro model that says summarize this article but it might not always be the best. Basically a fine-tuned Laura adapter has been trained on a bunch of specific tasks. In this case, summarizing articles. So that way when you use that adapter, the model behaves in a specific way and and is specifically good at what whatever your adapter is training it for. In this case, summarizing articles. So for my app, this is what we used. We used this model with this Laura adapter. And this got us some more in-depth summaries that weren't just like two sentences. It told us a lot more about what that full article was for. So essentially pass in the model name. In this case, we're using Allora. Pass that in. When you're using Allora, you do need to say raw true so it doesn't use a chat template and then my message is just summarize

the following which is the truncated content. Now this do work function is the thing that potentially will fail. So all I've done is put that into a function and then I just have a while loop with a max number of retries and the current number of retries. So basically it tries to summarize if it fails increases the number of retries and then just keeps going until either we have a summarized article or we've reached the max number of retries and from there returns the article contents back to the other side which then gets stored into the key value store. So that is a whirlwind tour of what this app does. Like I showed earlier, after that comes back, we have access to the summary and the article and then we render all of that as HTML because once this promise resolves, all of that JSX content gets turned into HTML and then inserted as the child of this page here.

Now, I talk about this at the end of the video after building this whole thing. This is a very simple setup and typically whenever you request the page and we don't have a summary available. So, let's try doing it right now. Actually, that responded quickly. That just means that all of our articles and summaries are already in that KV store.

But every now and then uh once that RSS feed has been updated with a new article, we have to go off and summarize it. So every now and then if you visit this page, it might take 20 30 seconds to load because it has to go off and wait for that AI response. Now in the real world, you don't want that kind of experience for your users. So you probably would use something like Cloudflare workers. And instead of summarizing this in this one worker, we could actually cue a job that says, "Hey, go summarize this article." Our main page could render and it could say something like waiting on article summary. And then whenever that worker finishes, it could put the summary in a KV store. And then when we come back to this page, we could see that that summary is available and then and show it here. So, so that's a more complex architecture uh that isn't supported on the free tier. But if you want to see something like that, let me know down in the comments. This is fairly trivial, fairly high level and and works for the needs of this website. So, all of this code is linked down in the description.

If you want to dive into it, if you want to clone it, use it yourself, feel free to. In the next part of this video, we're going to build this from the ground up, and you can follow along if you'd like. Okay, so now let's build this thing. If you head to hono.dev, click on get started, they have a command you can run to generate a basic Hono app. So, run this command here. I'm going to call this myin summary app. And for this I'm going to choose Cloudflare workers. And I'll go ahead and install dependencies. And I like to use PNPM for that. And now we can head into that directory and open it up with VS Code. Now if you look in the source directory, we have our entry point which just creates a basic Hono app. And if you look at our Wrangler.json.c, this is our configuration file that actually will start up the app. And if we head to our package.json, you can see that we have several scripts. Uh first up, I'm just going to run dev and we can get a basic Hono app running. So if I do pnpm dev, that starts up the wrangler CLI. And now it's listening on 8787. And there we go. We've got our Hono app says hello Hono. Now you'll also notice that

Chapter 4 16:14

Create a Hono App

there's this CF typgen script. Essentially, you can run this anytime you've added any new binding to your config file, but that will create the types. So that way whenever you're doing Cloudflare specific things in your codebase, TypeScript will be happy. So if you do ppm cf type gen, that'll run that. You can see right now I don't have any bindings, but those types were generated for me. And all of that binding and configuration information is stored in this wrangler.json C file. So you can see it has the name and then it also points to that entry point. And then there's lots of examples of other types of bindings that we can add. Now the first thing I want to do is just get this rendering some tsx. So I'm going to update this to be a tsx file instead.

Chapter 5 17:37

JSX Setup

And then my wrangler.json C, I'm going to point it to index.tsx instead. And then right here in my main handler, I'm just going to do a c.html. and right in line. Just do a nice little H1 that says hello world. Save that. We'll reload since we're pointing to a new entry point. And if we visit the app, look at that. It renders out as HTML. So, this is one of the beautiful things about Hono. It's ready to go out of the box with TSX. And all of that's preconfigured in the TS config. So, it knows exactly what to do. And we're essentially server side rendering some JSX code here. Now, since we're responding with JSX, we probably want a full HTML document with a head and a body and everything else. So, if you check out the Honodo docs, they have a JSX renderer middleware. Essentially, we can set up this middleware so that any particular route that is rendering JSX can automatically have a shell, right?

Like the HTML tag, the body, etc. And in this example, they're setting it up so that any route under/page will use this renderer and render that TSX right inside of that shell. Now, for my app, we're going to render everything with JSX. So, I'm actually going to set this up as a middleware for the whole app. So, right below where I create the app, I'm going to do an app.use, say everything, and then specify my renderer, and we can import that JSX renderer. Now, with this, instead of using C.HTML, we can use C.nder. And that essentially tells Hono that we should use our configured renderer instead of just returning that HTML directly. And with that, if we refresh the page, now it's using the renderer.

And if we look at the source, we can see that it has that shell. And so this is the perfect setup if we want to go ahead and add some common meta tags or CSS or anything like that. So let's do just that. And for this simple app, I'm going to use Pico CSS. It's basically a CSS framework with preconfigured styles for everything. So the moment you add it, it just styles everything by default. So to add this here in the renderer, we're going to add a head tag. Throw in that link. And now when it renders the page, it's going to use that CSS by default.

Chapter 6 19:43

Not Found Handler

Okay, so let's clean this up just a little bit. I'm actually not going to put the children inside of any element. It's just going to be right there inside the body. And the next thing I want to do is add a not found handler because right now, if I were to go to something that doesn't exist, you can see it uses the built-in notfound handler, but I actually want it to render some JSX. So in Hono there is a not found method that we can tap into and this gives us access to the context and here we can actually do some rendering. So right inside of here I'm just going to do a C. We'll add an H1 that just says not found and I specifically want to show which path they were requesting. So we can just get that from the context. So C.Rrec.path is the specific path that they were trying to go to. So just like that now when I go to a not found page it actually uses my JSX renderer. Now we could clean this up. Let's actually give the body a class of container. This is one of the few classes that are available via Pico that adds just some nice padding around the edges. Awesome. So, we have a not found

page and we have our homepage here. The last thing I'll do is set up an error handler. So, this is very similar. We can do app.on error and this gives us access to any error that was thrown so we can potentially show it in the UI. So I'm going to do a very similar thing, but instead of saying not found, we'll just say error. And right here, we'll display the error message. So this is just a standard JavaScript error object.

Chapter 7 20:43

Error Handler

So it should have a message property on it. And so now to test this out, let's actually throw an error. So in our homepage, let's pretend that something went wrong and throw an error right here. So now if I request my homepage, that error handler is going to kick in and render out that error message here. Now, one thing is if we take a look at the actual network request, it's still responding with a 200 status code. So, because this is the error page, we're going to go ahead and respond with a 500 status code right here. We'll just do C.

Status and just set that to an internal server error. And so, now when we request this page, it is responding with a 500 status code. Awesome. So, let's get rid of that that fake error there. And uh we're back to a basic app. So, from here, we're ready to go. We've got not found handling, error handling. We've got our basic JSX renderer. Now we can actually start implementing some app logic and if anything goes wrong, we're going to be able to see those errors in the UI and if anybody's like poking around on our site, we're going to be able to show them, hey, that path isn't found. Now, to list out the latest front page from HackerNews, they actually have an RSS feed available. So, if you go to news.wy combinator.com/rs, it actually responds with an XML RSS feed. So, this is what we're going to tap into to just get the latest posts on the Hacker News front page. So in my source directory, I'm going to create a new folder called lib. Inside of there, we'll create a hackernews.ts file. And this is where we'll make the request for the RSS feed. Now, we could parse this oursel. It is just XML data, but there's a nice package that I like to use that does all of this for us, and

Chapter 8 22:01

Requesting Hacker News RSS Feed

it's called feed extractor from the extractus or. So essentially we can pass it an RSS feed URL and it does all the normalization and figuring out of how to get the contents of that RSS feed and normalizes it into a nice usable JSON object. So let's install this as a dependency. And now to use it I do need to enable the Node.js compatibility mode. So if you take a look at our Wrangler.json C, you can see that workers have the possibility of enabling this NodeJS compat. And we are going to need this because that library uses some node specific things. This compatibility layer will slightly increase the size of your worker. Uh, but we're going to be able to use packages that were mint for NodeJS. So now let's try to use it. And right inside of here, I'll export a function get feed. It's going to run this codes here. We'll pass in that URL.

Make it an async function. And for now, just return data. And so this is going to be an array of everything in that feed. Now let's use this on our homepage. So right on the homepage we can just say the items are a call to get feed. We'll import that in, make this an async method. And now we can render them out. So for this, I'm going to use a fragment, and then just map over those items. And it is JSX, so I can do just that. So I'll say items.mmap. For each one, we're going to get the specific entry. And then for each of those entries, we're just going to render out nice little H2 with the title that we got back from the RSS feed. So when someone hits this page, it's going to grab the RSS feed, render out a bunch of little article elements, and then return the page. So let's start the app back up. And then now when we visit the homepage, look at that. It requests the feed and then renders all of these out.

Chapter 9 23:30

Rendering Hacker News RSS Feed

And this is all server side code. So if we look at the actual source of the rendered page, it's just HTML, right? So we're writing JSX. It looks like React code, but all of this stuff is running on the server side. There is no client side code at all. Now, let's make this look a little more interesting. If we check out Pico CSS, they do have a few different things we can do. And I think I want to turn this into an accordion.

So that way each title can expand and collapse. And inside of there, we're going to show the actual article summary. And the nice thing about Pico CSS is it just automatically styles standard semantic HTML elements. So you can see if we use a detail summary layout, it's going to automatically turn that into accordionss. So that's what I want. And they also have like an outline variant. That looks good to me. So I'm going to grab this. This is what we actually want instead of the article.

And the summary will be that title. And then the details themselves we'll put inside of a card. And this is where we're going to actually put the the summarized version that we get back from the AI. So right here, I'm just going to say summary coming soon. And with that, if we head to our homepage now, we've got a nice little UI and for any one of the articles, we can expand it. And this is where we'll show all of those details. Now, one thing I want to do is link off to the full article itself and then also link off to the hacker news comments. So on an article card, I can have a header section. And again, these are just semantic elements. And I want to render out an anchor tag that just says article. And then the link is going to be the entry.link. So that's automatically included in the RSS feed.

And then we'll add a couple of extra things here. So let's set the target to blank. So it opens up in a new tab. And then we'll also set our re for security. So we're going to say no follow and no opener. So now when these render out, they have a nice little link. We can click it. That goes to the actual page. And then the RSS feed also includes a link to the actual comments themselves, but it's not included by default because extractis tries to normalize the feed and the comments field isn't a normalized feed that's in a lot of RSS feeds. But extractis does allow us to pass in some options and there's this method called get extra entry fields. So for any one of the entries, we can take a look at it and grab extra stuff that it would not have pulled off before. For right now, I'm just going to log it and return nothing. Uh, but this is going to let us see those extra fills that we potentially have access to. So, if I refresh the page, we can take a look at the server logs and see that each one actually does include this comments property. And so, I just need to pass

that through. So, right here, I can just say comments is entry data comments. TypeScript is going to complain because this is the object type. We're just going to set it as the any type because we know it has that specific comments property. Uh but now in order for TypeScript to know that that comments field exists on the other side, I'll create a custom type that does let it know about the comments field. So I'll say that this is a feed entry. So feed entry is a type that comes from extractus and we'll say it's a feed entry array, but we can extend the type to say that it also has a comments property which is a string. So now on the other side we'll get access to the comments. And let's just add a little separator. And then right below it, we'll add another link that says comments. And here we'll just do entry doc comments. And so now for any one of our entries here, we have a link off to the hacker news comments as well. All right, now comes the fun part. For each of these articles, we actually want to get the article content and then send that off to an AI to be summarized. Now, we technically could just use the fetch

Chapter 10 27:27

Requesting and Extracting Article Content

API, but there typically is a lot of extra HTML and extra stuff whenever you request the contents of a page. So, there's a package that I like to use that's from Mozilla. It's called readability. And this is the actual code that's used inside of the readability extension. If you've ever used Firefox, like when you click this button, it turns a page into just its contents. But this package here on npm is the same code that's doing that. So, basically what we're going to do is request the direct source of any given article via its URL. So, request that source. We'll then pass that source into this package.

and it has a lot of smart code that knows to look specifically for the content section of like where is the article on this page and then once we have that that's ultimately what we're going to send to the API. So let's install this as a dependency and then we need one more dependency. So if you take a look at their docs they mention using it inside of Node.js you can use a package called JSDOM. So JSDOM is basically a pure JavaScript implementation of the DOM. I have had some issues trying to use JSDOM inside of a Cloudflare worker. So instead of JSDOM, I'm going to use a package called linkdom. And this works in a very similar way. Um it just doesn't do stuff that's not supported by Cloudflare workers. And you can see that we can pass in some HTML. And this actually gives us back a window object, a document object, but this is all without using an actual web browser. So I'm going to install this as a dependency as well. Now let's use it. So for each one of those links from the RSS feed, we're going to request the contents of the page, pass that contents into

readability, extract the actual document itself, and for now, we'll just render that on the page. So we'll basically have a page that shows the full article contents. Once we have that working, then we'll send that contents off to an API to be summarized. So I'm going to grab this example here, and then for each entry in our map here, I want to make that request. So we're going to do something really funny. We're going to do a promise.all all over this map.

We'll make each of these an async entry. And what I'll show you is that this will just work by default. And this might feel really weird if you worked in React or worked in Nex.js or whatever else, but basically this code is going to wait to be resolved on the server side before this render even gets called. So even without writing some async code, if we go back and just refresh our homepage, the page still renders just fine. So that means that for each one of these entries, we can go off and actually do something asynchronous. So right here for each one I'm going to grab a result and here we'll say get article and summary we're going to pass in that link and then let's define this method here.

So in our lib folder let's create an article.ts file and we're going to export an async function get article and summary that takes in the URL which is a string and I'll go ahead and paste in that example that I brought in. So essentially we need to import readability in. We're not going to be using JSDOM. So we'll update that. And then this code's going to actually happen inside of here. But now we need to set up linkd. So if we check out linkdom, the way that it works is we have this parse HTML function that gives us access to that document. So we'll copy this example. And the codes that we want to use just this parse HTML section. We can do this here. We'd pass in the HTML. Specifically, the thing that we want is the document. So, we're going to extract that out and that's what we'll pass directly into readability. Okay, that's looking good.

Now, let's request the actual HTML for that specific URL. And for this, we're just going to use fetch. So, we'll grab the response. We'll say fetch that specific URL. Now, the nice thing about using fetch inside of a Cloudflare worker is it actually has access to some automatic caching that'll happen in the Cloudflare backend. So here in our fetch options, there's actually a CF option.

And we can pass in the cache everything property. And here I can set this to true. And so now anytime a fetch request is made for that specific URL, if it's in the cache, it's just going to be served directly from the cache. So we're not going to bombard all these servers with a bunch of requests from our worker. Basically, that request will happen once and then any other time we try to request it again, it's going to serve it back from the cache. Now, if you check out the docs for cache using fetch, you can also pass in a TTL that's in seconds. So, the time to live. So, here we can say, let's just cache this request for that URL for 24 hours. So, here we'll do 60 seconds* 60 minutes time 24 hours. So now it'll only request that page once every 24 hours. From here, we can get back the HTML of the page just by saying response.ext. And then this HTML is what we actually want to pass into the parse HTML method.

that's provided by linkdom. So now we pass that document into readability. We parse it and this article here has a few different properties on it and the content property is the actual content of the page that got extracted. So our method get article and summary I think just wants to return two things. The article itself will be the content. So here I'm going to say article.content and then the summary is also going to be some text. And for now we can just use article.exer. So, this automatically grabs like the first few characters of the page. Eventually, we're going to replace this by actually summarizing it with an AI, but this will just give us a placeholder to show there. Now, it's possible that the parsing failed because we are just doing a straight up fetch for that HTML. And if that page uses any sort of like client side rendering or anything like that, we're we're not going to be able to access that. And that's fine. If you wanted to do that, you technically could spin up like a browser, but that would be a lot more processing power and potentially take a lot more time. So all I'm going to say is if we were unable to get that article, we just return nothing and we

won't even try to summarize it. So if not article.content, then we'll just return the article as null and the summary is null. So with that, we're now prepared to actually show the contents and right now the excerpt on the page itself. So let's import this function. Get article and summary. Now entry.link according to the feed entry types could be undefined, but we know that their RSS feed isn't going to respond with like a blank link. So, I'm just going to say, hey, that link does exist. This gives us back the result. And then right here where we say summary coming soon, instead of doing that, we will say um if result.art article has a value, we'll render some stuff out. Otherwise, we'll say unable to retrieve article. And right inside of here, we'll render out a div that has we'll say the summary. We'll show the results summary. And then below that, we'll have the article. and we'll render out the results. And between the two, let's just add a horizontal rule. And while I'm thinking about it, summary

technically could be empty as well, like if the AI failed to summarize or something like that. And so here we'll say if results summary is valid, render it out. Otherwise, we're just going to show an error message that says summary unavailable. Okay. So now when we request this page, it's going to go off fetch the contents of every single link, extract the content for every single link, and then render it out right inside of that detail summary card. So now with that, if I refresh the page, it's going to take a few seconds because it does have to go off and request the contents of every single page, but it is storing that in the cache. So now if we look at each one, we can see that it actually extracted out the contents of that article. That's awesome. Uh, but if we refresh the page, should be a little bit faster. It's not. It's probably the extracting does actually take a little bit of CPU time. So, it's not any faster than that. We can try requesting again.

Slower. That's okay. I still want to speed this up though because at this point, we've already extracted the article contents. I don't want to have to do that work every single time this page is loaded. And if we take a look here, this should be using the cache, but this bit of work right here, I'd also like to cach because I don't want to have to extract the content every single time this page is loaded. And for that we can use the cloudflare key value store. So essentially after extracting the article contents we'll store that in key value and then the next time we call this method if it's in key value just use it from there. So if you take a look at the workers KV documentation you can see that once we have it enabled in our cloudflare worker we can do envame of our keyspace and then we can put and get things from that key value store. So if you head to getting started, they give us a wrangler command that we can run to create a key value space. So I'm just going to copy the part without MPX and then I'll kill my app. From the command

Chapter 11 35:20

Storing Article Content in CF KV

line, you can just do PNPM wrangler kv namespace create. And here I'm going to call this articles. And so I didn't cover this, but I actually logged in with the Wrangler CLI. Uh if you try running this command and you're not logged in, it'll prompt you to open up the browser. you'll log into your Cloudflare account because this is actually creating those resources remotely. So we'll say yes here and now we'll have access to this key value namespace. So if we take a look in our Wrangler.json C, you can see that it's automatically enabled that namespace here. And now in order to get access to the types, we can use this script that was provided for us when we generated the app. So if I do pnpm cf typgen, you'll see that it updated cloudflare env to give us access to articles. And one last thing we'll do in the setup is make sure we install types/node.

So that way any nodejs specific things we're doing are going to have the correct types. Now in order to use this in our hono app, we can actually pass this env as our bindings when we create the hono app itself. So right here, we can pass in a custom type. I'm going to call this app env. And then I'm going to create a new file called types where I define this and specifically we want to use those bindings and those come from cloudflare. So after we generated those types these are instantly available cloudflare.env. And so now with this type available we'll import it in here and you can see inside of any hono method if I do c.env we're going to get access to those specific bindings. So c.env.articles is a key value namespace. So we have access to get, put, list, etc. So in order to use that key value name space inside of our method here, we're just going to pass it in. And instead of having a bunch of variables, I want to pass in an object of options. So if we head back to that get article and summary, I actually want this to take

in, let's call it options. And this is going to have a URL, which is a string. And we're also going to have the articles KV, which is a KV namespace. So this is a type automatically provided by Cloudflare. And now we'll have access to that. So right inside of here we can say options URL and then we can actually store things in this KV namespace and also retrieve things from that KV namespace. So on the other side we should pass in these options. We'll say the URL is that and the articles KV is C.env.articles.

So let's use it. Now after extracting the content or the article, we'll put it into the KV namespace. So really what I want here is a result variable that defaults to both things being null. And then if article.content has a value, then we'll set the result to be equal to that. And we'll make this overwritable.

And of course, I should define a type for this. So let's let's define it in our types here. So we're going to have this uh article summary type where the article is a string or null and the summary is string or null. And then now let's use that type. So this is the article summary and we need to invert this. So if article.content has a value pass it in and in this case excerpt could be string null or undefined. So let's update our type. So this is string or null or undefined for both of these.

Okay. So fetch using the cache extract the contents. We now have it in a variable. Now once we have it I want to put it into that key value store. So right here I'm going to say await the options. KV.put put and we'll specify the key. In this case, we're just going to use the URL itself as the key. So, we'll say options URL and then we want the value. And in this case, I'm just going to stringify that result. Now, the key value name space also accepts some options. So, we have access to things like expiration TTL. So, we could say that we only want to store the contents of this article for 24 hours and then the next time this request comes in, we'll potentially like re-reumize it.

I'm actually going to let it live in the cache forever because I don't want to resummize it later on. So that puts it into the KV namespace and then we'll just return it. And what we need to do now is at the beginning of the function is check to see does it already exist in the KV namespace. So right here we can say the result is options. KV.get the key is options. URL and in this case we want to bring back JSON. And we can specifically type it to say that hey because you're getting back JSON this is going to be of type article summary. And now we'll say if result has a value then just return it. So we're going to return it immediately from the cache. And here let's actually define our variable as a result here. Overwrite it here. And everything else looks good to go. Okay.

So request comes into this method. Is it in the key value store? If it is we immediately respond with it. That means we don't have to fetch the article from the cache. That means we don't have to extract its contents. We're immediately serving it up from the KB store. Then if not, we do all the same work as before. And afterwards, we put it into the KB store. So the next time this method is called, we can pull it out. That should be good to go. So now let's start our app back up. You can see that we have access to that KV namespace. And then let's refresh the homepage. We'll see that it takes a few seconds just like it did before. In this case, almost 4 seconds. But if we did everything right, we should see the contents. We do. If I request it again, so much faster. So in this case, 253 milliseconds. So it was able to retrieve all of those things from the KV store and not have to extract the article contents again. And you can see the more often requested every now and then because the the cache is hot, uh, it's able to pull those things out fairly quickly. So I'm going to do like several several requests. And

you can see that we do get some pretty quick response times uh when there's a lot of requests coming into our app. Okay, we're getting much closer. We have a fairly functional app. At this point, I actually want to render out the contents of the article itself. And if you look at the Mozilla readability docs, they mention you should be using something like DOM purify. So, we're actually going to go ahead and set up DOM purify as well. That way, if there's anything crazy in the return content or if the AI decides to start to misbehave and outputs some bad content as well, we're at least going to sanitize that and extract out any any potentially malicious code. So, let's also install DOM purify. Now, to get DOM purify working in a non-browser context, we have to do a similar thing where we can use something like JSDOM. So, they have this example here. We're going to set it up and then use linkd instead. So, let's grab our DOM purify import. And then right here once we have the article content is where we're going to do the sanitizing. So we can create a window.

Chapter 12 41:47

Rendering Article Content with DOMPurify

In this example they did new jsdom.window we can copy what we did earlier but extract the window element instead. So here we can say uh parse no html but only give me access to the window. And then that window we can pass into dom purify. Now the clean version we'll pass in the article.content and we're going to call this the clean article. And we want to do the same thing with the excerpt. Call it the clean excerpt. Turn both of those instead. Now DOM purify has some sensible defaults. But if you want to enable or disable certain things, you can look at their docs as well. So now if we take a look, any HTML that was returned from the content was purified.

So any scripts were removed, uh any potentially XSS attacks, all of that was removed using DOM purify. Now we can safely take this raw HTML and render it out on the page. Now, to render out HTML, you technically could use dangerously set inner HTML in RJSX. Hono is smart enough to find that and then render it out. But they also provide a helper that makes it a whole lot nicer to look at. So, we're going to import HTML and raw from hono HTML. But then back in our index.tsx, let's import raw and HTML in. And you can see when we're inside of any JSX, in this case, it's HTML, but we can use that raw helper to render it out right inside of the element itself. So now that we have access to raw right here, I want to use raw and pass in the article content and also raw and pass in the summary. And then in this case, we don't need need the HTML helper, just the raw helper. So that's going to take that raw HTML and actually render it out on the page. So just like that, I'm going to refresh. All of that stuff got served from the cache, so it's pretty quick to

Chapter 13 43:21

Render raw HTML with Hono

load. And now we've got some nice rendered content, right, for each of these articles. Now, I will say there is a bug for this. If you hover over any of the links, if those links weren't properly set on the article that we're pulling from, you'll see that they're referencing local host. Now, to fix this is actually cumbersome right now. So, if you've ever used JSDOM, you can actually set the base URL for any HTML content that you're loading in. As far as I could tell, linkdom doesn't support that yet. But, I did find this issue where someone showed how to do this.

Basically, after you've parsed a document, you can go through and update any image tag or anchor tag to use the actual URL as as the base URL. So, we're going to do this. So, after parsing and creating that document before passing it into readability, we're going to overwrite all of the images and anchor tags to use the base URL, which is going to be options URL. And so now any relative links or relative images will use the actual base URL instead. So this should be fine for now. Um hopefully linkdom eventually merges in some changes that does this by default. Like it would be really great if we could pass in something like URL or base URL here, but right now linkdom doesn't support that. So this should be all we need. I'm going to not load it from the cache one more time and then we should be off to the races to start summarizing with AI. So disable the cache, refresh the page, and now if we look at that example article, images actually load in because they have the right base URL.

And that link that was earlier linking to localhost will actually link off to the page itself. Okay, so this is awesome. Images are loading in. The links are correct. But one more thing I want to do is uh these links should also open in a new tab. And that's fairly trivial to do with the way that we've set this up. So for every link that we come across, so we'll just set their target to be blank. And then we'll set re to no open or no follow. Now with that, if I refresh the page now, any links that are in a specific article that we pulled out should open in a new tab. Now with that, I'm going to reenable the cache. So the page should load fairly quickly once we have the cache going. And it is. And now we can actually talk to the AI. Now it's important to note that that excerpt property that readability is extracting is just a best guess. like the page itself might have a summary element, but you'll see that for some of these it's just like a single sentence um or like in one case it's just like the date because that's the first thing that it saw in the article itself. So

the excerpt itself is not an AI summary. It's just readability's best guess as to what the excerpt is. When we use AI, we're going to pass this entire article contents in and it will in turn summarize the article so we can read the summary to determine if we want to read the full article. Now, to do this, we're going to use Cloudflare workers AI. They essentially give us access to many different types of models that we can call directly from within our worker, and that'll just incur usage on our Cloudflare bill. And for any one of the models, you can actually see what the cost is per million tokens. And for a lot of these models, they're actually in beta and are free to use when they're in beta. So if you look at summarization, there is a model that's exclusively tuned for summarizing articles. And right now it's free because it's in beta. So we'll use this one first. I'll show you that I've I've run into some issues where it kind of just cuts off and doesn't summarize the whole article.

Chapter 14 46:56

Summarizing Articles with CF Workers AI

Um, so we'll use this one just to get it working, but then I'll show you how we can actually use models with Laura. So, we can pass in basically a fine-tuned version to use a regular AI model that's been fine-tuned for summarizing instead of just passing an article to chat GBT. But first, let's get it working with just this Bart large CNN summarization model. So, on any one of these models, when you look in the docs, they'll typically have a usage example with TypeScript. So, it gives you some exact code that you can copy paste and then start using inside of your Cloudflare worker. So, we'll copy that. And here you can see they're updating the ENV to include an AI property. Let's do that as well. So if we head to our Wrangler.json C, they actually have an example of this where they add an AI binding that gives us access to Cloudflare AI. So I'm going to save this binding. We're then going to regenerate the Cloudflare types. So we'll get access to that binding AI on

our C.NV. And now let's use it as well. So in our get article and summary function, we also want to take in this AI variable which is going to be of type AI and then on this side we can pass in AI which is just going to be C.env.ai.

So now inside of the method we have access to AI. So once we have retrieved and purified the content itself, we can then pass that content to the AI to actually summarize it. So to test this out, I'm just going to do it with a single article. Um because I don't want to call AI for like the 30 articles on the homepage. So let's simplify things a bit. And here for get feed, instead of calling the homepage to get that list of items, I'm just going to hardcode it as a single item. That way we can test out our summarization. And then once that's working, we'll uh we'll call it with everything else. So this is going to be an array. Each one has a link. And then it also needs comments, which is a string. And then it needs a title, which is a string. And the one that we'll test this out on is just the the first link here from uh Jeff Gearling, uh, who's great, by the way. So subscribe on YouTube if you haven't. He has tons of awesome videos and really cool content

like this. Um, so we're going to summarize that link. We'll give it that title. And now if we load the homepage, it's just going to have that one single article so we can test out our our AI summaries. Now, back over in this method, right here is where I want to call the AI to summarize this clean article HTML contents. So if we copy that example code here, right here, I can say the summary response will just be our options.ai.

We specify the model. Our input text in this case is going to be the clean article. For the max length, I'm going to set this to 2,00 characters because I want it to be a fairly complete summary. And then here we get access to the response. And for a lot of these models, they actually have the types uh fairly laid out. So here, if we do response summary, this specifically is going to be a summary string. And now at this point, we don't need to grab the excerpt from readability anymore. So let's test this out. We're going to now disable our cache. So we actually are calling the AI and then we'll we'll store the summary in our key value store. But let's give it a try. We head to the homepage. We'll do a refresh. You can see it takes a little bit longer because it needs to reach out to that AI to get the summary.

But then we can see a fork of TRNU NAS that runs on ARM is now available. The TRNA project is running apps on Raspberry Pi. Sweet. So we've gotten a summary. Uh let's test it out with another article. Uh, this is the math behind GANs. Let's try it. Throw that URL in there. Date the title. Refresh the page. And so now that should go off.

Retrieve the contents. Attempt to summarize it. In this post, we'll take a deep dive into the math behind GANs. My primary source of reference is Ian Goodfellow's generative adversarial nets. Cool. So, this is working, but like I said earlier, this model isn't the best. If you want like a really in-depth summary, it's give it's literally just giving us like one sentence summaries, which is fine for some use cases, but I some of these articles are very long, so I want a more detailed summary. Now, if you look in their list of models, there's only one that's categorized as summarization, but we can use models that support Laura.

Chapter 15 51:51

Using LoRa Adapters with CF Workers AI

Uh, and if you look in their guides, there's a section under features fine-tunes using Laura adapters. So, Laura stands for low rank adaptation. Essentially, people can take an existing model and then fine-tune it for specific purposes. So, for instance, you can take something like the chat GBT OSS model and then specifically fine-tune it for creating summaries of articles. And that's what a lot of people have done. The cool thing about this is you could actually provide your own Laura adapter if it's for for the models that they support, which if you look in their models list and then filter by Laura, these are all of the models where you can use a Laura adapter with. So you could fine-tune your own, but there are a lot of public ones. So if you look on public Laura adapters, you can see there's a couple of examples here, and one of them is this CF public CNN summarization, and it's compatible with the Mistral Instruct model. So instead of just using that direct summarization model, we could use Mistl instruct and specify that we want to use

this public Laura. So to do this is very similar to just calling a model directly but in this case you'll notice that we have to pass in an array of messages and then pass in the Laura name. So over here we're going to change from using the BART large CNN model to use the mist 7B instruct. We can see that we want it to be raw. And the raw property is important there because for a lot of these models there's a thing called a chat template that they apply. But when you're using Laura you don't want to apply the chat template. So make it raw.

And then the content will say summarize the following. And then we can pass in our article there. So I'll turn this into a template string and then pass in the clean article. Now response summary is no longer a thing, but response.response is a thing. Uh which is a bit redundant.

So let's actually rename this variable to answer. And so then we can use answer.response. Okay. So now instead of using the summarization specific model, we're using this model with a public Laura and we should get a better more in detailed summarization. Let's give it a try. Now that took quite a long time to run for this one article. It was about 12 seconds, but let's take a look. But now it's a bit more involved. This post provides an in-depth analysis of gener generative adversarial networks by motivating the loss functions for the generator and discriminator models. The author uses notation to clarify the variables involved and explains the role of each model in the GAN. The post also discusses the use of binary cross entropy. This is great. So, instead of a single sentence, this gives me a much better idea what the article is about and then I can decide if I want to dive in and spend the eight minutes reading the the full article. Let's test this out on a few others. Let's go let's go back and test this out on that Jeff Gearling article as well and then we'll refresh the page. Oh no, we hit an error. Now, in my tests, I come across this every now and then. Not sure

exactly what it is, but basically whenever we make that call to AI, it sometimes gets an authentication error. Uh, if I refresh, it could try again. Then it gets an unknown internal error. If I refresh, we can try one more time. And there we go. It actually worked that time. Now we're going to wrap this function so that way we can handle failures. But let's see what it did.

Here we go. So a much better summary. The author shares his experience of installing Tuness on a Raspi which requires uh Yui support to do its hardware limitations. He discusses the steps of enabling UI on the Pi en enabling and updating the EPROM. After successful booting the EUI mode, the author installs TRUNAS. Wonderful. So, this is a much more involved summary that gives me a better idea of what the article is about. So, what we're going to do now is wrap that code so that way if it fails, we can retry and once it succeeds, we'll update our KV store that to actually have that summary. So, instead of just calling AI.run directly, I'm going to create a new file called summarize. It'll export a function called summarize. It's going to take in that AI which is of type AI and it's going to take in the content which is of type string like so. We'll grab this codes here, pull it into our summarize, make this an async function and pass in that content and then after all that we're going to return answer response which could be undefined. So we'll also return the empty string. So this just

Chapter 16 55:29

Setting Max Retries for AI Call

returns a string of the summary. Now over on the other side we'll run it. So let's just say the summary is summarize with options.ai and the article content. Import that function. And now we can just pass that summary in directly. And we do need to await it. Okay. So now that's nice and abstracted away. Now in this function we can attempt to retry if this fails. So I'm going to wrap this up into function called do work that actually does the hard part here. We will make it async.

And then now we can try to do it. So we'll add a try catch. We'll say the result is do the work. And if there was an error, we will potentially retry. So let's create a variable called max retries. We're going to set that to three. And then we'll say the number of retries currently is zero. So it has retrieded zero times. And now this is all just going to be inside of a while loop. So I'll say while retries is less than max retries, we're going to do the work. And if it fails and if the retries is still less than max retries, we're going to increase the number of retries that we've done and let it happen. And so that will increase retries. It'll go back around attempt to do the work again. But if we've made it out of here, then we've reached the max number of retries and we're going to basically not try again to summarize this article. So here we'll just return an error string that says unable to summarize max

retries reached. So now when we call this function if it fails it's going to try again. Finally if it works we'll get that data back on the other side. We then store that summary here and then ultimately that summary from the AI gets stored into our key value store. Now I'm going to add just a little bit of logging so we can see what's going on here. So, right before we summarize, just going to add a log that says summarizing and pass in that URL so we know what we're summarizing. And then in our summarizer, if an error ever does happen, I still want to see what that is. So, we're going to pass in error message here, like so. And I kind of do want to know what the URL is we're summarizing. So, I am going to pass in the URL here just so we can have that as an indicator in our errors. So we're going to say error summarizing pass in the URL and then the message and then on the other side we're going to pass in the URL as well. Lastly, we're also going to log if we reach the number of retries and then if the summary was successful, we'll log that too. So we'll

Chapter 17 57:54

Logging and Tweaking AI Summary Calls

say uh summarize that URL. Okay, so we should see all that kind of good logging. And if things fail, we're still at least going to render out the page. It'll just say that the summary wasn't found. So now if I refresh the page, this should go through all of that logic for this one article. So we're summarizing the article and then it summarized it. Took about 14 seconds and then we responded with the page. Okay, so it's working for one article. Now I'm ready to throw the entire Hacker News front page at it. So if we head back to our index.tsx, we're actually going to grab the feed to get all of those items.

And now we're going to try to summarize all of them. Now this is going to take a while, right? because we we saw that even for that one article it took like 20 seconds. Sometimes it takes like more. Um so it's going to take a while for the first page load to run. Then after that all of those summaries or missing summaries are stored in the key value store. So after that initial request for all the stuffs the page should render fairly quickly. So give it a go. We're going to refresh and it's off to the races. We can see that uh we now have sent all of those off to the API. We can see several of them failed because of uh the retries were reached, but we do have some errors that we could potentially fix, right? So, um the estimated number of input and maximum output tokens exceeds this model context. So, for some of these articles, they're just too long. So, we should probably shorten them before sending them to the AI. And then we can see that some of these finished summarizing. And then finally, it took about 71 seconds,

but the page itself was returned. And now we have summaries for each of these articles. And in this case, it said summary unavailable. That's okay. That just means it failed. We're going to try to fix some of the failures. Sometimes it can't get the article at all, which is fine. We don't even send that to the AI. Um, but it's looking good. Now, in my codes, I still have the cache disabled. So, if I refresh the page, it's going to try to do this again. But, let's fix some of these errors. So, like this one. So the most common one we got is the fact that the context window didn't support an article of that length. So the maximum context window length is 15,000 tokens. So we need to basically just truncate the article that we're sending to the AI. So we should do this right here. Let's just say the truncated content will be the content and let's split it on spaces and then we'll use the slice method. So the the

slice method says start at a certain index and then we'll end at a certain index. So let's just say we'll only allow up to I don't know 5,000 words. And this is just like a rough approximation of tokens. Tokens don't directly correspond to words. But let's do 5,000 and then join it back all together like so. So this should at least be able to summarize the first 5,000 words without running into that context limit. So I'm going to give this a try. We'll pass the truncated content instead.

and then refresh the page to see if we get rid of those context errors. And so now we can see that it's summarizing. We get an error of internal error, unknown internal error. So that's not the token limit error, which is good. So for most things, it's able to use that retry functionality and uh at least attempt to retry it multiple times if the AI did fail. Okay, so nothing errored out for token errors. And for the things that did come back, we do have their summaries. Looking good. Fantastic. So, at this point, I would probably play around with the number of tokens here and then get it to a good point where I'm not running into uh any max tokens because I want to send as much of the article as possible. So, I'm going to tweak that number. And then once all of that is working over here, I'm going to reenable the cache. And so after that, we can see how fast this page loads once we've got all the article contents and summaries in the key value store. Okay, seems like the sweet spot was about

5,500 characters. So that looks good. But now I've reenabled the cache and the request including all of those AI requests took about 50 seconds. But now with the cache enabled, if I refresh the page, it comes back in 300 milliseconds. So now we're in a really good spot where if anybody comes to my site, they're going to see these summaries and it's going to load fairly quickly to give them that article contents and that summary content. Now last piece here, let's clean it up, add a title, and deploy it to the web so other people can try my site out. Now on the homepage, I do want to display an H1 right above that just says the hackernew summary.

That looks good. And then let's try deploying. So you can see in the package.json JSON that it includes this deploy script here. So if I do a pnpm run deploy that is now going to deploy this to cloudflare and with that we can give it a try. Okay. So I did run into an error after deploying and essentially for some reason for some pages it can't create this readability constructor but we don't get the same error locally. So all I did was attempt to create the readability document. if it couldn't show the error and just return nothing because in that case we weren't able to summarize it to begin with. Um, and then redeployed this. And now here on my deployed server, you can see if I refresh, it's fairly instant because it is pulling all of these articles and summaries from the cache. So we have a fully working page with the whole feed.

Chapter 18 1:03:14

Deploying to Cloudflare

For the ones that it can't retrieve, it lets us know. But for the ones it can, uh, we can actually see the summaries, see the article. Fantastic. Now, for this tutorial, I'm going to stop here, but I want to talk a bit about how we can improve this. Now, waiting a minute for the initial page load is not an ideal experience. And really, what we want to be doing is calling that AI to summarize things in the background. Now, in order to do something like that, we might use cues. So, Cloudflare Q's is a product that they have available. And essentially with cues, you can have producers and consumers. And the idea here would be your main page whenever it renders for the first time, if there is an article that hasn't been summarized, it would produce an event that says, "Hey, please go summarize this article."

Chapter 19 1:04:10

Future Improvements

And then you could have a consumer worker that sees that that message came into the queue that says, "Please summarize this article." and then it could summarize it in the background, store the summary in KV store, and then the next time this page is requested, it could then pull in those values for anything that had been summarized in the background. And so that's how I go about updating this site to work in that way.

Now, Cloudflare cues aren't available on the free plan, so you do have to add your credit card if you want to be able to use cues. But there's potentially other ways you could go about this as well. You could actually just have two different Cloudflare workers and one worker could call the other worker to do the summaries, but then they're both accessing the same KB store. You you could work it out that way, but probably the best way to do it would be with with Q's. So, I'm not going to do that in this tutorial. If you'd like to see me working with Q's, let me know down in the comments. But that's all I have for you in this video. I'll see you in the next one.

Chapter 20 1:05:32

Thanks!

[Music] Hey, hey, hey. [Music]

Quote it with credit and a link. Quoting Syntax