<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Khriztian Moreno</title>
    <description>The latest articles on DEV Community by Khriztian Moreno (@khriztianmoreno).</description>
    <link>https://dev.to/khriztianmoreno</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F192167%2Fe34249a8-fbd3-48ce-8afc-1418d0b944a7.jpeg</url>
      <title>DEV Community: Khriztian Moreno</title>
      <link>https://dev.to/khriztianmoreno</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/khriztianmoreno"/>
    <language>en</language>
    <item>
      <title>AI Isn't Coming for Your Job. It's Coming for Your Advantage</title>
      <dc:creator>Khriztian Moreno</dc:creator>
      <pubDate>Mon, 02 Nov 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/khriztianmoreno/ai-isnt-coming-for-your-job-its-coming-for-your-advantage-2gg4</link>
      <guid>https://dev.to/khriztianmoreno/ai-isnt-coming-for-your-job-its-coming-for-your-advantage-2gg4</guid>
      <description>&lt;p&gt;Let me start without anesthesia: I think we're facing one of the deepest shifts in the history of software. But not for the reason everyone keeps repeating.&lt;/p&gt;

&lt;p&gt;The question is not whether AI will replace developers. That question is comfortable, almost reassuring, because it casts you as a victim just waiting for the verdict. The real question, the uncomfortable one, is this: &lt;strong&gt;what happens when building software stops being a competitive advantage?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's where the conversation actually worth having begins.&lt;/p&gt;

&lt;h2&gt;
  
  
  When writing code stops being the value
&lt;/h2&gt;

&lt;p&gt;For decades, software had a crystal-clear barrier to entry: building it was hard.&lt;/p&gt;

&lt;p&gt;A company had an idea. To turn it into code it hired developers, formed teams, invested months, and took on technical risk. We were the only ones who could turn an intention into a working product. That difficulty was our wall. And we lived comfortably behind it.&lt;/p&gt;

&lt;p&gt;AI is tearing that wall down.&lt;/p&gt;

&lt;p&gt;Today, a person with enough context builds in hours what used to take days or weeks. Not with the same quality, security, or maintainability—watch out for that—but the initial barrier is falling. And when a skill becomes more accessible, its economic value &lt;strong&gt;changes&lt;/strong&gt;. Always.&lt;/p&gt;

&lt;p&gt;Think about what happened with digital photography, video editing, or graphic design. The professions didn't disappear. What changed was &lt;em&gt;which activities were scarce&lt;/em&gt; and which ones people were willing to pay for.&lt;/p&gt;

&lt;p&gt;Code is becoming a &lt;strong&gt;means of production&lt;/strong&gt;, not the final product of our knowledge. That doesn't mean programming stops mattering. It means something subtler and more dangerous: that knowing how to program, &lt;strong&gt;on its own&lt;/strong&gt;, might stop setting you apart.&lt;/p&gt;

&lt;h2&gt;
  
  
  So where do we sit in the chain now?
&lt;/h2&gt;

&lt;p&gt;My hypothesis is that we're moving from being &lt;em&gt;software builders&lt;/em&gt; to being &lt;em&gt;people responsible for getting results through software&lt;/em&gt;. Look at how the position evolves, from the bottom up:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The executor&lt;/strong&gt; — &lt;em&gt;"Tell me what to build and I'll code it."&lt;/em&gt; Writing code, implementing components, solving well-defined tasks. Increasingly automatable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The systems engineer&lt;/strong&gt; — &lt;em&gt;"I decide how it should work and what can fail."&lt;/em&gt; Architecture, integration, security, observability, performance, reliability. Generated code needs technical judgment too.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The solution designer&lt;/strong&gt; — &lt;em&gt;"I identify the right problem and design the solution."&lt;/em&gt; Understanding the business, questioning requirements, weighing alternatives, coordinating whoever (or whatever) does the execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The owner of the outcome&lt;/strong&gt; — &lt;em&gt;"I'm accountable for this producing value."&lt;/em&gt; Connecting technology with revenue, costs, user experience, risk, and product decisions.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I don't think we all have to become managers or founders. Nor that these levels replace each other in a straight line. An engineer can keep programming most of the day and still be extremely valuable.&lt;/p&gt;

&lt;p&gt;The difference is where that value rests: less and less on &lt;strong&gt;how much code you produce&lt;/strong&gt;, more and more on the decisions you make, the problems you avoid, and the results you deliver.&lt;/p&gt;

&lt;h2&gt;
  
  
  And here's the unsettling part: we may need fewer engineers
&lt;/h2&gt;

&lt;p&gt;Let's be honest, because this is what almost nobody says to your face.&lt;/p&gt;

&lt;p&gt;There's no guarantee that the extra productivity creates more jobs for developers. And this isn't a distant forecast: it's already happening. Companies that used to need ten people to build and maintain a product now get similar results with four. Teams shrinking, costs being cut, and more and more decisions handed off to AI.&lt;/p&gt;

&lt;p&gt;Does that mean the apocalypse? Not necessarily. That same company can also build products that weren't economically viable before, opening up new opportunities. Both things can happen at once. The uncomfortable consequence is this: we could end up with &lt;strong&gt;more software in the world and, at the same time, fewer jobs dedicated to producing it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That changes the rules of professional competition. Before, a company justified a large team because it needed execution capacity. If that capacity multiplies, keeping the same headcount stops being an obvious decision.&lt;/p&gt;

&lt;p&gt;The question you'll hear more and more won't be &lt;em&gt;"how many developers do we need?"&lt;/em&gt;. It will be:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What results do we need, and what's the smallest team that can deliver them?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We don't know how far this process goes or at what pace. But building your career assuming today's structures will stay intact strikes me, right now, as a mistake.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what should we become?
&lt;/h2&gt;

&lt;p&gt;I'd bet on five capabilities. And pay attention to which one I'm saving for last, because I think it's the one almost nobody is watching.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Deep technical judgment.&lt;/strong&gt; You don't need to memorize more syntax than a model—you already lost that race, and that's fine. You need to spot when a solution is wrong, fragile, insecure, or too expensive. Reviewing, verifying, and debugging complex systems can be worth far more than producing code fast.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Product thinking.&lt;/strong&gt; Understanding &lt;em&gt;why&lt;/em&gt; something is being built. What problem it solves, who needs it, how success is measured. AI helps you implement a solution; it doesn't guarantee you're solving the right problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Orchestrating systems and agents.&lt;/strong&gt; Moving from doing every task with your own hands to designing workflows where agents research, code, test, and review. Your job becomes setting boundaries, validating results, and knowing when to step in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Communication and decision-making.&lt;/strong&gt; Turning ambiguous problems into concrete decisions. Defending an architecture, negotiating priorities, explaining risk, getting a team to move. When execution speeds up, this turns to gold.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Ownership and distribution.&lt;/strong&gt; Knowing how to build is one thing. Having access to customers, an audience, distribution, or a stake in a product is a very different one. If building gets cheaper, &lt;em&gt;finding who your work actually serves&lt;/em&gt; may be an even bigger advantage.&lt;/p&gt;

&lt;p&gt;The last one strikes me as the most important. Because there's a huge difference between being the person who builds another company's product and being the one who owns the relationship with the users, decides what to build, and captures part of the value. Not everyone has to start a company. But depending exclusively on a company needing your programming hours is an increasingly risky bet.&lt;/p&gt;

&lt;h2&gt;
  
  
  The trap: chasing the next trend
&lt;/h2&gt;

&lt;p&gt;And here's where you need to be careful, because there's a false exit that feels like the solution.&lt;/p&gt;

&lt;p&gt;It looks like it's enough to learn to use agents, master prompting, know the latest model, or switch editors every three months. That helps. But there's a paradox you have to see: &lt;strong&gt;if we all learn to use the same tools, that skill stops setting you apart and becomes a baseline requirement.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Today, knowing how to use AI distinguishes you. Tomorrow it'll be as expected as knowing how to use Git. That's why I split capabilities into two groups:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The ones that depreciate&lt;/strong&gt;: specific syntax, repetitive manual workflows, knowledge of tools that change every quarter.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The ones that compound&lt;/strong&gt;: judgment, experience with real systems, domain understanding, trust, reputation, relationships, knowledge of your users.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's not an absolute split—technical fundamentals will stay useful for a long time. But invest more in what &lt;strong&gt;compounds over the years&lt;/strong&gt; than in chasing what's trending this quarter. That last one is a treadmill: you run like crazy to stay in the same place.&lt;/p&gt;

&lt;h2&gt;
  
  
  And now a more uncomfortable, almost philosophical question
&lt;/h2&gt;

&lt;p&gt;For a long time we built part of our identity around a hard-to-acquire skill. We solved problems others couldn't. We built things others didn't even understand how to make. That skill gave us autonomy, recognition, and often a good economic position.&lt;/p&gt;

&lt;p&gt;What happens when a big part of that capability stops being exclusive?&lt;/p&gt;

&lt;p&gt;I think that's where &lt;strong&gt;two different crises&lt;/strong&gt; appear, and it's worth not confusing them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;economic&lt;/strong&gt; one: how do I make money in a market that needs fewer human work hours?&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;personal&lt;/strong&gt; one: who am I when a skill that defined me stops being exceptional?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And I suspect the second is the harder one to resolve. Because if your value depends on being better than a machine at writing code, you're competing on ground that shifts under your feet every single day.&lt;/p&gt;

&lt;p&gt;But if your identity rests on understanding problems, building useful things, learning, collaborating, and taking responsibility, then programming is still an extraordinary tool. Maybe more powerful than before.&lt;/p&gt;

&lt;p&gt;We don't have to defend the value of writing code. We have to figure out &lt;strong&gt;what we want to do with the ability to build almost anything.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  My bet, if you have engineering experience
&lt;/h2&gt;

&lt;p&gt;I wouldn't abandon engineering to become some supposed "AI expert." I'd do exactly the opposite: I'd use AI to &lt;strong&gt;extend the reach of my experience&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I'd try to become someone capable of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Designing a complete system and explaining why it should exist.&lt;/li&gt;
&lt;li&gt;Directing agents to speed up its construction without losing control.&lt;/li&gt;
&lt;li&gt;Evaluating quality, security, cost, and reliability.&lt;/li&gt;
&lt;li&gt;Understanding the business well enough to prioritize properly.&lt;/li&gt;
&lt;li&gt;Shipping products and checking whether anyone actually needs them.&lt;/li&gt;
&lt;li&gt;Building assets of my own: products, intellectual property, reputation, distribution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The combination is powerful because it joins technical capability, judgment, and autonomy. And no, not everyone needs to reach VP of Engineering, enterprise architect, or founder. There can still be an excellent technical career for people who enjoy solving complex problems. What we do need to question is whether the traditional paths will keep rewarding the same skills in the same way.&lt;/p&gt;

&lt;p&gt;My goal wouldn't be to be the developer who produces the most code. It would be to be someone who can turn an ambiguous opportunity into a real result, with fewer resources and less dependence on others. Not because I want teams to disappear, but because I want to &lt;strong&gt;have options&lt;/strong&gt; when the way we work changes.&lt;/p&gt;

&lt;p&gt;And I want to close by passing the ball back to you, because this conversation isn't solved by reading, it's solved by thinking. Out of this whole future, what actually worries you the most?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;That there'll be fewer jobs and lower salaries?&lt;/li&gt;
&lt;li&gt;That your experience and knowledge lose their value?&lt;/li&gt;
&lt;li&gt;That wealth concentrates in a few companies and people?&lt;/li&gt;
&lt;li&gt;Or that programming stops giving you the meaning and satisfaction it used to?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Answering one versus another isn't the same, because each leads to a different strategy. I already told you where I stand. Now it's your turn. Tell me in the comments: let's keep the conversation going there.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>career</category>
      <category>softwaredevelopment</category>
      <category>futureofwork</category>
    </item>
    <item>
      <title>Connecting your SPA's client state to WebMCP: reactive hooks and stores</title>
      <dc:creator>Khriztian Moreno</dc:creator>
      <pubDate>Wed, 28 Oct 2026 21:30:00 +0000</pubDate>
      <link>https://dev.to/khriztianmoreno/connecting-your-spas-client-state-to-webmcp-reactive-hooks-and-stores-21ka</link>
      <guid>https://dev.to/khriztianmoreno/connecting-your-spas-client-state-to-webmcp-reactive-hooks-and-stores-21ka</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/khriztianmoreno/webmcp-why-agents-shouldnt-have-to-guess-how-your-ui-works-1af1"&gt;first post in this series&lt;/a&gt;, the tools were registered once, at page load, against a store written in vanilla JavaScript. In the &lt;a href="https://dev.to/khriztianmoreno/semantic-markup-for-ai-turn-your-forms-into-webmcp-tools-without-javascript-1nod"&gt;second&lt;/a&gt;, there wasn't even any JavaScript: the tools were HTML forms. Both demos have something in common that your app probably doesn't: &lt;strong&gt;nothing mounts or unmounts.&lt;/strong&gt; The page loads, the tools exist, end of story.&lt;/p&gt;

&lt;p&gt;A real app doesn't look like that. It's an SPA: components that mount and unmount with every route change, state living in a store (Zustand, Redux, Pinia, signals, whatever you use), and a framework that re-renders whenever that state changes. The obvious question is &lt;em&gt;where&lt;/em&gt; you register tools in that world. The obvious answer ("in a &lt;code&gt;useEffect&lt;/code&gt;, of course") hides two traps that took me a while to name: tools that re-register on every render, and tools that answer with state from three minutes ago.&lt;/p&gt;

&lt;p&gt;This post is about the idea that gets you out of both: &lt;strong&gt;a WebMCP tool is a contract (name + schema + description), not a snapshot of state.&lt;/strong&gt; You register it once per component lifetime, you tie its unregistration to the unmount, and you read live state inside &lt;code&gt;execute&lt;/code&gt;, at call time. That way, a state change never re-registers anything.&lt;/p&gt;

&lt;p&gt;I built a demo in the same flight domain, now in React with a Zustand store. The code is in &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/tree/main/demos/webmcp-react-state" rel="noopener noreferrer"&gt;&lt;code&gt;demos/webmcp-react-state&lt;/code&gt;&lt;/a&gt; (the &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/webmcp-react-state/README.md" rel="noopener noreferrer"&gt;README&lt;/a&gt; explains how to run it). Like the previous ones, it has no build step: React, Zustand, and &lt;a href="https://github.com/developit/htm" rel="noopener noreferrer"&gt;&lt;code&gt;htm&lt;/code&gt;&lt;/a&gt; (JSX without a transform) load from a CDN.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the demo works
&lt;/h2&gt;

&lt;p&gt;&lt;br&gt;
    &lt;br&gt;
    Your browser doesn't support embedded video — &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/tree/main/demos/webmcp-react-state" rel="noopener noreferrer"&gt;clone the demo&lt;/a&gt; and run it locally instead.&lt;br&gt;
  &lt;/p&gt;&lt;br&gt;The full run: the stale closure, the naive hook against the contract, StrictMode, the seat map and the worker that misses its deadline.
  &lt;p&gt;&lt;/p&gt;

&lt;p&gt;The video shows the full run: renders climbing while registrations stay put, the naive hook's counters taking off, and StrictMode's unhandled rejections appearing in the page.&lt;/p&gt;

&lt;p&gt;The app is a trip cart: BOG → MAD flight results, an "Add" button on each one, and a cart with passengers and a subtotal. The state lives in a Zustand store. The &lt;code&gt;&amp;lt;TripCart&amp;gt;&lt;/code&gt; component owns four tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;get_cart_items&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;get_trip_state&lt;/code&gt;&lt;/strong&gt; (&lt;code&gt;readOnlyHint&lt;/code&gt;): readers. They have no endpoint behind them; they read the store.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;add_flight_to_cart&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;remove_from_cart&lt;/code&gt;&lt;/strong&gt;: they call the store's actions, and the UI reacts just as if you'd clicked.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are two more components with their own tool: a &lt;strong&gt;seat map&lt;/strong&gt; you can open and close (it owns &lt;code&gt;select_seat&lt;/code&gt;), and a &lt;strong&gt;fare quote&lt;/strong&gt; computed in a Web Worker (it owns &lt;code&gt;quote_fare&lt;/code&gt;). Next to the app there's a &lt;strong&gt;counters panel&lt;/strong&gt; (&lt;code&gt;&amp;lt;TripCart&amp;gt;&lt;/code&gt; commits, &lt;code&gt;registerTool()&lt;/code&gt; calls, aborted signals, &lt;code&gt;toolchange&lt;/code&gt; events, and unhandled rejections), a live &lt;strong&gt;&lt;code&gt;getTools()&lt;/code&gt; list&lt;/strong&gt;, and a &lt;strong&gt;simulated agent&lt;/strong&gt;, the same as in part 1: no LLM, a hard-coded plan, only &lt;code&gt;getTools()&lt;/code&gt; and &lt;code&gt;executeTool()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The lab lets you switch how &lt;code&gt;&amp;lt;TripCart&amp;gt;&lt;/code&gt; registers its tools (&lt;strong&gt;Contract hook&lt;/strong&gt;, &lt;strong&gt;Naive hook&lt;/strong&gt;, or &lt;strong&gt;Stale closure&lt;/strong&gt;) and toggle &lt;strong&gt;StrictMode&lt;/strong&gt; and &lt;strong&gt;Catch registration promise&lt;/strong&gt;. Each change remounts the React root and resets the counters.&lt;/p&gt;

&lt;p&gt;In my runs (headless Chrome 155, using the shim), with the same five changes (add three flights, add a passenger, remove a flight):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Contract hook:&lt;/strong&gt; &lt;code&gt;&amp;lt;TripCart&amp;gt;&lt;/code&gt; commits go from 1 to 6. &lt;code&gt;registerTool()&lt;/code&gt; calls stay at &lt;strong&gt;5&lt;/strong&gt; (the four cart tools plus &lt;code&gt;quote_fare&lt;/code&gt;), with 0 aborts and 5 &lt;code&gt;toolchange&lt;/code&gt; events.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Naive hook:&lt;/strong&gt; the same 6 commits, but &lt;strong&gt;25 registrations, 20 aborts, and 45 &lt;code&gt;toolchange&lt;/code&gt; events&lt;/strong&gt;. Every cart tool shows &lt;code&gt;registered ×6&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stale closure:&lt;/strong&gt; 5 registrations, all calm... and when the agent reads the cart after you've added a flight by hand, it gets &lt;strong&gt;an empty cart&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The problem: the tool as a snapshot
&lt;/h2&gt;

&lt;p&gt;Here's how most of us would write it on the first try. The component already has the state at hand, so you build the tool right there and register it in an effect:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** Anti-pattern: `tool` is a new object every render, so this re-registers every render. */&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useNaiveWebMCPTool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;modelContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;catchRegistration&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WebMCPContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AbortController&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;registerWithSignal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;modelContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;tool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;catchRegistration&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;modelContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;catchRegistration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;tool&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;tool&lt;/code&gt; object comes from the component's render, with an &lt;code&gt;execute&lt;/code&gt; that closes over that render's &lt;code&gt;cart&lt;/code&gt;. Because it's a new object every time, the effect sees a different dep on every render: abort, register, abort, register. The values are correct, but you're paying for it with an unregister + register pair per tool per render, and each one fires a &lt;code&gt;toolchange&lt;/code&gt;. The spec doesn't coalesce them: every registration and every unregistration notifies the documents. That's why 5 cart changes turn into 45 events.&lt;/p&gt;

&lt;p&gt;Why does that matter? Because &lt;code&gt;toolchange&lt;/code&gt; is how the page announces that its tool set changed: an in-page agent listens for the event, and a browser agent learns about it through its own internal mechanism. If your app fires it every time someone types into a field, you're telling the agent "everything changed" when nothing did. What an agent does with that mid-conversation (re-reading the list, ignoring it, getting confused) depends on the agent.&lt;/p&gt;

&lt;p&gt;The "obvious" fix (empty deps) is the other trap:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** Anti-pattern: registered once, but `tool.execute` is the first render's closure. */&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useStaleWebMCPTool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;modelContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;catchRegistration&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WebMCPContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AbortController&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;registerWithSignal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;modelContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;tool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;catchRegistration&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="c1"&gt;// eslint-disable-next-line react-hooks/exhaustive-deps&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;modelContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;catchRegistration&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One registration, perfect counters, and a classic stale closure: &lt;code&gt;execute&lt;/code&gt; is the first render's function forever. In the demo, you add Iberia's flight by hand, the UI shows it, and the agent's &lt;code&gt;get_cart_items&lt;/code&gt; answers &lt;code&gt;"items": []&lt;/code&gt;. That's the worst kind of bug: there's no error, the agent just reasons on top of a lie.&lt;/p&gt;

&lt;p&gt;Both traps come from the same mistake: treating the tool as &lt;strong&gt;a snapshot of state&lt;/strong&gt; that has to be "refreshed." It isn't. Name, description, and schema don't change when the cart changes. What changes is the answer, and the answer is computed when the agent asks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 1: there is no &lt;code&gt;unregisterTool()&lt;/code&gt;, the lifecycle is an &lt;code&gt;AbortSignal&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Before the hook, one detail you'll see wrong in plenty of places (it was in my original plan for this post, too): &lt;strong&gt;the spec has no &lt;code&gt;unregisterTool()&lt;/code&gt;.&lt;/strong&gt; The &lt;code&gt;ModelContext&lt;/code&gt; IDL has exactly &lt;code&gt;registerTool(tool, { signal, exposedTo })&lt;/code&gt;, &lt;code&gt;getTools()&lt;/code&gt;, &lt;code&gt;executeTool()&lt;/code&gt;, and three events: &lt;code&gt;toolchange&lt;/code&gt;, &lt;code&gt;toolactivated&lt;/code&gt;, and &lt;code&gt;toolcancel&lt;/code&gt;. That's it.&lt;/p&gt;

&lt;p&gt;You unregister by aborting the &lt;code&gt;signal&lt;/code&gt; you passed at registration time. The spec's &lt;a href="https://webmachinelearning.github.io/webmcp/" rel="noopener noreferrer"&gt;&lt;code&gt;registerTool()&lt;/code&gt; algorithm&lt;/a&gt; adds these abort steps to the signal: &lt;em&gt;unregister the tool&lt;/em&gt;, and &lt;em&gt;reject the promise with the abort reason&lt;/em&gt;. Both happen synchronously, when you call &lt;code&gt;abort()&lt;/code&gt;. In the demo's shim, &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/webmcp-react-state/webmcp-shim.js" rel="noopener noreferrer"&gt;&lt;code&gt;webmcp-shim.js&lt;/code&gt;&lt;/a&gt;, it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;abort&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;#unregister&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ignored if the promise already resolved&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;once&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To React, this is a gift. An &lt;code&gt;AbortController&lt;/code&gt; per effect, &lt;code&gt;abort()&lt;/code&gt; in the cleanup, and the tool's lifecycle is the component's lifecycle, the same idea we saw with declarative forms, where the tool lived and died with the &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; element. It's also the pattern the web already uses for &lt;code&gt;fetch&lt;/code&gt; and &lt;code&gt;addEventListener&lt;/code&gt;: one signal, one cancellation.&lt;/p&gt;

&lt;p&gt;Two more spec details that matter here: unregistering a tool &lt;strong&gt;doesn't cancel an execution already in flight&lt;/strong&gt; (the registration signal controls availability; the &lt;code&gt;executeTool()&lt;/code&gt; signal controls each invocation), and if the signal arrives already aborted, &lt;code&gt;registerTool()&lt;/code&gt; rejects right away without registering anything.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 2: &lt;code&gt;useWebMCPTool&lt;/code&gt;, line by line
&lt;/h2&gt;

&lt;p&gt;This is the contract hook, in &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/webmcp-react-state/use-webmcp-tool.js" rel="noopener noreferrer"&gt;&lt;code&gt;use-webmcp-tool.js&lt;/code&gt;&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useWebMCPTool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;modelContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;catchRegistration&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WebMCPContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;executeRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;useLayoutEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;executeRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tool&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;schemaKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inputSchema&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;annotationsKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;annotations&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;modelContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// no WebMCP here (yet): nothing to register&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AbortController&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inputSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;schemaKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;annotations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;annotationsKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;registerWithSignal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;modelContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;inputSchema&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;annotations&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;executeRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;current&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;catchRegistration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// There is no unregisterTool(). Aborting the signal is the unregistration.&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;modelContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;catchRegistration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;schemaKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;annotationsKey&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Piece by piece:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;executeRef&lt;/code&gt; + &lt;code&gt;useLayoutEffect&lt;/code&gt; with no deps.&lt;/strong&gt; After every commit, the ref points to the latest render's &lt;code&gt;execute&lt;/code&gt;. The function registered with the browser never changes: it's a trampoline that jumps to &lt;code&gt;executeRef.current&lt;/code&gt; &lt;em&gt;when the agent calls it&lt;/em&gt;. I update the ref in an effect, not during render, because writing refs during render is something React asks you not to do.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The deps are the contract.&lt;/strong&gt; &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;title&lt;/code&gt;, &lt;code&gt;description&lt;/code&gt;, and the schema and annotations serialized as strings. Serializing them means you can pass an inline object (&lt;code&gt;inputSchema: { type: 'object', … }&lt;/code&gt;) without a new identity on every render forcing a re-registration. If the contract really changes, the hook does re-register, and that's correct: that's exactly when the agent should get a &lt;code&gt;toolchange&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;catchRegistration&lt;/code&gt;&lt;/strong&gt; is a lab toggle, so the demo can show what happens without the &lt;code&gt;.catch&lt;/code&gt;. In your app, it doesn't exist: you always catch. More on that in building block 4.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The cleanup is a single line.&lt;/strong&gt; &lt;code&gt;controller.abort()&lt;/code&gt;. No names, no maps, no "did it register yet?" bookkeeping.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The proof is in the seat map, &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/webmcp-react-state/app.js" rel="noopener noreferrer"&gt;&lt;code&gt;app.js&lt;/code&gt;&lt;/a&gt;. &lt;code&gt;select_seat&lt;/code&gt; is built inline on every render, and its &lt;code&gt;execute&lt;/code&gt; closes over the component's local state (&lt;code&gt;occupied&lt;/code&gt;). When you hit "Refresh availability," the state changes, the component re-renders, and the next call from the agent sees the new availability. The counter still says &lt;code&gt;registered ×1&lt;/code&gt;. In my run, &lt;code&gt;select_seat({ seat: "12A" })&lt;/code&gt; failed with "Seat 12A is occupied," I refreshed, and the same call succeeded, without a single new registration.&lt;/p&gt;

&lt;p&gt;One subtlety worth knowing: &lt;strong&gt;the ref points to the latest &lt;em&gt;committed&lt;/em&gt; render&lt;/strong&gt;, not to the store "right now." If the agent mutates the store and calls another tool before React commits, the ref still has the previous closure. For component-local state, that's the best you can get. For store state, there's something better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 3: readers read the store, not an endpoint
&lt;/h2&gt;

&lt;p&gt;When the state lives in a store, you don't need the render at all. The cart tools in &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/webmcp-react-state/cart-tools.js" rel="noopener noreferrer"&gt;&lt;code&gt;cart-tools.js&lt;/code&gt;&lt;/a&gt; are module-level constants, and &lt;code&gt;execute&lt;/code&gt; calls &lt;code&gt;useTripStore.getState()&lt;/code&gt;, Zustand's synchronous getter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cartTools&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;getCartItems&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;CART_CONTRACTS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getCartItems&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;cartView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;useTripStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getState&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useContractCartTools&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;useWebMCPTool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cartTools&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getCartItems&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;useWebMCPTool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cartTools&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getTripState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;useWebMCPTool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cartTools&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addFlightToCart&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;useWebMCPTool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cartTools&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;removeFromCart&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;getState()&lt;/code&gt; is the store &lt;em&gt;now&lt;/em&gt;, not the last render. No stale closures, and no "one commit behind." The mutators do the same: &lt;code&gt;add_flight_to_cart&lt;/code&gt; calls the same &lt;code&gt;addToCart&lt;/code&gt; action as the UI's button, and Zustand notifies the subscribed components. The agent doesn't touch the DOM or know React exists.&lt;/p&gt;

&lt;p&gt;Notice what's &lt;strong&gt;not&lt;/strong&gt; here: there's no &lt;code&gt;GET /api/cart&lt;/code&gt;. The cart the agent reads is the user's, in this tab, with whatever they just changed by hand. In the demo, the agent adds IB-6584, I add AV-010 by hand and bump the passengers to 2, and "Re-read cart" answers &lt;code&gt;IB-6584, AV-010 · 2 pax · subtotal $2862&lt;/code&gt;, exactly what's on screen. A backend endpoint would have answered with whatever the server knew, which isn't necessarily the same thing. This is part 1's thesis applied to an SPA: the tool is another entry point into the logic and state you already have.&lt;/p&gt;

&lt;p&gt;The same pattern works for Redux (&lt;code&gt;store.getState()&lt;/code&gt;), Jotai (&lt;code&gt;store.get(atom)&lt;/code&gt;), or signals (&lt;code&gt;signal.value&lt;/code&gt;/&lt;code&gt;peek()&lt;/code&gt;): every serious store has a way to read outside of render. Use it inside &lt;code&gt;execute&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 4: StrictMode and the promise you have to catch
&lt;/h2&gt;

&lt;p&gt;Turn on StrictMode in the demo. In development, React mounts each component, runs its effects, simulates an unmount (cleanup), and mounts again. With our hook, that's register → abort → register for every tool. The counters on mount: &lt;strong&gt;10 registrations, 5 aborts, 15 &lt;code&gt;toolchange&lt;/code&gt; events&lt;/strong&gt;, and no errors. After that, the same five cart changes still leave it at 10.&lt;/p&gt;

&lt;p&gt;Why doesn't the second registration fail with &lt;code&gt;InvalidStateError&lt;/code&gt; ("a tool with that name already exists")? Because of the order of the spec's algorithm. The duplicate-name check is &lt;strong&gt;synchronous&lt;/strong&gt;, and so is the entry in the tool map. The abort steps run &lt;strong&gt;synchronously&lt;/strong&gt; too, inside &lt;code&gt;controller.abort()&lt;/code&gt;. So by the time StrictMode's second mount calls &lt;code&gt;registerTool()&lt;/code&gt;, the first entry is already gone. If unregistration were async, StrictMode would blow up on every mount. The spec itself uses this pattern in an example: &lt;code&gt;ac.abort()&lt;/code&gt; immediately followed by &lt;code&gt;registerTool()&lt;/code&gt; to update a tool's schema. Note that the example is there to warn about a race: an execution the agent starts right at that moment may target either the old tool or the new one, and hit a schema it didn't expect. One more reason not to re-register on every render.&lt;/p&gt;

&lt;p&gt;But there's a second consequence, and this one does bite. The &lt;code&gt;registerTool()&lt;/code&gt; promise doesn't resolve synchronously: it resolves in a queued task, after &lt;code&gt;toolchange&lt;/code&gt; fires. StrictMode aborts &lt;strong&gt;before&lt;/strong&gt; that task runs. And per the spec, aborting rejects the promise with the abort reason. If nobody catches it, you get an unhandled rejection on every development mount, one per tool.&lt;/p&gt;

&lt;p&gt;Turn off "Catch registration promise" with StrictMode on, and the demo shows it in the page (it listens for &lt;code&gt;unhandledrejection&lt;/code&gt; on &lt;code&gt;window&lt;/code&gt;): &lt;strong&gt;5 rejections&lt;/strong&gt; on mount, all &lt;code&gt;AbortError: signal is aborted without reason&lt;/code&gt;. That's why the hook always goes through this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;registerWithSignal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;modelContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;tool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;catchRegistration&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;registration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;modelContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;registerTool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;catchRegistration&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// lab toggle: leave the promise unhandled&lt;/span&gt;
  &lt;span class="nx"&gt;registration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Aborting before the promise resolves rejects it with the abort reason.&lt;/span&gt;
    &lt;span class="c1"&gt;// That's the expected path for StrictMode and fast unmounts.&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;AbortError&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`[webmcp] registerTool("&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;tool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;") failed:`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An &lt;code&gt;AbortError&lt;/code&gt; is the expected path: StrictMode, or a component that unmounts before its registration resolves (a fast route change, say). Anything else (&lt;code&gt;InvalidStateError&lt;/code&gt; from a duplicate name, &lt;code&gt;NotAllowedError&lt;/code&gt; from the &lt;code&gt;tools&lt;/code&gt; Permissions Policy, &lt;code&gt;InvalidStateError&lt;/code&gt; if the document isn't fully active) is a real bug, and you want to see it. The opposite case is just as instructive: with StrictMode off and no catch, opening and closing the seat map doesn't produce a rejection, because the abort arrives &lt;em&gt;after&lt;/em&gt; the promise resolved, and rejecting an already-resolved promise does nothing.&lt;/p&gt;

&lt;p&gt;Don't "fix" this by turning StrictMode off. StrictMode is doing its job: showing you that your effect isn't resilient to an unmount at the wrong moment. In production, that moment happens too, just less often.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 5: Next.js, client-side only
&lt;/h2&gt;

&lt;p&gt;If your SPA is a Next.js app, there's one more rule: &lt;code&gt;modelContext&lt;/code&gt; exists only on &lt;code&gt;Document&lt;/code&gt;. There's no &lt;code&gt;document&lt;/code&gt; during server rendering or in React Server Components, so tools are registered on the client: in a &lt;code&gt;'use client'&lt;/code&gt; component, inside an effect.&lt;/p&gt;

&lt;p&gt;The trap is thinking &lt;code&gt;'use client'&lt;/code&gt; means "this only runs in the browser." It doesn't: client components are also pre-rendered on the server. What never runs on the server is &lt;strong&gt;effects&lt;/strong&gt;. That's why the hook registers in &lt;code&gt;useEffect&lt;/code&gt; and never touches &lt;code&gt;document.modelContext&lt;/code&gt; during render or at the module's top level. This snippet &lt;strong&gt;isn't part of the demo&lt;/strong&gt; (there's no Next app in the repo), but it uses the demo's own hook and tools:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WebMCPContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useWebMCPTool&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./use-webmcp-tool&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;cartTools&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./cart-tools&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;WebMCPProvider&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;modelContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setModelContext&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Effects only run in the browser, so `document` exists here.&lt;/span&gt;
  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setModelContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;modelContext&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;WebMCPContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;modelContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;catchRegistration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;WebMCPContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;CartTools&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;useWebMCPTool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cartTools&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getCartItems&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;useWebMCPTool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cartTools&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addFlightToCart&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the server, &lt;code&gt;modelContext&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt; and the hook's &lt;code&gt;if (!modelContext) return&lt;/code&gt; does nothing. In the browser, the provider resolves it after hydration, and the tools are registered. Because the tools are tied to components, a route that doesn't render &lt;code&gt;&amp;lt;CartTools /&amp;gt;&lt;/code&gt; doesn't expose them: client-side navigation unmounts the component, the cleanup aborts, the tool disappears from the list, and &lt;code&gt;toolchange&lt;/code&gt; fires.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 6: the store in a Worker, the tool on the main thread
&lt;/h2&gt;

&lt;p&gt;Same rule, different consequence: if &lt;code&gt;modelContext&lt;/code&gt; only exists on &lt;code&gt;Document&lt;/code&gt;, &lt;strong&gt;a Worker can't register tools.&lt;/strong&gt; There's a &lt;a href="https://github.com/webmachinelearning/webmcp/blob/main/docs/service-workers.md" rel="noopener noreferrer"&gt;service worker explainer&lt;/a&gt; (first published on August 28, 2025) that proposes registering service workers as WebMCP providers, so an agent could call tools without the site open. It's a proposal, it's not in the spec, and the demo doesn't use it.&lt;/p&gt;

&lt;p&gt;What does work today is a pattern: the state lives in a Worker, and the tool on the main thread is a proxy. In the demo, the fare calculator (taxes, fees, seat pricing) lives in &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/webmcp-react-state/fare-worker.js" rel="noopener noreferrer"&gt;&lt;code&gt;fare-worker.js&lt;/code&gt;&lt;/a&gt;, and &lt;code&gt;quote_fare&lt;/code&gt; talks to it over &lt;code&gt;postMessage&lt;/code&gt;, with request ids and a timeout. This is the core of &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/webmcp-react-state/fare-client.js" rel="noopener noreferrer"&gt;&lt;code&gt;fare-client.js&lt;/code&gt;&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;aborted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nextId&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onAbort&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;settle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)?.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;settle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)?.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DOMException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`The fare worker did not answer "&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;" within &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;timeoutMs&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; ms.`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;TimeoutError&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;timeoutMs&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;abort&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onAbort&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;once&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="nx"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;removeEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;abort&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onAbort&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three details that seem minor until they're missing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The id&lt;/strong&gt; matches each response to its request. Without it, two concurrent quotes could get crossed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The timeout&lt;/strong&gt; is what keeps the agent from hanging if the Worker gets stuck. Check "Slow worker (3 s)": &lt;code&gt;quote_fare&lt;/code&gt; fails after 1.5 s with &lt;code&gt;TimeoutError&lt;/code&gt;, and the agent gets a clear error instead of a promise that never settles. The Worker's late answer is dropped, because its id no longer has a pending entry.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The &lt;code&gt;signal&lt;/code&gt;&lt;/strong&gt; is the one &lt;code&gt;execute&lt;/code&gt; receives. If the agent cancels the invocation, the request to the Worker is canceled too.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The tool itself (&lt;code&gt;quote_fare&lt;/code&gt;) uses the same contract hook as the others. Its &lt;code&gt;execute&lt;/code&gt; reads the cart with &lt;code&gt;getState()&lt;/code&gt; and sends it to the Worker. The tool is the contract on the main thread; the state is wherever it fits best.&lt;/p&gt;

&lt;h2&gt;
  
  
  An honest caveat: what this demo doesn't prove
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It's shim-only.&lt;/strong&gt; All my numbers come from the in-page shim, which I aligned with the &lt;code&gt;registerTool()&lt;/code&gt; algorithm on the points this post depends on: synchronous map insert, synchronous duplicate check, abort → synchronous unregister + rejection, async resolve, and &lt;code&gt;toolchange&lt;/code&gt;. The demo detects &lt;code&gt;document.modelContext&lt;/code&gt; and uses it if it exists, but &lt;strong&gt;I haven't tested the native path&lt;/strong&gt;. If a browser's implementation differs from the spec in timing, the StrictMode numbers could change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The spec is a Draft Community Group Report&lt;/strong&gt;, outside the standards track. The surface I describe (no &lt;code&gt;unregisterTool()&lt;/code&gt;, the signal as the lifecycle) is the one in the current text, and it can change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How an agent reacts to &lt;code&gt;toolchange&lt;/code&gt; mid-conversation is up to the agent.&lt;/strong&gt; The spec defines the event, not what an agent does with it. The naive hook's 45 events are a cost on the page side; I can't measure the cost on the agent side.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The service worker explainer is a proposal.&lt;/strong&gt; The Worker pattern in the demo doesn't depend on it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I learned building this
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A tool is a contract.&lt;/strong&gt; Name, description, and schema describe &lt;em&gt;what you can do&lt;/em&gt;, not &lt;em&gt;what's there right now&lt;/em&gt;. If the contract doesn't change, the registration doesn't either.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The &lt;code&gt;AbortSignal&lt;/code&gt; is the lifecycle.&lt;/strong&gt; There's no &lt;code&gt;unregisterTool()&lt;/code&gt;, and you don't need one: a controller per effect and &lt;code&gt;abort()&lt;/code&gt; in the cleanup tie the tool to the component, the same way the declarative API ties it to the &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;State gets read at call time.&lt;/strong&gt; &lt;code&gt;getState()&lt;/code&gt; for stores, a ref for component-local state. The agent's question arrives later than the render, so the answer should be computed later too.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The spec's timing explains React's behavior.&lt;/strong&gt; The synchronous unregister is why StrictMode doesn't break; the async resolve is why you have to catch the promise. Reading the algorithm saved me a couple of hours of guessing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the next posts in the series, we'll look at invoking forms from the imperative API, how to design human-in-the-loop permissions, and how to debug all of this with DevTools.&lt;/p&gt;




&lt;h3&gt;
  
  
  To keep exploring
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The WebMCP spec:&lt;/strong&gt; &lt;a href="https://webmachinelearning.github.io/webmcp/" rel="noopener noreferrer"&gt;webmachinelearning.github.io/webmcp&lt;/a&gt;. The &lt;code&gt;registerTool()&lt;/code&gt; and "unregister a tool" algorithms are the basis for building blocks 1 and 4.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The service worker explainer:&lt;/strong&gt; &lt;a href="https://github.com/webmachinelearning/webmcp/blob/main/docs/service-workers.md" rel="noopener noreferrer"&gt;&lt;code&gt;docs/service-workers.md&lt;/code&gt;&lt;/a&gt;, the proposal for tools without an open tab.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Part 1 of the series:&lt;/strong&gt; &lt;a href="https://dev.to/blog/webmcp-fundamentals"&gt;WebMCP — Why agents shouldn't have to guess how your UI works&lt;/a&gt;, with &lt;code&gt;registerTool()&lt;/code&gt;, &lt;code&gt;getTools()&lt;/code&gt;, and &lt;code&gt;executeTool()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Part 2 of the series:&lt;/strong&gt; &lt;a href="https://dev.to/blog/webmcp-declarative-forms"&gt;Semantic markup for AI: turn your forms into WebMCP tools without JavaScript&lt;/a&gt;, with the declarative API.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The full demo:&lt;/strong&gt; &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/tree/main/demos/webmcp-react-state" rel="noopener noreferrer"&gt;&lt;code&gt;demos/webmcp-react-state&lt;/code&gt;&lt;/a&gt;. Switch to the naive hook and watch the counters, turn on StrictMode without the catch, and open and close the seat map.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Which store does your SPA use, and where would you register its first tool? I'd love to hear about it. Let's talk on &lt;a href="https://twitter.com/khriztianmoreno" rel="noopener noreferrer"&gt;X/Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/khriztianmoreno/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" alt="Profile" width="148" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webmcp</category>
      <category>aiagents</category>
      <category>mcp</category>
      <category>chromeai</category>
    </item>
    <item>
      <title>Offline-first AI — IndexedDB as Gemini Nano's memory</title>
      <dc:creator>Khriztian Moreno</dc:creator>
      <pubDate>Tue, 20 Oct 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/khriztianmoreno/offline-first-ai-indexeddb-as-gemini-nanos-memory-2f0b</link>
      <guid>https://dev.to/khriztianmoreno/offline-first-ai-indexeddb-as-gemini-nanos-memory-2f0b</guid>
      <description>&lt;p&gt;Most AI interfaces we build today share the same Achilles' heel: they're empty shells without a connection. We treat LLMs as a remote HTTP endpoint to invoke on every interaction, assuming a perfect connectivity that rarely exists in the real world. With &lt;strong&gt;Gemini Nano built into the browser via the Prompt API&lt;/strong&gt;, that dependency disappears — inference can finally move straight to the GPU or NPU on the user's own device.&lt;/p&gt;

&lt;p&gt;Running a model locally, though, only solves half the equation.&lt;/p&gt;

&lt;p&gt;The moment you move AI to the client, you run into a classic web-persistence problem: a session created with &lt;code&gt;LanguageModel.create()&lt;/code&gt; lives only in the tab's volatile memory. If a maintenance inspector is in the basement of an electrical substation documenting a pump failure, and the tab gets discarded in the background or the device restarts mid-flight-mode, the model loses the entire thread of the conversation. There's no server, no Redis, to rescue that state. For an on-device AI experience to actually be production-usable, &lt;strong&gt;it needs persistent, local memory&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That's where &lt;strong&gt;IndexedDB&lt;/strong&gt; becomes Gemini Nano's natural co-pilot.&lt;/p&gt;

&lt;p&gt;Combine the two and you don't just give the model durable memory through the &lt;code&gt;initialPrompts&lt;/code&gt; parameter — you also solve the energy problem: hashing each query locally lets you return cached answers in 0ms, saving the device's battery by never waking the model to process the same input twice.&lt;/p&gt;

&lt;p&gt;To ground this pattern in something real, I built &lt;strong&gt;FieldTech Zero&lt;/strong&gt;: an offline-first technical field notebook. It takes raw incident notes written in the field, automatically extracts a structured JSON report with diagnostics and severities, and lets you ask contextual follow-up questions about earlier inspections — even after closing and reloading the browser 30 meters underground.&lt;/p&gt;

&lt;p&gt;The full, runnable code is in &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/tree/main/demos/offline-first-ai-indexeddb-gemini-nano" rel="noopener noreferrer"&gt;&lt;code&gt;demos/offline-first-ai-indexeddb-gemini-nano&lt;/code&gt;&lt;/a&gt; (the &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/offline-first-ai-indexeddb-gemini-nano/README.md" rel="noopener noreferrer"&gt;README&lt;/a&gt; has the Chromium flags to turn on the Prompt API).&lt;/p&gt;

&lt;p&gt;Next, let's walk through how this architecture is actually orchestrated: from the model's reactive download cycle to the persistence flow that keeps the session from going amnesiac without ever touching the network.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the demo works
&lt;/h2&gt;

&lt;p&gt;&lt;br&gt;
    &lt;br&gt;
    Your browser doesn't support embedded video — &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/tree/main/demos/offline-first-ai-indexeddb-gemini-nano" rel="noopener noreferrer"&gt;clone the demo&lt;/a&gt; and run it locally instead.&lt;br&gt;
  &lt;/p&gt;&lt;br&gt;The full run: process a note, hit cache on a repeat, reload the page, and ask a follow-up the model can still answer — offline.
  &lt;p&gt;&lt;/p&gt;

&lt;p&gt;The full scenario is "FieldTech Zero": the technician writes the note, the app extracts structured JSON — component, severity, diagnostic, suggested action, whether a part needs replacing — and the whole flow comes down to four steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Type a note, hit "Process note."&lt;/strong&gt; Gemini Nano extracts the JSON locally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Submit the exact same note again.&lt;/strong&gt; The badge switches to &lt;code&gt;cache (IndexedDB)&lt;/code&gt; — no inference runs the second time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reload the page.&lt;/strong&gt; The session history is still there, restored from IndexedDB.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ask a follow-up that references the earlier note&lt;/strong&gt; — e.g. "what was the sensor pressure reading?" — with no network involved. The model still remembers, because the reload rebuilt its context from IndexedDB, not from a live connection.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As you can see in the video above, that's exactly the test: process the pump note, reload the page, then ask &lt;em&gt;"What was the sensor pressure reading on the pump from the earlier note?"&lt;/em&gt;, offline. Here's what it answers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"component"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Auxiliary pump 3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"severity"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"LOW"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"diagnostic"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"The sensor reading on the auxiliary pump 3 is 4.2 bar."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"suggestedAction"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Monitor the pump pressure closely and investigate the leak."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"partReplacementRequired"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It pulled "4.2 bar" out of a note it had never seen in &lt;em&gt;this&lt;/em&gt; page load — only in the IndexedDB history restored into the session on boot.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem: a Gemini Nano session doesn't survive a reload
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;LanguageModel.create()&lt;/code&gt; gives you a session object that lives in memory. Refresh the tab and it's gone — along with everything the user told it. For a chat toy that's mildly annoying. For a field tool where the technician's phone locks itself between inspections, or the tab gets backgrounded and killed, it's a dealbreaker: the app forgets who it's talking to and why.&lt;/p&gt;

&lt;p&gt;The fix isn't a bigger context window. It's not keeping state in memory at all — it's writing every turn to IndexedDB and rebuilding the session's context from disk every time the page loads.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 1: two IndexedDB stores
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/offline-first-ai-indexeddb-gemini-nano/db.js" rel="noopener noreferrer"&gt;&lt;code&gt;db.js&lt;/code&gt;&lt;/a&gt; keeps two object stores — &lt;code&gt;history&lt;/code&gt; for conversation turns, &lt;code&gt;cache&lt;/code&gt; for responses keyed by a hash of the prompt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;hashPrompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TextEncoder&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;digest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subtle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SHA-256&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getCachedResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;openDB&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cache&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;readonly&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;objectStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cache&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onsuccess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onerror&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The cache isn't just latency optimization — on a device running inference on battery, a repeated question (a technician re-reading the same error code twice) is a repeated cost you don't need to pay twice. Hashing the prompt and checking IndexedDB first is close to free by comparison.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 2: restoring context with &lt;code&gt;initialPrompts&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;LanguageModel.create()&lt;/code&gt; accepts &lt;code&gt;initialPrompts&lt;/code&gt; — an array of &lt;code&gt;{ role, content }&lt;/code&gt; turns that seed the session before it takes its first real prompt. &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/offline-first-ai-indexeddb-gemini-nano/nano-loader.js" rel="noopener noreferrer"&gt;&lt;code&gt;nano-loader.js&lt;/code&gt;&lt;/a&gt; reads the history straight out of IndexedDB and hands it in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LocalGeminiManager&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;onStatus&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;onStatus&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{});&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;withResolvers&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ready&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_resolve&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_reject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialPrompts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LanguageModel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LanguageModel unavailable&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ready&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;LanguageModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;initialPrompts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="cm"&gt;/* monitor below */&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ready&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Promise.withResolvers()&lt;/code&gt; (Baseline since March 2024) is what makes &lt;code&gt;ready&lt;/code&gt; usable from outside the class without an awkward callback: it hands back &lt;code&gt;{ promise, resolve, reject }&lt;/code&gt; as three independent values, so &lt;code&gt;init()&lt;/code&gt; can resolve or reject the same promise other code is already &lt;code&gt;await&lt;/code&gt;ing, instead of nesting a &lt;code&gt;new Promise((resolve, reject) =&amp;gt; { ... })&lt;/code&gt; around the whole method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 3: tracking the download, defensively
&lt;/h2&gt;

&lt;p&gt;The first time a user opens the app, the model itself might need to download. The &lt;code&gt;monitor&lt;/code&gt; option reports progress — but the exact shape of the &lt;code&gt;downloadprogress&lt;/code&gt; event has drifted across Chrome versions and even across Google's own docs: one example computes &lt;code&gt;loaded * 100&lt;/code&gt; (implying a 0–1 fraction), another computes &lt;code&gt;(loaded / total) * 100&lt;/code&gt; (implying byte counts). Rather than bet on one, &lt;code&gt;nano-loader.js&lt;/code&gt; handles both:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;monitor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;downloadprogress&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;loaded&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;loaded&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onStatus&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;downloading&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;progress&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pct&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the kind of defensive code that looks unnecessary until an origin-trial API ships a breaking change under your feet. It cost one extra line.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 4: cache → local → cloud, in that order
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/offline-first-ai-indexeddb-gemini-nano/orchestrator.js" rel="noopener noreferrer"&gt;&lt;code&gt;orchestrator.js&lt;/code&gt;&lt;/a&gt; is the hybrid part: check the cache, then the local model, then — only if there's a connection and no local model — fall back to a server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;localReady&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nano&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pretendOffline&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onLine&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;No local model and no connection — nothing left to fall back to.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/generate&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* ... */&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cloud fallback unreachable — see server-reference/ in the repo.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One thing I did deliberately: &lt;strong&gt;the cloud fallback in this demo isn't deployed.&lt;/strong&gt; There's no server running behind it and no API key anywhere in the repo — a public static demo is not where you put either. What ships instead is &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/offline-first-ai-indexeddb-gemini-nano/server-reference/index.js" rel="noopener noreferrer"&gt;&lt;code&gt;server-reference/index.js&lt;/code&gt;&lt;/a&gt;, a real Node.js implementation of the &lt;code&gt;/api/generate&lt;/code&gt; endpoint using &lt;code&gt;@google/genai&lt;/code&gt;, for you to drop into your own backend:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;ai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generateContent&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gemini-flash-latest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;turn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;turn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;role&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;assistant&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;model&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;turn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt; &lt;span class="p"&gt;}],&lt;/span&gt;
    &lt;span class="p"&gt;})),&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prompt&lt;/span&gt; &lt;span class="p"&gt;}]&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without it deployed, the demo's fallback path fails loudly with an explanatory error instead of hanging — which is itself the behavior worth shipping: a hybrid system should tell you which tier answered, and fail clearly when none can, rather than leave the user staring at a spinner.&lt;/p&gt;

&lt;h2&gt;
  
  
  An honest caveat: the model doesn't always return clean JSON
&lt;/h2&gt;

&lt;p&gt;The system prompt asks for JSON only, no prose. Most of the time that's exactly what comes back. Sometimes it isn't — a stray sentence before the object, or markdown fences around it. &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/offline-first-ai-indexeddb-gemini-nano/main.js" rel="noopener noreferrer"&gt;&lt;code&gt;main.js&lt;/code&gt;&lt;/a&gt; strips fences and tries &lt;code&gt;JSON.parse&lt;/code&gt;, and falls back to showing the raw text if that fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cleaned&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/``&lt;/span&gt;&lt;span class="err"&gt;`
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;endraw&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="s2"&gt;```/g, '').trim();
  reportEl.textContent = JSON.stringify(JSON.parse(cleaned), null, 2);
} catch {
  reportEl.textContent = text; // model didn't return clean JSON — show raw output
}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't ship structured extraction against a small on-device model without a parse fallback. It will eventually hand you something that isn't valid JSON, and a silent crash is worse than showing the raw string.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I learned wrestling with this (and why you should try it)
&lt;/h2&gt;

&lt;p&gt;Building this left me with a feeling close to when we first started shipping Service Workers to production years ago: &lt;strong&gt;native support changes the rules of the game, but it forces you to think about the architecture backwards.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Three takeaways straight from the trenches:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;On-device AI isn't a cloud replacement — it's what keeps your user from getting stranded.&lt;/strong&gt; Don't ask Gemini Nano to draft a 40-page paper in the browser. Use it to structure, classify, summarize, or filter at the edge. That's where speed and privacy are unbeatable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your memory isn't RAM anymore.&lt;/strong&gt; Treating the model's context as volatile is mistake number one. If you're building conversational interfaces or client-side agents, IndexedDB has to be the session's hard drive from day one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Battery performance matters.&lt;/strong&gt; Hashing queries to serve identical answers from disk isn't paranoia — on a phone or an industrial tablet, saving NPU/GPU cycles is the difference between finishing the shift and running out of battery halfway through a tunnel.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The pattern isn't limited to incident reports. Think of a Markdown editor that suggests tags on a plane, a recipe app recalculating portions in a kitchen with no signal, or medical triage in a rural area. &lt;strong&gt;"No signal" was never supposed to mean "blank screen."&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  To keep exploring
&lt;/h3&gt;

&lt;p&gt;If this got you curious about the code or where the ecosystem is headed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The full demo repo:&lt;/strong&gt; &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/tree/main/demos/offline-first-ai-indexeddb-gemini-nano" rel="noopener noreferrer"&gt;&lt;code&gt;demos/offline-first-ai-indexeddb-gemini-nano&lt;/code&gt;&lt;/a&gt; — clone it, check the Chromium flags you need, and run the Node.js fallback.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chrome's Built-in AI docs:&lt;/strong&gt; Google's guide to enabling and experimenting with the &lt;a href="https://developer.chrome.com/docs/ai/built-in" rel="noopener noreferrer"&gt;Prompt API in the browser&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Web Machine Learning Community Group:&lt;/strong&gt; the formal &lt;a href="https://github.com/explainers-by-googlers/prompt-api" rel="noopener noreferrer"&gt;Prompt API explainer&lt;/a&gt;, if you want to see how this interface is being standardized across browsers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IndexedDB Best Practices (web.dev):&lt;/strong&gt; essential reading on &lt;a href="https://web.dev/articles/indexeddb-best-practices" rel="noopener noreferrer"&gt;working with reliable local storage&lt;/a&gt; without blocking the main thread.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Have you shipped offline flows in production, or are you already poking at Chrome's local AI APIs? I'd love to hear what use cases make sense to you, or what broke along the way. Let's talk on &lt;a href="https://twitter.com/khriztianmoreno" rel="noopener noreferrer"&gt;X/Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/khriztianmoreno/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" alt="Profile" width="148" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webperf</category>
      <category>offlinefirst</category>
      <category>indexeddb</category>
      <category>chromeai</category>
    </item>
    <item>
      <title>Semantic markup for AI: turn your forms into WebMCP tools without JavaScript</title>
      <dc:creator>Khriztian Moreno</dc:creator>
      <pubDate>Tue, 20 Oct 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/khriztianmoreno/semantic-markup-for-ai-turn-your-forms-into-webmcp-tools-without-javascript-1nod</link>
      <guid>https://dev.to/khriztianmoreno/semantic-markup-for-ai-turn-your-forms-into-webmcp-tools-without-javascript-1nod</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/khriztianmoreno/webmcp-why-agents-shouldnt-have-to-guess-how-your-ui-works-1af1"&gt;first post in this series&lt;/a&gt;, every WebMCP tool was a JavaScript object: a name, a description, a hand-written &lt;code&gt;inputSchema&lt;/code&gt;, and an &lt;code&gt;execute&lt;/code&gt; function that called the store. It works, and for logic that lives in JavaScript it's exactly what you want. But look at your app for a second: where does most of what a user can &lt;em&gt;do&lt;/em&gt; actually live? Search, filter, subscribe, book, pay, leave a comment. Almost all of it is a &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And a form is already, to a large extent, a tool description. It has a name per field (&lt;code&gt;name&lt;/code&gt;), types (&lt;code&gt;type="date"&lt;/code&gt;, &lt;code&gt;type="email"&lt;/code&gt;), rules (&lt;code&gt;required&lt;/code&gt;, &lt;code&gt;pattern&lt;/code&gt;, &lt;code&gt;min&lt;/code&gt;, &lt;code&gt;max&lt;/code&gt;), closed sets of options (&lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt;), and a submission endpoint. The only thing it's missing for an agent to understand it is natural-language context: what the form does and what each field means.&lt;/p&gt;

&lt;p&gt;That's what the &lt;strong&gt;WebMCP declarative API&lt;/strong&gt; proposes: a handful of attributes that turn an ordinary &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; into a tool. &lt;strong&gt;The basic tool needs zero lines of JavaScript&lt;/strong&gt;, and it degrades perfectly: a human still sees a normal form. JavaScript only comes back for one thing: answering the agent without navigating.&lt;/p&gt;

&lt;p&gt;I built a demo in the same flight domain as the previous post. The code is in &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/tree/main/demos/webmcp-declarative-forms" rel="noopener noreferrer"&gt;&lt;code&gt;demos/webmcp-declarative-forms&lt;/code&gt;&lt;/a&gt; (the &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/webmcp-declarative-forms/README.md" rel="noopener noreferrer"&gt;README&lt;/a&gt; explains how to run it).&lt;/p&gt;

&lt;h2&gt;
  
  
  How the demo works
&lt;/h2&gt;

&lt;p&gt;&lt;br&gt;
    &lt;br&gt;
    Your browser doesn't support embedded video — &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/tree/main/demos/webmcp-declarative-forms" rel="noopener noreferrer"&gt;clone the demo&lt;/a&gt; and run it locally instead.&lt;br&gt;
  &lt;/p&gt;&lt;br&gt;The full run: the agent searching on its own, stopping at the booking form for you to review, and the three ways to cancel.
  &lt;p&gt;&lt;/p&gt;

&lt;p&gt;The app is SkyFare again, but this time there's no &lt;code&gt;registerTool()&lt;/code&gt; anywhere. It's &lt;strong&gt;two plain HTML forms&lt;/strong&gt; with declarative attributes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;search_flights&lt;/code&gt;&lt;/strong&gt; has &lt;code&gt;toolautosubmit&lt;/code&gt;: the agent fills it and the form submits itself.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;book_flight&lt;/code&gt;&lt;/strong&gt; doesn't: the agent fills it, the submit button gets focus, and &lt;strong&gt;you&lt;/strong&gt; review and confirm.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next to them are two panels. One is a &lt;strong&gt;simulated agent&lt;/strong&gt; (no LLM, same as part 1: the plan is hard-coded) with the goal &lt;em&gt;find the cheapest nonstop flight BOG → MAD for 2 passengers and book it for Ana Gómez&lt;/em&gt;. The other is a &lt;strong&gt;schema inspector&lt;/strong&gt; that shows, live, the JSON Schema that comes out of each form.&lt;/p&gt;

&lt;p&gt;In my runs (headless Chrome 154, using the shim):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The agent discovers 2 tools&lt;/strong&gt; from the markup, without the page registering anything.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;search_flights&lt;/code&gt; fills and submits itself.&lt;/strong&gt; The agent gets 745 characters of JSON with the flights, and the URL doesn't change: zero navigations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;book_flight&lt;/code&gt; fills and then waits.&lt;/strong&gt; The form gets highlighted, and the "Confirm booking" button pulses and has focus. The agent can't finish until you click. When you do, it gets 313 characters of JSON with the confirmation.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;While it waits, you can cancel in three different ways: the agent cancels, you reset the form, or the page changes the &lt;code&gt;toolname&lt;/code&gt;. All three cut off the in-flight invocation.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem: every tool needed JavaScript
&lt;/h2&gt;

&lt;p&gt;Part 1 left us with a core idea: a tool isn't a second implementation of the feature, it's another entry point into the one you already have. But with the imperative API, you have to build that entry point by hand. This is what &lt;code&gt;search_flights&lt;/code&gt; looked like in the first demo's &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/webmcp-fundamentals/tools.js" rel="noopener noreferrer"&gt;&lt;code&gt;tools.js&lt;/code&gt;&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;search_flights&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Search flights&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Search available flights between two airports. Returns every matching flight with id, airline, times, number of stops and price in USD.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;inputSchema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;properties&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;IATA code of the departure airport, e.g. "BOG".&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="nx"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;IATA code of the arrival airport, e.g. "MAD".&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;date&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Departure date, YYYY-MM-DD.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nx"&gt;required&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;origin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;destination&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;annotations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;readOnlyHint&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;flights&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;searchFlights&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;flights&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;flights&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at the &lt;code&gt;inputSchema&lt;/code&gt;. That block &lt;strong&gt;repeats information your form already has&lt;/strong&gt;: that &lt;code&gt;origin&lt;/code&gt; is a required string, that &lt;code&gt;date&lt;/code&gt; is a date. And because it's duplicated, it can drift: the day someone adds a &lt;code&gt;pattern&lt;/code&gt; to the origin input, the schema never hears about it. On top of that, not everything on your site sits behind a JavaScript function. Plenty of forms just &lt;code&gt;POST&lt;/code&gt; to the server and that's it. To expose them with the imperative API, you'd have to rewrite them first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 1: the same contract, written in HTML
&lt;/h2&gt;

&lt;p&gt;This is the demo's search form, in &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/webmcp-declarative-forms/index.html" rel="noopener noreferrer"&gt;&lt;code&gt;index.html&lt;/code&gt;&lt;/a&gt; (I left out the cabin and passenger fields so it reads better):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt;
  &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"search-form"&lt;/span&gt;
  &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"submitted.html"&lt;/span&gt;
  &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"get"&lt;/span&gt;
  &lt;span class="na"&gt;toolname=&lt;/span&gt;&lt;span class="s"&gt;"search_flights"&lt;/span&gt;
  &lt;span class="na"&gt;tooldescription=&lt;/span&gt;&lt;span class="s"&gt;"Search flights between two airports on a given date. Returns each flight's id, airline, times, number of stops and per-passenger price."&lt;/span&gt;
  &lt;span class="na"&gt;toolautosubmit&lt;/span&gt;
  &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"tool-form grid grid-cols-2 gap-3 sm:grid-cols-4"&lt;/span&gt;
&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"field"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;From
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"origin"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt; &lt;span class="na"&gt;pattern=&lt;/span&gt;&lt;span class="s"&gt;"[A-Za-z]{3}"&lt;/span&gt; &lt;span class="na"&gt;maxlength=&lt;/span&gt;&lt;span class="s"&gt;"3"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"BOG"&lt;/span&gt; &lt;span class="na"&gt;autocomplete=&lt;/span&gt;&lt;span class="s"&gt;"off"&lt;/span&gt; &lt;span class="na"&gt;title=&lt;/span&gt;&lt;span class="s"&gt;"3-letter airport code"&lt;/span&gt;
      &lt;span class="na"&gt;toolparamdescription=&lt;/span&gt;&lt;span class="s"&gt;"Origin airport, as a 3-letter IATA code (e.g. BOG)"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"field"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;To
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"destination"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt; &lt;span class="na"&gt;pattern=&lt;/span&gt;&lt;span class="s"&gt;"[A-Za-z]{3}"&lt;/span&gt; &lt;span class="na"&gt;maxlength=&lt;/span&gt;&lt;span class="s"&gt;"3"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"MAD"&lt;/span&gt; &lt;span class="na"&gt;autocomplete=&lt;/span&gt;&lt;span class="s"&gt;"off"&lt;/span&gt; &lt;span class="na"&gt;title=&lt;/span&gt;&lt;span class="s"&gt;"3-letter airport code"&lt;/span&gt;
      &lt;span class="na"&gt;toolparamdescription=&lt;/span&gt;&lt;span class="s"&gt;"Destination airport, as a 3-letter IATA code (e.g. MAD)"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"field col-span-2"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Date
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"date"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"date"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt; &lt;span class="na"&gt;min=&lt;/span&gt;&lt;span class="s"&gt;"2026-10-13"&lt;/span&gt; &lt;span class="na"&gt;max=&lt;/span&gt;&lt;span class="s"&gt;"2027-10-12"&lt;/span&gt;
      &lt;span class="na"&gt;toolparamdescription=&lt;/span&gt;&lt;span class="s"&gt;"Departure date"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pattern comes straight from the &lt;a href="https://github.com/webmachinelearning/webmcp/blob/main/declarative-api-explainer.md" rel="noopener noreferrer"&gt;declarative API explainer&lt;/a&gt;, which shows the same side-by-side with a car search. There are only a few attributes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;On the &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt;:&lt;/strong&gt; &lt;code&gt;toolname&lt;/code&gt; (the equivalent of the imperative tool's &lt;code&gt;name&lt;/code&gt;), &lt;code&gt;tooldescription&lt;/code&gt; (the equivalent of &lt;code&gt;description&lt;/code&gt;), and &lt;code&gt;toolautosubmit&lt;/code&gt;, a boolean attribute we'll get to in building block 3.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;On each control:&lt;/strong&gt; the usual &lt;code&gt;name&lt;/code&gt; becomes the property name, and &lt;code&gt;toolparamdescription&lt;/code&gt; provides its description.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One detail you'll see wrong in a few places: &lt;strong&gt;there is no &lt;code&gt;toolparam&lt;/code&gt; attribute&lt;/strong&gt;. The property name is the control's &lt;code&gt;name&lt;/code&gt;, the same one that already travels with the form submission. The only new thing per control is the description.&lt;/p&gt;

&lt;p&gt;And notice what &lt;em&gt;isn't&lt;/em&gt; there: no &lt;code&gt;execute&lt;/code&gt;, no &lt;code&gt;registerTool()&lt;/code&gt;, no &lt;code&gt;AbortController&lt;/code&gt; to unregister. According to the explainer, when a form with these attributes is inserted, removed, or has those attributes changed, the browser creates a new declarative tool. The tool's lifecycle is the element's lifecycle.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 2: from form to JSON Schema (and why it's TBD)
&lt;/h2&gt;

&lt;p&gt;For the agent to know how to fill the form, the browser has to "compile" it into an &lt;code&gt;inputSchema&lt;/code&gt;. The explainer calls this &lt;em&gt;input schema synthesis&lt;/em&gt;, and here's the part I most want to be clear about: &lt;strong&gt;the algorithm doesn't exist in the spec yet.&lt;/strong&gt; The section literally says TODO: it still needs to specify how &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt;, and attributes like &lt;code&gt;step&lt;/code&gt; and &lt;code&gt;min&lt;/code&gt; reduce to a JSON Schema with &lt;code&gt;anyOf&lt;/code&gt;, &lt;code&gt;oneOf&lt;/code&gt;, &lt;code&gt;minimum&lt;/code&gt;, and &lt;code&gt;maximum&lt;/code&gt;. Chromium is implementing "a loose version" of it to trial.&lt;/p&gt;

&lt;p&gt;So the demo ships &lt;strong&gt;our own approximation&lt;/strong&gt;, in &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/webmcp-declarative-forms/schema-synthesis.js" rel="noopener noreferrer"&gt;&lt;code&gt;schema-synthesis.js&lt;/code&gt;&lt;/a&gt;, and the inspector shows it with a label that says so. This table is what I'd &lt;em&gt;expect&lt;/em&gt; to happen, not what a standard says:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;In the HTML&lt;/th&gt;
&lt;th&gt;Our approximation in JSON Schema&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;required&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The property goes into &lt;code&gt;required&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pattern="[A-Za-z]{3}"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;"pattern": "^(?:[A-Za-z]{3})$"&lt;/code&gt; (HTML anchors patterns implicitly; JSON Schema doesn't)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;minlength&lt;/code&gt; / &lt;code&gt;maxlength&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;minLength&lt;/code&gt; / &lt;code&gt;maxLength&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;type="email"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"format": "email"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;type="date"&lt;/code&gt; with &lt;code&gt;min&lt;/code&gt; / &lt;code&gt;max&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;"format": "date"&lt;/code&gt;; the range goes into a &lt;code&gt;$comment&lt;/code&gt;, because JSON Schema has no standard keyword for date ranges&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;type="number" min="0" max="3"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;"type": "integer", "minimum": 0, "maximum": 3&lt;/code&gt; (the default &lt;code&gt;step&lt;/code&gt; is 1, hence integer)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;type="checkbox"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"type": "boolean"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;"oneOf": [{ "const": "economy", "title": "Economy" }, …]&lt;/code&gt;; the empty placeholder is dropped when the select is &lt;code&gt;required&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Form-associated custom element&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;"type": "string"&lt;/code&gt; with a &lt;code&gt;$comment&lt;/code&gt;: the mapping is unspecified&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Building this taught me two things. First, &lt;strong&gt;your HTML validation becomes documentation for the agent&lt;/strong&gt;. That &lt;code&gt;pattern&lt;/code&gt; you added for the user now also tells the model what format the field expects. Second, some things don't translate well: a date range has no standard keyword, and a custom control is opaque. Validation still runs when the form is submitted (it's HTML, after all), but the agent can't see it ahead of time.&lt;/p&gt;

&lt;p&gt;The inspector updates through a &lt;code&gt;MutationObserver&lt;/code&gt;. Tick the &lt;code&gt;toolautosubmit&lt;/code&gt; checkbox or rename the tool, and you'll see the schema re-synthesized on the spot.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 3: &lt;code&gt;toolautosubmit&lt;/code&gt; or human review
&lt;/h2&gt;

&lt;p&gt;This is the most important design decision in the API, and it's a single boolean attribute:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;With &lt;code&gt;toolautosubmit&lt;/code&gt;&lt;/strong&gt;, the agent fills the form and submits it on the user's behalf. That makes sense for a search: it's read-only and has no consequences.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Without &lt;code&gt;toolautosubmit&lt;/code&gt;&lt;/strong&gt;, once the agent is done filling, &lt;strong&gt;the browser brings the submit button into focus&lt;/strong&gt;, and the agent should tell the user to check the form and submit it manually.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In part 1, the "Approve / Deny" before booking was implemented by my simulated agent reading &lt;code&gt;consequentialHint&lt;/code&gt;. Here, the human in the loop comes for free: &lt;strong&gt;it's the form itself&lt;/strong&gt;, with the agent's data in plain view, and the same button as always. No JavaScript, no separate dialog.&lt;/p&gt;

&lt;p&gt;To make sure the user notices a form waiting for them, the explainer proposes two CSS pseudo-classes: &lt;code&gt;:tool-form-active&lt;/code&gt; for the &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; whose tool is "running", and &lt;code&gt;:tool-submit-active&lt;/code&gt; for its submit button. A script can't make a real pseudo-class match, so the shim mirrors them as &lt;code&gt;data-*&lt;/code&gt; attributes, and &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/webmcp-declarative-forms/style.css" rel="noopener noreferrer"&gt;&lt;code&gt;style.css&lt;/code&gt;&lt;/a&gt; has both versions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;form&lt;/span&gt;&lt;span class="nd"&gt;:tool-form-active&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;outline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="nb"&gt;dashed&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--review&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;outline-offset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;/* ... */&lt;/span&gt;

&lt;span class="nt"&gt;form&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;data-tool-form-active&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;outline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="nb"&gt;dashed&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--review&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;outline-offset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They're separate rules on purpose: a browser that doesn't know &lt;code&gt;:tool-form-active&lt;/code&gt; drops the whole rule, so if you put both versions in a single selector list, that browser would drop the shim's version too.&lt;/p&gt;

&lt;p&gt;The invocation can also be cut off. According to the explainer, if the form is reset or its tool declaration changes (say, its &lt;code&gt;toolname&lt;/code&gt;), the in-flight invocation is canceled and the agent is notified. In the demo you can try it with the "Clear" and "Rename to reserve_flight" buttons while the agent is waiting.&lt;/p&gt;

&lt;h3&gt;
  
  
  Find, preview, commit
&lt;/h3&gt;

&lt;p&gt;After part 1, a reader asked a question worth its weight in gold: if the agent no longer has to guess which button to press, how does a site express the difference between &lt;strong&gt;finding&lt;/strong&gt; an option, &lt;strong&gt;previewing&lt;/strong&gt; the result, and &lt;strong&gt;committing&lt;/strong&gt; a change? Those are three distinct intents, and today WebMCP covers them unevenly:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Intent&lt;/th&gt;
&lt;th&gt;Imperative API (part 1)&lt;/th&gt;
&lt;th&gt;Declarative API (this part)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Find&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;readOnlyHint&lt;/code&gt; on the tool&lt;/td&gt;
&lt;td&gt;a search form with &lt;code&gt;toolautosubmit&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Preview&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;not a concept: you model it as a separate read-only tool (e.g. &lt;code&gt;quote_booking&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;the agent-filled form waiting for review (&lt;code&gt;:tool-form-active&lt;/code&gt;, focus on the submit button)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Commit&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;consequentialHint&lt;/code&gt;, plus your own approval UI&lt;/td&gt;
&lt;td&gt;the human presses submit (no &lt;code&gt;toolautosubmit&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The awkward part: the declarative API has &lt;strong&gt;no&lt;/strong&gt; equivalent to &lt;code&gt;readOnlyHint&lt;/code&gt; or &lt;code&gt;consequentialHint&lt;/code&gt;. The only attributes the explainer proposes are &lt;code&gt;toolname&lt;/code&gt;, &lt;code&gt;tooldescription&lt;/code&gt;, &lt;code&gt;toolautosubmit&lt;/code&gt;, and &lt;code&gt;toolparamdescription&lt;/code&gt;. The only signal that "this has consequences" is the absence of &lt;code&gt;toolautosubmit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What follows is my own reading, not something in the explainer: the web has had this distinction for decades. In HTTP, &lt;code&gt;GET&lt;/code&gt; is a &lt;em&gt;safe&lt;/em&gt; method, meant for reading, and &lt;code&gt;POST&lt;/code&gt; isn't. A well-built form already declares its intent through its &lt;code&gt;method&lt;/code&gt;: search goes over &lt;code&gt;GET&lt;/code&gt;, booking over &lt;code&gt;POST&lt;/code&gt;. Note that the demo's booking form uses &lt;code&gt;GET&lt;/code&gt; only because a static server rejects &lt;code&gt;POST&lt;/code&gt;; a real app would use &lt;code&gt;POST&lt;/code&gt;. If tool synthesis ended up using that signal, agents would inherit semantics the web already understands instead of inventing new ones. Solving this properly, with permissions and confirmations, is the topic of this series' human-in-the-loop post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 4: &lt;code&gt;agentInvoked&lt;/code&gt; and &lt;code&gt;respondWith()&lt;/code&gt;, where JavaScript comes back
&lt;/h2&gt;

&lt;p&gt;So far, zero JavaScript. But there's a gap: when the agent submits the search form, what does it get back? Without JavaScript, the form navigates to its &lt;code&gt;action&lt;/code&gt;, as always. The explainer had an idea for that case: use the first &lt;code&gt;&amp;lt;script type="application/ld+json"&amp;gt;&lt;/code&gt; on the destination page as the response. That idea is now &lt;strong&gt;under debate&lt;/strong&gt; in &lt;a href="https://github.com/webmachinelearning/webmcp/issues/135" rel="noopener noreferrer"&gt;issue #135&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The alternative, which the demo uses, is to answer from the page. The explainer adds two members to &lt;code&gt;SubmitEvent&lt;/code&gt;: &lt;code&gt;agentInvoked&lt;/code&gt;, to tell whether an agent triggered the submission, and &lt;code&gt;respondWith(promise)&lt;/code&gt;, to hand the agent a response without navigating. The rule: you must call &lt;code&gt;preventDefault()&lt;/code&gt; &lt;strong&gt;before&lt;/strong&gt; &lt;code&gt;respondWith()&lt;/code&gt;. Here's how it looks in &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/webmcp-declarative-forms/app.js" rel="noopener noreferrer"&gt;&lt;code&gt;app.js&lt;/code&gt;&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;searchForm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Enhanced path: answer in-page instead of navigating to the GET action.&lt;/span&gt;
  &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;flights&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;searchFlights&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromEntries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FormData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchForm&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
  &lt;span class="nf"&gt;renderResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resultsEl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;flights&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;bookForm&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;agentInvoked&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// The agent gets structured data back; the page never navigates.&lt;/span&gt;
    &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;respondWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;flights&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is what I liked most about the approach: &lt;strong&gt;it's the same handler for humans and agents.&lt;/strong&gt; If you fill in the search yourself, &lt;code&gt;agentInvoked&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;, the results render on the page, and that's it. If the agent fills it, exactly the same thing happens, and the agent also gets &lt;code&gt;{ flights }&lt;/code&gt; as data, so it doesn't have to read the results list. The only agent-specific line is the &lt;code&gt;if&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One detail the explainer doesn't settle: when the agent fills &lt;code&gt;book_flight&lt;/code&gt; and you click "Confirm booking", is that submission &lt;code&gt;agentInvoked&lt;/code&gt;? The shim says yes, so the agent receives the confirmation. That's my call, not something the text says.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 5: Web Components that take part
&lt;/h2&gt;

&lt;p&gt;The explainer talks about &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; and &lt;a href="https://html.spec.whatwg.org/#form-associated-element" rel="noopener noreferrer"&gt;form-associated elements&lt;/a&gt;. It doesn't mention &lt;code&gt;&amp;lt;details&amp;gt;&lt;/code&gt;, tables, or microformats. But here's an important detail: a custom element with &lt;code&gt;static formAssociated = true&lt;/code&gt; and &lt;code&gt;ElementInternals&lt;/code&gt; &lt;strong&gt;is&lt;/strong&gt; a form-associated element per HTML. So a Web Component can take part. The demo has one, &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/webmcp-declarative-forms/passenger-stepper.js" rel="noopener noreferrer"&gt;&lt;code&gt;passenger-stepper.js&lt;/code&gt;&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PassengerStepper&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;HTMLElement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;formAssociated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;internals&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;attachInternals&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It shows up in &lt;code&gt;form.elements&lt;/code&gt;, submits its value with &lt;code&gt;setFormValue()&lt;/code&gt;, validates with &lt;code&gt;setValidity()&lt;/code&gt;, and responds to reset with &lt;code&gt;formResetCallback()&lt;/code&gt;. To the shim, it's just another control.&lt;/p&gt;

&lt;p&gt;Now, what follows is &lt;strong&gt;my inference, not spec&lt;/strong&gt;: the explainer doesn't say how a custom element maps to a schema. An &lt;code&gt;&amp;lt;input type="number" min="1" max="6"&amp;gt;&lt;/code&gt; tells the agent "integer between 1 and 6". My custom element, on the other hand, is a black box: the browser has no way of knowing which values it accepts. That's why, in our approximation, it comes out as a &lt;code&gt;string&lt;/code&gt; with a &lt;code&gt;$comment&lt;/code&gt;, and the demo's agent sends &lt;code&gt;"2"&lt;/code&gt;. Validation still works on submit, but the agent loses the type and the range. If you can use a native control, the agent comes out ahead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building block 6: real progressive enhancement
&lt;/h2&gt;

&lt;p&gt;Since it's all HTML, the demo works without JavaScript. Both forms &lt;code&gt;GET&lt;/code&gt; &lt;code&gt;submitted.html&lt;/code&gt;, a static page that shows you what arrived. The stepper has a fallback too: in the markup, it wraps a plain &lt;code&gt;&amp;lt;input type="number" name="passengers"&amp;gt;&lt;/code&gt;, and when the custom element upgrades, it copies its contract (&lt;code&gt;name&lt;/code&gt;, &lt;code&gt;min&lt;/code&gt;, &lt;code&gt;max&lt;/code&gt;, and &lt;code&gt;toolparamdescription&lt;/code&gt;) and replaces it. Without JavaScript, the element never upgrades and the form submits the plain input.&lt;/p&gt;

&lt;p&gt;To try it without touching your browser settings, the "View without the page script" link loads the page with &lt;code&gt;?nojs&lt;/code&gt;, and &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/webmcp-declarative-forms/main.js" rel="noopener noreferrer"&gt;&lt;code&gt;main.js&lt;/code&gt;&lt;/a&gt; stops before doing anything:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;URLSearchParams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nojs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;documentElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;nojs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;boot&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a simulation, and the UI says so. For the real test, disable JavaScript in DevTools. That's how I verified it with Playwright: with JavaScript off, the booking form navigates to &lt;code&gt;submitted.html?flight=IB-6584&amp;amp;passenger=Ana+G%C3%B3mez&amp;amp;…&lt;/code&gt; like any form from the 2000s.&lt;/p&gt;

&lt;p&gt;That's the underlying argument: &lt;strong&gt;declarative attributes cost nobody anything.&lt;/strong&gt; A browser that doesn't understand them ignores them, a human sees the same form as always, and the page that already worked keeps working.&lt;/p&gt;

&lt;h2&gt;
  
  
  An honest caveat: this is even earlier than part 1
&lt;/h2&gt;

&lt;p&gt;If the imperative API is a proposal, the declarative one is a step further back:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It only exists in an explainer.&lt;/strong&gt; It's not in the &lt;a href="https://webmachinelearning.github.io/webmcp/" rel="noopener noreferrer"&gt;spec&lt;/a&gt;, which is itself a Draft Community Group Report off the standards track.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Schema synthesis is TBD.&lt;/strong&gt; Everything in the table is our approximation. Chromium is trialing its own loose version, and the result may differ.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How the response gets back to the agent is under debate&lt;/strong&gt; (&lt;a href="https://github.com/webmachinelearning/webmcp/issues/135" rel="noopener noreferrer"&gt;issue #135&lt;/a&gt;). &lt;code&gt;respondWith()&lt;/code&gt; is in the explainer, but the no-JavaScript path (&lt;code&gt;application/ld+json&lt;/code&gt; on the destination page) is still being discussed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The event names don't match.&lt;/strong&gt; The explainer talks about &lt;code&gt;toolactivated&lt;/code&gt; and &lt;code&gt;toolcanceled&lt;/code&gt;. The spec IDL, on the other hand, defines &lt;code&gt;ontoolcancel&lt;/code&gt;. It's also still open whether they fire at &lt;code&gt;Window&lt;/code&gt; or at the &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt;. Meanwhile, the shim fires both &lt;code&gt;toolcanceled&lt;/code&gt; and &lt;code&gt;toolcancel&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integration with &lt;code&gt;getTools()&lt;/code&gt; / &lt;code&gt;executeTool()&lt;/code&gt; is TBD.&lt;/strong&gt; The explainer says declarative tools will almost certainly be invokable through them, but the details aren't there. That's why the shim exposes its own &lt;code&gt;getTools()&lt;/code&gt; / &lt;code&gt;executeTool()&lt;/code&gt; and leaves &lt;code&gt;document.modelContext&lt;/code&gt; alone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I haven't tested the native path.&lt;/strong&gt; There's no documented way to detect support for the declarative API, so the demo doesn't invent one: the declarative behavior is &lt;strong&gt;always&lt;/strong&gt; the shim. If your browser exposes the imperative &lt;code&gt;document.modelContext&lt;/code&gt;, the badge shows it, but that tells you nothing about forms.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I learned building this
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Your forms were already a contract.&lt;/strong&gt; &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;type&lt;/code&gt;, &lt;code&gt;required&lt;/code&gt;, and &lt;code&gt;pattern&lt;/code&gt; have been describing what your app expects for years. The declarative API doesn't ask for a new contract; it asks you to add the descriptions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Human-in-the-loop can be an attribute.&lt;/strong&gt; Leaving out &lt;code&gt;toolautosubmit&lt;/code&gt; is enough to make the user review a consequential action, with a UI they already know. It's the cheapest form of control I've seen so far in this series.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One handler for humans and agents.&lt;/strong&gt; &lt;code&gt;agentInvoked&lt;/code&gt; + &lt;code&gt;respondWith()&lt;/code&gt; doesn't make you fork your logic: you just add a structured response at the end of the path that already existed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Native beats custom, at least for the agent.&lt;/strong&gt; A Web Component can take part, but a native &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; gives the model types and ranges that an opaque component hides.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the next posts in the series, we'll look at what happens when state lives in an SPA's hooks and stores, how to design human-in-the-loop permissions beyond a form, and how to debug all of this with DevTools.&lt;/p&gt;




&lt;h3&gt;
  
  
  To keep exploring
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The declarative API explainer:&lt;/strong&gt; &lt;a href="https://github.com/webmachinelearning/webmcp/blob/main/declarative-api-explainer.md" rel="noopener noreferrer"&gt;&lt;code&gt;declarative-api-explainer.md&lt;/code&gt;&lt;/a&gt;, the source for everything in this post.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The issue about the agent response:&lt;/strong&gt; &lt;a href="https://github.com/webmachinelearning/webmcp/issues/135" rel="noopener noreferrer"&gt;webmachinelearning/webmcp#135&lt;/a&gt;, where the way a form's result gets back to the agent is being discussed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The WebMCP spec:&lt;/strong&gt; &lt;a href="https://webmachinelearning.github.io/webmcp/" rel="noopener noreferrer"&gt;webmachinelearning.github.io/webmcp&lt;/a&gt;, the Draft Community Group Report (the imperative API).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Part 1 of the series:&lt;/strong&gt; &lt;a href="https://dev.to/blog/webmcp-fundamentals"&gt;WebMCP — Why agents shouldn't have to guess how your UI works&lt;/a&gt;, with &lt;code&gt;registerTool()&lt;/code&gt;, &lt;code&gt;getTools()&lt;/code&gt;, and &lt;code&gt;executeTool()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The full demo:&lt;/strong&gt; &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/tree/main/demos/webmcp-declarative-forms" rel="noopener noreferrer"&gt;&lt;code&gt;demos/webmcp-declarative-forms&lt;/code&gt;&lt;/a&gt; — run the agent, leave it waiting on the booking form, and try all three ways to cancel.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How many of your app's forms could be tools tomorrow just by adding descriptions? I'd love to hear which one you'd expose first. Let's talk on &lt;a href="https://twitter.com/khriztianmoreno" rel="noopener noreferrer"&gt;X/Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/khriztianmoreno/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" alt="Profile" width="148" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webmcp</category>
      <category>aiagents</category>
      <category>mcp</category>
      <category>chromeai</category>
    </item>
    <item>
      <title>Some methods beyond console.log in Javascript</title>
      <dc:creator>Khriztian Moreno</dc:creator>
      <pubDate>Tue, 20 Oct 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/khriztianmoreno/some-methods-beyond-consolelog-in-javascript-34kp</link>
      <guid>https://dev.to/khriztianmoreno/some-methods-beyond-consolelog-in-javascript-34kp</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk34yrc4herbviu81crvq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk34yrc4herbviu81crvq.png" alt="Some methods beyond console.log in Javascript" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Often, during debugging, JavaScript developers tend to use the &lt;code&gt;console.log()&lt;/code&gt; method to print values. But there are some other console methods that make life much easier. Want to know what these methods are? Let's get to know them!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. &lt;code&gt;console.table()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Displaying long arrays or objects is a headache using the &lt;code&gt;console.log()&lt;/code&gt; method, but with &lt;code&gt;console.table()&lt;/code&gt; we have a much more elegant way to do it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Matrix&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;matrix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;apple&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;banana&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cherry&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rs 80/kg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rs 100/kg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rs 120/kg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5 ⭐&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4 ⭐&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4.5 ⭐&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Maps&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;family&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;span class="nx"&gt;family&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mother&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jane&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Smith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;family&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;father&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Smith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;family&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;daughter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Emily&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Smith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;family&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F37w9ez8w43n3ybe3bl94.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F37w9ez8w43n3ybe3bl94.png" alt="console.table()" width="681" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. &lt;code&gt;console.trace()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Having trouble debugging a function? Wondering how the execution flows? &lt;code&gt;console.trace()&lt;/code&gt; is your friend!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;innerFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trace&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;innerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5422ywzh99jnppjifyqi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5422ywzh99jnppjifyqi.png" alt="console.trace()" width="681" height="170"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. &lt;code&gt;console.error()&lt;/code&gt; and &lt;code&gt;console.warn()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Tired of boring logs? Spice things up with &lt;code&gt;console.error()&lt;/code&gt; and &lt;code&gt;console.warn()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is an error message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is a warning message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is a log message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faqjpe6dwijmt7gvi3ra7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faqjpe6dwijmt7gvi3ra7.png" alt="console.error()" width="681" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. &lt;code&gt;console.assert()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This is another brilliant debugging tool! If the assertion fails, the console will print the trace.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a is not equal to -1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a is negative&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmfqqjbfcb6592mcjar01.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmfqqjbfcb6592mcjar01.png" alt="console.assert()" width="678" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. &lt;code&gt;console.time()&lt;/code&gt;, &lt;code&gt;console.timeEnd()&lt;/code&gt;, and &lt;code&gt;console.timeLog()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Need to check how long something is taking? Timer methods are there to rescue you!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;timeout-timer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timeEnd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;timeout-timer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timeLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;timeout-timer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp23gnvotxhkr7ejvklhf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp23gnvotxhkr7ejvklhf.png" alt="console.time()" width="680" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NOTE: setTimeouts are not executed immediately, which results in a slight deviation from the expected time.&lt;/p&gt;

&lt;p&gt;That's all folks! I hope this helps you become a better dev!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" alt="Profile" width="148" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a class="mentioned-user" href="https://dev.to/khriztianmoreno"&gt;@khriztianmoreno&lt;/a&gt; on &lt;a href="https://twitter.com/khriztianmoreno" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; and &lt;a href="https://github.com/khriztianmoreno" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>A senior stands out for his thinking</title>
      <dc:creator>Khriztian Moreno</dc:creator>
      <pubDate>Tue, 20 Oct 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/khriztianmoreno/a-senior-stands-out-for-his-thinking-16d0</link>
      <guid>https://dev.to/khriztianmoreno/a-senior-stands-out-for-his-thinking-16d0</guid>
      <description>&lt;p&gt;&lt;strong&gt;The analysis of edge cases: a key to evaluating senior developers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The world of programming is constantly changing, but many companies still use outdated evaluation methods to measure the skills of senior developers. While theoretical tests are useful for measuring basic knowledge, they do not accurately reflect a senior's ability to solve complex real-world problems.&lt;/p&gt;

&lt;p&gt;&lt;a href="/posts/edge-cases.webp" class="article-body-image-wrapper"&gt;&lt;img src="/posts/edge-cases.webp" alt="The analysis of edge cases"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why are traditional technical tests not enough?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Tests based solely on theoretical concepts fail to capture the true value of a senior developer. A senior's experience is measured by their ability to anticipate problems, design scalable solutions, and optimize systems. A good senior developer not only masters the code but also knows how to handle exceptions, manage complexity, and think critically in uncertain scenarios.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The practical approach: edge case analysis&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A key exercise to evaluate a senior developer is to present them with a realistic scenario and ask them to identify and solve the edge cases. Take, for example, the development of a page with search and pagination. In a technical interview, an experienced senior should be able to think of extreme scenarios that could break the functionality, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Search with no results&lt;/strong&gt;: How would you handle a search that returns no results? How would you inform the user in a friendly manner and what impact would this have on performance?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Malicious terms&lt;/strong&gt;: What if a user enters malicious code or unexpected characters in the search field? How would you protect the application from SQL injection or XSS attacks?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-existent pages&lt;/strong&gt;: If the system receives a request for a non-existent page or with incorrect parameters, how would you handle URL validation to avoid 404 errors? What alternatives would you offer the user?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Excessive results&lt;/strong&gt;: Imagine the search returns millions of results. How would you optimize pagination to prevent the application from crashing or becoming slow? What techniques would you use to ensure efficient loading?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why is this approach important?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Evaluating a candidate in these scenarios not only measures their technical knowledge but also their ability to think proactively, manage exceptions, and anticipate problems. A senior developer must have the ability to design robust solutions that not only work in ideal cases but also cover those exceptional cases that may arise during the daily operation of the system.&lt;/p&gt;

&lt;p&gt;Edge case analysis is a clear way to measure a senior's mindset. It is not enough for the candidate to solve a problem in the ideal scenario. The true test is how they respond when things do not go as planned, and if they can find effective solutions to maintain the stability, security, and usability of the system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How should a senior be evaluated?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Realistic and practical tests&lt;/strong&gt;: Beyond theoretical concepts, interviews should focus on challenges that reflect the real problems a senior developer faces. This involves evaluating how they manage edge cases and their ability to design scalable and secure solutions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Evaluation of architectural decision-making&lt;/strong&gt;: Seniors must be able to make informed decisions about software architecture, scalability, performance, and security. Interviews should explore these capabilities in depth.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Evaluation of interpersonal and leadership skills&lt;/strong&gt;: Interviews should also include dynamics that explore communication, teamwork, and leadership skills, as these are crucial aspects of a senior's role.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;In conclusion:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Traditional technical tests have their place, but they are not enough to effectively evaluate a senior developer. Companies should focus on a more holistic evaluation, where edge cases and critical decisions about software architecture and design are at the center of the assessment. Only then can we ensure that the selected candidates are truly capable of bringing the expected value to a development team.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What do you think about this?&lt;/strong&gt; Do you agree that technical interviews for seniors should focus more on edge case analysis? What other aspects do you consider important for evaluating a senior?&lt;/p&gt;

&lt;h4&gt;
  
  
  References
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Special thanks 🙏🏻 for their &lt;a href="https://x.com/fmontes/status/1863583811130343597" rel="noopener noreferrer"&gt;example&lt;/a&gt; to &lt;a href="https://x.com/fmontes" rel="noopener noreferrer"&gt;Freddy Montes&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/swlh/taking-the-edge-off-of-edge-cases-7b3008d83a57" rel="noopener noreferrer"&gt;Taking the Edge off of Edge Cases&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" alt="Profile" width="148" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a class="mentioned-user" href="https://dev.to/khriztianmoreno"&gt;@khriztianmoreno&lt;/a&gt; 🚀
&lt;/h4&gt;

</description>
      <category>webdev</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Using await at the top level in ES modules</title>
      <dc:creator>Khriztian Moreno</dc:creator>
      <pubDate>Tue, 20 Oct 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/khriztianmoreno/using-await-at-the-top-level-in-es-modules-51ae</link>
      <guid>https://dev.to/khriztianmoreno/using-await-at-the-top-level-in-es-modules-51ae</guid>
      <description>&lt;h1&gt;
  
  
  Using await at the top level in ES modules
&lt;/h1&gt;

&lt;p&gt;As I was refactoring one of my React projects last week, I realized how much JavaScript has evolved since I first started coding. Remember when we had to wrap everything in callbacks? Then came Promises, followed by async/await. And now, in 2025, I find myself regularly using a feature that quietly changed the game for JavaScript developers: top-level await in ES modules.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Evolution of Asynchronous JavaScript
&lt;/h2&gt;

&lt;p&gt;Through my journey as a developer, I've witnessed the transformation of asynchronous JavaScript. In the early days, handling async operations meant callback hell—nested functions that became increasingly difficult to read and maintain. Then Promises arrived, offering a more structured approach but still requiring &lt;code&gt;.then()&lt;/code&gt; chains.&lt;/p&gt;

&lt;p&gt;When async/await appeared, it was a revelation. Suddenly our asynchronous code could look synchronous! But there was still one limitation that frustrated me daily: we could only use &lt;code&gt;await&lt;/code&gt; inside async functions. This meant wrapping our module initialization code in unnecessary boilerplate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The old way we had to do things&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchImportantData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nf"&gt;initializeApp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ES2022 changed everything by introducing top-level await. After using it extensively in my projects over the past few years, I can confidently say it's one of those features that once you start using, you never want to go back.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Exactly Is Top-Level await?
&lt;/h2&gt;

&lt;p&gt;Top-level await is exactly what it sounds like—the ability to use the &lt;code&gt;await&lt;/code&gt; keyword directly at the top level of your ES modules, outside of async functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This now works in ES modules&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchImportantData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;initializeApp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No more wrapping in async IIFEs (Immediately Invoked Function Expressions). No more unnecessary nesting. Just clean, straightforward code that expresses your intent directly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-World Use Cases From My Projects
&lt;/h2&gt;

&lt;p&gt;Over the past year, I've found myself reaching for top-level await in several common scenarios:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Configuration Loading
&lt;/h3&gt;

&lt;p&gt;In my latest web app, I needed to fetch configuration before initializing the application. Top-level await made this beautifully simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// config.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/configuration&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Other modules can now import this config&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Dynamic Module Loading
&lt;/h3&gt;

&lt;p&gt;I recently built a dashboard where features needed to be loaded based on user permissions. Top-level await paired perfectly with dynamic imports:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;analyticsModule&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userPermissions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;analytics&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;analyticsModule&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./features/analytics.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;enabledFeatures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;analytics&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;analyticsModule&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="c1"&gt;// other features...&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Database Connection Initialization
&lt;/h3&gt;

&lt;p&gt;For a Node.js backend service I worked on, establishing the database connection before exporting the module was crucial:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// db.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;createDatabaseConnection&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Gotchas I've Learned the Hard Way
&lt;/h2&gt;

&lt;p&gt;After using top-level await on several projects, I've encountered some pitfalls worth sharing:&lt;/p&gt;

&lt;h3&gt;
  
  
  Module Execution Is Not What You Might Expect
&lt;/h3&gt;

&lt;p&gt;I once spent hours debugging a mysterious load order issue before I understood a key behavior: when a module uses top-level await, any module that imports it will pause execution until those awaited promises resolve.&lt;/p&gt;

&lt;p&gt;In one particularly painful debugging session, I discovered that having a chain of modules with top-level await created a cascading delay effect that significantly slowed my application startup.&lt;/p&gt;

&lt;h3&gt;
  
  
  Watch Out for Circular Dependencies
&lt;/h3&gt;

&lt;p&gt;In a complex React application I was developing, I created an unintentional circular dependency between two modules that both used top-level await. The result? A runtime error that was much harder to debug than usual circular dependency issues.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Avoid patterns like this:&lt;/span&gt;
&lt;span class="c1"&gt;// moduleA.js&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dataA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchDataA&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;dataB&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./moduleB.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// moduleB.js&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dataB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchDataB&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;dataA&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./moduleA.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Compatibility in 2025
&lt;/h2&gt;

&lt;p&gt;The good news is that in 2025, top-level await is well-supported across the JavaScript ecosystem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All modern browsers fully support it&lt;/li&gt;
&lt;li&gt;Node.js v16+ has stable support&lt;/li&gt;
&lt;li&gt;Major frameworks like React, Vue, and Angular have embraced it&lt;/li&gt;
&lt;li&gt;Bundlers like Vite, Webpack, and Rollup handle it elegantly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just remember that top-level await only works in ES modules (not CommonJS), so you'll need to use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Files with the &lt;code&gt;.mjs&lt;/code&gt; extension, or&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.js&lt;/code&gt; files with &lt;code&gt;"type": "module"&lt;/code&gt; in your package.json&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why You Should Embrace Top-Level await Today
&lt;/h2&gt;

&lt;p&gt;As I look back at my JavaScript code from just a few years ago, I'm amazed at how much more elegant and maintainable it's become thanks to features like top-level await. It's not just syntactic sugar—it fundamentally changes how we structure our modules.&lt;/p&gt;

&lt;p&gt;By eliminating boilerplate async wrappers, our code becomes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;More readable&lt;/li&gt;
&lt;li&gt;More maintainable&lt;/li&gt;
&lt;li&gt;More directly expressive of our intent&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The best part? This isn't some experimental feature we need to be cautious about anymore. It's a stable part of the language that you can confidently use in production code today.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;If you're still not using top-level await in your projects, start today! Update your Node.js version if needed, ensure you're using ES modules, and begin refactoring those async IIFEs into clean, top-level async operations.&lt;/p&gt;

&lt;p&gt;In my experience, this small change will make a noticeable difference in your code quality and developer happiness. As we move further into 2025 and beyond, I expect top-level await to become as standard in our JavaScript modules as imports themselves.&lt;/p&gt;

&lt;p&gt;Have you started using top-level await in your projects yet? I'd love to hear about your experiences in the comments below!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is immutability?</title>
      <dc:creator>Khriztian Moreno</dc:creator>
      <pubDate>Tue, 20 Oct 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/khriztianmoreno/what-is-immutability-42m</link>
      <guid>https://dev.to/khriztianmoreno/what-is-immutability-42m</guid>
      <description>&lt;p&gt;Immutability in React and Redux&lt;/p&gt;

&lt;p&gt;Immutability can be a confusing topic, and it appears everywhere in React, Redux, and JavaScript in general.&lt;/p&gt;

&lt;p&gt;You may have encountered errors where your &lt;strong&gt;React&lt;/strong&gt; components do not re-render, even though you know you have changed the &lt;em&gt;props&lt;/em&gt;, and someone says, "You should be making immutable state updates." Maybe you or one of your teammates regularly writes &lt;em&gt;reducers&lt;/em&gt; in &lt;strong&gt;Redux&lt;/strong&gt; that mutate the state, and we have to constantly correct them (the &lt;em&gt;reducers&lt;/em&gt;, or our teammates 😄).&lt;/p&gt;

&lt;p&gt;It's complicated. It can be very subtle, especially if you're not sure what to look for. And honestly, if you're not sure why it's important, it's hard to care.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is immutability?
&lt;/h2&gt;

&lt;p&gt;First of all, immutable is the opposite of mutable, and mutable means changeable, modifiable... capable of being altered.&lt;/p&gt;

&lt;p&gt;So, something that is &lt;strong&gt;im&lt;/strong&gt;mutable, then, is something that cannot be changed.&lt;/p&gt;

&lt;p&gt;Taken to the extreme, this means that instead of having traditional variables, you would constantly be creating new values and replacing the old ones. JavaScript is not that extreme, but some languages do not allow mutation (Elixir, Erlang, and ML come to mind).&lt;/p&gt;

&lt;p&gt;While JavaScript is not a purely functional language, it can sometimes pretend to be. Certain operations with arrays in JS are immutable (meaning they return a new array, rather than modifying the original). String operations are always immutable (they create a new string with the changes). And you can also write your own functions that are immutable. You just need to know a few rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  A code example with mutation
&lt;/h2&gt;

&lt;p&gt;Let's look at an example to see how mutability works. We'll start with this person object here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fiv873gqgdmqlppn0wjx2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fiv873gqgdmqlppn0wjx2.png" width="656" height="870"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let's say we write a function that gives a person special powers:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp2wjmsvs89b8691dx4wd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp2wjmsvs89b8691dx4wd.png" width="800" height="621"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, everyone has the same power. No matter, invisibility is cool. Let's give some special powers to &lt;strong&gt;super Mafe.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7qdtskiwbab3a67vu372.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7qdtskiwbab3a67vu372.png" width="800" height="618"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This function giveAwesomePowers &lt;em&gt;mutates&lt;/em&gt; the person object that is passed into it. If you run this code, you'll see that the first time we print a person, &lt;strong&gt;Mafe&lt;/strong&gt; has no specialPower property. But then, the second time, she suddenly has the specialPower of invisibility.&lt;/p&gt;

&lt;p&gt;The object returned by giveAwesomePowers is the same object that was passed in, but it has been modified internally. Its properties have changed. It has been &lt;em&gt;mutated&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I want to say this again because it's important: the internal parts of the object have changed, but the object reference has not. It's the same object on the outside (so an equality check like person === samePerson will be true).&lt;/p&gt;

&lt;p&gt;If we want the giveAwesomePowers function not to modify the person, we'll have to make some changes.&lt;/p&gt;

&lt;p&gt;We won't go into much detail about the following code, because it will take center stage in the next post of this series.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx25evo3ksjq85bfclfjs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx25evo3ksjq85bfclfjs.png" alt="**Object.assign()**" width="800" height="614"&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;Object.assign()&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you haven't seen Object.assign, what it does is assign properties from one object to another. Without modifying the original object, in short, Object.assign creates a copy of the original object with a new specialPower property.&lt;/p&gt;

&lt;p&gt;👋 In my next post, we will address what a &lt;strong&gt;pure function&lt;/strong&gt; is and why it is closely related to immutability, and we will focus on understanding the concept of &lt;strong&gt;side effects&lt;/strong&gt;. 🤝&lt;/p&gt;

&lt;p&gt;I hope this was helpful and/or taught you something new!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" alt="Profile" width="148" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a class="mentioned-user" href="https://dev.to/khriztianmoreno"&gt;@khriztianmoreno&lt;/a&gt; 🚀
&lt;/h4&gt;

</description>
      <category>javascript</category>
      <category>redux</category>
      <category>react</category>
    </item>
    <item>
      <title>The Web Stack You Cannot Ignore in 2026</title>
      <dc:creator>Khriztian Moreno</dc:creator>
      <pubDate>Tue, 20 Oct 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/khriztianmoreno/the-web-stack-you-cannot-ignore-in-2026-1nmh</link>
      <guid>https://dev.to/khriztianmoreno/the-web-stack-you-cannot-ignore-in-2026-1nmh</guid>
      <description>&lt;p&gt;&lt;a href="/posts/the-web-stack-you-cannot-ignore-in-2026.png" class="article-body-image-wrapper"&gt;&lt;img src="/posts/the-web-stack-you-cannot-ignore-in-2026.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After going through roadmaps, specs, Chrome Dev Summit talks, and real signals from production, my prediction is simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Web development in 2026 moves toward more native capabilities, less unnecessary JavaScript, and performance you can measure in the real world.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This isn’t a “cool tools” list.&lt;br&gt;
These are the areas that become &lt;strong&gt;non-optional&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Performance (Core Web Vitals + Soft Navigation)
&lt;/h2&gt;

&lt;p&gt;If you only fix one thing, fix this.&lt;br&gt;
&lt;strong&gt;Performance is the priority. No debate.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why it will be vital in 2026
&lt;/h3&gt;

&lt;p&gt;Google is doubling down on &lt;em&gt;real user experience&lt;/em&gt;, not synthetic benchmarks.&lt;br&gt;
Soft Navigation also changes how modern SPAs (and “MPA-like” apps) are evaluated.&lt;/p&gt;

&lt;p&gt;In 2026:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you don’t improve &lt;strong&gt;INP&lt;/strong&gt; and &lt;strong&gt;LCP&lt;/strong&gt;, you don’t just “lose SEO” — you lose conversions.&lt;/li&gt;
&lt;li&gt;If you don’t measure soft navigations correctly, you’ll ship “faster” routes with &lt;strong&gt;fake metrics&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What changes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;CLS stops being “cosmetic”.&lt;/li&gt;
&lt;li&gt;INP fully replaces the old “FID mindset”.&lt;/li&gt;
&lt;li&gt;SPA performance gets judged like an MPA.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What you should master
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;web-vitals&lt;/code&gt; in production&lt;/li&gt;
&lt;li&gt;Long tasks (and what creates them)&lt;/li&gt;
&lt;li&gt;Soft navigation heuristics&lt;/li&gt;
&lt;li&gt;RUM &amp;gt; Lighthouse&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://web.dev/vitals" rel="noopener noreferrer"&gt;Web Vitals&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.chrome.com/docs/web-platform/soft-navigations-experiment" rel="noopener noreferrer"&gt;Soft Navigation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.chrome.com/docs/crux" rel="noopener noreferrer"&gt;CrUX&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. Identity: Passkeys + FedCM
&lt;/h2&gt;

&lt;p&gt;Traditional login is dying.&lt;br&gt;
It just doesn’t know it yet.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why it will be vital in 2026
&lt;/h3&gt;

&lt;p&gt;Passwords are both a technical and legal liability.&lt;br&gt;
Passkeys reduce friction &lt;em&gt;and&lt;/em&gt; fraud.&lt;br&gt;
FedCM is the browser’s real answer to identity in a world without third‑party cookies.&lt;/p&gt;

&lt;p&gt;In 2026:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A product without passkeys will be perceived as &lt;strong&gt;outdated&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;“Classic OAuth” without FedCM will degrade (or break) flows users care about.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What changes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Passwordless becomes normal.&lt;/li&gt;
&lt;li&gt;Browser-native login UI becomes the expectation.&lt;/li&gt;
&lt;li&gt;Less JS. More platform.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What you should master
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;WebAuthn&lt;/li&gt;
&lt;li&gt;Passkeys UX patterns&lt;/li&gt;
&lt;li&gt;FedCM flows&lt;/li&gt;
&lt;li&gt;Privacy-preserving identity&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.chrome.com/docs/identity/fedcm/overview" rel="noopener noreferrer"&gt;FedCM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.chrome.com/docs/identity/passkeys" rel="noopener noreferrer"&gt;Passkeys&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.chrome.com/docs/identity/webauthn" rel="noopener noreferrer"&gt;WebAuthn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. Fugu / PWA APIs
&lt;/h2&gt;

&lt;p&gt;The web talks to hardware now.&lt;br&gt;
The debate is over — what’s left is execution.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why it will be vital in 2026
&lt;/h3&gt;

&lt;p&gt;Web apps compete directly with native when the capability gap is small.&lt;br&gt;
Browsers keep shipping standards-based APIs, which means fewer dependencies and less glue code.&lt;/p&gt;

&lt;p&gt;In 2026:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;WebUSB, File System Access, and Badging stop being “rare”.&lt;/li&gt;
&lt;li&gt;PWAs feel more and more like first-class apps when the use case fits.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What changes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Real offline capabilities&lt;/li&gt;
&lt;li&gt;Deeper OS integration&lt;/li&gt;
&lt;li&gt;Faster UX without native wrappers&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What you should master
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;File System Access API&lt;/li&gt;
&lt;li&gt;Background Sync&lt;/li&gt;
&lt;li&gt;Badging API&lt;/li&gt;
&lt;li&gt;PWA install heuristics&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.chrome.com/docs/capabilities/" rel="noopener noreferrer"&gt;Web capabilities&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://web.dev/progressive-web-apps/" rel="noopener noreferrer"&gt;Progressive Web Apps&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. AI for Web Developers (Built-in AI APIs)
&lt;/h2&gt;

&lt;p&gt;AI stops being “just a SaaS”.&lt;br&gt;
It becomes part of the browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why it will be vital in 2026
&lt;/h3&gt;

&lt;p&gt;Lower latency.&lt;br&gt;
More privacy (because local is the new default).&lt;br&gt;
And better UX without forcing every product to build an expensive AI backend.&lt;/p&gt;

&lt;p&gt;This is not “embed ChatGPT”.&lt;br&gt;
This is &lt;strong&gt;native AI&lt;/strong&gt;, progressively enhanced.&lt;/p&gt;

&lt;p&gt;In 2026:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On-device AI becomes the default &lt;em&gt;when available&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;AI-driven UX becomes a real differentiator.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What changes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Smaller, faster models running locally&lt;/li&gt;
&lt;li&gt;Fewer external calls&lt;/li&gt;
&lt;li&gt;UI patterns that adapt in context&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What you should master
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;On-device inference constraints (and fallbacks)&lt;/li&gt;
&lt;li&gt;AI UX patterns (assistive, not intrusive)&lt;/li&gt;
&lt;li&gt;Privacy-first AI&lt;/li&gt;
&lt;li&gt;Progressive enhancement with AI&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.chrome.com/docs/ai/" rel="noopener noreferrer"&gt;AI in Chrome&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. DevTools &amp;amp; Browser Automation
&lt;/h2&gt;

&lt;p&gt;Traditional debugging doesn’t scale.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why it will be vital in 2026
&lt;/h3&gt;

&lt;p&gt;Apps get more complex.&lt;br&gt;
Performance issues get more subtle.&lt;br&gt;
And manual testing simply isn’t viable if you want speed &lt;em&gt;and&lt;/em&gt; quality.&lt;/p&gt;

&lt;p&gt;In 2026:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Observability from DevTools becomes a daily habit.&lt;/li&gt;
&lt;li&gt;Automation becomes part of the workflow, not a “QA phase”.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What changes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Smarter DevTools&lt;/li&gt;
&lt;li&gt;More integrated testing&lt;/li&gt;
&lt;li&gt;Debugging centered on real UX&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What you should master
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Advanced Performance panel workflows&lt;/li&gt;
&lt;li&gt;Lighthouse CI&lt;/li&gt;
&lt;li&gt;Puppeteer / Playwright&lt;/li&gt;
&lt;li&gt;Tracing and deep profiling&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.chrome.com/docs/devtools/" rel="noopener noreferrer"&gt;Chrome DevTools&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.chrome.com/docs/lighthouse/" rel="noopener noreferrer"&gt;Lighthouse&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  My final prediction (no marketing)
&lt;/h2&gt;

&lt;p&gt;If I had to bet on only one foundation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Performance + Identity&lt;/strong&gt; will be the base.&lt;br&gt;
Everything else sits on top of that.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The web in 2026 will be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;More native&lt;/li&gt;
&lt;li&gt;Faster&lt;/li&gt;
&lt;li&gt;More private&lt;/li&gt;
&lt;li&gt;Less dependent on “framework magic”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rest is noise.&lt;/p&gt;

&lt;p&gt;I hope this has been helpful and/or taught you something new!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" alt="Profile" width="148" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a class="mentioned-user" href="https://dev.to/khriztianmoreno"&gt;@khriztianmoreno&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Until next time.&lt;/p&gt;

</description>
      <category>webperf</category>
      <category>identity</category>
      <category>pwa</category>
      <category>ai</category>
    </item>
    <item>
      <title>How to mock an HTTP request with Jest 💻</title>
      <dc:creator>Khriztian Moreno</dc:creator>
      <pubDate>Tue, 20 Oct 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/khriztianmoreno/how-to-mock-an-http-request-with-jest-3k3g</link>
      <guid>https://dev.to/khriztianmoreno/how-to-mock-an-http-request-with-jest-3k3g</guid>
      <description>&lt;p&gt;Today I wanted to show you how to properly write a test.&lt;/p&gt;

&lt;p&gt;But anyone can figure out how to run a simple test. And here, we're looking to help you find answers you won't find anywhere else.&lt;/p&gt;

&lt;p&gt;So I thought we'd take things one step further.&lt;/p&gt;

&lt;p&gt;Let's run a more complex test, where you'll have to mock 1 or 2 parts of the function you're testing.&lt;/p&gt;

&lt;p&gt;[In case you're new here: mock is like using a stunt double in a movie. It's a way to replace a complicated part of your code (like calling an API) with something simpler that pretends to be real, so you can test the rest of your code easily.]&lt;/p&gt;

&lt;p&gt;MY testing framework of choice is Jest, because it makes everything so much easier:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Zero Configuration:&lt;/strong&gt; One of the main advantages of Jest is its zero-configuration setup. It is designed to work out of the box with minimal configuration, making it very attractive for projects that want to implement tests quickly and efficiently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Snapshot Testing:&lt;/strong&gt; Jest introduced the concept of Snapshot Testing, which is particularly useful for testing UI components. It takes a snapshot of a component’s rendered output and ensures that it doesn’t change unexpectedly in future tests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Built-In Mocking and Spies:&lt;/strong&gt; Jest comes with built-in support for mock functions, modules, and timers, making it easy to test components or functions in isolation without worrying about their dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Asynchronous Testing Support:&lt;/strong&gt; Jest supports asynchronous testing out of the box, which is essential for testing in modern JavaScript applications that often rely on asynchronous operations like API calls or database queries.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fau3m8kw0rtsrclm6s8fo.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fau3m8kw0rtsrclm6s8fo.jpg" alt=" " width="540" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;De todos modos, entremos en las pruebas:&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Setting up your project
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create a new project directory and navigate to it&lt;/li&gt;
&lt;li&gt;Initialize a new npm project: &lt;code&gt;npm init -y&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Install Jest: &lt;code&gt;npm install --save-dev jest&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Install axios to make HTTP requests: &lt;code&gt;npm install axios&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These are the basic requirements. Nothing new or fancy here. Let's get started.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Write a function with an API call
&lt;/h3&gt;

&lt;p&gt;Now, let's say you log into some kind of application. StackOverflow, for example. Most likely, at the top right you'll see information about your profile. Maybe your full name and username, for example.&lt;/p&gt;

&lt;p&gt;In order to get these, we typically have to make an API call to get them. So, let's see how we would do that.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a file called &lt;code&gt;user.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Inside &lt;code&gt;user.js&lt;/code&gt;, write a function that makes an API call. For example, using axios to retrieve user data:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// user.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`https://api.example.com/users/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Create the Test File
&lt;/h3&gt;

&lt;p&gt;Okay, now that we have a function that brings us the user based on the ID we requested, let's see how we can test it.&lt;/p&gt;

&lt;p&gt;Remember, we want something that works always and for all developers.&lt;/p&gt;

&lt;p&gt;Which means we don't want to depend on whether the server is running or not (since this is not what we are testing).&lt;/p&gt;

&lt;p&gt;And we don't want to depend on the users we have in the database.&lt;/p&gt;

&lt;p&gt;Because in my database, ID1 could belong to my admin user, while in your database, ID1 could belong to YOUR admin user.&lt;/p&gt;

&lt;p&gt;This means that the same function would give us different results. Which would cause the test to fail, even though the function works correctly.&lt;/p&gt;

&lt;p&gt;Read on to see how we tackled this problem using mocks.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a file called &lt;code&gt;user.test.js&lt;/code&gt; in the same directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside this file, import the function you want to test:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getUser&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Write your test case, mock the call, and retrieve mock data.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;should fetch user data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Mock data to be returned by the Axios request&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mockUserData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mockResolvedValue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;mockUserData&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="c1"&gt;// Call the function&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Assert that the Axios get method was called correctly&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://api.example.com/users/1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Assert that the function returned the correct data&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockUserData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: Run the test
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Add a test script to your &lt;code&gt;package.json&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"jest"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Run your tests with &lt;code&gt;npm test&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Step 5: Review the results
&lt;/h4&gt;

&lt;p&gt;Jest will display the result of your test in the terminal. The test should pass, indicating that &lt;code&gt;getUser&lt;/code&gt; is returning the mocked data as expected.&lt;/p&gt;

&lt;p&gt;Congratulations, you now have a working test with Jest and Mocking.&lt;/p&gt;

&lt;p&gt;I hope this was helpful and/or made you learn something new!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" alt="Profile" width="148" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a class="mentioned-user" href="https://dev.to/khriztianmoreno"&gt;@khriztianmoreno&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>testing</category>
      <category>node</category>
      <category>jest</category>
    </item>
    <item>
      <title>Rust is revolutionizing JavaScript development!</title>
      <dc:creator>Khriztian Moreno</dc:creator>
      <pubDate>Tue, 20 Oct 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/khriztianmoreno/rust-is-revolutionizing-javascript-development-1541</link>
      <guid>https://dev.to/khriztianmoreno/rust-is-revolutionizing-javascript-development-1541</guid>
      <description>&lt;p&gt;&lt;a href="https://rspack.dev/" rel="noopener noreferrer"&gt;Rspack&lt;/a&gt; just released &lt;a href="https://rspack.dev/blog/announcing-1-0" rel="noopener noreferrer"&gt;version 1.0 &lt;/a&gt;, and the dream of using Rust-based build tools to speed up the JavaScript ecosystem is more alive than ever. How did we get here? Early last year, a team of developers at ByteDance was facing performance issues maintaining the company’s “many large monolithic applications.” So they did what any good developer would do: they blamed webpack. But they didn’t stop there. In March 2023, they released Rspack v0.1, a high-performance JavaScript bundler written in Rust and designed to be fully compatible with the webpack ecosystem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.rspack.dev%2Frspack%2Frspack-logo.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.rspack.dev%2Frspack%2Frspack-logo.svg" alt="Rspack" width="578" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fast forward to today, and Rspack now has 100k weekly downloads and has introduced key improvements that make it production-ready:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Better performance: New features like lazy compilation and other performance optimizations make Rspack 1.0 build times over &lt;a href="https://github.com/rspack-contrib/performance-compare" rel="noopener noreferrer"&gt;20x faster&lt;/a&gt; than webpack 5.&lt;/li&gt;
&lt;li&gt;Increased webpack compatibility: Over 80% of the top 50 downloaded webpack plugins can now be used in Rspack, bringing it closer to becoming a true drop-in replacement for webpack.&lt;/li&gt;
&lt;li&gt;Reduced complexity: They created a new toolchain called Rstack that includes separate projects like Rsbuild, Rspress, and Rslib, each targeting different use cases. This reduces the complexity of setting up an all-in-one tool like Rspack (or webpack), while still maintaining flexibility.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Bottom line: Rspack offers a pretty simple value proposition for developers: if you already use webpack, it will make it much easier for you to migrate to its bundler which is faster, easier to use, and still fully compatible with the webpack API. Time will tell if that will be enough to convince the masses to try it out.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Streaming AI without destroying your INP</title>
      <dc:creator>Khriztian Moreno</dc:creator>
      <pubDate>Tue, 20 Oct 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/khriztianmoreno/streaming-ai-without-destroying-your-inp-27no</link>
      <guid>https://dev.to/khriztianmoreno/streaming-ai-without-destroying-your-inp-27no</guid>
      <description>&lt;p&gt;Your AI feature is the new INP culprit.&lt;/p&gt;

&lt;p&gt;For years, the usual suspects for a bad &lt;a href="https://web.dev/articles/inp" rel="noopener noreferrer"&gt;Interaction to Next Paint&lt;/a&gt; score were the same: bloated bundles, unthrottled scroll handlers, a chart library re-rendering on every mouse move. Now there's a new one, and it's the feature everyone is shipping right now — streaming a language model's answer straight into the DOM.&lt;/p&gt;

&lt;p&gt;This post has a full, runnable demo behind it — &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/tree/main/demos/streaming-ai-without-destroying-your-inp" rel="noopener noreferrer"&gt;&lt;code&gt;demos/streaming-ai-without-destroying-your-inp&lt;/code&gt;&lt;/a&gt;, in the repo that powers this site. It needs Chrome flags most readers won't have enabled, so there's no hosted version to click into — clone it (or just read the files on GitHub) and run it locally per the &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/streaming-ai-without-destroying-your-inp/README.md" rel="noopener noreferrer"&gt;README&lt;/a&gt;. Rather than pasting isolated snippets, I'm going to walk through the actual files: a naive implementation that wrecks your INP, and an optimized one that doesn't, both wired to a live INP readout so the difference is something you can watch happen, not just take my word for.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the demo works
&lt;/h2&gt;

&lt;p&gt;Run it locally and you'll see three things: a prompt box with a strategy switch above it, an output area, and an INP log below. The flow is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Pick a strategy&lt;/strong&gt; — "Naive (per-chunk, no yield)" or "Optimized (sentence batching + yield)" — with the same prompt in both.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hit Generate.&lt;/strong&gt; The model starts streaming into the output area using whichever strategy you picked.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;While it's still generating, click "🔘 Click me while it's generating"&lt;/strong&gt; a few times. That button does nothing on purpose — its only job is to give you something to interact with mid-stream, the same way a user would tap "stop" or scroll while your chat UI is still talking.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Watch the INP log.&lt;/strong&gt; Every click reports its measured latency there, in real time, via &lt;code&gt;web-vitals&lt;/code&gt;' &lt;code&gt;onINP&lt;/code&gt; — no DevTools required, though you should open DevTools' Performance panel too for the full trace.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hit Stop&lt;/strong&gt; any time to see the &lt;code&gt;AbortController&lt;/code&gt; actually cancel the in-flight generation, instead of leaving a zombie session writing to a component nobody's looking at anymore.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Run the naive strategy first, mash the probe button, and watch the log fill with high numbers. Then switch to optimized, same prompt, and watch the same clicks come back low. That side-by-side is the whole point of the demo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why streaming text breaks INP
&lt;/h2&gt;

&lt;p&gt;On-device models like Gemini Nano expose their output through the &lt;a href="https://developer.chrome.com/docs/ai/prompt-api" rel="noopener noreferrer"&gt;Prompt API&lt;/a&gt;'s &lt;code&gt;promptStreaming()&lt;/code&gt;, which returns a &lt;code&gt;ReadableStream&lt;/code&gt; of text chunks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;LanguageModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;promptStreaming&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Explain quantum entanglement.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;await &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;chunk&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things about that loop are easy to miss and both matter for performance:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Each &lt;code&gt;chunk&lt;/code&gt; is an &lt;strong&gt;incremental delta&lt;/strong&gt;, not the full text so far. You have to concatenate them yourself to reconstruct the answer.&lt;/li&gt;
&lt;li&gt;Chunks arrive fast — often several per second — and the obvious thing to do with each one is to append it to the DOM immediately, so the text "types itself" on screen.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That second instinct is exactly what the demo's &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/streaming-ai-without-destroying-your-inp/naive.js" rel="noopener noreferrer"&gt;&lt;code&gt;naive.js&lt;/code&gt;&lt;/a&gt; implements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;naiveStreamAnswer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onChunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;LanguageModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;promptStreaming&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;await &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;delta&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;onChunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every DOM write triggered by &lt;code&gt;onChunk&lt;/code&gt; is a task on the main thread: a text node mutation, a reflow, sometimes a re-render if you're piping the chunk through a framework's state. Do that dozens of times per second and you've built a wall of small-but-frequent tasks that never lets the main thread breathe. If the user clicks "stop" or types into another field while the model is still talking, that input sits in the queue behind your DOM churn — and that queueing delay is exactly what INP measures.&lt;/p&gt;

&lt;p&gt;The fix isn't "stream slower." It's controlling &lt;strong&gt;where&lt;/strong&gt; you flush to the DOM and &lt;strong&gt;how often&lt;/strong&gt; you hand control back to the browser — which is what the demo's other implementation, &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/streaming-ai-without-destroying-your-inp/lib.js" rel="noopener noreferrer"&gt;&lt;code&gt;lib.js&lt;/code&gt;&lt;/a&gt;, does.&lt;/p&gt;

&lt;h2&gt;
  
  
  The building blocks
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Batch by sentence, not by token
&lt;/h3&gt;

&lt;p&gt;Painting per-token feels responsive in a demo, but it's the most expensive possible cadence — and for languages like Spanish, breaking mid-word or mid-punctuation looks broken. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter" rel="noopener noreferrer"&gt;&lt;code&gt;Intl.Segmenter&lt;/code&gt;&lt;/a&gt; (Baseline since April 2024) gives you locale-aware sentence boundaries for free:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;segmenter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Intl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Segmenter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;es&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;granularity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sentence&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;segment&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;segmenter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;segment&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// one full sentence at a time&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Segmenting by sentence means one DOM write per sentence instead of one per token — often an order of magnitude fewer updates — and it respects abbreviations and punctuation rules per locale, which a naive &lt;code&gt;text.split('. ')&lt;/code&gt; does not.&lt;/p&gt;

&lt;p&gt;Here's what that looks like on an actual run, prompting the demo with "Write two short sentences describing a sunrise over the mountains." Gemini Nano streamed back 37 deltas — mostly word fragments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Golden" " light" " kissed" " the" " jagged" " peaks" "," " slowly"
" chasing" " away" " the" " lingering" " shadows" " of" " night" "."
"  " "Warm" " hues" " painted" " the" " sky" "," " transforming"
" the" " mountains" " into" " majestic" " silhouettes" " bathed"
" in" " a" " new" " dawn" "." " " "\n\n\n\n"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The naive strategy turns every one of those into its own DOM write — 37 tasks. Run the exact same deltas through the sentence-batching logic above and you get 5 writes, 2 of which carry all the actual content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. "Golden light kissed the jagged peaks, slowly chasing away the lingering shadows of night.  "
2. "Warm hues painted the sky, transforming the mountains into majestic silhouettes bathed in a new dawn. \n"
3. "\n"   4. "\n"   5. "\n"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same model output, same total text — 37 DOM writes down to 5, and the last three are trailing newlines the model tacked on at the end, not meaningful content. That's the entire optimization in one concrete before/after.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Give the main thread room to breathe
&lt;/h3&gt;

&lt;p&gt;Even at sentence granularity, a long answer can still queue up enough DOM writes to block input. Between updates, &lt;code&gt;lib.js&lt;/code&gt; hands control back to the browser with &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Scheduler/yield" rel="noopener noreferrer"&gt;&lt;code&gt;scheduler.yield()&lt;/code&gt;&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;yieldToMain&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;globalThis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scheduler&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="k"&gt;yield&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;scheduler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;yield&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;scheduler.yield()&lt;/code&gt; isn't Baseline yet, so the fallback matters — without it, this whole strategy silently stops working on browsers that don't support it. The point of yielding isn't to slow the stream down; it's to insert a checkpoint where a pending click or keystroke can be processed before you resume writing.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Let people cancel it
&lt;/h3&gt;

&lt;p&gt;An &lt;code&gt;AbortController&lt;/code&gt; should exist from the moment generation starts, not as an afterthought — a model that keeps flushing sentences to a component the user has already navigated away from is its own kind of bug. In the demo, &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/streaming-ai-without-destroying-your-inp/main.js" rel="noopener noreferrer"&gt;&lt;code&gt;main.js&lt;/code&gt;&lt;/a&gt; creates the controller on every "Generate" click and wires the "Stop" button straight to &lt;code&gt;controller.abort()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Putting it together
&lt;/h2&gt;

&lt;p&gt;This is the full &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/streaming-ai-without-destroying-your-inp/lib.js" rel="noopener noreferrer"&gt;&lt;code&gt;lib.js&lt;/code&gt;&lt;/a&gt; — sentence batching, yielding, and cancellation, in one function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;streamAnswer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onSentence&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;LanguageModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;segmenter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Intl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Segmenter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;es&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;granularity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sentence&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;promptStreaming&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;cursor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;await &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;delta&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// The last segment in a partial buffer might still be growing,&lt;/span&gt;
    &lt;span class="c1"&gt;// so only flush the ones that are already complete.&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;segments&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;segmenter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;complete&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;segments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;segment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;yieldToMain&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="nf"&gt;onSentence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;segment&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;cursor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;segment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;onSentence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One honest caveat, straight from testing this in the demo: re-segmenting the whole buffer on every chunk means the cost grows with the length of the answer, and &lt;code&gt;Intl.Segmenter&lt;/code&gt; can occasionally revise where a boundary falls once more text arrives (an abbreviation like "Dr." resolving differently once the next word shows up). For sentence-level batching this is rarely noticeable, but it's worth knowing before you assume the output is byte-identical to segmenting the final text in one pass.&lt;/p&gt;

&lt;p&gt;The on-screen INP log comes from &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/streaming-ai-without-destroying-your-inp/metrics.js" rel="noopener noreferrer"&gt;&lt;code&gt;metrics.js&lt;/code&gt;&lt;/a&gt;, which is short enough to paste in full — it just wires &lt;code&gt;web-vitals&lt;/code&gt;' &lt;code&gt;onINP&lt;/code&gt; straight to a &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;onINP&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://unpkg.com/web-vitals@5?module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;onINP&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)?.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;tagName&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;unknown target&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;li&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;ms — &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;inp-log&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)?.&lt;/span&gt;&lt;span class="nf"&gt;prepend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;LanguageModel&lt;/code&gt; isn't defined yet in your browser when you run it, the page will tell you — the &lt;a href="https://github.com/khriztianmoreno/khriztianmoreno.dev/blob/main/demos/streaming-ai-without-destroying-your-inp/README.md" rel="noopener noreferrer"&gt;README&lt;/a&gt; covers the Chrome flag you need.&lt;/p&gt;

&lt;h2&gt;
  
  
  Measuring before and after
&lt;/h2&gt;

&lt;p&gt;Don't ship this on faith — profile it. When I ran the naive strategy in this demo and let a long answer stream while clicking the probe button, DevTools' Performance panel showed a dense row of small tasks in the main thread track, back-to-back, no gaps — and the probe clicks logged high in the INP log. Switching to the optimized strategy with the same prompt, the trace shows short tasks separated by visible gaps, and the same probe clicks come back low.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;metric.entries&lt;/code&gt; in the &lt;code&gt;onINP&lt;/code&gt; callback points you straight at the specific interaction that was slow and which task blocked it — that's your evidence, not a guess, for whether the batching actually helped. Run it yourself with your own prompts; the numbers will vary by device and answer length, but the shape of the two traces won't.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's next
&lt;/h2&gt;

&lt;p&gt;This same shape — small, frequent updates fighting the main thread — shows up anywhere a model streams into a live page, not just chat UIs. Chrome's built-in AI stack keeps expanding (the &lt;a href="https://developer.chrome.com/docs/ai/rewriter-api" rel="noopener noreferrer"&gt;Rewriter API&lt;/a&gt; for on-device rewriting, &lt;a href="https://developer.chrome.com/docs/ai/webmcp" rel="noopener noreferrer"&gt;WebMCP&lt;/a&gt; for exposing page actions to agents, both covered at &lt;a href="https://developer.chrome.com/blog/chrome-at-io26" rel="noopener noreferrer"&gt;Chrome at I/O '26&lt;/a&gt;), and every one of them will hand you output the same way: incrementally, on your terms for when to paint it.&lt;/p&gt;

&lt;p&gt;The API gives you the tokens. Keeping INP healthy is still your job.&lt;/p&gt;

&lt;p&gt;I hope this has been helpful and/or taught you something new!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rdbt84oh93va3vi70m5.png" alt="Profile"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a class="mentioned-user" href="https://dev.to/khriztianmoreno"&gt;@khriztianmoreno&lt;/a&gt; 🚀&lt;/p&gt;

</description>
      <category>webperf</category>
      <category>corewebvitals</category>
      <category>inp</category>
      <category>chromeai</category>
    </item>
  </channel>
</rss>
