<?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: Ganesh Joshi</title>
    <description>The latest articles on DEV Community by Ganesh Joshi (@ganeshjoshi).</description>
    <link>https://dev.to/ganeshjoshi</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%2F3812945%2F5c98e077-4bc4-4521-8bf1-006bca4726f1.png</url>
      <title>DEV Community: Ganesh Joshi</title>
      <link>https://dev.to/ganeshjoshi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ganeshjoshi"/>
    <language>en</language>
    <item>
      <title>How the JavaScript engine works: execution context, call stack and the event loop</title>
      <dc:creator>Ganesh Joshi</dc:creator>
      <pubDate>Sat, 05 Dec 2026 05:51:32 +0000</pubDate>
      <link>https://dev.to/ganeshjoshi/how-the-javascript-engine-works-execution-context-call-stack-and-the-event-loop-3fa</link>
      <guid>https://dev.to/ganeshjoshi/how-the-javascript-engine-works-execution-context-call-stack-and-the-event-loop-3fa</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was created with AI assistance and reviewed for accuracy before publishing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most JavaScript bugs that feel mysterious are not mysterious. A variable that is &lt;code&gt;undefined&lt;/code&gt; when you were sure it had a value, a &lt;code&gt;setTimeout&lt;/code&gt; of zero milliseconds that runs after a promise, a "Maximum call stack size exceeded" error from a function that looks fine: each one is the engine doing exactly what its rules say. The rules are just rarely shown in one place.&lt;/p&gt;

&lt;p&gt;This post walks through those rules in the order the engine meets them. First, what an engine is and what it does to your source. Then the global execution context and how functions get their own. Then the call stack, the heap and the event loop, which together decide &lt;em&gt;when&lt;/em&gt; your code runs. At the end there is a visualizer that lets you step through a script one move at a time, so you can watch all of it happen instead of taking my word for it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a JavaScript engine is
&lt;/h2&gt;

&lt;p&gt;An engine is the program that reads JavaScript text and runs it. Chrome, Edge and Node.js use V8. Firefox uses SpiderMonkey. Safari and Bun use JavaScriptCore. They differ a lot inside, but they all implement the same language specification, so they agree on what your code means.&lt;/p&gt;

&lt;p&gt;Two things are worth separating early, because people blur them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The engine&lt;/strong&gt; runs the language: variables, functions, objects, promises, the call stack.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The environment&lt;/strong&gt; wraps the engine and adds things that are not part of the language: &lt;code&gt;setTimeout&lt;/code&gt;, &lt;code&gt;fetch&lt;/code&gt;, the DOM, &lt;code&gt;console&lt;/code&gt;, file access. The browser is one environment. Node.js is another.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;setTimeout&lt;/code&gt; is not in the JavaScript specification at all. The browser provides it, and then hands the engine a callback to run later. That split is the key to the event loop, which comes later in this post.&lt;/p&gt;

&lt;h2&gt;
  
  
  From source text to running code
&lt;/h2&gt;

&lt;p&gt;When the engine receives your script it does not run the text directly. It turns it into something cheaper to execute, in stages. The details below describe V8, the most documented engine, and the other engines follow the same broad shape.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Parsing.&lt;/strong&gt; A scanner splits the text into tokens (&lt;code&gt;const&lt;/code&gt;, &lt;code&gt;total&lt;/code&gt;, &lt;code&gt;=&lt;/code&gt;, &lt;code&gt;42&lt;/code&gt;). A parser then arranges the tokens into an &lt;strong&gt;abstract syntax tree&lt;/strong&gt;, a tree of nodes such as a variable declaration containing an identifier and a literal. A syntax error is found here, which is why a stray bracket stops the whole file before a single line runs. Engines also parse lazily: V8 only skims a function's body with a lighter preparser, which still catches syntax errors, and parses it fully when the function is first called, because most code on a page never runs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Bytecode.&lt;/strong&gt; The tree is compiled into bytecode, a compact instruction list for a simple virtual machine. V8's interpreter, Ignition, runs it. Starting here keeps start-up fast and memory low. Functions that run a few times get compiled quickly into plain machine code by a baseline compiler called Sparkplug.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Optimisation.&lt;/strong&gt; While the bytecode runs, the engine watches which functions are hot, meaning called often, and which types they see. A function that has only ever added two small integers can be recompiled into machine code that assumes exactly that. V8's optimising compilers (Maglev, a fast mid tier, and TurboFan, the top tier) do this. If the assumption breaks, say the function is suddenly called with a string, the engine throws the optimised code away and falls back to a lower tier. This is called deoptimisation, and it is why code that keeps the same shapes and types tends to be faster.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Garbage collection.&lt;/strong&gt; Objects live on the heap, and nobody frees them by hand. The collector finds objects that can no longer be reached from your running code and reclaims them. V8 splits the heap into a young and an old generation, because most objects die young, and collects the young one often and cheaply (a scavenge), leaving the full mark-and-compact pass over the whole heap for less often.&lt;/p&gt;

&lt;p&gt;You can look at stage one yourself. Paste any script into the &lt;a href="https://dev.to/tools/javascript-ast-explorer"&gt;JavaScript AST explorer&lt;/a&gt; and it shows the syntax tree the parser builds, and selecting text in the editor highlights the matching node. It is a quick way to see that &lt;code&gt;a + b * c&lt;/code&gt; is not a line of text to the engine but a tree with the multiplication nested below the addition.&lt;/p&gt;

&lt;h2&gt;
  
  
  The global execution context
&lt;/h2&gt;

&lt;p&gt;Everything your code does happens inside an &lt;strong&gt;execution context&lt;/strong&gt;: the record the engine keeps about the code that is running right now. It holds which variables exist, what &lt;code&gt;this&lt;/code&gt; is, and which outer scope to look in when a name is not found locally.&lt;/p&gt;

&lt;p&gt;When a script starts, the engine creates the &lt;strong&gt;global execution context&lt;/strong&gt; first. It is created in two phases, and the split explains most of what people call hoisting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creation phase.&lt;/strong&gt; Before running a single line, the engine scans the code and sets things up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It creates the global object (&lt;code&gt;window&lt;/code&gt; in a browser, &lt;code&gt;globalThis&lt;/code&gt; in general) and sets &lt;code&gt;this&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Function declarations are stored in full, so you can call them before the line where they are written.&lt;/li&gt;
&lt;li&gt;Variables declared with &lt;code&gt;var&lt;/code&gt; are registered and set to &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Variables declared with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are registered but left &lt;em&gt;uninitialised&lt;/em&gt;. They exist, but touching them is an error until the declaration line runs. That gap is called the temporal dead zone.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Execution phase.&lt;/strong&gt; Now the engine runs the code line by line, assigning values as it goes.&lt;/p&gt;

&lt;p&gt;Here is why that matters:&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="nb"&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="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// undefined, not an error&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nb"&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="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: Cannot access 'total' before initialization&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;sayHi&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="c1"&gt;// works, the whole function was stored in the creation phase&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHi&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="nb"&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="s1"&gt;'hi'&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;Nothing was "moved to the top". The engine had already registered &lt;code&gt;count&lt;/code&gt; during creation, with the value &lt;code&gt;undefined&lt;/code&gt;, and had registered &lt;code&gt;total&lt;/code&gt; as unusable. "Hoisting" is just the visible effect of the creation phase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function execution contexts
&lt;/h2&gt;

&lt;p&gt;Every time you call a function, the engine creates a &lt;strong&gt;function execution context&lt;/strong&gt; for that call, with the same two phases. The creation phase registers the function's parameters, its own &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt; and inner function declarations, and works out &lt;code&gt;this&lt;/code&gt;. The execution phase then runs the body.&lt;/p&gt;

&lt;p&gt;Each context also carries a reference to the context it was &lt;em&gt;defined&lt;/em&gt; in. That reference is the &lt;strong&gt;scope chain&lt;/strong&gt;. When your code uses a name, the engine looks in the current context, then in the one it was defined inside, then outward until it reaches the global context. If the name is nowhere, you get a &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Note the word &lt;em&gt;defined&lt;/em&gt;. Lookup follows where the function was written, not where it was called from. That single rule is what makes closures work:&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;makeCounter&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;let&lt;/span&gt; &lt;span class="n"&gt;count&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;return&lt;/span&gt; &lt;span class="kd"&gt;function&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="n"&gt;count&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;return&lt;/span&gt; &lt;span class="n"&gt;count&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;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;makeCounter&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="nb"&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="nf"&gt;next&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;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="nb"&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="nf"&gt;next&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;span class="c1"&gt;// 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;makeCounter&lt;/code&gt; returns, its context is finished, yet &lt;code&gt;count&lt;/code&gt; is still alive. The inner function holds a reference to the scope it was defined in, so the engine keeps that scope on the heap for as long as the function can still be reached. That retained scope is the closure.&lt;/p&gt;

&lt;h2&gt;
  
  
  The call stack
&lt;/h2&gt;

&lt;p&gt;The engine can do one thing at a time, and it needs to remember where to go back to when a function finishes. It does that with the &lt;strong&gt;call stack&lt;/strong&gt;: a last-in, first-out list of execution contexts.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The global context is pushed first and stays at the bottom.&lt;/li&gt;
&lt;li&gt;Calling a function pushes a new context on top.&lt;/li&gt;
&lt;li&gt;When a function returns, its context is popped and the engine resumes in the one beneath.&lt;/li&gt;
&lt;li&gt;The context on top is the code running right now.
&lt;/li&gt;
&lt;/ul&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;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&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;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&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="nf"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&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;function&lt;/span&gt; &lt;span class="nf"&gt;printSquare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&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="nf"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&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;span class="nf"&gt;printSquare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&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;When &lt;code&gt;printSquare(4)&lt;/code&gt; runs, the stack grows to four frames at its tallest: the global context, then &lt;code&gt;printSquare&lt;/code&gt;, then &lt;code&gt;square&lt;/code&gt;, then &lt;code&gt;multiply&lt;/code&gt;. It then unwinds as each call returns, and &lt;code&gt;console.log&lt;/code&gt; gets its own short-lived frame on the way down. The stack traces you see in error messages are a printout of this list at the moment something went wrong.&lt;/p&gt;

&lt;p&gt;The stack has a fixed size. A function that calls itself with no way to stop keeps pushing frames until the engine runs out:&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;forever&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="nf"&gt;forever&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;span class="nf"&gt;forever&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="c1"&gt;// RangeError: Maximum call stack size exceeded&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact limit depends on the engine and the size of each frame, but it is in the thousands to tens of thousands of calls, not millions. When you see that error, the fix is almost always a missing base case, or a loop that should replace deep recursion.&lt;/p&gt;

&lt;p&gt;Because there is exactly one stack, JavaScript is &lt;strong&gt;single-threaded&lt;/strong&gt;: one piece of code runs at a time, and nothing can interrupt it. A long loop on the stack blocks everything, including clicks and painting, until it finishes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The heap
&lt;/h2&gt;

&lt;p&gt;The stack holds the &lt;em&gt;what is running&lt;/em&gt; records. The &lt;strong&gt;heap&lt;/strong&gt; holds the objects, arrays and functions those records point to. A variable that holds an object really holds a reference, a pointer into the heap. That is why assigning an object to a second variable does not copy it, and why a closure can keep an object alive after the function that made it has left the stack.&lt;/p&gt;

&lt;p&gt;Primitive values such as numbers and booleans are small and are stored directly, while objects go to the heap and are reclaimed by the garbage collector once nothing can reach them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Waiting without blocking: the event loop
&lt;/h2&gt;

&lt;p&gt;If the engine can only run one thing at a time, how does a page fetch data or wait a second without freezing? It hands the waiting to the environment and carries on.&lt;/p&gt;

&lt;p&gt;When you call &lt;code&gt;setTimeout&lt;/code&gt;, the engine does not wait. It asks the browser to start a timer, which runs &lt;em&gt;outside&lt;/em&gt; the JavaScript thread, in what is usually drawn as the &lt;strong&gt;Web APIs&lt;/strong&gt; area. The engine finishes its current work. When the timer expires, the browser does not run your callback directly, because the stack might be busy. It places the callback in a queue.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;event loop&lt;/strong&gt; is the rule that moves work from the queues to the stack. It runs forever, and each turn goes roughly like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run the current script or task until the call stack is empty.&lt;/li&gt;
&lt;li&gt;Drain the &lt;strong&gt;microtask queue&lt;/strong&gt;: run every microtask, including any new ones added while draining, until it is empty.&lt;/li&gt;
&lt;li&gt;Let the browser render, if it needs to.&lt;/li&gt;
&lt;li&gt;Take the &lt;strong&gt;next task&lt;/strong&gt; from the task queue and run it, then go back to step 2.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are two queues, and the difference between them is the most important detail in this whole topic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;task queue&lt;/strong&gt; (also called the macrotask queue) holds things like timer callbacks, user events and message events. One task runs per turn of the loop.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;microtask queue&lt;/strong&gt; holds promise reactions (&lt;code&gt;.then&lt;/code&gt;, &lt;code&gt;.catch&lt;/code&gt;, &lt;code&gt;.finally&lt;/code&gt;, and the code after an &lt;code&gt;await&lt;/code&gt;) and &lt;code&gt;queueMicrotask&lt;/code&gt; callbacks. The whole queue is emptied after the current script or task, before the loop touches the next task or lets the browser paint.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So microtasks always jump ahead of tasks. Try to predict this before reading on:&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="nb"&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="s1"&gt;'start'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nb"&gt;setTimeout&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&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="s1"&gt;'timeout'&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;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;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="p"&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="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="nb"&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="s1"&gt;'promise'&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;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nb"&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="s1"&gt;'end'&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 output is &lt;code&gt;start&lt;/code&gt;, &lt;code&gt;end&lt;/code&gt;, &lt;code&gt;promise&lt;/code&gt;, &lt;code&gt;timeout&lt;/code&gt;. Here is why:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;console.log('start')&lt;/code&gt; runs on the stack and prints immediately.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setTimeout&lt;/code&gt; hands a zero-millisecond timer to the Web APIs. The timer expires almost at once, and its callback goes into the &lt;strong&gt;task queue&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Promise.resolve().then(...)&lt;/code&gt; has an already-resolved promise, so its callback goes into the &lt;strong&gt;microtask queue&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log('end')&lt;/code&gt; prints. The script is finished and the stack is empty.&lt;/li&gt;
&lt;li&gt;The loop drains the microtask queue first, so &lt;code&gt;promise&lt;/code&gt; prints.&lt;/li&gt;
&lt;li&gt;Only then does the loop take the next task, so &lt;code&gt;timeout&lt;/code&gt; prints.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The zero in &lt;code&gt;setTimeout(fn, 0)&lt;/code&gt; does not mean "now". It means "as soon as the loop gets to the task queue and the stack is empty", and by then every microtask has already run. It is also a minimum. A busy stack makes the callback late, and browsers apply a minimum delay to timers that are nested deeply or running in background tabs.&lt;/p&gt;

&lt;h3&gt;
  
  
  async and await are promises underneath
&lt;/h3&gt;

&lt;p&gt;An &lt;code&gt;async&lt;/code&gt; function runs like a normal function until its first &lt;code&gt;await&lt;/code&gt;. At that point it pauses, the rest of the function is scheduled as a microtask to continue once the awaited promise settles, and control returns to whoever called 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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;a&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="nb"&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="s1"&gt;'a1'&lt;/span&gt;&lt;span class="p"&gt;)&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;b&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="nb"&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="s1"&gt;'a2'&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;b&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="nb"&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="s1"&gt;'b'&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="nb"&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="s1"&gt;'s'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;a&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="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="p"&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="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="nb"&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="s1"&gt;'p'&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="nb"&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="s1"&gt;'e'&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 output is &lt;code&gt;s&lt;/code&gt;, &lt;code&gt;a1&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, &lt;code&gt;e&lt;/code&gt;, &lt;code&gt;a2&lt;/code&gt;, &lt;code&gt;p&lt;/code&gt;. Calling &lt;code&gt;a()&lt;/code&gt; runs synchronously up to the &lt;code&gt;await&lt;/code&gt;, so &lt;code&gt;a1&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; print before &lt;code&gt;e&lt;/code&gt;. The continuation &lt;code&gt;a2&lt;/code&gt; was queued as a microtask before the &lt;code&gt;.then&lt;/code&gt; callback was queued, so it goes first. Once you see &lt;code&gt;await&lt;/code&gt; as "queue the rest of this function as a microtask", the ordering stops looking random.&lt;/p&gt;

&lt;p&gt;One note for Node.js: it has its own phases in the loop, and &lt;code&gt;process.nextTick&lt;/code&gt; callbacks run even before promise microtasks. The core idea, that the stack empties, then microtasks drain, then the next task runs, is the same.&lt;/p&gt;

&lt;h2&gt;
  
  
  Watch it happen
&lt;/h2&gt;

&lt;p&gt;Reading about queues is one thing. Seeing the stack grow and shrink while a timer counts down is much better for making it stick, so I built a tool for it: the &lt;a href="https://dev.to/tools/javascript-execution-visualizer"&gt;JavaScript Event Loop and Call Stack Visualizer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You paste a short script (or pick one of the examples), press &lt;strong&gt;Visualize&lt;/strong&gt;, and then step through it with Next, Back and Play. At every step you can see:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The code&lt;/strong&gt;, with the line that is about to run highlighted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The call stack&lt;/strong&gt; as a tower of blocks. The global context sits at the bottom, each call stacks on top, and the block on top is the code running now. Each block shows its variables, including &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; bindings that are still in the temporal dead zone, and whether the context is in its creation or execution phase.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Web APIs area&lt;/strong&gt;, where timers wait with a dial counting down.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The microtask queue and the task queue&lt;/strong&gt;, with the job that runs next marked.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The event loop itself&lt;/strong&gt;, showing whether it is running the script, draining microtasks, picking the next task or idle.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The console output&lt;/strong&gt;, built up as the run progresses.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A good first run is the "Timeout vs promise" example from the post above. Step through it and watch the &lt;code&gt;setTimeout&lt;/code&gt; callback sit in the task queue while the &lt;code&gt;then&lt;/code&gt; callback runs first. Then try "Hoisting" to see &lt;code&gt;var&lt;/code&gt; start as &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; start as "not initialised", and "async / await" to see the continuation land in the microtask queue. Then change the code and predict the output before you press Next.&lt;/p&gt;

&lt;p&gt;Two things about how it works, so you know what you are looking at:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your code is never passed to &lt;code&gt;eval&lt;/code&gt; or &lt;code&gt;Function&lt;/code&gt;. The tool includes a small interpreter for plain scripts, written to record every step, so it can show internals a real engine does not expose. It runs entirely in your browser, and nothing you type is sent anywhere.&lt;/li&gt;
&lt;li&gt;It is a model, not V8. Timers use a &lt;strong&gt;virtual clock&lt;/strong&gt;, so a 1000 ms timer does not make you wait a second. It handles the core of the language, including functions, closures, &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, promises, &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt;, and timers. It does not support classes, imports or regular expressions, and a run is capped at a limited number of steps so that an infinite loop stops instead of hanging the page. For the behaviours in this post, the order things run in is what matters, and that is what it shows.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A short checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The engine parses your code into a tree, compiles it to bytecode, and optimises the hot paths while it runs.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;global execution context&lt;/strong&gt; is created first, in two phases. Creation registers names (&lt;code&gt;var&lt;/code&gt; as &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; as unusable until their line, functions in full). Execution runs the lines.&lt;/li&gt;
&lt;li&gt;Every call creates a &lt;strong&gt;function execution context&lt;/strong&gt; and pushes it on the &lt;strong&gt;call stack&lt;/strong&gt;. Name lookup follows the &lt;strong&gt;scope chain&lt;/strong&gt;, which follows where a function was &lt;em&gt;defined&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;A closure is a function plus the scope it was defined in, kept alive on the &lt;strong&gt;heap&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;There is one stack, so one thing runs at a time. Waiting is handed to the environment, and finished work comes back through queues.&lt;/li&gt;
&lt;li&gt;After the stack empties, the &lt;strong&gt;event loop&lt;/strong&gt; drains the &lt;strong&gt;microtask queue&lt;/strong&gt; completely, then runs one task from the &lt;strong&gt;task queue&lt;/strong&gt;, and repeats.&lt;/li&gt;
&lt;li&gt;Promise reactions and the code after &lt;code&gt;await&lt;/code&gt; are microtasks. Timers are tasks. That is why &lt;code&gt;promise&lt;/code&gt; prints before &lt;code&gt;timeout&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to go further, the &lt;a href="https://dev.to/tools/javascript-ast-explorer"&gt;JavaScript AST explorer&lt;/a&gt; shows what the parser makes of your code, and the &lt;a href="https://dev.to/tools/javascript-execution-visualizer"&gt;event loop visualizer&lt;/a&gt; shows what the engine does with it next. Use them together and the path from a line of text to a running program stops being a black box.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Building a Multilingual Video-Invite Maker: What I Learned</title>
      <dc:creator>Ganesh Joshi</dc:creator>
      <pubDate>Fri, 04 Dec 2026 17:32:46 +0000</pubDate>
      <link>https://dev.to/ganeshjoshi/building-a-multilingual-video-invite-maker-what-i-learned-4n5j</link>
      <guid>https://dev.to/ganeshjoshi/building-a-multilingual-video-invite-maker-what-i-learned-4n5j</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was created with AI assistance and reviewed for accuracy before publishing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For the last few weeks I have been building Manuhar, a maker for wedding and birthday video invites in Hindi, Gujarati and English. A family types names, dates and a venue, picks a design, and gets a vertical MP4 to send on WhatsApp.&lt;/p&gt;

&lt;p&gt;On paper that is a form and a video template. In practice almost every hard problem came from three places: Indic text inside a video frame, a render that has to finish on an ordinary phone, and wording that has to read naturally in three languages. These are the notes I wish I had at the start.&lt;/p&gt;

&lt;h2&gt;
  
  
  One composition for the preview and the video
&lt;/h2&gt;

&lt;p&gt;The designs are &lt;a href="https://www.remotion.dev/" rel="noopener noreferrer"&gt;Remotion&lt;/a&gt; compositions at 1080x1920. Remotion lets you write a video as React components, which suits this work: a design is a layout with timing, and the text in it comes from props.&lt;/p&gt;

&lt;p&gt;The same composition is used twice. While someone edits, a Remotion &lt;code&gt;&amp;lt;Player&amp;gt;&lt;/code&gt; shows it live in the page. When they are done, &lt;code&gt;renderMediaOnWeb&lt;/code&gt; from &lt;code&gt;@remotion/web-renderer&lt;/code&gt; draws it frame by frame and encodes an MP4 with WebCodecs, in the browser. Nothing goes to a server, and the family's photos never leave the phone.&lt;/p&gt;

&lt;p&gt;Using one composition for both is the point. If the preview and the final video came from different code, they would drift, and the person would approve one thing and receive another. Sharing the code is not enough to prove they match, though, so there is a test for it. A script pauses the Player at a frame, screenshots it, renders that same frame range to MP4, pulls the frame out and compares the two images with PSNR. It runs across three designs and fails below 30 dB.&lt;/p&gt;

&lt;p&gt;One detail from writing it: rendering a single frame fails, because the encoder needs more than one frame to flush. The check renders a short range and takes the first frame of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The web renderer is not Chrome
&lt;/h2&gt;

&lt;p&gt;The first surprise was that the web renderer does not paint a page the way the browser does. It walks the DOM and draws it into a canvas itself, and it covers most of CSS, not all of it.&lt;/p&gt;

&lt;p&gt;When I compared its output with stills from Remotion's command-line renderer, the scores were 21.9, 20.4 and 25.1 dB on three designs. That is a visible difference, not noise. Two causes accounted for it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Box-shadow spread is not drawn.&lt;/strong&gt; Designs that used a spread shadow as a thin outline lost the outline.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A border on an &lt;code&gt;overflow: hidden&lt;/code&gt; box is clipped.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The fix was to stop leaning on those features: real borders where there had been shadows, and a negative margin equal to the border width where a border had to sit outside its box. After that the preview test above became the guard, so a new design that uses an unsupported feature fails before anyone sees it.&lt;/p&gt;

&lt;p&gt;The general lesson: if a library re-implements rendering, treat its output as a different browser and test against it, not against what Chrome shows you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content Security Policy, for one page only
&lt;/h2&gt;

&lt;p&gt;The site has a strict CSP. The render page needed three exceptions, and finding the first one took a while because it did not fail loudly. With inline styles blocked, the first frame rendered correctly and every frame after it came out wrong. The renderer sets inline styles while it draws SVG, so &lt;code&gt;style-src&lt;/code&gt; needs &lt;code&gt;'unsafe-inline'&lt;/code&gt; on that page.&lt;/p&gt;

&lt;p&gt;The other two were &lt;code&gt;worker-src blob:&lt;/code&gt;, for a small timer the renderer runs in a worker, and a &lt;code&gt;connect-src&lt;/code&gt; entry for Remotion's usage telemetry, which cannot be switched off. All three apply to the render page only. Every other page keeps the strict policy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fonts by script
&lt;/h2&gt;

&lt;p&gt;Indic text in video has two separate problems: getting the right glyphs, and getting them early enough.&lt;/p&gt;

&lt;p&gt;The first version loaded every font face on every card, about 1 MB, Gujarati and Sanskrit faces included on an English card. Now the faces are grouped (a small core set, a Sanskrit face, Devanagari, Gujarati and a few display families) and a card asks for the groups its text needs. The test is a Unicode range per script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;DEVA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;\&lt;span class="n"&gt;u0900&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;\&lt;span class="n"&gt;u097F&lt;/span&gt;\&lt;span class="n"&gt;u1CD0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;\&lt;span class="n"&gt;u1CFF&lt;/span&gt;\&lt;span class="n"&gt;uA8E0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;\&lt;span class="n"&gt;uA8FF&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="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;GUJ&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;\&lt;span class="n"&gt;u0A80&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;\&lt;span class="n"&gt;u0AFF&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every card loads the core set. The Gujarati faces load only when a Gujarati letter appears anywhere on the card, and the extra Devanagari faces only when Hindi text outside the core-drawn lines needs them. Loading a face too many costs a few kilobytes, while loading one too few changes how the card looks, so the rules lean towards loading.&lt;/p&gt;

&lt;p&gt;Getting them early enough matters more in video than on a web page. A page that shows a fallback font for 200 ms looks a little odd. A video renders a fallback font into the first frames and keeps it forever. The renderer waits for the fonts to finish loading before it draws frame one.&lt;/p&gt;

&lt;p&gt;For the site's own wordmark the faces are subset. Yatra One went from 70 KB to a few KB for the six letters it shows. Subsetting Indic fonts has a trap: the conjuncts are built by OpenType layout features, and a subset that drops them renders broken words. The build keeps every feature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python -m fontTools.subset Face.ttf --text=&lt;span class="s2"&gt;"..."&lt;/span&gt; \
  --layout-features=&lt;span class="s1"&gt;'*'&lt;/span&gt; --flavor=woff2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It also checks the sha256 of each source face, so if a font file is swapped the build stops instead of quietly cutting a subset from the wrong file.&lt;/p&gt;

&lt;p&gt;Line height was the last font problem. Devanagari and Gujarati stack marks above and below the letter, so a line height set by eye on Latin text clips them. The spacing is measured from the ink of the tallest clusters in each script, words like श्री and ક્ષ્મી, not from the font's nominal size.&lt;/p&gt;

&lt;h2&gt;
  
  
  Phones are slower than you think
&lt;/h2&gt;

&lt;p&gt;Rendering video in the browser is affordable for a laptop and expensive for a phone. On my laptop the cost per frame was 560, 541 and 551 ms at 480p, 720p and 1080p. The resolution barely mattered. Walking the page and drawing it into a canvas is the cost, not the pixels.&lt;/p&gt;

&lt;p&gt;That changed how I thought about quality. Dropping to a smaller size saves little time, so the size should be chosen per device rather than set low for everyone. For a slow device, the useful lever is the length of the wait, not the resolution.&lt;/p&gt;

&lt;p&gt;Before a render, the site runs a short test render of the person's own video, measures frames per second, then aborts it. From that it estimates the full render time at each size and picks the biggest allowed size that finishes within five minutes. A reading that says "slow" is trusted only if the page was not throttled before and after it and a second timing agrees, because a phone that is busy for a moment should not be told it is slow.&lt;/p&gt;

&lt;p&gt;The aborted test render taught me something about WebCodecs. Cancelling mid-render left a &lt;code&gt;VideoFrame&lt;/code&gt; open, and the console reported that a VideoFrame was garbage collected without being closed. Frames hold decoder memory, and the browser only warns you. The frame callback now tracks the frame it holds and closes it on abort.&lt;/p&gt;

&lt;p&gt;Some phones get no live preview at all. A device with &lt;code&gt;deviceMemory&lt;/code&gt; or &lt;code&gt;hardwareConcurrency&lt;/code&gt; of 2 or less sees a still card instead of the Player. An unknown value counts as capable, because Safari does not expose &lt;code&gt;deviceMemory&lt;/code&gt;, and treating every iPhone as weak would be wrong.&lt;/p&gt;

&lt;p&gt;There is also a server render path, built and tested, that stays switched off. A study for it timed a 720p wedding invite at 262 seconds in a laptop browser and 84 to 133 minutes under a 6x CPU throttle, a rough stand-in for a slow phone. A container on a server did 1080p in roughly 19 to 35 seconds, with the Devanagari shaped correctly. The path is built and passed its test runs, but it is hidden, and every video, HD included, is made on the person's own phone. That is also what lets the photos stay there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Photos stay on the device
&lt;/h2&gt;

&lt;p&gt;A photo is decoded with &lt;code&gt;createImageBitmap&lt;/code&gt; and &lt;code&gt;imageOrientation: "from-image"&lt;/code&gt;, downscaled on a canvas to 1600 px on the long side and saved as a JPEG at quality 0.9. Redrawing through a canvas also drops the EXIF data, including any GPS location. The result lives as a blob URL in the tab's memory. Nothing is uploaded, so there is nothing to delete later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Localisation is more than translation
&lt;/h2&gt;

&lt;p&gt;The card language and the site language are separate settings. A Gujarati family may want an English site and a Gujarati invite. The page ships in English, and the Hindi and Gujarati interface strings load only when someone picks one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dates are built without &lt;code&gt;Intl&lt;/code&gt;.&lt;/strong&gt; Different phones carry different ICU data, and the same &lt;code&gt;Intl.DateTimeFormat&lt;/code&gt; call can print a different line on two phones. On a wedding card that is not acceptable. The code has its own month names, its own words for morning and evening (सुबह and शाम, સવારે and સાંજે), and native digits unless the family chooses Latin ones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Some numbers must be words.&lt;/strong&gt; Gujarati ordinals are formed with મો, so a fifth birthday would be ૫મો. But the Gujarati digit ૫ looks very like the letter પ, and ૫મો reads as પમો, which is not a word. Ordinals one to ten are written as words. Digits start at eleven.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Grammar can save you a question.&lt;/strong&gt; A Hindi birthday line reads का 5वाँ जन्मदिवस. The ordinal agrees with जन्मदिवस, a masculine noun, not with the child, so the line never needs to ask whether the child is a boy or a girl.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A careful language review finds what tests do not.&lt;/strong&gt; A strict read of the Gujarati copy found a Devanagari vowel sign, ी, inside Gujarati text, where it looked almost right. The same pass rewrote phrasing that was correct but not how anyone would say it. Separately, a test now checks that no Gujarati letters appear in Hindi price text, and the reverse.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing all of it
&lt;/h2&gt;

&lt;p&gt;The test suite has over a thousand checks. Some are ordinary unit tests. The ones that earned their keep compare pixels: the preview against the render, and a font check that holds the frames to 45 dB after a font change (after one swap, 31 of 35 frames came out byte-identical). When the output is a video, the most reliable test is to render it and look at the frames, with numbers rather than eyes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use one composition for the preview and the final render, and prove they match with a frame comparison.&lt;/li&gt;
&lt;li&gt;Treat a re-implemented renderer as a separate browser. Test against its output.&lt;/li&gt;
&lt;li&gt;Load fonts by script, wait for them before the first frame, and keep layout features when subsetting Indic faces.&lt;/li&gt;
&lt;li&gt;On phones, the cost is drawing, not pixels. Measure the device and choose the size per device.&lt;/li&gt;
&lt;li&gt;Close every &lt;code&gt;VideoFrame&lt;/code&gt;, especially on abort.&lt;/li&gt;
&lt;li&gt;Do not trust &lt;code&gt;Intl&lt;/code&gt; for text that must read the same on every phone.&lt;/li&gt;
&lt;li&gt;Have every language's copy reviewed on its own, not just tested.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to see where all this ended up, Manuhar is live at &lt;a href="https://manuhar.app" rel="noopener noreferrer"&gt;manuhar.app&lt;/a&gt;. Pick a language and a design and watch the preview change as you type.&lt;/p&gt;

&lt;p&gt;Manuhar is still being built, and it will keep changing. What is live today is yours to try at &lt;a href="https://manuhar.app" rel="noopener noreferrer"&gt;manuhar.app&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>typescript</category>
      <category>video</category>
    </item>
    <item>
      <title>Post-quantum crypto after the lattice papers: what developers should do</title>
      <dc:creator>Ganesh Joshi</dc:creator>
      <pubDate>Fri, 04 Dec 2026 05:29:54 +0000</pubDate>
      <link>https://dev.to/ganeshjoshi/post-quantum-crypto-after-the-lattice-papers-what-developers-should-do-2oj9</link>
      <guid>https://dev.to/ganeshjoshi/post-quantum-crypto-after-the-lattice-papers-what-developers-should-do-2oj9</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was created with AI assistance and reviewed for accuracy before publishing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you saw headlines about quantum computers and encryption in the last week of November, here is the short answer first: nothing you rely on today is broken, and nothing needs an emergency change. HTTPS, your passwords, your bank's app and the post-quantum standards themselves all work exactly as they did before. No quantum computer that can break modern encryption exists.&lt;/p&gt;

&lt;p&gt;What did happen: Anthropic published four research papers on lattice cryptography, the maths under the new post-quantum standards. A rumour about RSA-2048 went round social media. And the US government repeated, with more urgency, advice it had already given: keep moving to post-quantum cryptography on the existing timeline. Here is what was published, what it means for the code you ship, and a short list of things worth planning.&lt;/p&gt;

&lt;h2&gt;
  
  
  What was published
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The lattice papers.&lt;/strong&gt; On 30 November Anthropic released four papers and a Lean 4 formalisation of their proofs in its &lt;code&gt;formal-math&lt;/code&gt; repository on GitHub. The README says the results "were discovered by an internal research Claude model" and that the formal proofs were written by AI models.&lt;/p&gt;

&lt;p&gt;The headline paper describes a quantum algorithm for Module-LWE, the hard problem that ML-KEM (FIPS 203) and ML-DSA (FIPS 204) are built on. It runs in quasi-polynomial time, which is asymptotically much faster than the exponential attacks known before. That is a real result in the theory, and cryptographers are treating it as one.&lt;/p&gt;

&lt;p&gt;What it is not is a practical attack. The paper's own title calls the algorithms "galactic", and the README says plainly that they "are not practical for the parameter sets of ML-KEM and ML-DSA." Two details explain why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The speed-up kicks in as the ring dimension grows. ML-KEM and ML-DSA fix that dimension at 256 and get stronger security by raising the module rank instead, which is the parameter the new attack handles worst. Its running time grows with the rank to the eighth power in the exponent.&lt;/li&gt;
&lt;li&gt;It is a quantum algorithm. It needs a large, error-corrected quantum computer that does not exist, and at real parameter sizes it needs far more operations than the best older attacks, which were already out of reach.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The independent read.&lt;/strong&gt; A group calling itself the Cryptography Incident Review Group published an initial assessment the same day. Anthropic had shared the results with several of its members on 16 November. Their summary: the attack costs vastly more than earlier attacks on ML-KEM and ML-DSA, there is no improvement in classical attacks on mainstream lattice schemes, and nothing changes for unstructured lattices. Their first recommendation is the one that matters for most of us: "We recommend continuing the transition to post-quantum cryptography."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The RSA-2048 rumour.&lt;/strong&gt; There is no evidence for it. The four papers are about lattices and do not mention factoring, and no lab has claimed it. RSA is as secure today as it was a month ago.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The government response.&lt;/strong&gt; On 2 December the Office of the National Cyber Director, the NSA and CISA issued a joint statement. It says the recent advances "do not present an immediate and widespread threat" to the cryptography in use, while asking everyone to upgrade quickly. The NSA published an advisory alongside it. Anthropic, Amazon, Apple, Cloudflare, Google, Meta, Microsoft, OpenAI and Oracle signed a statement backing the guidance to move to the NIST post-quantum algorithms as soon as possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The deadlines, which were already set.&lt;/strong&gt; NIST published the final version of IR 8547 on 27 October. RSA, ECDSA, EdDSA and classic Diffie-Hellman are deprecated after 2030 and disallowed after 2035, at every key size. The draft had let larger keys such as RSA-3072 and P-256 run until 2035; the final version removed that. In Europe, the Commission proposed a Quantum Act on 25 November that would make public bodies covered by NIS2 ask for post-quantum capable products when they buy certain digital systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what actually changed?
&lt;/h2&gt;

&lt;p&gt;Less than the headlines suggest, and in a useful direction.&lt;/p&gt;

&lt;p&gt;The reason for post-quantum work is long-term and has not changed: someone could record encrypted traffic today and try to decrypt it years from now, if a large quantum computer is ever built. That is called "harvest now, decrypt later". It is a planning concern, not something happening to your traffic today, and it is why key exchange gets upgraded first and signatures can follow.&lt;/p&gt;

&lt;p&gt;What the papers add is new knowledge, not a new danger. Lattice schemes now have a quantum attack that is better in theory than anyone had shown, found with heavy help from an AI model. The standard parameters still hold by a huge margin. This is how cryptography is supposed to work: researchers probe the maths in public, and the standards keep a wide safety margin for exactly this reason. The review group's practical advice is to use the highest security level your application supports and to keep using hybrids.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hybrids are doing their job
&lt;/h2&gt;

&lt;p&gt;Almost all post-quantum TLS on the web today is hybrid. The key exchange called &lt;code&gt;X25519MLKEM768&lt;/code&gt; runs classical X25519 and ML-KEM-768 together and combines both secrets. An attacker has to break both to read the traffic. If a future result weakens ML-KEM further, X25519 still protects you against everyone without a quantum computer. If a quantum computer arrives, ML-KEM protects you.&lt;/p&gt;

&lt;p&gt;This is the design working as intended. Keep the hybrid on: do not switch to pure ML-KEM, and do not switch back to classical-only because of one theoretical result.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I found on my own site
&lt;/h2&gt;

&lt;p&gt;This blog sits behind Cloudflare, which answers &lt;code&gt;/cdn-cgi/trace&lt;/code&gt; with the key exchange it negotiated. I asked it from three clients on the same Windows machine:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Client&lt;/th&gt;
&lt;th&gt;TLS library&lt;/th&gt;
&lt;th&gt;Key exchange&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Chromium-based browser&lt;/td&gt;
&lt;td&gt;BoringSSL&lt;/td&gt;
&lt;td&gt;&lt;code&gt;X25519MLKEM768&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;curl&lt;/code&gt; from Git Bash&lt;/td&gt;
&lt;td&gt;OpenSSL 1.1.1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;X25519&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Node.js 22.17&lt;/td&gt;
&lt;td&gt;OpenSSL 3.0.16&lt;/td&gt;
&lt;td&gt;could not even request it&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The browser was already post-quantum and nobody had to do anything. The two command line tools were not, and Node rejected the group name outright:&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="n"&gt;tls&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'node:tls'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;tls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'ganeshjoshi.dev'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;443&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;servername&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'ganeshjoshi.dev'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;ecdhCurve&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'X25519MLKEM768'&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;span class="c1"&gt;// Node 22 with OpenSSL 3.0: Error: Failed to set ECDH curve&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the pattern to expect in your own stack. Browsers and big CDNs moved first. The places still to upgrade are the ones nobody looks at: server-to-server calls, CLI tools, CI runners, old base images and the hop from your CDN back to your origin.&lt;/p&gt;

&lt;p&gt;You can run the same check against any site on Cloudflare:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -s https://example.com/cdn-cgi/trace &lt;span class="o"&gt;|&lt;/span&gt; grep kex
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If it prints &lt;code&gt;X25519MLKEM768&lt;/code&gt;, that client and that edge agree on a post-quantum key exchange. If it prints &lt;code&gt;X25519&lt;/code&gt;, the connection is still secure today, it just has not picked up the post-quantum upgrade yet, and the client is usually the side to update.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to plan
&lt;/h2&gt;

&lt;p&gt;None of this is urgent in the sense of "patch tonight". It is ordinary planned work with a 2030 horizon, and most of it is library upgrades.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Inventory before you migrate.&lt;/strong&gt; Find where you use RSA, ECDH, ECDSA and Ed25519: TLS termination, service-to-service mTLS, JWT signing keys, SSH keys, code signing, anything that encrypts data you keep for years. You cannot plan a 2030 deadline without this list.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix key exchange first.&lt;/strong&gt; It is the part exposed to harvest now, decrypt later, and it is mostly a library upgrade. OpenSSL 3.5 (April 2025) added ML-KEM, ML-DSA and SLH-DSA, and Go has shipped &lt;code&gt;crypto/mlkem&lt;/code&gt; since 1.24. Check which OpenSSL your Node, Python, nginx and container images actually link against, not which one you think they use. &lt;code&gt;node -p process.versions.openssl&lt;/code&gt; takes a second.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check the origin leg.&lt;/strong&gt; A post-quantum edge in front of a classical origin protects the visitor's hop and nothing else. If you run your own origin servers behind a CDN, see whether they offer the hybrid group.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep hybrids on and pick the stronger setting.&lt;/strong&gt; ML-KEM-768 is NIST's recommended default. Where you choose parameters yourself, do not go lower to save a few hundred bytes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Leave signatures for a planned step.&lt;/strong&gt; ML-DSA certificates are much larger than ECDSA ones and the public certificate ecosystem is not ready for them. Signatures cannot be harvested and broken later in the same way, so they can follow key exchange. Plan them, but do not rush them into production this month.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build for swapping.&lt;/strong&gt; The lesson of this week is that the algorithm you pick may need replacing sooner than you planned. Keep algorithm names in configuration, not scattered through code, and make key rotation a routine job rather than an incident.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;p&gt;Nothing you use today is broken, and there is no reason for alarm. ML-KEM and ML-DSA still hold, and the new attack is theoretical, far beyond any machine that exists or is in sight. The US government and most of the big platforms used the moment to say the same thing the deadlines already said: move to post-quantum key exchange, use hybrids, and finish by 2030. Your browser is probably already there. Your servers, scripts and CI are where the work is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Anthropic, &lt;a href="https://github.com/anthropics/formal-math/tree/main/lattices" rel="noopener noreferrer"&gt;Four papers on lattices: a Lean 4 formalization&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Cryptography Incident Review Group, &lt;a href="https://cirg.org/statements/1/" rel="noopener noreferrer"&gt;Initial assessment of Anthropic's findings on lattice cryptography&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;ONCD, NSA and CISA, &lt;a href="https://www.cisa.gov/news-events/news/joint-statement-recent-advances-cryptographic-and-cybersecurity-capabilities" rel="noopener noreferrer"&gt;Joint statement on recent advances in cryptographic and cybersecurity capabilities&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Anthropic, &lt;a href="https://www.anthropic.com/news/joint-statement-post-quantum-cryptography" rel="noopener noreferrer"&gt;Joint statement on post-quantum cryptography&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;NIST, &lt;a href="https://csrc.nist.gov/pubs/ir/8547/final" rel="noopener noreferrer"&gt;IR 8547: Transition to post-quantum cryptography standards&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Anthropic, &lt;a href="https://www.anthropic.com/research/discovering-cryptographic-weaknesses" rel="noopener noreferrer"&gt;Discovering cryptographic weaknesses with Claude&lt;/a&gt; (July 2026, earlier work on HAWK and reduced-round AES)&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>security</category>
      <category>cryptography</category>
      <category>node</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Electron Hardening for AI-Powered Desktop Clients</title>
      <dc:creator>Ganesh Joshi</dc:creator>
      <pubDate>Sun, 29 Nov 2026 17:24:32 +0000</pubDate>
      <link>https://dev.to/ganeshjoshi/electron-hardening-for-ai-powered-desktop-clients-3am4</link>
      <guid>https://dev.to/ganeshjoshi/electron-hardening-for-ai-powered-desktop-clients-3am4</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was created with AI assistance and reviewed for accuracy before publishing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In a web app, cross-site scripting means an attacker runs JavaScript in a browser tab. In an &lt;a href="https://www.electronjs.org/docs/latest/tutorial/security" rel="noopener noreferrer"&gt;Electron&lt;/a&gt; app with the wrong settings, the same bug means an attacker reads the user's home directory.&lt;/p&gt;

&lt;p&gt;AI desktop clients sit in the worst part of this space. They exist precisely because they can see local files, they render text produced by a model rather than by you, and that text frequently contains markup and code blocks. You are rendering untrusted content in a process that may have filesystem access.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three settings that decide everything
&lt;/h2&gt;

&lt;p&gt;Modern Electron defaults are safe. The danger is in tutorials and older codebases that turn them off to make something work quickly:&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="n"&gt;win&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;BrowserWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;webPreferences&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;contextIsolation&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="c1"&gt;// must stay true&lt;/span&gt;
    &lt;span class="n"&gt;nodeIntegration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;     &lt;span class="c1"&gt;// must stay false&lt;/span&gt;
    &lt;span class="n"&gt;sandbox&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="c1"&gt;// keep unless you have a specific reason&lt;/span&gt;
    &lt;span class="n"&gt;preload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;path&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="n"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'preload.js'&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;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;nodeIntegration: true&lt;/code&gt; gives renderer JavaScript direct access to &lt;code&gt;require&lt;/code&gt;, which means any injected script can call &lt;code&gt;child_process.exec&lt;/code&gt;. &lt;code&gt;contextIsolation: false&lt;/code&gt; lets page scripts reach into the preload script's context and take whatever it holds. Together they turn a rendering bug into full compromise.&lt;/p&gt;

&lt;p&gt;If a dependency only works with these disabled, the dependency is the problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Expose functions, not capabilities
&lt;/h2&gt;

&lt;p&gt;The preload script is the bridge, and the temptation is to make it general. Do not pass through primitives that can do 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="c1"&gt;// Wrong: this is filesystem access with extra steps.&lt;/span&gt;
&lt;span class="n"&gt;contextBridge&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exposeInMainWorld&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'api'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'utf8'&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;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Right: one narrow operation the app actually needs.&lt;/span&gt;
&lt;span class="n"&gt;contextBridge&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exposeInMainWorld&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'api'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;openDocument&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ipcRenderer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'document:open'&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;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first version means any injected script reads &lt;code&gt;/etc/passwd&lt;/code&gt; or the user's SSH keys. The second gives the renderer no path parameter at all: the main process shows the file picker, the user chooses, and only the chosen file's contents come back. The authority stays with the user rather than with the caller.&lt;/p&gt;

&lt;p&gt;That is the general principle. Every function you expose should be a specific action with the decision-making on the main side, not a capability the renderer can point wherever it likes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Validate IPC as if it were a public API
&lt;/h2&gt;

&lt;p&gt;Because it is one. Any code running in the renderer can invoke your handlers with whatever arguments it likes.&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="n"&gt;ipcMain&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'document:save'&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="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&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;// Confirm the sender is a window you created.&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="nf"&gt;isTrustedFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;senderFrame&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="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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="n"&gt;parsed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SaveSchema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;safeParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// shape and type&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="n"&gt;parsed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;success&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="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;path&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="n"&gt;userDataDir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parsed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// Confirm the resolved path did not escape the directory.&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="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userDataDir&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sep&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="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parsed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contents&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="n"&gt;ok&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;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 path check is the one people miss. A &lt;code&gt;name&lt;/code&gt; of &lt;code&gt;../../.ssh/authorized_keys&lt;/code&gt; resolves outside your data directory, and string concatenation will not catch it. Resolve first, then verify the result is still inside the boundary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model output is untrusted input
&lt;/h2&gt;

&lt;p&gt;This is the part specific to AI clients. A response may contain HTML, a link, an image reference, or a code block, and your renderer decides what to do with each.&lt;/p&gt;

&lt;p&gt;Never use &lt;code&gt;innerHTML&lt;/code&gt; on model output. Render markdown through a parser that escapes HTML by default, and sanitise the result before it reaches the DOM. A model reproducing an attacker's prompt injection is a realistic delivery path for a payload, and it arrives looking exactly like a normal answer.&lt;/p&gt;

&lt;p&gt;Links deserve their own handling. An unhandled &lt;code&gt;window.open&lt;/code&gt; or a target of &lt;code&gt;_blank&lt;/code&gt; can navigate your app window to an arbitrary URL, replacing your interface with someone else's page inside a trusted frame:&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="n"&gt;win&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;webContents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setWindowOpenHandler&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="n"&gt;url&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/^&lt;/span&gt;&lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt;\&lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="c1"&gt;//.test(url)) shell.openExternal(url);  // hand it to the OS browser&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'deny'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                              &lt;span class="c1"&gt;// never open in-app&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="n"&gt;win&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;webContents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'will-navigate'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&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="n"&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="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;Everything external goes to the system browser. The app window navigates nowhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  Turn off the debugging conveniences before shipping
&lt;/h2&gt;

&lt;p&gt;Two things commonly ship enabled by accident. The remote debugging port, which lets any local process attach to your renderer and execute code in it. And permissive &lt;code&gt;webSecurity&lt;/code&gt;, which teams disable during development to load local files and then forget.&lt;/p&gt;

&lt;p&gt;Both should be off in production builds, and worth asserting in the packaging step rather than trusting memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Update Electron on a schedule, not on demand
&lt;/h2&gt;

&lt;p&gt;Electron bundles Chromium, so your app carries whatever browser vulnerabilities that version has, and unlike a website you do not get fixes when the user updates their browser. Every published Chromium security fix applies to you until you ship a new Electron.&lt;/p&gt;

&lt;p&gt;Treat the upgrade as routine maintenance with a slot in the release cycle. An AI client that reads local files running a year-old Chromium is a meaningful risk regardless of how careful the rest of the code is.&lt;/p&gt;

&lt;p&gt;Electron publishes a &lt;a href="https://www.electronjs.org/docs/latest/tutorial/security" rel="noopener noreferrer"&gt;security checklist&lt;/a&gt; that changes between major versions. Re-reading it at each upgrade takes ten minutes and is the highest-value ten minutes in the process.&lt;/p&gt;

</description>
      <category>electron</category>
      <category>ai</category>
      <category>security</category>
      <category>desktop</category>
    </item>
    <item>
      <title>GraphQL DataLoader for AI Gateway Resolvers</title>
      <dc:creator>Ganesh Joshi</dc:creator>
      <pubDate>Sun, 22 Nov 2026 16:52:24 +0000</pubDate>
      <link>https://dev.to/ganeshjoshi/graphql-dataloader-for-ai-gateway-resolvers-52e7</link>
      <guid>https://dev.to/ganeshjoshi/graphql-dataloader-for-ai-gateway-resolvers-52e7</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was created with AI assistance and reviewed for accuracy before publishing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;GraphQL's N+1 problem is well known and usually discussed in terms of database rows. Putting a GraphQL gateway in front of AI services makes it considerably more expensive, because the thing being fetched N times is no longer a row. It is a model call, a policy check, or a tool registry lookup, each with real latency and sometimes a per-call price.&lt;/p&gt;

&lt;p&gt;A query asking for twenty conversations and their summaries can become one query for the conversations and twenty separate summarisation calls. &lt;a href="https://github.com/graphql/dataloader" rel="noopener noreferrer"&gt;DataLoader&lt;/a&gt; is the standard fix, and the standard way to get it wrong is to share one across requests.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the resolver shape causes it
&lt;/h2&gt;

&lt;p&gt;GraphQL resolvers run per field, per object. The engine has no way to know that twenty sibling resolvers are all going to call the same service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;resolvers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;Conversation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Runs once per conversation in the result set.&lt;/span&gt;
    &lt;span class="n"&gt;summary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;summariseService&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="n"&gt;conv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&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;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;Ask for a list of twenty conversations and this fires twenty times. Each call is independent, so they run concurrently, which means the downstream service sees a burst of twenty rather than a steady stream. That is how a gateway trips its own provider's rate limits while looking idle in aggregate.&lt;/p&gt;

&lt;h2&gt;
  
  
  A loader collapses the burst into one call
&lt;/h2&gt;

&lt;p&gt;DataLoader sits between the resolver and the service. It collects keys requested within a single tick of the event loop, then calls your batch function once with all of them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;DataLoader&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'dataloader'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;summaryLoader&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;DataLoader&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="n"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;string&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="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="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;summariseService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getMany&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;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ids&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="n"&gt;byId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&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="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&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;span class="c1"&gt;// Must return results in the same order as the keys.&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ids&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;byId&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="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&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="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;Two rules make the batch function correct. The array you return must be the same length as the keys array and in the same order, because DataLoader matches results to callers positionally. And a missing key must produce &lt;code&gt;null&lt;/code&gt; or an &lt;code&gt;Error&lt;/code&gt; in its slot rather than being omitted, otherwise every result after the gap resolves to the wrong object. That misalignment is a genuinely nasty bug: it returns real data, just attached to the wrong parent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create loaders per request, always
&lt;/h2&gt;

&lt;p&gt;This is the mistake that matters most, and it is a security issue rather than a performance one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Wrong: one loader for the lifetime of the process.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;loader&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;DataLoader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;batchFn&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="n"&gt;server&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;ApolloServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;context&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="o"&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="n"&gt;loader&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;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Right: a fresh set for every request.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;server&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;ApolloServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;context&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="o"&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="n"&gt;loaders&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;createLoaders&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;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;DataLoader caches. A module-scope loader therefore caches across users. The first request for conversation &lt;code&gt;abc&lt;/code&gt; populates the cache, and every later request for &lt;code&gt;abc&lt;/code&gt; gets that cached value regardless of who is asking, whether they still have access, or whether the underlying data has changed.&lt;/p&gt;

&lt;p&gt;In a multi-tenant AI product that means one tenant's conversation summary served to another. The permission check in your resolver does not save you, because the cache hit happens inside the loader, below that check.&lt;/p&gt;

&lt;p&gt;Per-request loaders give you batching within a request, which is where the N+1 lives, and no sharing across requests, which is where the risk lives. That is the correct trade.&lt;/p&gt;

&lt;h2&gt;
  
  
  Include the tenant in the cache key
&lt;/h2&gt;

&lt;p&gt;Even within one request, keys that are not globally unique are dangerous. If a document id is only unique per tenant, key the loader on the pair:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;docLoader&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;DataLoader&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="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;tenantId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;docId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&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;span class="o"&gt;=&amp;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="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cacheKeyFn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="sb"&gt;`${k.tenantId}:${k.docId}`&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;Object keys need an explicit &lt;code&gt;cacheKeyFn&lt;/code&gt;, because the default cache uses identity comparison and two structurally identical objects are different keys. Without it you get no cache hits at all, which is a silent performance loss rather than a correctness one, but worth knowing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Batching does not bound fan-out
&lt;/h2&gt;

&lt;p&gt;DataLoader turns twenty calls into one. It does not stop a client asking for two thousand.&lt;/p&gt;

&lt;p&gt;GraphQL lets the caller choose the shape of the query, and a nested query over an AI gateway can request an enormous amount of work in a few lines. Batching makes that cheaper without making it bounded.&lt;/p&gt;

&lt;p&gt;Two limits are worth having:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Control&lt;/th&gt;
&lt;th&gt;Prevents&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Query depth limit&lt;/td&gt;
&lt;td&gt;Deeply nested traversals that multiply resolver calls&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Query cost analysis&lt;/td&gt;
&lt;td&gt;Wide queries that are shallow but request thousands of expensive fields&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Cost analysis is the more useful of the two for AI backends, because the expensive fields are not necessarily the deep ones. Assign a weight to each field reflecting what it actually costs, sum it before execution, and reject queries over a threshold. A field that triggers a model call should carry a weight orders of magnitude above one that reads a column.&lt;/p&gt;

&lt;h2&gt;
  
  
  Measure calls, not milliseconds
&lt;/h2&gt;

&lt;p&gt;The way to know any of this is working is to count downstream calls per GraphQL request. Latency alone will not tell you, because concurrent N+1 calls can look acceptable in wall-clock time while costing twenty times what they should.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="n"&gt;context&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="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="n"&gt;counts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="o"&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="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="n"&gt;loaders&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;createLoaders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;onFinish&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;downstreamCalls&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="n"&gt;counts&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;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;Log that per request and the pathology is obvious: a request that fetched twenty conversations should show one summary call, not twenty. If you see the resolver count tracking the result count, a loader is either missing or being bypassed somewhere in that path.&lt;/p&gt;

&lt;p&gt;That single log line is the whole feedback loop. Without it, an N+1 reintroduced by a new resolver is invisible until the provider bill arrives.&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>ai</category>
      <category>dataloader</category>
      <category>performance</category>
    </item>
    <item>
      <title>Prisma Migrations When AI Features Touch Your Database</title>
      <dc:creator>Ganesh Joshi</dc:creator>
      <pubDate>Sun, 15 Nov 2026 16:02:42 +0000</pubDate>
      <link>https://dev.to/ganeshjoshi/prisma-migrations-when-ai-features-touch-your-database-4521</link>
      <guid>https://dev.to/ganeshjoshi/prisma-migrations-when-ai-features-touch-your-database-4521</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was created with AI assistance and reviewed for accuracy before publishing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;AI features arrive with a distinctive database footprint. You need somewhere to keep conversations, the messages inside them, the tool calls each message triggered, and the feedback users leave. Individually these look like ordinary tables. Collectively they are the fastest-growing thing in your schema, because a single user session writes dozens of rows where a normal feature writes one.&lt;/p&gt;

&lt;p&gt;That growth rate is what makes migrations on these tables different. A schema change that is instant on your &lt;code&gt;users&lt;/code&gt; table can lock a &lt;code&gt;messages&lt;/code&gt; table with fifty million rows for long enough to take the product down.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model messages as append-only from the start
&lt;/h2&gt;

&lt;p&gt;The most useful early decision is treating message rows as immutable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;model Message {
  id             String   @id @default(cuid())
  conversationId String
  role           String   // user | assistant | tool
  content        String   @db.Text
  createdAt      DateTime @default(now())

  conversation Conversation @relation(fields: [conversationId], references: [id], onDelete: Cascade)

  @@index([conversationId, createdAt])
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Append-only tables are far easier to migrate. There are no in-place updates to contend with, backfills can run in ranges without racing live writes, and you can archive old partitions without touching application logic.&lt;/p&gt;

&lt;p&gt;The compound index on &lt;code&gt;[conversationId, createdAt]&lt;/code&gt; is the one query this table actually serves: fetch a conversation's messages in order. A single-column index on &lt;code&gt;conversationId&lt;/code&gt; still requires a sort afterwards, which on a long conversation is the difference between a fast read and a slow one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Expand and contract, never rename
&lt;/h2&gt;

&lt;p&gt;Prisma will happily generate a migration that renames or retypes a column. On a large table under load, that is how you cause an outage. The safe pattern splits it into steps that are each individually harmless.&lt;/p&gt;

&lt;p&gt;Suppose &lt;code&gt;Message.content&lt;/code&gt; needs to become structured JSON:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Expand.&lt;/strong&gt; Add &lt;code&gt;contentJson&lt;/code&gt; as a nullable column. Nullable additions do not rewrite the table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dual-write.&lt;/strong&gt; Deploy code that writes both columns and reads the old one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backfill.&lt;/strong&gt; Populate &lt;code&gt;contentJson&lt;/code&gt; for existing rows in batches.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Switch reads.&lt;/strong&gt; Deploy code that reads the new column, still writing both.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contract.&lt;/strong&gt; Once nothing reads it, drop &lt;code&gt;content&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Every step is independently reversible, and at no point does a deploy require the schema and the code to change simultaneously. That last property is the real prize: it means a rollback never leaves you with code expecting a column that no longer exists.&lt;/p&gt;

&lt;p&gt;Backfills should run in bounded batches rather than one statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&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="k"&gt;for&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;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;batch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;contentJson&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="n"&gt;select&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;id&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="n"&gt;content&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="n"&gt;take&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="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;span class="n"&gt;cursor&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;skip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&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="n"&gt;orderBy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'asc'&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;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="n"&gt;batch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&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;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;await&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;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;batch&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&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="n"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;contentJson&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="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&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="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;span class="n"&gt;cursor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;batch&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="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&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;A single &lt;code&gt;UPDATE&lt;/code&gt; across fifty million rows holds locks and generates enough WAL to cause its own problems. Batching keeps each transaction short and lets you stop partway without losing work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Watch for the index build that locks
&lt;/h2&gt;

&lt;p&gt;Adding an index to a growing table is the other common outage. By default Postgres takes a lock that blocks writes for the duration of the build.&lt;/p&gt;

&lt;p&gt;Prisma Migrate generates plain &lt;code&gt;CREATE INDEX&lt;/code&gt;. On a large table you want the concurrent form, which requires editing the generated SQL by hand:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Edit the generated migration before applying it.&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="no"&gt;CONCURRENTLY&lt;/span&gt; &lt;span class="s2"&gt;"Message_conversationId_createdAt_idx"&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="s2"&gt;"Message"&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"conversationId"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"createdAt"&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;CONCURRENTLY&lt;/code&gt; cannot run inside a transaction, so the migration file needs its transaction wrapper removed as well. It is slower and it can fail partway, leaving an invalid index you have to drop and retry. Both are better than blocking writes on your busiest table.&lt;/p&gt;

&lt;p&gt;This is the strongest argument for reading generated migration SQL rather than applying it unseen. The Prisma schema is a good abstraction, and the SQL it produces is where the operational consequences live.&lt;/p&gt;

&lt;h2&gt;
  
  
  Plan the retention policy before you need it
&lt;/h2&gt;

&lt;p&gt;Conversation data grows without bound and most of it is never read again. Decide early what happens to it, because retrofitting deletion onto a table with foreign keys pointing at it is considerably harder.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;onDelete: Cascade&lt;/code&gt; on the relation, as in the model above, means deleting a conversation removes its messages automatically. That is convenient and it is also a large transaction if the conversation is long, so batch deletions by conversation rather than by date range across the whole table.&lt;/p&gt;

&lt;p&gt;Retention is also frequently a compliance requirement rather than a housekeeping one. If users can request deletion of their data, the tool call log is easy to forget: it often contains the prompt text, which is the thing they are asking you to delete.&lt;/p&gt;

&lt;h2&gt;
  
  
  Review the SQL, especially when it is generated
&lt;/h2&gt;

&lt;p&gt;The practical rule that prevents most of the above: no migration reaches production without a person reading the SQL.&lt;/p&gt;

&lt;p&gt;This matters more now that schema changes are often drafted by an assistant. A generated migration is usually correct in the sense that it produces the described schema. Whether it does so without locking a table for four minutes is a different question, and not one the generator is reasoning about.&lt;/p&gt;

&lt;p&gt;Read the file, check for table rewrites, index builds, and &lt;code&gt;NOT NULL&lt;/code&gt; additions on existing columns, and confirm each one is safe at your current row counts. That review takes a minute and is the cheapest insurance available.&lt;/p&gt;

</description>
      <category>prisma</category>
      <category>ai</category>
      <category>database</category>
      <category>sql</category>
    </item>
    <item>
      <title>Core Web Vitals When Your Hero Section Is an AI Chat</title>
      <dc:creator>Ganesh Joshi</dc:creator>
      <pubDate>Sun, 08 Nov 2026 15:45:29 +0000</pubDate>
      <link>https://dev.to/ganeshjoshi/core-web-vitals-when-your-hero-section-is-an-ai-chat-4lo</link>
      <guid>https://dev.to/ganeshjoshi/core-web-vitals-when-your-hero-section-is-an-ai-chat-4lo</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was created with AI assistance and reviewed for accuracy before publishing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Putting a live AI chat at the top of a landing page is a good demo and a bad hero. Every property that makes it compelling, that it is interactive, that it streams, that it responds to what you type, is also a property that &lt;a href="https://web.dev/articles/vitals" rel="noopener noreferrer"&gt;Core Web Vitals&lt;/a&gt; penalises. The chat is usually the largest element on screen, so it becomes the LCP element. It grows as tokens arrive, so it moves everything under it. And it is the first thing a visitor touches, so it owns your INP.&lt;/p&gt;

&lt;p&gt;None of that is unavoidable. It just means the hero has to be designed backwards from the metric.&lt;/p&gt;

&lt;h2&gt;
  
  
  The chat should not be your LCP element
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://web.dev/articles/lcp" rel="noopener noreferrer"&gt;LCP&lt;/a&gt; measures when the largest block of content in the viewport finishes rendering. If that block is a chat transcript that appears after hydration, your LCP cannot happen until JavaScript has downloaded, parsed, and run. On a mid-range phone over slow 4G that is several seconds you do not control.&lt;/p&gt;

&lt;p&gt;The fix is to make something else the largest element, and to make that thing static.&lt;/p&gt;

&lt;p&gt;In practice this means the hero is a headline, a subhead, and a chat panel that is present but visually secondary. Render the headline server-side as real text. Text has no decode step, no network request beyond the HTML itself, and it is eligible to be the LCP element the moment the document paints.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The h1 is the LCP candidate. It ships in the HTML and needs no JS.&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;section&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"hero"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Ask anything about your codebase&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Answers grounded in your own repository, not the public internet.&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ChatPanel&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;section&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the design genuinely requires the chat to dominate the fold, then pre-render its first state on the server: a greeting message, a couple of example prompts, the input box. That markup is real content, it counts for LCP, and it arrives with the document.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reserve the space before the tokens arrive
&lt;/h2&gt;

&lt;p&gt;Streaming output is the most reliable way to generate layout shift. Each chunk makes the transcript taller, and everything below it moves down. That is textbook &lt;a href="https://web.dev/articles/cls" rel="noopener noreferrer"&gt;CLS&lt;/a&gt;, and unlike LCP it keeps accumulating for as long as the response runs.&lt;/p&gt;

&lt;p&gt;Give the transcript a fixed height, or a min-height large enough for a typical answer, and let it scroll internally:&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="nc"&gt;.chat-transcript&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;min-height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;22rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c"&gt;/* space is reserved before the first token */&lt;/span&gt;
  &lt;span class="nl"&gt;overflow-y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;overscroll-behavior&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;contain&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 related mistakes are worth naming because they look harmless.&lt;/p&gt;

&lt;p&gt;The first is animating the container open as the response starts. Transitioning &lt;code&gt;height&lt;/code&gt; or &lt;code&gt;width&lt;/code&gt; recalculates layout on every frame and shifts whatever sits below. If you want the panel to feel like it is opening, animate &lt;code&gt;transform&lt;/code&gt; instead. &lt;code&gt;transform&lt;/code&gt; and &lt;code&gt;opacity&lt;/code&gt; are the only two properties the compositor can handle without touching layout.&lt;/p&gt;

&lt;p&gt;The second is the typing indicator. A three-dot bubble that is inserted, then removed, then replaced by the answer causes two shifts on its own. Render it inside the space you already reserved, and swap its contents rather than its box.&lt;/p&gt;

&lt;h2&gt;
  
  
  INP belongs to the input box, not the model
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://web.dev/articles/inp" rel="noopener noreferrer"&gt;INP&lt;/a&gt; measures the delay between an interaction and the next frame the browser paints. It does not measure how long your model takes to answer. This distinction is worth internalising, because it means a slow model does not have to mean a bad INP score.&lt;/p&gt;

&lt;p&gt;What ruins INP is doing work on the main thread inside the event handler. Tokenising the prompt, building embeddings client-side, or serialising a large conversation history before the request goes out all block the frame.&lt;/p&gt;

&lt;p&gt;Keep the handler to the minimum that produces visible feedback:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;onSubmit&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="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Paint first: echo the user's message and clear the input.&lt;/span&gt;
  &lt;span class="nf"&gt;appendMessage&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="nx"&gt;text&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nf"&gt;setInput&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="c1"&gt;// Everything expensive happens after, off the interaction's critical path.&lt;/span&gt;
  &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sendToModel&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The user sees their message appear immediately. The request, the streaming, and the rendering of the reply all happen afterwards, and none of it is attributed to that interaction.&lt;/p&gt;

&lt;h2&gt;
  
  
  Watch TTFB if the route is server-rendered
&lt;/h2&gt;

&lt;p&gt;If the page server-renders anything that depends on a model or a vector database, that work sits in front of your &lt;a href="https://web.dev/articles/ttfb" rel="noopener noreferrer"&gt;TTFB&lt;/a&gt;, and TTFB is inside LCP. A route that waits 400ms on a similarity search before it can send a byte has spent 400ms of an LCP budget that is roughly 2.5 seconds on mobile.&lt;/p&gt;

&lt;p&gt;The hero almost never needs that data. Render the shell immediately and let the dynamic part stream in or load client-side. In frameworks with streaming SSR, put the model-dependent region inside a boundary so the rest of the document flushes without waiting for it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Measure in the field, not on your laptop
&lt;/h2&gt;

&lt;p&gt;Lab tools are useful for finding causes and useless for judging severity. Your development machine has a fast CPU, a warm cache, and a local server, which is the combination least likely to reproduce what a phone on a train experiences.&lt;/p&gt;

&lt;p&gt;Two habits are worth building:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Habit&lt;/th&gt;
&lt;th&gt;Why it matters&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Read field data, not lab scores&lt;/td&gt;
&lt;td&gt;LCP and CLS are scored at the 75th percentile of real visits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Confirm which element is actually LCP&lt;/td&gt;
&lt;td&gt;It is often not the one you assumed, and optimising the wrong element wastes the work&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The second one catches more mistakes than any other check. You can log it directly:&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;new&lt;/span&gt; &lt;span class="nc"&gt;PerformanceObserver&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;list&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;entry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getEntries&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;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="s1"&gt;LCP element:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;element&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;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;startTime&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ms&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;observe&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;largest-contentful-paint&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;buffered&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run that on the deployed site, on a throttled connection, and see what it names. If it points at your chat transcript, the rest of this post is the work list. If it points at the headline, the hero is already shaped correctly and your budget is going somewhere else.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;p&gt;An AI chat hero is worth building. It just cannot be the thing the browser is waiting on. Make static text the largest element, reserve the transcript's space before the first token lands, keep the submit handler down to visible feedback, and stream anything the model touches instead of blocking the document on it. The demo stays impressive and the metrics stop paying for it.&lt;/p&gt;

</description>
      <category>webvitals</category>
      <category>ai</category>
      <category>performance</category>
      <category>frontend</category>
    </item>
    <item>
      <title>React error #418: five hydration bugs I shipped</title>
      <dc:creator>Ganesh Joshi</dc:creator>
      <pubDate>Sun, 01 Nov 2026 15:43:35 +0000</pubDate>
      <link>https://dev.to/ganeshjoshi/react-error-418-five-hydration-bugs-i-shipped-ae3</link>
      <guid>https://dev.to/ganeshjoshi/react-error-418-five-hydration-bugs-i-shipped-ae3</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was created with AI assistance and reviewed for accuracy before publishing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If your production console shows &lt;code&gt;Minified React error #418&lt;/code&gt;, React is telling you that the HTML the server sent and the first render in the browser did not match. The development build spells it out ("Hydration failed because the server rendered HTML didn't match the client") and shows a diff. The production build gives you a number and a link to &lt;code&gt;react.dev/errors/418&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It is easy to treat it as console noise, because the page still works. It is not free. When hydration fails, React throws away the server-rendered tree and renders the whole root again in the browser. On my site that one error took mobile Total Blocking Time from 0 to 420 ms and the Lighthouse performance score from 96 to 83, and pushed Largest Contentful Paint 1.2 seconds past First Contentful Paint, which had always been equal.&lt;/p&gt;

&lt;p&gt;I have shipped this error five different ways on one Next.js site. Here is each one, how it was found, and the fix that stuck.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Randomness or time in a &lt;code&gt;useState&lt;/code&gt; initializer
&lt;/h2&gt;

&lt;p&gt;The games on the site generate a puzzle, a word or a board. The obvious code seeds state when the component is created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Wrong: the initializer runs on the server and again in the browser.&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;puzzle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPuzzle&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;generatePuzzle&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The initializer runs twice, once during prerendering and once during hydration, and &lt;code&gt;Math.random()&lt;/code&gt; gives a different answer each time. The same thing happened in a lorem ipsum generator, a mock data generator and a password generator. Anything that reads &lt;code&gt;Date.now()&lt;/code&gt; or &lt;code&gt;new Date()&lt;/code&gt; while rendering has the same problem.&lt;/p&gt;

&lt;p&gt;The fix is a deterministic first value, with the random one set after mount:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;puzzle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPuzzle&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="nx"&gt;INITIAL_PUZZLE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// fixed, the same everywhere&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="nf"&gt;setPuzzle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;generatePuzzle&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// runs only in the browser, after hydration&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 first frame shows the fixed puzzle for an instant. For a game that is fine. For anything where the flash matters, render a placeholder of the same size until the effect runs.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Formatting with the machine's locale
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Wrong&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;span&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;downloads&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLocaleString&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;lt;/&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;time&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;post&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="nf"&gt;toLocaleDateString&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;lt;/&lt;/span&gt;&lt;span class="nt"&gt;time&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With no locale argument, &lt;code&gt;toLocaleString()&lt;/code&gt; uses whatever the current machine prefers. The server that prerenders the page (on Vercel, en-US in UTC) and a visitor in Germany or India produce different strings, so the text differs and hydration fails. It never reproduces on your own machine if your machine matches the server.&lt;/p&gt;

&lt;p&gt;Pass the locale you rendered with, or move the formatting after mount:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;span&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;downloads&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLocaleString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;en-US&lt;/span&gt;&lt;span class="dl"&gt;'&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;lt;/&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To catch this class of bug, load the deployed page in a browser with a different locale and time zone. In Playwright that is two options on the context:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;context&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;browser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;newContext&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;locale&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;de-DE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;timezoneId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;America/Los_Angeles&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;
  
  
  3. Browser-only APIs while rendering
&lt;/h2&gt;

&lt;p&gt;Five tools on the site called something during render that only exists, or only behaves the same, in a browser: &lt;code&gt;DOMParser&lt;/code&gt; in an XPath evaluator, &lt;code&gt;document&lt;/code&gt; in an SVG animation tool and an HTML-to-Pug converter, and &lt;code&gt;URL.createObjectURL&lt;/code&gt; in an SVG viewer. That last one is a good example of why this can hide: on the server it produced nothing useful, in the browser it produced a &lt;code&gt;blob:&lt;/code&gt; URL, and the visible symptom was a broken image, not an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Wrong: runs during render on the server too.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectURL&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;Blob&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;svg&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;image/svg+xml&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;The fix is to compute these only after the component has mounted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useHasMounted&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;mounted&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setMounted&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;false&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="nf"&gt;setMounted&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;mounted&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;mounted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useHasMounted&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;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&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="nx"&gt;mounted&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectURL&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;Blob&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;svg&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;image/svg+xml&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="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="nx"&gt;mounted&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;svg&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;Remember to revoke the URL when nothing can reach it any more, but that is a separate bug.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. &lt;code&gt;usePathname()&lt;/code&gt; in a prerendered component
&lt;/h2&gt;

&lt;p&gt;This is the one that cost the most, because I fixed it and then shipped it again.&lt;/p&gt;

&lt;p&gt;The site's navigation highlights the current page. The highlight compared the path:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pathname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;usePathname&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;isActive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pathname&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;href&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 deployed homepage this logged #418 on every visit. It never happened locally, with &lt;code&gt;next dev&lt;/code&gt; or with a local production build. The comparison for the &lt;code&gt;/&lt;/code&gt; link came out differently in Vercel's prerender of the index route than in the browser, so the active class differed between the server HTML and the first client render.&lt;/p&gt;

&lt;p&gt;The first time, I fixed it in one header. Later a different navigation design carried the same comparison into a new component, and it went live again. That second time is the one that took Lighthouse from 96 to 83.&lt;/p&gt;

&lt;p&gt;The fix that stuck is a shared hook that gates only the &lt;code&gt;/&lt;/code&gt; comparison on mount and leaves every other link server-rendered, so the other routes keep their active state from the first paint. Simplified, it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&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;useIsRouteActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&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;pathname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;usePathname&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;mounted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useHasMounted&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;href&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&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="nx"&gt;mounted&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;pathname&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&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="nx"&gt;pathname&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;href&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startsWith&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="nx"&gt;href&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second part of the fix was making sure it could not come back. The repository has a small script that runs before every build and fails when a client component compares &lt;code&gt;usePathname()&lt;/code&gt; directly. A lint rule would do the same job. Writing the lesson down had not been enough.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. A script that edits &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; before React gets there
&lt;/h2&gt;

&lt;p&gt;The site sets a colour scheme and, on festival days, a few &lt;code&gt;data-&lt;/code&gt; attributes on &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; from an inline script, so the right look paints before any JavaScript bundle loads. React then hydrates &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;, finds attributes it did not render, and complains.&lt;/p&gt;

&lt;p&gt;This is the one case where &lt;code&gt;suppressHydrationWarning&lt;/code&gt; is the right tool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="na"&gt;suppressHydrationWarning&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It only silences attribute and text differences on that one element, not its children, which is exactly the scope you want. Two rules keep it honest: only the element the script edits gets it (here &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;, not &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt;), and the script only ever sets attributes, never content.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;suppressHydrationWarning&lt;/code&gt; is not a fix for the first four causes. Those are real differences in what renders, and hiding the warning leaves the re-render in place.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to find yours
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Read the console on the deployed site,&lt;/strong&gt; not just locally. Three of these five never reproduced on my machine.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run the development build once&lt;/strong&gt; to get the readable message and the diff React prints, which names the element and the two values.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Load the page with a foreign locale and time zone.&lt;/strong&gt; It finds cause 2 and anything else that reads the environment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Search the codebase&lt;/strong&gt; for &lt;code&gt;Math.random&lt;/code&gt;, &lt;code&gt;Date.now&lt;/code&gt;, &lt;code&gt;new Date(&lt;/code&gt;, &lt;code&gt;toLocale&lt;/code&gt;, &lt;code&gt;window.&lt;/code&gt;, &lt;code&gt;document.&lt;/code&gt;, &lt;code&gt;DOMParser&lt;/code&gt; and &lt;code&gt;usePathname&lt;/code&gt; inside components that render on the server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The common thread is simple: everything a component renders the first time must be computable on the server, from the same inputs, with the same answer. Anything that depends on the browser, the clock, chance or the visitor waits until after mount.&lt;/p&gt;

</description>
      <category>react</category>
      <category>nextjs</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Node Streams for Large RAG Uploads and Embedding Pipelines</title>
      <dc:creator>Ganesh Joshi</dc:creator>
      <pubDate>Sun, 01 Nov 2026 15:43:33 +0000</pubDate>
      <link>https://dev.to/ganeshjoshi/node-streams-for-large-rag-uploads-and-embedding-pipelines-248n</link>
      <guid>https://dev.to/ganeshjoshi/node-streams-for-large-rag-uploads-and-embedding-pipelines-248n</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was created with AI assistance and reviewed for accuracy before publishing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most retrieval pipelines start the same way. Read the uploaded file, extract its text, split it into chunks, embed the chunks, write them to the database. Written with &lt;code&gt;await readFile()&lt;/code&gt; at the top it is about fifteen lines, and it works perfectly until a customer uploads a document larger than the memory you have.&lt;/p&gt;

&lt;p&gt;The problem is not just peak memory. It is that a single large upload can take down the process handling everyone else's, and that failures arrive after you have already spent money embedding half the file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the memory actually goes
&lt;/h2&gt;

&lt;p&gt;There are three places a naive pipeline buffers, and they compound:&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;buf&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;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;        &lt;span class="c1"&gt;// 1. whole file in memory&lt;/span&gt;
&lt;span class="kd"&gt;const&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="nf"&gt;extractText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;     &lt;span class="c1"&gt;// 2. whole extracted text&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;chunks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;splitIntoChunks&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="c1"&gt;// 3. every chunk, at once&lt;/span&gt;
&lt;span class="k"&gt;await&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;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chunks&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;embed&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;    &lt;span class="c1"&gt;// and every request in flight&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A 400MB PDF can hold well over a gigabyte across those stages before a single embedding call returns. The final line is the worst of them: &lt;code&gt;Promise.all&lt;/code&gt; over ten thousand chunks issues ten thousand concurrent HTTP requests, which will exhaust sockets, trip provider rate limits, and produce a rejection that discards all the successful work alongside the failed call.&lt;/p&gt;

&lt;h2&gt;
  
  
  Streams turn peak memory into steady memory
&lt;/h2&gt;

&lt;p&gt;The stream version processes a window at a time, so memory tracks chunk size rather than file size:&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;pipeline&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;node:stream/promises&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;createReadStream&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;node:fs&lt;/span&gt;&lt;span class="dl"&gt;'&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;pipeline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;createReadStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;extractTextStream&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;      &lt;span class="c1"&gt;// Transform: bytes to text&lt;/span&gt;
  &lt;span class="nf"&gt;chunkStream&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;size&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="na"&gt;overlap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="nf"&gt;embedStream&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;concurrency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="nf"&gt;writeToDatabase&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;Use &lt;code&gt;pipeline&lt;/code&gt; from &lt;code&gt;node:stream/promises&lt;/code&gt; rather than chaining &lt;code&gt;.pipe()&lt;/code&gt;. It propagates errors through every stage and destroys the whole chain when one fails, which is the difference between a clean failure and a leaked file descriptor plus a half-written batch. Chained &lt;code&gt;.pipe()&lt;/code&gt; calls do neither.&lt;/p&gt;

&lt;h2&gt;
  
  
  Backpressure is the part that matters
&lt;/h2&gt;

&lt;p&gt;Backpressure is the mechanism that makes this work, and it is easy to accidentally disable.&lt;/p&gt;

&lt;p&gt;When a writable stage cannot keep up, it stops accepting data, and that signal travels back up the chain until the file read pauses. Embedding is always the slowest stage in a RAG pipeline, by orders of magnitude: parsing is CPU-bound and fast, embedding is a network round trip. Backpressure is what stops the parser from racing ahead and queuing the entire document in front of the embedder.&lt;/p&gt;

&lt;p&gt;You lose it the moment a stage buffers internally:&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;// Breaks backpressure: collects everything, then acts.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;all&lt;/span&gt; &lt;span class="o"&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;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;all&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&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="c1"&gt;// Preserves it: one item at a time, with await between.&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;source&lt;/span&gt;&lt;span class="p"&gt;)&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;handle&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;The &lt;code&gt;for await&lt;/code&gt; loop is the simplest correct consumer. It will not request the next chunk until the body has finished, so the upstream source stays paused while you work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Batch the embedding calls
&lt;/h2&gt;

&lt;p&gt;Embedding one chunk per request wastes most of the round trip. Every provider accepts arrays, and a batch of 50 to 100 costs roughly the same wall-clock time as a batch of one.&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;batched&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&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;batch&lt;/span&gt; &lt;span class="o"&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;item&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;batch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;batch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;batch&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;batch&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="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;batch&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="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;batch&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// the tail matters&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;batch&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nf"&gt;batched&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chunks&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;vectors&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;embedMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;batch&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;c&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;c&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="k"&gt;await&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;insertMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;batch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;vectors&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;Forgetting the final partial batch is a common bug, and a quiet one: every document silently loses its last few dozen chunks, which usually includes the conclusion.&lt;/p&gt;

&lt;p&gt;Insert as you go rather than accumulating vectors for a single write at the end. Embeddings are large, holding them all defeats the purpose of streaming, and incremental writes mean a failure at 80% keeps the first 80%.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make it resumable, because it will fail
&lt;/h2&gt;

&lt;p&gt;A long ingest will be interrupted. The process restarts, the network blips, the provider rate limits you. If a failure means starting over, you pay for every embedding twice.&lt;/p&gt;

&lt;p&gt;Record progress alongside the data:&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;await&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;insertMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;batch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;vectors&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;await&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;updateJob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jobId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;lastChunkIndex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;batch&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;index&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On restart, skip to &lt;code&gt;lastChunkIndex&lt;/code&gt; and continue. This ordering matters: write the chunks first, then the progress marker. If it crashes between the two you re-embed one batch, which is cheap. Reverse the order and a crash loses that batch entirely, with the job convinced it succeeded.&lt;/p&gt;

&lt;p&gt;For rate limits specifically, retry the batch with exponential backoff rather than failing the job. A 429 is a request to wait, not an error condition.&lt;/p&gt;

&lt;h2&gt;
  
  
  Verify with the number that matters
&lt;/h2&gt;

&lt;p&gt;The test for all of this is not whether it completes. It is whether memory stays flat.&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="nf"&gt;setInterval&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;mb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;memoryUsage&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;heapUsed&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1024&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="s2"&gt;`heap: &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;mb&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;MB`&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run your largest realistic document through and watch. A correct streaming pipeline holds roughly steady regardless of file size. If the line climbs with the document, something in the chain is still buffering, and the profile will tell you which stage by where the climb begins.&lt;/p&gt;

&lt;p&gt;That single graph is worth more than any amount of reasoning about the code, because every one of the mistakes above looks fine when you read it.&lt;/p&gt;

</description>
      <category>node</category>
      <category>rag</category>
      <category>ai</category>
      <category>streams</category>
    </item>
    <item>
      <title>“Mindblowing” AI Results? Read the Paper or Model Card First</title>
      <dc:creator>Ganesh Joshi</dc:creator>
      <pubDate>Sun, 01 Nov 2026 15:43:02 +0000</pubDate>
      <link>https://dev.to/ganeshjoshi/mindblowing-ai-results-read-the-paper-or-model-card-first-52nk</link>
      <guid>https://dev.to/ganeshjoshi/mindblowing-ai-results-read-the-paper-or-model-card-first-52nk</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was created with AI assistance and reviewed for accuracy before publishing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A striking benchmark number crosses your feed. The claim is usually accurate and the framing is usually wrong, because the number arrived without the conditions that produced it. Those conditions are in the paper, and reading for them takes about fifteen minutes.&lt;/p&gt;

&lt;p&gt;This is a practical skill rather than an academic one. You do not need to evaluate the methodology like a reviewer. You need to work out whether the result applies to what you are building.&lt;/p&gt;

&lt;h2&gt;
  
  
  Read it out of order
&lt;/h2&gt;

&lt;p&gt;Nobody has time to read papers linearly, and you do not need to. Four sections answer most questions:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Abstract&lt;/strong&gt;, for the claim as the authors state it. Compare this to the claim as it reached you. The difference is often the entire story.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Limitations&lt;/strong&gt;, which is where the caveats live and is the section least likely to survive summarisation. Many venues now require it, which is a gift to anyone reading quickly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Experimental setup&lt;/strong&gt;, for what was actually measured. This is where you find out that the evaluation used a specific subset, or allowed multiple attempts, or gave the model tools.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Results tables&lt;/strong&gt;, including the baselines. A large improvement over a weak baseline is a different result from a small improvement over a strong one.&lt;/p&gt;

&lt;p&gt;Introductions and related work are the least useful parts for this purpose. Skip them without guilt.&lt;/p&gt;

&lt;h2&gt;
  
  
  The questions that change what a number means
&lt;/h2&gt;

&lt;p&gt;A few specifics account for most of the distance between a headline and reality:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How many attempts?&lt;/strong&gt; A score where the model gets several tries and any success counts is not comparable to one where the first answer is the answer. Both are legitimate measurements of different things, and only one resembles a user typing a question once.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What was the model given?&lt;/strong&gt; Tool access, retrieval, a scaffold that retries and self-corrects, or a carefully engineered prompt. All are reasonable in research and none are free in production.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Which subset?&lt;/strong&gt; Evaluations often report on a filtered slice, sometimes for good reasons. The filtering criteria decide how much the number generalises.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Contamination?&lt;/strong&gt; For benchmarks that predate the model's training data, some of the test set may have been in it. Good papers address this directly. Its absence is worth noticing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Who is comparing?&lt;/strong&gt; A lab reporting its own model against competitors chose the evaluation conditions. That is not dishonest, and it is not neutral either.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model cards deserve more attention than papers
&lt;/h2&gt;

&lt;p&gt;For anything you are considering using, the model card or system card is usually the higher-value read. It is written by the people who built the thing, it states intended use and known weaknesses, and it tends to be blunter about limitations than a launch post.&lt;/p&gt;

&lt;p&gt;It is also where you find the operational facts that determine whether you can build on it: context limits, supported modalities, safety behaviour, and the conditions under which performance degrades.&lt;/p&gt;

&lt;p&gt;The pattern worth internalising is that the marketing post and the model card describe the same system with very different emphases, and the second one is the one written for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wait for someone else to try it
&lt;/h2&gt;

&lt;p&gt;The strongest signal on a surprising result arrives weeks later, when independent groups attempt to reproduce it. Reproductions get a fraction of the attention of the original and carry considerably more information.&lt;/p&gt;

&lt;p&gt;There is usually no cost to waiting. If a technique genuinely works, it will still work in a month, and by then you will know whether it survives contact with someone who did not have an interest in the outcome.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this looks like in practice
&lt;/h2&gt;

&lt;p&gt;You do not need to do this for everything. Match the effort to the decision.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;Effort&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Curiosity&lt;/td&gt;
&lt;td&gt;Read the abstract, move on&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Might affect a roadmap decision&lt;/td&gt;
&lt;td&gt;Abstract, limitations, setup&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Planning to build on it&lt;/td&gt;
&lt;td&gt;The above, plus the model card and any reproductions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Citing it to convince someone&lt;/td&gt;
&lt;td&gt;Read it properly, including baselines&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That last row is the one worth being strict about. Repeating a number you have not verified attaches your credibility to someone else's summary, and when it turns out the evaluation allowed ten attempts, it is your recommendation that was wrong.&lt;/p&gt;

&lt;p&gt;None of this requires cynicism. Real progress happens constantly and is worth being excited about. It just means the excitement should come after the fifteen minutes rather than before, and that you can say where a claim came from when somebody asks.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>research</category>
      <category>science</category>
      <category>learning</category>
    </item>
    <item>
      <title>CSP and Third-Party AI Widgets: Copilot, Chat, and Scripts</title>
      <dc:creator>Ganesh Joshi</dc:creator>
      <pubDate>Sun, 01 Nov 2026 15:43:01 +0000</pubDate>
      <link>https://dev.to/ganeshjoshi/csp-and-third-party-ai-widgets-copilot-chat-and-scripts-46h6</link>
      <guid>https://dev.to/ganeshjoshi/csp-and-third-party-ai-widgets-copilot-chat-and-scripts-46h6</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was created with AI assistance and reviewed for accuracy before publishing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Adding a vendor's AI chat widget is a two-line change to your HTML and a much larger change to your security posture. The script runs with the same privileges as your own code: it can read the DOM, read &lt;code&gt;localStorage&lt;/code&gt;, and make requests as the logged-in user.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP" rel="noopener noreferrer"&gt;Content Security Policy&lt;/a&gt; is how you bound that. The catch is that most widget install guides ask you to weaken exactly the directives that were doing the work.&lt;/p&gt;

&lt;h2&gt;
  
  
  The install instructions are the problem
&lt;/h2&gt;

&lt;p&gt;Vendor docs commonly suggest one of these:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;script-src 'self' 'unsafe-inline' 'unsafe-eval' https://widget.vendor.com;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each addition removes a specific protection. &lt;code&gt;'unsafe-inline'&lt;/code&gt; allows any injected &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag to execute, which is the primary thing CSP exists to prevent. &lt;code&gt;'unsafe-eval'&lt;/code&gt; allows strings to be compiled into code, re-opening a class of injection that is otherwise dead. A wildcard host allows anything the vendor ever hosts, including a compromised subdomain.&lt;/p&gt;

&lt;p&gt;You can usually avoid all three. Treat the vendor's snippet as a starting point to be narrowed, not a requirement.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nonces instead of unsafe-inline
&lt;/h2&gt;

&lt;p&gt;If the widget needs an inline bootstrap, give that specific script a nonce rather than allowing every inline script on the page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// middleware.ts&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randomUUID&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;csp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="s2"&gt;`script-src 'self' 'nonce-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;' https://widget.vendor.com`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;`connect-src 'self' https://api.vendor.com`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;`frame-src https://widget.vendor.com`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;`img-src 'self' data: https://cdn.vendor.com`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;`style-src 'self' 'nonce-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;nonce&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="s2"&gt;`object-src 'none'`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;`base-uri 'self'`&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="s1"&gt;; &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;A nonce is generated per response and must therefore come from a dynamically rendered layer. That is a real constraint: a fully static page cannot carry a per-request nonce, so on prerendered routes you are choosing between hashes and moving the widget behind an iframe.&lt;/p&gt;

&lt;p&gt;Hashes work when the inline content is fixed. Compute the SHA-256 of the script body and allowlist that digest. It breaks the moment the vendor changes a byte, which is either a maintenance burden or an early warning, depending on your temperament.&lt;/p&gt;

&lt;h2&gt;
  
  
  Know which directive each behaviour needs
&lt;/h2&gt;

&lt;p&gt;Widgets fail in confusing ways when one directive is missing, because the browser blocks silently from the page's perspective:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Directive&lt;/th&gt;
&lt;th&gt;Governs&lt;/th&gt;
&lt;th&gt;Missing it looks like&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;script-src&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Loading the widget bundle&lt;/td&gt;
&lt;td&gt;Nothing renders at all&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;connect-src&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Its API calls and WebSockets&lt;/td&gt;
&lt;td&gt;Widget appears, never responds&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;frame-src&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Widgets that render in an iframe&lt;/td&gt;
&lt;td&gt;Empty box where the panel should be&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;img-src&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Avatars, logos, uploaded images&lt;/td&gt;
&lt;td&gt;Broken images inside the widget&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;style-src&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Injected stylesheets&lt;/td&gt;
&lt;td&gt;Unstyled widget, correct behaviour&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The second row causes the most support tickets. The panel opens, the user types, and nothing comes back, because &lt;code&gt;connect-src&lt;/code&gt; is blocking the API host while &lt;code&gt;script-src&lt;/code&gt; allows the bundle. The two are frequently different domains.&lt;/p&gt;

&lt;h2&gt;
  
  
  Roll it out in report-only first
&lt;/h2&gt;

&lt;p&gt;Never enforce a new policy blind. &lt;code&gt;Content-Security-Policy-Report-Only&lt;/code&gt; applies the rules, reports what would have been blocked, and blocks nothing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Content-Security-Policy-Report-Only: script-src 'self' https://widget.vendor.com; report-uri /api/csp-report
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it for long enough to cover real traffic, including the paths where the widget behaves differently. Then read the reports, add the origins you genuinely need, and only afterwards switch the header name to enforce.&lt;/p&gt;

&lt;p&gt;Expect noise. Browser extensions inject scripts into your pages and generate violations you cannot fix and should not act on. Filter reports by whether the blocked URI is something you recognise before treating any of it as a signal.&lt;/p&gt;

&lt;h2&gt;
  
  
  A widget you cannot trust belongs in an iframe
&lt;/h2&gt;

&lt;p&gt;CSP limits where code comes from. It does not limit what that code does once it is running in your page.&lt;/p&gt;

&lt;p&gt;If the widget handles anything sensitive, or if you simply do not want a third party reading your DOM, put it in an iframe on a different origin. The browser's same-origin policy then does the isolation that CSP cannot: the widget cannot read your cookies, your &lt;code&gt;localStorage&lt;/code&gt;, or the contents of the page around it. Communication goes through &lt;code&gt;postMessage&lt;/code&gt;, where you control exactly what crosses the boundary.&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="nb"&gt;window&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;message&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="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;origin&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://widget.vendor.com&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="c1"&gt;// check first&lt;/span&gt;
  &lt;span class="nf"&gt;handle&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;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;That origin check is not optional. Without it any page that can reach your frame can send it messages, and the handler will process them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test it on the built site, not in development
&lt;/h2&gt;

&lt;p&gt;This is the part most teams get wrong. Development servers routinely relax CSP because bundler tooling needs &lt;code&gt;'unsafe-eval'&lt;/code&gt; for hot reloading. Which means development cannot tell you whether production will work.&lt;/p&gt;

&lt;p&gt;Verify against a production build, and listen for violations directly rather than reading the console by eye:&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="nb"&gt;document&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;securitypolicyviolation&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="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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;violatedDirective&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;blockedURI&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;That event fires even when the failure is swallowed by a &lt;code&gt;try/catch&lt;/code&gt; inside the widget, which is common. A vendor bundle that catches its own network errors and renders an empty state gives you no console error at all, and that event is the only thing that tells you the policy was the cause.&lt;/p&gt;

&lt;h2&gt;
  
  
  The shape of a good outcome
&lt;/h2&gt;

&lt;p&gt;You should end with a policy that names every vendor origin explicitly, has no wildcards, no &lt;code&gt;'unsafe-eval'&lt;/code&gt;, and no blanket &lt;code&gt;'unsafe-inline'&lt;/code&gt;. It should be verified against a production build with a violation listener attached, and anything you genuinely cannot constrain should be sitting in a cross-origin iframe where it can only do damage to itself.&lt;/p&gt;

</description>
      <category>csp</category>
      <category>security</category>
      <category>ai</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>Anthropic Model Names and Deprecations: Build a Changelog Habit</title>
      <dc:creator>Ganesh Joshi</dc:creator>
      <pubDate>Sun, 11 Oct 2026 15:18:59 +0000</pubDate>
      <link>https://dev.to/ganeshjoshi/anthropic-model-names-and-deprecations-build-a-changelog-habit-1jn9</link>
      <guid>https://dev.to/ganeshjoshi/anthropic-model-names-and-deprecations-build-a-changelog-habit-1jn9</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was created with AI assistance and reviewed for accuracy before publishing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Every model provider retires models. The identifier you pass in the &lt;code&gt;model&lt;/code&gt; field is a version string with a lifecycle, and unlike your npm dependencies nothing in your toolchain tells you when it is about to end.&lt;/p&gt;

&lt;p&gt;The failure is undramatic and annoying. A string that has worked for a year starts returning errors, and the person on call has to find where it is configured, which turns out to be five places, three of them not in the application repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  Find every place the string lives
&lt;/h2&gt;

&lt;p&gt;Before anything else, find out how many there are. In most codebases the answer is more than expected:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-rn&lt;/span&gt; &lt;span class="s2"&gt;"claude-"&lt;/span&gt; &lt;span class="nt"&gt;--include&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"*.ts"&lt;/span&gt; &lt;span class="nt"&gt;--include&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"*.py"&lt;/span&gt; &lt;span class="nt"&gt;--include&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"*.env*"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--include&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"*.yml"&lt;/span&gt; &lt;span class="nt"&gt;--include&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"*.tf"&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; node_modules
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ones that hurt are outside the application code: a Terraform variable, a CI secret, a dashboard config, a seed script, a colleague's local &lt;code&gt;.env&lt;/code&gt;. Each is a separate thing to update under time pressure, and the ones you forget fail later than the ones you find.&lt;/p&gt;

&lt;h2&gt;
  
  
  One module owns the mapping
&lt;/h2&gt;

&lt;p&gt;Collapse them into a single place that maps a role to an identifier, and have the application refer to roles:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// models.ts is the only file that names a model.&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;MODELS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MODEL_CHAT&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;claude-sonnet-4-5&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;summarize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MODEL_SUMMARIZE&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;claude-haiku-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="k"&gt;as&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Everywhere else:&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;messages&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="na"&gt;model&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="nx"&gt;chat&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 environment variable allows an override without a deploy, which is what you want during an incident. The default in code means a missing variable does not produce an empty model field and a confusing error.&lt;/p&gt;

&lt;p&gt;Naming by role rather than by model is the part that pays off. &lt;code&gt;MODELS.summarize&lt;/code&gt; stays meaningful across three model generations; &lt;code&gt;CLAUDE_HAIKU_MODEL&lt;/code&gt; becomes a lie the first time you point it at something else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Version the choice deliberately
&lt;/h2&gt;

&lt;p&gt;Providers usually offer both a dated identifier and a moving alias. They fail in opposite directions.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Form&lt;/th&gt;
&lt;th&gt;Behaviour&lt;/th&gt;
&lt;th&gt;Risk&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Dated, e.g. &lt;code&gt;claude-haiku-4-5-20251001&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Frozen&lt;/td&gt;
&lt;td&gt;Sunsets on a schedule&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alias, e.g. a latest-style pointer&lt;/td&gt;
&lt;td&gt;Moves&lt;/td&gt;
&lt;td&gt;Behaviour changes without a deploy&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For production, the dated form is usually right, for the same reason you commit a lockfile: you want the thing you tested to be the thing that runs. The cost is that you own the upgrade, which is the point.&lt;/p&gt;

&lt;p&gt;Aliases are fine in development and evaluation environments, where discovering a change early is useful rather than alarming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Subscribe, then check on a schedule
&lt;/h2&gt;

&lt;p&gt;Providers publish deprecation notices in documentation and changelogs, and send console or email notices to account owners. Two habits close the gap.&lt;/p&gt;

&lt;p&gt;Route provider notification emails to a shared inbox rather than an individual. Deprecation notices have a way of arriving while the person who set up the account is on holiday, or after they have left.&lt;/p&gt;

&lt;p&gt;Then check the deprecations page as a recurring task, roughly monthly. That sounds primitive next to automation, and it is more reliable than most automation, because a page layout change silently breaks a scraper while a calendar reminder does not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the upgrade routine boring
&lt;/h2&gt;

&lt;p&gt;When a new identifier is available, the path should already exist:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Point staging at the new model via the environment variable.&lt;/li&gt;
&lt;li&gt;Run your evaluation set against it and compare outputs to the current model.&lt;/li&gt;
&lt;li&gt;Check cost and latency, since both frequently change.&lt;/li&gt;
&lt;li&gt;Promote by changing the production variable.&lt;/li&gt;
&lt;li&gt;Update the default in code so the variable is no longer load-bearing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Step two is the one that requires prior investment. If you do not have a stored set of representative inputs with known-good outputs, every model change is a leap, and the only feedback is user complaints a week later. A few dozen cases is enough to catch a regression in format, tone, or refusal behaviour.&lt;/p&gt;

&lt;p&gt;Cost and latency deserve their own check because they move independently of quality. A newer model that is better and three times the price is a product decision, not an automatic upgrade.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fail loudly on an unknown model
&lt;/h2&gt;

&lt;p&gt;Add a startup assertion that the configured identifier is one you recognise:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;KNOWN&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;Set&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;values&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="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;KNOWN&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="nx"&gt;configured&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="s2"&gt;`Unknown model: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;configured&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A typo in an environment variable otherwise surfaces as a runtime API error on the first user request, in whichever code path happens to run first. Failing at boot puts the error in the deploy log, where someone is already watching.&lt;/p&gt;

&lt;p&gt;None of this is sophisticated. It is the same discipline you already apply to dependency versions, extended to a string that behaves exactly like one but arrived through a different door.&lt;/p&gt;

</description>
      <category>anthropic</category>
      <category>claude</category>
      <category>api</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
