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