<?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: Khaled Saeed</title>
    <description>The latest articles on DEV Community by Khaled Saeed (@khaledsaeed18).</description>
    <link>https://dev.to/khaledsaeed18</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%2F3318749%2F4b825580-54c0-43f4-933a-71ae06de53c7.png</url>
      <title>DEV Community: Khaled Saeed</title>
      <link>https://dev.to/khaledsaeed18</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/khaledsaeed18"/>
    <language>en</language>
    <item>
      <title>I tried to prove my secret-sharing tool correct in Lean 4. The proof found an attack.</title>
      <dc:creator>Khaled Saeed</dc:creator>
      <pubDate>Fri, 04 Dec 2026 22:46:53 +0000</pubDate>
      <link>https://dev.to/khaledsaeed18/i-tried-to-prove-my-secret-sharing-tool-correct-in-lean-4-the-proof-found-an-attack-3bol</link>
      <guid>https://dev.to/khaledsaeed18/i-tried-to-prove-my-secret-sharing-tool-correct-in-lean-4-the-proof-found-an-attack-3bol</guid>
      <description>&lt;p&gt;I'm taking a cryptography course in my master's degree this semester. When we reached AES, we had to learn &lt;strong&gt;Galois fields&lt;/strong&gt;: a strange kind of arithmetic where a byte plus itself is zero, and multiplication is done "modulo a polynomial". In class it is one step inside AES.&lt;/p&gt;

&lt;p&gt;The same field is the heart of a project I have been building: &lt;a href="https://github.com/KhaledSaeed18/qedshare" rel="noopener noreferrer"&gt;&lt;strong&gt;qedshare&lt;/strong&gt;&lt;/a&gt;, a small command-line tool for &lt;strong&gt;Shamir's Secret Sharing&lt;/strong&gt;, written in &lt;a href="https://lean-lang.org" rel="noopener noreferrer"&gt;Lean 4&lt;/a&gt;. What makes it different from other tools is that its important properties are &lt;strong&gt;mathematical theorems checked by a computer&lt;/strong&gt;, not just tests.&lt;/p&gt;

&lt;p&gt;I'll explain the idea in simple terms first, then show how the proofs are built, and at the end I'll list what is still not proven.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Code:&lt;/strong&gt; &lt;a href="https://github.com/KhaledSaeed18/qedshare" rel="noopener noreferrer"&gt;https://github.com/KhaledSaeed18/qedshare&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API docs&lt;/strong&gt; (every definition and theorem): &lt;a href="https://khaledsaeed18.github.io/qedshare/" rel="noopener noreferrer"&gt;https://khaledsaeed18.github.io/qedshare/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Download:&lt;/strong&gt; &lt;a href="https://github.com/KhaledSaeed18/qedshare/releases/latest" rel="noopener noreferrer"&gt;latest release&lt;/a&gt; (Linux and macOS binaries)&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/KhaledSaeed18" rel="noopener noreferrer"&gt;
        KhaledSaeed18
      &lt;/a&gt; / &lt;a href="https://github.com/KhaledSaeed18/qedshare" rel="noopener noreferrer"&gt;
        qedshare
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Formally verified Shamir's Secret Sharing CLI in Lean 4 over GF(2^8). Reconstruction correctness and perfect secrecy are theorems checked by Lean; branch-free, table-free field arithmetic.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;qedshare&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Shamir's Secret Sharing, with the important parts proven in Lean 4.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Pronounced "Q-E-D share". Q.E.D. is how a proof ends.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/KhaledSaeed18/qedshare/actions/workflows/ci.yml" rel="noopener noreferrer"&gt;&lt;img src="https://github.com/KhaledSaeed18/qedshare/actions/workflows/ci.yml/badge.svg" alt="CI"&gt;&lt;/a&gt;
&lt;a href="https://khaledsaeed18.github.io/qedshare/" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://github.com/KhaledSaeed18/qedshare/actions/workflows/docs.yml/badge.svg" alt="API docs"&gt;&lt;/a&gt;
&lt;a href="https://github.com/KhaledSaeed18/qedshare/releases" rel="noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/778f2a8fd4170c21c74da605d7625a441753e6c9d49e61de846e6d7d91fa9dfe/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f4b68616c6564536165656431382f71656473686172653f6c6162656c3d72656c6561736526636f6c6f723d626c7565" alt="Release"&gt;&lt;/a&gt;
&lt;a href="https://github.com/KhaledSaeed18/qedshare/lean-toolchain" rel="noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/86cd5b14981c3bc7ef652e769b5beed10e193fe8206a5673b01ddc570949765b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c65616e2d76342e33362e302d306233643931" alt="Lean"&gt;&lt;/a&gt;
&lt;a href="https://github.com/KhaledSaeed18/qedshare#guarantees" rel="noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/9f435db36bd4a83228d753a04b014fb2a142d110d3569a1c685ac5b1a9467580/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736f7272792d302d627269676874677265656e" alt="sorry"&gt;&lt;/a&gt;
&lt;a href="https://github.com/KhaledSaeed18/qedshare/proofs/QedShareProofs/Axioms.lean" rel="noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/16f5a7999013a2cb796630d4c9bd0edbc6c5dd4fc2600d2303bb979bea8baf96/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6178696f6d25323061756469742d696e25323043492d627269676874677265656e" alt="Axiom audit"&gt;&lt;/a&gt;
&lt;a href="https://github.com/KhaledSaeed18/qedshare/LICENSE" rel="noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/1839907eb9fb8d6d05ff532e1870a7008790873fdcc0c17480337c508a844c23/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4170616368652d2d322e302d626c7565" alt="License"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/KhaledSaeed18/qedshare#quick-start" rel="noopener noreferrer"&gt;Quick start&lt;/a&gt; ·
&lt;a href="https://github.com/KhaledSaeed18/qedshare#how-it-works" rel="noopener noreferrer"&gt;How it works&lt;/a&gt; ·
&lt;a href="https://github.com/KhaledSaeed18/qedshare#guarantees" rel="noopener noreferrer"&gt;Guarantees&lt;/a&gt; ·
&lt;a href="https://github.com/KhaledSaeed18/qedshare#word-shares" rel="noopener noreferrer"&gt;Word shares&lt;/a&gt; ·
&lt;a href="https://github.com/KhaledSaeed18/qedshare#threat-model" rel="noopener noreferrer"&gt;Threat model&lt;/a&gt; ·
&lt;a href="https://khaledsaeed18.github.io/qedshare/" rel="nofollow noopener noreferrer"&gt;API docs&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Split a secret (a password-manager master key, an SSH key, a wallet seed) into &lt;code&gt;n&lt;/code&gt; shares so that
&lt;strong&gt;any &lt;code&gt;k&lt;/code&gt; shares rebuild it&lt;/strong&gt; and &lt;strong&gt;any &lt;code&gt;k − 1&lt;/code&gt; shares reveal nothing at all&lt;/strong&gt;. qedshare is a
small command-line tool whose key properties are &lt;strong&gt;theorems checked by Lean's kernel&lt;/strong&gt;, about the
exact code the executable runs, not just tests.&lt;/p&gt;
&lt;div class="markdown-alert markdown-alert-warning"&gt;
&lt;p class="markdown-alert-title"&gt;Warning&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Early development, not audited.&lt;/strong&gt; The CLI works and every guarantee below is proven (zero
&lt;code&gt;sorry&lt;/code&gt;; CI fails on any). Do not use it for real secrets yet.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Contents&lt;/h2&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/KhaledSaeed18/qedshare#highlights" rel="noopener noreferrer"&gt;Highlights&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/KhaledSaeed18/qedshare#quick-start" rel="noopener noreferrer"&gt;Quick start&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/KhaledSaeed18/qedshare#how-it-works" rel="noopener noreferrer"&gt;How it works&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/KhaledSaeed18/qedshare#usage" rel="noopener noreferrer"&gt;Usage&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/KhaledSaeed18/qedshare#word-shares" rel="noopener noreferrer"&gt;Word shares&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/KhaledSaeed18/qedshare#guarantees" rel="noopener noreferrer"&gt;Guarantees&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/KhaledSaeed18/qedshare#what-you-have-to-trust" rel="noopener noreferrer"&gt;What you have to trust&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/KhaledSaeed18/qedshare#threat-model" rel="noopener noreferrer"&gt;Threat model&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/KhaledSaeed18/qedshare#share-format" rel="noopener noreferrer"&gt;Share format&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/KhaledSaeed18/qedshare#design" rel="noopener noreferrer"&gt;Design&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/KhaledSaeed18/qedshare#building" rel="noopener noreferrer"&gt;Building&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/KhaledSaeed18/qedshare#prior-work" rel="noopener noreferrer"&gt;Prior&lt;/a&gt;…&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/KhaledSaeed18/qedshare" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Split a secret into &lt;code&gt;n&lt;/code&gt; shares. Any &lt;code&gt;k&lt;/code&gt; of them rebuild it. Any &lt;code&gt;k − 1&lt;/code&gt; reveal nothing.&lt;/li&gt;
&lt;li&gt;"It rebuilds the secret", "fewer shares reveal nothing" and "forged shares are refused" are proven for &lt;strong&gt;every&lt;/strong&gt; input, and Lean checks the proofs on every build.&lt;/li&gt;
&lt;li&gt;One proof attempt failed, and the reason was a real attack. The fix is proven too.&lt;/li&gt;
&lt;li&gt;Not proven: file handling, the OS random number generator, the compiler, timing. A normal review found real bugs there.&lt;/li&gt;
&lt;li&gt;It is not audited. Please don't use it for real secrets yet.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;You have one very important secret, for example the master password of your password manager. You have two fears:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Losing it.&lt;/strong&gt; With one copy, a lost copy means the secret is gone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Someone stealing it.&lt;/strong&gt; With many copies, any stolen copy gives everything away.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Secret sharing solves both. You cut the secret into pieces called &lt;strong&gt;shares&lt;/strong&gt; and you choose two numbers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;n&lt;/code&gt;: how many shares to make (say 5),&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;k&lt;/code&gt;: how many are needed to rebuild the secret (say 3).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Any 3 of the 5 shares rebuild the secret, so you can lose 2. Someone who holds only 2 learns &lt;strong&gt;nothing at all&lt;/strong&gt;. Not "very little". Nothing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjcr49qjhq2svfu3ybukd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjcr49qjhq2svfu3ybukd.jpg" alt="Five sealed envelopes on a wooden desk: three together next to a brass key, two set apart" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The idea, with a line
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F35a5dw6avjj0rwdzyqc3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F35a5dw6avjj0rwdzyqc3.jpg" alt="A blue thread pulled tight between two brass pins on a sheet of paper" width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Think of a straight line on paper. One point is not enough to know the line, because infinitely many lines pass through it. Two points fix it exactly.&lt;/p&gt;

&lt;p&gt;So for "any 2 shares rebuild it":&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Put the secret where the line crosses the vertical axis (at &lt;code&gt;x = 0&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Draw a &lt;strong&gt;random&lt;/strong&gt; line through that point.&lt;/li&gt;
&lt;li&gt;Give each person one other point on the line: &lt;code&gt;(1, f(1))&lt;/code&gt;, &lt;code&gt;(2, f(2))&lt;/code&gt;, and so on.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Two people together can redraw the line and read the secret. One person alone has one point, and every possible secret still fits it.&lt;/p&gt;

&lt;p&gt;For &lt;code&gt;k = 3&lt;/code&gt; you use a curve that needs 3 points (&lt;code&gt;f(x) = s + r₁·x + r₂·x²&lt;/code&gt;), and so on. Rebuilding the curve from its points is called &lt;strong&gt;Lagrange interpolation&lt;/strong&gt;. Longer secrets are split one byte at a time, each byte with its own random curve.&lt;/p&gt;

&lt;p&gt;This is Adi Shamir's scheme from 1979. It fits on a napkin.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where my course comes in
&lt;/h2&gt;

&lt;p&gt;Normal numbers don't work well here, because they grow without limit and division gives fractions. Shamir's scheme needs a &lt;strong&gt;finite field&lt;/strong&gt;: a fixed set of values where you can add, subtract, multiply and divide, and always stay inside the set.&lt;/p&gt;

&lt;p&gt;qedshare uses &lt;strong&gt;GF(2⁸)&lt;/strong&gt;, the same 256-element field as AES:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;every byte is one element,&lt;/li&gt;
&lt;li&gt;addition is XOR, so &lt;code&gt;a + a = 0&lt;/code&gt; and subtraction is the same as addition,&lt;/li&gt;
&lt;li&gt;multiplication is polynomial multiplication modulo &lt;code&gt;x⁸ + x⁴ + x³ + x + 1&lt;/code&gt; (&lt;code&gt;0x11B&lt;/code&gt;), the AES polynomial,&lt;/li&gt;
&lt;li&gt;every nonzero byte has an inverse, which is what makes the division in Lagrange interpolation possible.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Several other things from the course show up too:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;In the course&lt;/th&gt;
&lt;th&gt;In qedshare&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Galois fields (the AES chapter)&lt;/td&gt;
&lt;td&gt;All the arithmetic is in GF(2⁸) with the AES polynomial&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The one-time pad, the "unbreakable" cipher&lt;/td&gt;
&lt;td&gt;Shamir has the same kind of security: perfect, not "hard to break"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Random numbers&lt;/td&gt;
&lt;td&gt;The curve must be truly random, like one-time pad key bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Modular inverses and fast exponentiation&lt;/td&gt;
&lt;td&gt;The inverse of a byte is &lt;code&gt;a²⁵⁴&lt;/code&gt;, computed by squaring and multiplying&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GMAC, authentication in GF(2¹²⁸)&lt;/td&gt;
&lt;td&gt;Forged shares are caught with a tag in GF(2¹²⁸)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Secret sharing itself is not in our textbook, but it is built from the same maths.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using it
&lt;/h2&gt;

&lt;p&gt;Split a secret into 5 shares, where any 3 rebuild it:&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;printf&lt;/span&gt; &lt;span class="s1"&gt;'correct horse battery staple\n'&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; secret.txt
$ qedshare split --threshold 3 --shares 5 &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; secret.txt &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; shares.txt
$ cut -c1-60 shares.txt
qs3-f0a935f0-03-01-7dc2c3608c0d11908d1ebcceb19ed816248022835
qs3-f0a935f0-03-02-14b4e934a1b86263c6a68e6ff3647cf916e48a326
qs3-f0a935f0-03-03-0a19582648d607d323d740d227dac68e4610cdc34
qs3-f0a935f0-03-04-ba9cfa6dc2d20546c84a66a4c624818995d83f201
qs3-f0a935f0-03-05-a4314b7f2bbc60f62d3ba819129a3bfec52c78d13
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Give each line to a different person or place. To get the secret back, give any 3 of them to &lt;code&gt;combine&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ sed -n &lt;span class="s1"&gt;'1p;4p;5p'&lt;/span&gt; shares.txt &lt;span class="o"&gt;|&lt;/span&gt; qedshare combine
correct horse battery staple

$ sed -n &lt;span class="s1"&gt;'1p;4p'&lt;/span&gt; shares.txt &lt;span class="o"&gt;|&lt;/span&gt; qedshare combine
qedshare: error: this secret needs 3 shares, but only 2 were given
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A long hex string is fine for copy-paste and awful for a pen. So &lt;code&gt;--words&lt;/code&gt; writes each share as ordinary English words that you can write on paper:&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;printf&lt;/span&gt; &lt;span class="s1"&gt;'send 100 to bob'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; qedshare split -k 2 -n 3 --words
acquire rifle oblige ring cactus oak supreme solar charge casino hotel social be…
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hex shares and word shares can be mixed in one &lt;code&gt;combine&lt;/code&gt;. (Word shares use the BIP-39 word list from crypto wallets, but they are &lt;strong&gt;not&lt;/strong&gt; wallet seed phrases. Only the list is borrowed.)&lt;/p&gt;

&lt;p&gt;Two more commands look at shares without showing the secret:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ sed -n &lt;span class="s1"&gt;'1p;4p;5p'&lt;/span&gt; shares.txt &lt;span class="o"&gt;|&lt;/span&gt; qedshare verify
ok: the 3 shares fit together and rebuild a secret of 29 bytes

$ sed -n &lt;span class="s1"&gt;'1p;4p'&lt;/span&gt; shares.txt &lt;span class="o"&gt;|&lt;/span&gt; qedshare info
stdin line 1: qs3 hex share, &lt;span class="nb"&gt;set&lt;/span&gt; f0a935f0, index 1, threshold 3, secret of 29 bytes
stdin line 2: qs3 hex share, &lt;span class="nb"&gt;set&lt;/span&gt; f0a935f0, index 4, threshold 3, secret of 29 bytes
2 different shares of &lt;span class="nb"&gt;set&lt;/span&gt; f0a935f0: 1 more needed to rebuild the secret &lt;span class="o"&gt;(&lt;/span&gt;threshold 3&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The secret is only ever read from stdin or a file, never from the command line, where it would end up in your shell history. Install steps are in the &lt;a href="https://github.com/KhaledSaeed18/qedshare#quick-start" rel="noopener noreferrer"&gt;quick start&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why proofs, when the maths is this simple?
&lt;/h2&gt;

&lt;p&gt;Because implementations still get it wrong, in small ways that matter a lot. Some classic bugs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Handing out the share at &lt;code&gt;x = 0&lt;/code&gt;.&lt;/strong&gt; That point &lt;em&gt;is&lt;/em&gt; the secret.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Forcing the top random coefficient to be nonzero.&lt;/strong&gt; It sounds like harmless hygiene ("make sure the curve really has degree &lt;code&gt;k − 1&lt;/code&gt;"). It quietly breaks secrecy: with &lt;code&gt;k − 1&lt;/code&gt; shares, some secrets become more likely than others.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accepting duplicate shares,&lt;/strong&gt; or shares from two different splits.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Using normal &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;*&lt;/code&gt; on bytes&lt;/strong&gt; where field operations were meant. The code runs, the output looks fine, and it is wrong.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiplying with lookup tables&lt;/strong&gt; on secret data, which can leak through cache timing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A &lt;strong&gt;test&lt;/strong&gt; tries examples: "I split this secret, combined it, and got it back, 1,000 times." It says nothing about the cases you did not try.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;proof&lt;/strong&gt; covers every case at once: every secret, every &lt;code&gt;k&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt;, every random choice. It is like school geometry. You don't measure a million triangles to know the angles add up to 180°. You prove it once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lean in one minute
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://lean-lang.org" rel="noopener noreferrer"&gt;Lean 4&lt;/a&gt; is two things in one language:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A normal programming language.&lt;/strong&gt; You write functions and compile them to a native program. The whole tool is written in it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A proof checker.&lt;/strong&gt; In the same file you write a claim about your function and a proof of it. Lean checks every step. If one step is wrong or missing, the file does not compile.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I'm mostly a TypeScript developer, so I think of it as a much stronger type checker. TypeScript can check "this function returns a string". Lean can check "this function returns the original secret, for every possible input".&lt;/p&gt;

&lt;p&gt;Lean has a word for "I will prove this later": &lt;code&gt;sorry&lt;/code&gt;. This repo has zero of them, and the build fails if one appears.&lt;/p&gt;

&lt;h2&gt;
  
  
  The one design rule that makes it provable
&lt;/h2&gt;

&lt;p&gt;The most important decision is not a proof. &lt;strong&gt;Randomness is an argument, not an effect:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight lean"&gt;&lt;code&gt;&lt;span class="o"&gt;/--&lt;/span&gt; &lt;span class="nc"&gt;Split&lt;/span&gt; &lt;span class="sb"&gt;`secret`&lt;/span&gt; &lt;span class="n"&gt;into&lt;/span&gt; &lt;span class="sb"&gt;`p.n`&lt;/span&gt; &lt;span class="n"&gt;shares&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt; &lt;span class="sb"&gt;`p.k`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="n"&gt;coefficients&lt;/span&gt; &lt;span class="sb"&gt;`rand`&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;one&lt;/span&gt; &lt;span class="kt"&gt;list&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="sb"&gt;`k − 1`&lt;/span&gt; &lt;span class="n"&gt;coefficients&lt;/span&gt; &lt;span class="n"&gt;per&lt;/span&gt; &lt;span class="n"&gt;secret&lt;/span&gt; &lt;span class="n"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="o"&gt;-/&lt;/span&gt;
&lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;split&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="nc"&gt;Params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt; &lt;span class="no"&gt;GF256&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rand&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt; &lt;span class="nc"&gt;Poly&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nc"&gt;Except&lt;/span&gt; &lt;span class="nc"&gt;SplitError&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt; &lt;span class="nc"&gt;Share&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nc"&gt;ValidRand&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="n"&gt;rand&lt;/span&gt; &lt;span class="k"&gt;then&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;indices&lt;/span&gt; &lt;span class="n"&gt;p&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;map&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shareAt&lt;/span&gt; &lt;span class="n"&gt;secret&lt;/span&gt; &lt;span class="n"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;error&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;badRandomness&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The core library never asks the operating system for random bytes. It receives them as input. Only a thin outer layer reads files and the OS random number generator. Because of this, a theorem can say "for &lt;strong&gt;every&lt;/strong&gt; possible randomness…", and that is exactly the shape a secrecy statement needs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Field → Poly → Shamir → Encoding → CLI      (the tool, no heavy dependencies)
                                    ↑
                  proofs/  (separate package, uses Mathlib, Lean's maths library)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is proven
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. The careful multiply equals the textbook multiply
&lt;/h3&gt;

&lt;p&gt;A field element is its own type, not a plain byte, so the compiler will not let normal &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;*&lt;/code&gt; leak in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight lean"&gt;&lt;code&gt;&lt;span class="n"&gt;structure&lt;/span&gt; &lt;span class="no"&gt;GF256&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UInt8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The textbook multiply is easy to read, but it has an &lt;code&gt;if&lt;/code&gt; on its input bits, and that can leak secret data through timing. The version the tool runs has &lt;strong&gt;no branches and no lookup tables&lt;/strong&gt;. It replaces each &lt;code&gt;if&lt;/code&gt; with a mask that is &lt;code&gt;0x00&lt;/code&gt; or &lt;code&gt;0xFF&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So there are two functions, a simple one and a careful one, and a theorem that they are the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight lean"&gt;&lt;code&gt;&lt;span class="o"&gt;/--&lt;/span&gt; &lt;span class="nc"&gt;The&lt;/span&gt; &lt;span class="n"&gt;constant&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="n"&gt;byte&lt;/span&gt; &lt;span class="n"&gt;multiply&lt;/span&gt; &lt;span class="n"&gt;equals&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;textbook&lt;/span&gt; &lt;span class="n"&gt;byte&lt;/span&gt; &lt;span class="n"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;for&lt;/span&gt; &lt;span class="n"&gt;every&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="o"&gt;-/&lt;/span&gt;
&lt;span class="n"&gt;theorem&lt;/span&gt; &lt;span class="n"&gt;mulByte_eq_mulSpecByte&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UInt8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;mulByte&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mulSpecByte&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt;
  &lt;span class="n"&gt;simp&lt;/span&gt; &lt;span class="n"&gt;only&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mulByte&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mulSpecByte&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;xtime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;xtimeSpec&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bitSet&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="n"&gt;bv_decide&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;bv_decide&lt;/code&gt; hands the question to a SAT solver, and Lean then checks the solver's certificate. That covers all 65,536 input pairs in one step.&lt;/p&gt;

&lt;p&gt;I use this pattern everywhere in the project: a simple version, a fast version, and a theorem that they agree. I think about the simple one, and the machine runs the fast one.&lt;/p&gt;

&lt;p&gt;One fun detail: the solver &lt;strong&gt;timed out&lt;/strong&gt; on &lt;code&gt;(a · b) · c = a · (b · c)&lt;/code&gt;. The fix was a little algebra first. Every byte is the XOR of its 8 single bits, and multiplication is linear over XOR, so it is enough to prove the law when &lt;code&gt;a&lt;/code&gt; has one bit set. That is a much smaller problem. Automation does the dull part, and the maths gives it the right shape.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Any &lt;code&gt;k&lt;/code&gt; shares rebuild the secret
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight lean"&gt;&lt;code&gt;&lt;span class="o"&gt;/--&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt; &lt;span class="sb"&gt;`k`&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;more&lt;/span&gt; &lt;span class="n"&gt;shares&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;distinct&lt;/span&gt; &lt;span class="n"&gt;indices&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;taken&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="n"&gt;one&lt;/span&gt; &lt;span class="n"&gt;successful&lt;/span&gt; &lt;span class="sb"&gt;`split`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;make&lt;/span&gt; &lt;span class="sb"&gt;`combine`&lt;/span&gt; &lt;span class="nb"&gt;return&lt;/span&gt; &lt;span class="n"&gt;exactly&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="o"&gt;-/&lt;/span&gt;
&lt;span class="n"&gt;theorem&lt;/span&gt; &lt;span class="n"&gt;combine_split&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="nc"&gt;Params&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt; &lt;span class="no"&gt;GF256&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;rand&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt; &lt;span class="nc"&gt;Poly&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;shares&lt;/span&gt; &lt;span class="n"&gt;chosen&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt; &lt;span class="nc"&gt;Share&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;split&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="n"&gt;secret&lt;/span&gt; &lt;span class="n"&gt;rand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="n"&gt;shares&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hsub&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; ∀ &lt;span class="n"&gt;sh&lt;/span&gt; ∈ &lt;span class="n"&gt;chosen&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sh&lt;/span&gt; ∈ &lt;span class="n"&gt;shares&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hnd&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chosen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&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;x&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="nc"&gt;Nodup&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hk&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="n"&gt;k&lt;/span&gt; ≤ &lt;span class="n"&gt;chosen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;combine&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="n"&gt;chosen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="n"&gt;secret&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read what it covers: any parameters, any secret, &lt;strong&gt;any randomness&lt;/strong&gt;, and any choice of at least &lt;code&gt;k&lt;/code&gt; different shares.&lt;/p&gt;

&lt;p&gt;The key fact, "a curve of degree below &lt;code&gt;k&lt;/code&gt; is fixed by &lt;code&gt;k&lt;/code&gt; of its points", already exists in &lt;a href="https://github.com/leanprover-community/mathlib4" rel="noopener noreferrer"&gt;Mathlib&lt;/a&gt;, Lean's large maths library. Mathlib is several GB to build, so it lives in a separate &lt;code&gt;proofs/&lt;/code&gt; package. People who only want the tool never download it.&lt;/p&gt;

&lt;p&gt;Rejecting bad input is proven too: too few shares, duplicate shares, and a share at &lt;code&gt;x = 0&lt;/code&gt; are all refused.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Fewer than &lt;code&gt;k&lt;/code&gt; shares reveal nothing
&lt;/h3&gt;

&lt;p&gt;This is my favourite part. "Reveals nothing" becomes a precise claim:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Take any &lt;code&gt;k − 1&lt;/code&gt; shares and any two secrets of the same length. There is a one-to-one matching between the random choices, such that each random choice for the first secret gives &lt;strong&gt;exactly the same shares&lt;/strong&gt; as its partner does for the second secret.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So whatever &lt;code&gt;k − 1&lt;/code&gt; shares you are looking at, exactly as many random choices produce them from one secret as from any other. Every secret is equally likely. This is the same kind of security as the one-time pad.&lt;/p&gt;

&lt;p&gt;The matching is simple: add to the curve a second curve that is &lt;strong&gt;zero at the shares you hold&lt;/strong&gt; and nonzero at &lt;code&gt;x = 0&lt;/code&gt;. Your shares do not change, but the secret does. Adding it twice cancels out (in this field &lt;code&gt;a + a = 0&lt;/code&gt;), so the matching is its own inverse.&lt;/p&gt;

&lt;p&gt;There is a nice payoff. Remember the "force the top coefficient to be nonzero" bug? A &lt;code&gt;split&lt;/code&gt; with that bug &lt;strong&gt;cannot satisfy this theorem&lt;/strong&gt;, because the theorem is about all randomness. The whole class of bug is ruled out.&lt;/p&gt;

&lt;p&gt;(Credit: the same idea appears in Vocdoni's &lt;a href="https://github.com/vocdoni/masked-shamir-shares-lean" rel="noopener noreferrer"&gt;masked Shamir shares&lt;/a&gt; formalization over an abstract field. qedshare applies it to the concrete byte-level code that runs.)&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Forged shares are refused
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx3ywrxa80gp1ivxn3yat.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx3ywrxa80gp1ivxn3yat.jpg" alt="Five brass keys in a row, one of them a different shape" width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Shamir's scheme assumes people hand back the shares they were given. What if one person changes theirs on purpose?&lt;/p&gt;

&lt;p&gt;The maths is linear, so a cheater can shift the rebuilt secret by any amount they like &lt;strong&gt;without knowing the secret&lt;/strong&gt;. A checksum does not help, because they recompute it. A script in the repo does exactly this against an unprotected format: the secret was "send 100 to bob" and &lt;code&gt;combine&lt;/code&gt; happily returned "send 900 to bob".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The simple fix: bring one extra share.&lt;/strong&gt; With &lt;code&gt;k + 1&lt;/code&gt; shares, &lt;code&gt;combine&lt;/code&gt; rebuilds the curve from &lt;code&gt;k&lt;/code&gt; of them and checks that the others lie on it. A forged share is always refused (&lt;code&gt;combine_detects_forgery&lt;/code&gt;). There is no probability and no hardness assumption involved. With &lt;code&gt;k + 2&lt;/code&gt; or more, the tool also names the bad share:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ qedshare combine forged.txt share-2.txt share-3.txt share-4.txt
qedshare: error: the shares do not all fit together: forged.txt is the odd one out (the other 3 agree with each other), so it is probably wrong or forged
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;With exactly &lt;code&gt;k&lt;/code&gt; shares&lt;/strong&gt; there is nothing extra to compare against. So before splitting, the tool adds a random key and a &lt;strong&gt;tag&lt;/strong&gt; computed from the secret and the key, in the larger field GF(2¹²⁸). This is called an AMD code (Cramer, Dodis, Fehr, Padró and Wichs, 2008). After rebuilding, &lt;code&gt;combine&lt;/code&gt; recomputes the tag and refuses on a mismatch. The cheaters never see the key, so they have to guess. For a 1 MiB secret their chance is about 2⁻¹¹².&lt;/p&gt;

&lt;p&gt;Two details I enjoyed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Building GF(2¹²⁸).&lt;/strong&gt; GMAC uses this field too, but I built it differently: as a tower on top of our GF(2⁸), in four steps, where each step doubles the number of bytes (1 → 2 → 4 → 8 → 16). It reuses the careful byte multiply, so there are still no tables and no branches.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The textbook parameter was wrong for this use.&lt;/strong&gt; A cheater can also change a share's &lt;em&gt;index&lt;/em&gt;, which is just a number in the share text. The textbook analysis does not cover that. With the textbook setting, I found a change that passed the check for all 4,096 keys I tried. A different choice of one parameter fixes it, and that is proven (&lt;code&gt;amd_detect&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. The proof that found a hole
&lt;/h3&gt;

&lt;p&gt;My plan said: "with exactly &lt;code&gt;k&lt;/code&gt; shares, the tag catches every forgery." I had 3,000 random simulations that agreed.&lt;/p&gt;

&lt;p&gt;Then I tried to prove it, and the proof would not go through. To finish it, I had to compute one quantity exactly, and it turned out the cheaters could &lt;strong&gt;choose the index of their forged share so that the honest shares cancel out&lt;/strong&gt;. The rebuilt result then does not depend on the real secret at all, and they can replace it with any secret they like, with a valid tag.&lt;/p&gt;

&lt;p&gt;Random simulations almost never pick that index. An attacker picks it on purpose. I wrote the attack, ran it against the real program, and &lt;code&gt;combine&lt;/code&gt; printed &lt;code&gt;attacker chosen!&lt;/code&gt; with a success exit code.&lt;/p&gt;

&lt;p&gt;So the first version of the theorem had an honest extra condition, and the README said exactly what held and what did not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix: a token on every share.&lt;/strong&gt; &lt;code&gt;split&lt;/code&gt; now draws one more random curve &lt;code&gt;G&lt;/code&gt; and gives share &lt;code&gt;i&lt;/code&gt; a 16-byte &lt;strong&gt;token&lt;/strong&gt; &lt;code&gt;G(i)&lt;/code&gt;. &lt;code&gt;combine&lt;/code&gt; rebuilds &lt;code&gt;G&lt;/code&gt; and checks every share's token. The cheaters hold &lt;code&gt;k − 1&lt;/code&gt; values of &lt;code&gt;G&lt;/code&gt;, and that is one too few to predict it anywhere else:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If they cancel the honest shares, the honest tokens no longer fit.&lt;/li&gt;
&lt;li&gt;If they forge a share at an index they do not hold, they need a token they cannot predict.&lt;/li&gt;
&lt;li&gt;If they only use indices they hold, the tag from before catches them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The final theorem (&lt;code&gt;combine_detects_forgery_tok&lt;/code&gt;) has no conditions left: any forging strategy, any number of honest shares, any indices. Every forgery is refused except with probability about 2⁻¹¹² for a 1 MiB secret. A companion theorem shows the tokens leak nothing about the secret. The attack still runs in CI on every push, and now it has to fail.&lt;/p&gt;

&lt;p&gt;One more condition was hiding: the theorem had the honest shares &lt;em&gt;first&lt;/em&gt; in the list. So there is a theorem that &lt;code&gt;combine&lt;/code&gt; gives the same answer for &lt;strong&gt;every order&lt;/strong&gt; of the shares (&lt;code&gt;combine_perm&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;(I had first planned Feldman verifiable secret sharing here. I dropped it: it needs a whole new primitive, it weakens perfect secrecy for low-entropy secrets, and it protects against a cheating &lt;em&gt;dealer&lt;/em&gt;, while here the dealer is you.)&lt;/p&gt;

&lt;h3&gt;
  
  
  6. The program around the maths
&lt;/h3&gt;

&lt;p&gt;"The core is proven" says little if the glue around it is wrong. So the glue is split in two: pure functions from text to text, and a thin part that only reads and writes. The pure part has its own theorems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every share that &lt;code&gt;split&lt;/code&gt; writes, in hex or in words, reads back to the same share.&lt;/li&gt;
&lt;li&gt;Whatever &lt;code&gt;split&lt;/code&gt; prints, &lt;code&gt;combine&lt;/code&gt; on any &lt;code&gt;k&lt;/code&gt; or more of those lines, in any order, returns exactly the secret (&lt;code&gt;recoverSecret_makeShares&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;combine&lt;/code&gt; accepts, its answer is exactly what the proven core computes, so the forgery guarantees apply to the real program.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The proofs like simple lists, and the machine does not. A 1 MiB secret with threshold 20 needed &lt;strong&gt;1.3 GB&lt;/strong&gt; of memory to split. I did not rewrite the proven code. I used the same pattern as the multiply: a second, memory-light version on byte arrays, and a theorem that both compute the same thing. Memory went from 1.3 GB to 300 MB for splitting and from 714 MB to 41 MB for combining, and no existing theorem was touched.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the proofs did not find
&lt;/h2&gt;

&lt;p&gt;Before one release I reviewed the whole tool the ordinary way. Every bug was in the part with no theorems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;split --out-dir&lt;/code&gt; created each share file with default access rights and narrowed them afterwards, so a share was briefly readable by other users.&lt;/li&gt;
&lt;li&gt;If writing the share files failed halfway, the files already written stayed behind.&lt;/li&gt;
&lt;li&gt;A byte-order mark at the start of a file made the first share unreadable.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--shares 1_0&lt;/code&gt; was read as 10, and a repeated option silently took the last value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of these contradicts a theorem. &lt;strong&gt;Proofs move the bugs&lt;/strong&gt; to where the proofs are not, and that is where review and tests have to look.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you still have to trust
&lt;/h2&gt;

&lt;p&gt;A proof is only as good as what it rests on. The repo has an &lt;strong&gt;axiom audit&lt;/strong&gt;: a file that prints the assumptions behind every headline theorem and fails the build if anything unexpected appears.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight lean"&gt;&lt;code&gt;&lt;span class="nc"&gt;QedShare&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Shamir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;combine_split&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;propext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Classical&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Quot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="n"&gt;bv_decide&lt;/span&gt; &lt;span class="n"&gt;certificate&lt;/span&gt; &lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;QedShare&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Shamir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;perfect_secrecy_card&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;propext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Classical&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Quot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="n"&gt;bv_decide&lt;/span&gt; &lt;span class="n"&gt;certificate&lt;/span&gt; &lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;QedShare&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Shamir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;combine_detects_forgery_tok&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;propext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Classical&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Quot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="n"&gt;bv_decide&lt;/span&gt; &lt;span class="n"&gt;certificate&lt;/span&gt; &lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The three names are Lean's standard axioms, used all through Mathlib. Each &lt;code&gt;bv_decide&lt;/code&gt; step adds one more: "Lean's compiled checker accepted the SAT solver's certificate."&lt;/p&gt;

&lt;p&gt;What is &lt;strong&gt;not&lt;/strong&gt; proven:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;File handling and argument parsing.&lt;/strong&gt; Tested on Linux and macOS, not proven.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The OS random number generator.&lt;/strong&gt; The theorems assume the randomness is uniform. They cannot check that it is.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Lean compiler and the C toolchain.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Timing.&lt;/strong&gt; The multiply has no branches and no table lookups, but "constant time" is a property of the final machine code, and that is not proven.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Naming the bad share when there are several.&lt;/strong&gt; With exactly one bad share the name is proven right. With two or more it is a hint.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Old &lt;code&gt;qs2&lt;/code&gt; shares&lt;/strong&gt; still have the hole from section 5 when you bring exactly &lt;code&gt;k&lt;/code&gt; shares. New shares are &lt;code&gt;qs3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It has not been audited.&lt;/strong&gt; Please do not use it for real secrets yet.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The full list, with theorem names, is in the README under &lt;a href="https://github.com/KhaledSaeed18/qedshare#guarantees" rel="noopener noreferrer"&gt;Guarantees&lt;/a&gt; and &lt;a href="https://github.com/KhaledSaeed18/qedshare#threat-model" rel="noopener noreferrer"&gt;Threat model&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Will the shares still work in ten years?
&lt;/h2&gt;

&lt;p&gt;A share is only useful if a later version of the tool can still read it. So since v0.18.0 the &lt;code&gt;qs3&lt;/code&gt; share format is &lt;a href="https://github.com/KhaledSaeed18/qedshare#format-stability" rel="noopener noreferrer"&gt;frozen&lt;/a&gt;: every later version will read &lt;code&gt;qs3&lt;/code&gt; shares, in hex and in words. Any change to what &lt;code&gt;split&lt;/code&gt; writes gets a new name (&lt;code&gt;qs4&lt;/code&gt;), and &lt;code&gt;qs3&lt;/code&gt; stays readable.&lt;/p&gt;

&lt;p&gt;This is a promise, not a theorem. It is held by tests: fixed shares sit in the test suite, and on every push &lt;code&gt;split&lt;/code&gt; must write exactly those strings and &lt;code&gt;combine&lt;/code&gt; must read them back.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Design for the proof before writing the code.&lt;/strong&gt; Passing randomness in as an argument cost almost nothing. Without it, the main theorems could not even be stated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simple version + fast version + a theorem that they agree&lt;/strong&gt; is a great pattern. It worked for a one-byte multiply and for the whole program's memory use.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automation needs the right shape.&lt;/strong&gt; The SAT solver proves 16-bit facts at once and times out on 24-bit ones. A little algebra fixed it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A proof attempt is a very thorough code review.&lt;/strong&gt; 3,000 random simulations said the plan was fine. The proof found the attack.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read the theorem, not its name.&lt;/strong&gt; Twice my README said a little more than the Lean statement did. Checking them line by line found both.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Proofs move the bugs.&lt;/strong&gt; After the core was proven, every bug was in file handling and argument parsing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Be honest about what is trusted.&lt;/strong&gt; "Formally verified" means nothing without saying &lt;em&gt;what&lt;/em&gt; is verified.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Source code:&lt;/strong&gt; &lt;a href="https://github.com/KhaledSaeed18/qedshare" rel="noopener noreferrer"&gt;https://github.com/KhaledSaeed18/qedshare&lt;/a&gt; (Apache-2.0)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API docs&lt;/strong&gt;, every definition and theorem with source links: &lt;a href="https://khaledsaeed18.github.io/qedshare/" rel="noopener noreferrer"&gt;https://khaledsaeed18.github.io/qedshare/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Releases&lt;/strong&gt;, with binaries for Linux and macOS: &lt;a href="https://github.com/KhaledSaeed18/qedshare/releases" rel="noopener noreferrer"&gt;https://github.com/KhaledSaeed18/qedshare/releases&lt;/a&gt;. This post describes &lt;a href="https://github.com/KhaledSaeed18/qedshare/releases/tag/v0.18.1" rel="noopener noreferrer"&gt;v0.18.1&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Guarantees, trusted base and threat model:&lt;/strong&gt; the &lt;a href="https://github.com/KhaledSaeed18/qedshare#guarantees" rel="noopener noreferrer"&gt;README&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Adi Shamir, &lt;em&gt;How to Share a Secret&lt;/em&gt;, Communications of the ACM, 1979&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://lean-lang.org" rel="noopener noreferrer"&gt;Lean 4&lt;/a&gt; · &lt;a href="https://github.com/leanprover-community/mathlib4" rel="noopener noreferrer"&gt;Mathlib&lt;/a&gt; · &lt;a href="https://lean-lang.org/doc/api/Lean/Elab/Tactic/BVDecide.html" rel="noopener noreferrer"&gt;&lt;code&gt;bv_decide&lt;/code&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Christof Paar and Jan Pelzl, &lt;em&gt;Understanding Cryptography&lt;/em&gt;, the textbook of my course&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are learning cryptography, try building one small thing from your course and proving one fact about it. The proofs are hard in a satisfying way, and when the build goes green you &lt;em&gt;know&lt;/em&gt; something, instead of hoping your tests were thorough enough.&lt;/p&gt;

</description>
      <category>cryptography</category>
      <category>security</category>
      <category>lean</category>
      <category>functional</category>
    </item>
    <item>
      <title>Microservices Solve a Problem You Don't Have</title>
      <dc:creator>Khaled Saeed</dc:creator>
      <pubDate>Thu, 23 Jul 2026 16:25:35 +0000</pubDate>
      <link>https://dev.to/khaledsaeed18/microservices-solve-a-problem-you-dont-have-pc5</link>
      <guid>https://dev.to/khaledsaeed18/microservices-solve-a-problem-you-dont-have-pc5</guid>
      <description>&lt;p&gt;Somewhere right now, a three-person team is drawing boxes on a whiteboard. There is an &lt;code&gt;auth-service&lt;/code&gt;, an &lt;code&gt;orders-service&lt;/code&gt;, a &lt;code&gt;notifications-service&lt;/code&gt;, a message broker between them, and an API gateway in front. The product has zero users. The team has never operated a distributed system. And every box on that whiteboard is about to become a repository, a deployment pipeline, a set of environment variables, and a production alert at 2 a.m.&lt;/p&gt;

&lt;p&gt;This post makes one argument, carefully: microservices are an organizational scaling pattern, not a technical upgrade. But before arguing against a thing, let's define it properly.&lt;/p&gt;

&lt;h2&gt;
  
  
  First: what are microservices, actually?
&lt;/h2&gt;

&lt;p&gt;Microservices are an architectural style where an application is built as a collection of small, independently deployable services. Each service:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;owns a single business capability (payments, orders, notifications)&lt;/li&gt;
&lt;li&gt;runs in its own process&lt;/li&gt;
&lt;li&gt;owns its own data model and persistence, with no shared tables&lt;/li&gt;
&lt;li&gt;talks to other services over the network, through HTTP or gRPC calls, or through messages in a broker&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The opposite is the monolith: one codebase, one process, one deployable artifact. A request comes in, function calls happen in-process, one database transaction commits, done.&lt;/p&gt;

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

&lt;p&gt;The promise on the microservices side sounds great. Services scale on their own, deploy on their own, fail on their own, and can even be written in different languages. &lt;a href="https://netflixtechblog.com/" rel="noopener noreferrer"&gt;Netflix&lt;/a&gt;, &lt;a href="https://aws.amazon.com/builders-library/" rel="noopener noreferrer"&gt;Amazon&lt;/a&gt;, and &lt;a href="https://www.uber.com/blog/microservice-architecture/" rel="noopener noreferrer"&gt;Uber&lt;/a&gt; run this way, and their engineering blogs are the canon of our industry.&lt;/p&gt;

&lt;p&gt;Here is the catch. Copying their architecture without their problems is like buying a fleet of trucks because you saw a logistics company do it. The trucks are not the business. The trucks are what the business was forced to buy once it got big.&lt;/p&gt;

&lt;h2&gt;
  
  
  What problem do microservices actually solve?
&lt;/h2&gt;

&lt;p&gt;Strip away the conference talks and the answer is narrow. &lt;/p&gt;

&lt;p&gt;Microservices exist so that many teams can deploy independently without coordinating with each other. That is the core of it. Everything else, like independent scaling, mixed tech stacks, and fault isolation, is real but secondary: often achievable with simpler techniques first, or only economically meaningful at a scale most systems never reach.&lt;/p&gt;

&lt;p&gt;Amazon did not move to services primarily because a monolith could not handle the traffic. They moved because hundreds of teams were stepping on each other: merge conflicts, release trains, one team's bug blocking another team's launch. The service boundary is a social contract. It says: you own this, I own that, we talk through an API, and neither of us needs to attend the other's standup.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Organizations which design systems are constrained to produce designs which are copies of the communication structures of these organizations."&lt;br&gt;
Melvin Conway, 1967&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Conway's Law is usually quoted as a warning. Read it the other way and it becomes the whole justification for microservices. If your communication structure is one team in one Slack channel, your natural system design is one deployable unit. Splitting that team's codebase into nine services does not decouple anything. The same people still coordinate every change, except now they do it across network boundaries, mismatched API versions, and distributed transactions.&lt;/p&gt;

&lt;p&gt;The cost curves cross late:&lt;/p&gt;

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

&lt;p&gt;A monolith's coordination cost grows with headcount. Microservices trade that for a large fixed operational tax that grows more slowly. Below the crossover point, which usually means several independent teams and not several developers, you are paying the tax with no refund.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bill nobody itemizes
&lt;/h2&gt;

&lt;p&gt;Every microservice split converts problems you understand into problems you don't:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concern&lt;/th&gt;
&lt;th&gt;Monolith&lt;/th&gt;
&lt;th&gt;Microservices&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Function call&lt;/td&gt;
&lt;td&gt;Nanoseconds, in-process, type-checked, easy to refactor in your editor&lt;/td&gt;
&lt;td&gt;A network hop: latency, retries, timeouts, serialization, partial failure, versioned contracts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Transaction&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;BEGIN ... COMMIT&lt;/code&gt;, the database does the hard part&lt;/td&gt;
&lt;td&gt;Sagas, outbox pattern, idempotency keys, compensating actions, eventual consistency you must explain to product&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Debugging&lt;/td&gt;
&lt;td&gt;One stack trace, one log stream, a breakpoint&lt;/td&gt;
&lt;td&gt;Distributed tracing (OpenTelemetry, collectors, sampling), correlation IDs, log aggregation, all before you can even ask "what happened?"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Refactor&lt;/td&gt;
&lt;td&gt;Rename symbol, done, the compiler finds every caller&lt;/td&gt;
&lt;td&gt;A cross-service migration: deprecation windows, dual writes, contract tests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Local dev&lt;/td&gt;
&lt;td&gt;&lt;code&gt;pnpm dev&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;docker compose up&lt;/code&gt; times 12, or a shared staging environment everyone breaks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deploy&lt;/td&gt;
&lt;td&gt;One pipeline, one artifact, clean rollback&lt;/td&gt;
&lt;td&gt;N pipelines, compatibility matrices, and the question "which versions are live right now?"&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;None of these distributed-systems problems are unsolvable. They are just expensive, and the expense is permanent. Sagas, tracing, and contract tests are not a one-time setup fee. They are a mortgage on every feature you ship afterward.&lt;/p&gt;

&lt;p&gt;Here is what the same checkout flow looks like once it is distributed, with the failure modes labeled honestly:&lt;/p&gt;

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

&lt;p&gt;Same feature, same team. But the function call became nine failure modes, four pipelines, four schemas, and one saga.&lt;/p&gt;

&lt;h2&gt;
  
  
  "But we need to scale." Do you, though?
&lt;/h2&gt;

&lt;p&gt;Scaling is the most common justification and the least examined one. Three facts get lost:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Monoliths scale horizontally just fine.&lt;/strong&gt; A stateless Node/Express or NestJS monolith behind a load balancer scales the same way a microservice does. You add replicas. For most of its growth, Stack Overflow served one of the highest-traffic sites on the internet from a handful of servers running what is essentially a monolith. Shopify's core application has stayed a modular Rails monolith through flash-sale traffic that would terrify most microservice shops, with services extracted only at the edges. GitHub's core application has historically remained a large Rails monolith despite enormous scale. Basecamp is a monolith on purpose.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. The bottleneck is almost never "the app tier can't be split."&lt;/strong&gt; It is a slow query, a missing index, an N+1, an unbounded queue, or a hot row. Microservices fix none of these. They often add latency and complexity without removing the underlying bottleneck, because the query you used to do with one &lt;code&gt;JOIN&lt;/code&gt; is now two HTTP calls and a merge in memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. "Independent scaling" usually saves pennies.&lt;/strong&gt; Unless one workload has a very different resource profile (GPU inference, video transcoding, heavy PDF rendering, and those do deserve extraction, more on that below), the money saved by scaling components separately is a rounding error next to the engineering cost of the split.&lt;/p&gt;

&lt;p&gt;Ask around about the last ten "we can't scale" incidents on any team:&lt;/p&gt;

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

&lt;p&gt;&lt;em&gt;Illustrative distribution, not survey data. The point is the shape: check your own team's postmortems against it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Postmortems overwhelmingly point at the data layer and at specific hot paths. Those are problems solved by indexes, caching, and targeted extraction, not by decomposing the entire application. Only the last bar is the one microservices actually address.&lt;/p&gt;

&lt;h2&gt;
  
  
  The alternative is not a big ball of mud
&lt;/h2&gt;

&lt;p&gt;The honest objection to monoliths is real. Many of them rot into tangled dependency graphs where everything imports everything and no change is safe. But the cure for bad boundaries is boundaries, not networks. This is the modular monolith: one deployable, internally split into modules that behave like services without the distributed tax.&lt;/p&gt;

&lt;p&gt;The rules are few and enforceable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Modules own their domain.&lt;/strong&gt; &lt;code&gt;orders/&lt;/code&gt;, &lt;code&gt;payments/&lt;/code&gt;, &lt;code&gt;inventory/&lt;/code&gt;, each with its own controllers, services, repositories, and (this part matters) its own tables. No module touches another module's tables. Ever.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Modules expose a small public API.&lt;/strong&gt; A facade, a set of exported functions, or in-process events. Everything else is private.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The boundary is enforced by tooling, not discipline.&lt;/strong&gt; Lint it. Discipline is what fails at 6 p.m. on a Friday before a deadline.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a TypeScript codebase this is almost free. ESLint's import rules (or &lt;code&gt;dependency-cruiser&lt;/code&gt;, or Nx module-boundary tags) turn architectural intent into CI failures:&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;// eslint.config.ts, the architecture as a lint rule&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s1"&gt;'import/no-restricted-paths'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;zones&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="c1"&gt;// nobody reaches into another module's internals&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="s1"&gt;'./src/modules/orders'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="s1"&gt;'./src/modules/!(orders)/**/internal/**'&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;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'./src/modules/payments'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="s1"&gt;'./src/modules/!(payments)/**/internal/**'&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;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// modules/payments/index.ts, the module's entire public surface&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;capturePayment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;refundPayment&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'./internal/service'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;PaymentResult&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'./internal/types'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// everything under internal/ is invisible to the rest of the app&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For workflows that cross modules, prefer in-process domain events over direct calls. &lt;code&gt;OrderPlaced&lt;/code&gt; fires, and the payments and notifications modules subscribe. You get the decoupled shape of an event-driven system with exactly-once delivery, zero brokers, and a stack trace that still makes sense. And if you ever extract a module, the event seam is already exactly where the network will go.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The point worth remembering:&lt;/strong&gt; the real benefit of microservices is enforced boundaries. A modular monolith gives you the boundaries and lets you skip the enforcement-by-network part. You keep &lt;code&gt;BEGIN ... COMMIT&lt;/code&gt;, keep the debugger, keep one pipeline, and keep the option to split later, because well-bounded modules are exactly what extract cleanly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  When microservices are the right call
&lt;/h2&gt;

&lt;p&gt;To be clear, none of this means microservices are bad. Distributed systems are a serious engineering achievement, and the companies running thousands of services are making the correct choice for their situation. The mistake is treating microservices as a maturity milestone rather than a response to specific operational pressure. A three-person startup and Amazon can both be architecting correctly while building completely different systems.&lt;/p&gt;

&lt;p&gt;So this is not monolith absolutism. There are legitimate triggers, and you can recognize them because they are pains you have observed, not pains you are predicting:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Team topology pressure.&lt;/strong&gt; Multiple genuinely independent teams (say 4+ teams, 25+ engineers) whose release schedules keep colliding: release trains, merge queues, "we can't ship because their migration is mid-flight." This is the signal.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A very different workload.&lt;/strong&gt; One component needs GPUs, a different runtime, 100 times the scale, or a different compliance boundary (card data, health records). Extract that component. One service, not a whole architecture.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Isolation as a product requirement.&lt;/strong&gt; A truly untrusted or unstable subsystem, like user-submitted code execution or a flaky third-party integration, where limiting the blast radius is worth a process boundary.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A naturally separate lifecycle.&lt;/strong&gt; A component that ships to a different destination, like an edge worker, an embedded agent, or a public API with its own versioning contract.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice what is missing from this list: "microservices are more modern," "we might need to scale someday," "the resume wants Kubernetes." Also notice that the legitimate cases mostly justify extracting one thing. The path is monolith, then modular monolith, then selective extraction, and each step should be forced by evidence.&lt;/p&gt;

&lt;p&gt;The decision, honestly drawn:&lt;/p&gt;

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

&lt;p&gt;Every "no" exits early to the cheaper option. Even the final "yes" produces selective extraction, not a nine-box whiteboard.&lt;/p&gt;

&lt;h2&gt;
  
  
  "We'll just start with microservices so we don't have to migrate later"
&lt;/h2&gt;

&lt;p&gt;This is the most tempting counterargument, so it deserves a direct answer: starting distributed does not avoid the migration. It guarantees a worse one.&lt;/p&gt;

&lt;p&gt;Service boundaries are the hardest thing to change in a distributed system. Moving a responsibility between two modules in a monolith is an afternoon and a compiler pass. Moving it between two services is a multi-week project with dual writes and a deprecation window. And early in a product's life, your understanding of the domain is at its lowest. You will draw the boundaries wrong, because you do not yet know where the product is going. Premature microservices do not lock in good architecture. They lock in your earliest misunderstandings, at the layer where mistakes are most expensive to fix.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Field note:&lt;/strong&gt; even teams famous for distributed systems say this out loud. Amazon's own Prime Video team published a case study describing how they consolidated a distributed, serverless monitoring pipeline into a single monolithic process, reporting an infrastructure cost reduction of over 90% for that workload. The team that helped write the microservices canon will happily merge services when the workload says so.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A monolith with clean modules keeps every option open. Boundaries wrong? Refactor, the compiler has your back. Boundaries right and one team outgrows a module? Extract it, the seam already exists and the events already flow through it. The modular monolith is the reversible decision. The early split is the irreversible one: extracting a well-bounded module is a project, but merging two services whose boundaries turned out wrong is an ordeal. When in doubt, choose the door that swings both ways.&lt;/p&gt;

&lt;p&gt;This is not a new position. Martin Fowler made the same argument years ago in &lt;a href="https://martinfowler.com/bliki/MonolithFirst.html" rel="noopener noreferrer"&gt;Monolith First&lt;/a&gt;, and named the cost side of the trade in &lt;a href="https://martinfowler.com/bliki/MicroservicePremium.html" rel="noopener noreferrer"&gt;Microservice Premium&lt;/a&gt;: don't pay the premium until complexity forces you to.&lt;/p&gt;

&lt;h2&gt;
  
  
  What if you are already in microservices?
&lt;/h2&gt;

&lt;p&gt;Then this is not an argument to merge everything tomorrow. Architecture accumulates constraints the same way code does, and a big-bang consolidation is just the original mistake in reverse. Instead, stop the bleeding: resist further fragmentation unless a new split solves a pain you have actually observed. Merge services whose boundaries turned out wrong, especially the ones that always deploy together, since those are a distributed monolith already paying network prices for nothing. And put real investment into the operational floor: tracing, contracts, and local dev, because if you are going to pay the tax, at least collect the benefits. Good architecture is not about maximizing or minimizing service count. It is about matching the system to the organization and the workload you actually have.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to do on Monday
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Default to one deployable.&lt;/strong&gt; One repo, one pipeline, one process, replicated horizontally behind a load balancer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spend the saved effort on module boundaries.&lt;/strong&gt; Domain-owned folders, private internals, a lint rule that fails the build on cross-module reach-ins, module-owned tables.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use in-process domain events for cross-module workflows.&lt;/strong&gt; They are your future extraction seams, and you get them for free.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix scaling where scaling actually breaks:&lt;/strong&gt; indexes, query shape, caching, connection pools, background-job isolation. Measure before you architect.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write down your extraction triggers now:&lt;/strong&gt; team count, deploy-contention frequency, divergent workload. That way the future decision is made by evidence instead of fashion.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If a trigger fires, extract one module.&lt;/strong&gt; Not the system. One module, along an existing seam, with the outbox pattern at the boundary. Then measure again.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Microservices are a genuinely great answer to the question "how do fifty teams ship without meetings?" If that is not your question, do not buy the answer. Build a modular monolith you are proud of, draw your boundaries in code before you draw them over the network, and let the org chart tell you when it is time to split. It will not be shy about it.&lt;/p&gt;

</description>
      <category>microservices</category>
      <category>backend</category>
      <category>architecture</category>
      <category>software</category>
    </item>
    <item>
      <title>Access and Refresh Tokens in Token-Based Authentication</title>
      <dc:creator>Khaled Saeed</dc:creator>
      <pubDate>Sat, 02 Aug 2025 22:09:39 +0000</pubDate>
      <link>https://dev.to/khaledsaeed18/access-and-refresh-tokens-in-token-based-authentication-2a9o</link>
      <guid>https://dev.to/khaledsaeed18/access-and-refresh-tokens-in-token-based-authentication-2a9o</guid>
      <description>&lt;p&gt;Most modern web apps and APIs use tokens to know who is making a request. Many of them use two tokens: an access token and a refresh token. This article explains what each one does, why one token is not enough, and how to handle the refresh token safely.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is token-based authentication?
&lt;/h2&gt;

&lt;p&gt;After a successful login, the server gives the client a token. The client sends that token with every later request to prove who the user is.&lt;/p&gt;

&lt;p&gt;The most common format is a JWT (JSON Web Token). A JWT carries a few facts about the user, such as the user id and an expiry time, and the server signs it. The server can check the signature on each request, so it knows the token was not changed.&lt;/p&gt;

&lt;p&gt;A JWT is signed, not encrypted. Anyone who holds it can read what is inside, so never put passwords or other secrets in it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sessions vs. tokens
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Session-based authentication&lt;/strong&gt; keeps the state on the server:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The user logs in, and the server creates a session.&lt;/li&gt;
&lt;li&gt;The browser gets a session id in a cookie.&lt;/li&gt;
&lt;li&gt;On each request, the server looks up the session by its id.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Token-based authentication&lt;/strong&gt; moves the state into the token:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The user logs in, and the server creates a signed token.&lt;/li&gt;
&lt;li&gt;The client sends the token with each request.&lt;/li&gt;
&lt;li&gt;The server checks the signature and the expiry time. It does not need a lookup.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why stateless helps
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scaling&lt;/strong&gt;: any server can verify the token, so there is no session store to share between servers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Microservices&lt;/strong&gt;: each service can verify the token on its own.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mobile apps and SPAs&lt;/strong&gt;: a token works the same way for a browser, a phone app, or another server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separate frontend and backend&lt;/strong&gt;: they can live on different domains and deploy on their own.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is one cost. A signed token stays valid until it expires, and the server cannot easily take it back. That cost is the reason for the second token.&lt;/p&gt;




&lt;h2&gt;
  
  
  Access tokens vs. refresh tokens
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Access tokens
&lt;/h3&gt;

&lt;p&gt;An access token is a short-lived token that gives access to protected resources. Think of it as a day pass.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lifetime&lt;/strong&gt;: usually 5 to 15 minutes, sometimes up to an hour&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Usage&lt;/strong&gt;: sent with every API request, in the &lt;code&gt;Authorization: Bearer &amp;lt;token&amp;gt;&lt;/code&gt; header&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Storage&lt;/strong&gt;: in memory in the browser is the safest choice. Avoid &lt;code&gt;localStorage&lt;/code&gt;, because any script that runs on the page can read it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Risk if stolen&lt;/strong&gt;: limited, because it expires soon&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Refresh tokens
&lt;/h3&gt;

&lt;p&gt;A refresh token is a long-lived token with one job: to get a new access token. It is never sent to the normal API endpoints.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lifetime&lt;/strong&gt;: days or weeks&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Usage&lt;/strong&gt;: sent only to the refresh endpoint&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Storage&lt;/strong&gt;: an &lt;code&gt;HttpOnly&lt;/code&gt; cookie in the browser, or the secure storage of the operating system in a mobile app&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Risk if stolen&lt;/strong&gt;: high, because it lives long. This is why it needs more protection.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The flow
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpkle45fgwsnslo938h0v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpkle45fgwsnslo938h0v.png" alt="Token flow: login, API requests, expiry, and refresh" width="800" height="603"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The user logs in and receives both tokens.&lt;/li&gt;
&lt;li&gt;The client calls the API with the access token.&lt;/li&gt;
&lt;li&gt;The access token expires, and the API answers &lt;code&gt;401 Unauthorized&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The client sends the refresh token to the auth server and receives new tokens.&lt;/li&gt;
&lt;li&gt;The client repeats the request with the new access token.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The user does not notice any of this.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why do we use both?
&lt;/h2&gt;

&lt;p&gt;Each single-token design has a problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One long-lived access token:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If it is stolen, the attacker has access for a long time.&lt;/li&gt;
&lt;li&gt;It is hard to revoke. The server would have to check a blocklist on every request, which removes the benefit of being stateless.&lt;/li&gt;
&lt;li&gt;It travels with every request, so it has many chances to leak into logs or be stolen by a script.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;One short-lived access token:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The user has to log in again every few minutes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two tokens solve both problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The token that travels with every request is short-lived. If it is stolen, it is useful for a few minutes.&lt;/li&gt;
&lt;li&gt;The token that lives long is sent to one endpoint only, and it can be stored where scripts cannot read it.&lt;/li&gt;
&lt;li&gt;The server can end a session at any time by revoking the refresh token. The access token then stops working when it expires, a few minutes later.&lt;/li&gt;
&lt;li&gt;The user stays logged in, because the client refreshes the access token in the background.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One point is often missed. Access tokens are stateless, but refresh tokens usually are not. The server keeps a record of each refresh token so that it can revoke it. This is a good trade: the lookup happens once every few minutes at the refresh endpoint, and not on every API request.&lt;/p&gt;




&lt;h2&gt;
  
  
  Security tips for refresh tokens
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Store the refresh token in an HttpOnly cookie
&lt;/h3&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;refreshCookieOptions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;httpOnly&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;// JavaScript on the page cannot read it&lt;/span&gt;
  &lt;span class="n"&gt;secure&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;// sent over HTTPS only&lt;/span&gt;
  &lt;span class="n"&gt;sameSite&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'strict'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// not sent on cross-site requests (CSRF protection)&lt;/span&gt;
  &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'/auth'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="c1"&gt;// sent only to the auth routes&lt;/span&gt;
  &lt;span class="n"&gt;maxAge&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="c1"&gt;// 7 days&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/auth/login'&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;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&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;user&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;verifyCredentials&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;user&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Invalid credentials'&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;refreshToken&lt;/span&gt; &lt;span class="p"&gt;}&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;generateTokens&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cookie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'refreshToken'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;refreshToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;refreshCookieOptions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;accessToken&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;Why a cookie with these flags:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;httpOnly&lt;/code&gt; keeps the token away from scripts, so an XSS bug cannot read it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;secure&lt;/code&gt; and &lt;code&gt;sameSite&lt;/code&gt; stop it from being sent over plain HTTP or from another site.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;path&lt;/code&gt; limits it to the auth routes, so it is not attached to every API request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The client keeps the access token in memory. After a page reload, the client calls the refresh endpoint to get a new one.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Keep refresh tokens in your database, and rotate them
&lt;/h3&gt;

&lt;p&gt;Store a record for each refresh token on the server. Store a hash of the token and not the token itself, the same way you store passwords. If the database leaks, the hashes cannot be used to log in.&lt;/p&gt;

&lt;p&gt;Each time a refresh token is used, replace it with a new one. This is called rotation.&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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/auth/refresh'&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;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;refreshToken&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cookies&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;refreshToken&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Missing refresh token'&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;// Look up the record by the hash of the token&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;record&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;findRefreshToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;refreshToken&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="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expiresAt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&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;return&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Invalid refresh token'&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;// The token was already used once. It may have been stolen,&lt;/span&gt;
  &lt;span class="c1"&gt;// so end every session of this user.&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;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;revoked&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;revokeAllUserRefreshTokens&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;userId&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Invalid refresh token'&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;// Rotate: the old token can never be used again&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;revokeRefreshToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;user&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;getUserById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;userId&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="n"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;refreshToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;newRefreshToken&lt;/span&gt; &lt;span class="p"&gt;}&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;generateTokens&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cookie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'refreshToken'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newRefreshToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;refreshCookieOptions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;accessToken&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;Rotation also gives you theft detection. If an old refresh token appears a second time, either the attacker or the real user is holding a copy that was already replaced. You cannot tell which one, so the safe response is to revoke all sessions of that user and ask for a new login.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Set expiry and give users a way out
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Give refresh tokens an idle timeout. With rotation, each new token restarts the timer, so active users stay logged in and unused sessions expire.&lt;/li&gt;
&lt;li&gt;Add an absolute limit as well, for example 30 days, after which the user must log in again.&lt;/li&gt;
&lt;li&gt;Revoke refresh tokens when the user changes their password.
&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="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/auth/logout'&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;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;refreshToken&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cookies&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;refreshToken&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;revokeRefreshTokenByHash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;refreshToken&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clearCookie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'refreshToken'&lt;/span&gt;&lt;span class="p"&gt;,&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="s1"&gt;'/auth'&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="s1"&gt;'Logged out'&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="c1"&gt;// Log out from every device&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/auth/logout-all'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;requireAuth&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;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&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;await&lt;/span&gt; &lt;span class="nf"&gt;revokeAllUserRefreshTokens&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clearCookie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'refreshToken'&lt;/span&gt;&lt;span class="p"&gt;,&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="s1"&gt;'/auth'&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="s1"&gt;'Logged out from all devices'&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;h3&gt;
  
  
  4. Watch for unusual activity
&lt;/h3&gt;

&lt;p&gt;Save some context with each refresh token, such as the device and the rough location. When a refresh request looks very different, treat it with care.&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;checkRefreshRequest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&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;sameDevice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;userAgent&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;userAgent&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;sameRegion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;country&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;sameDevice&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;sameRegion&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;sendSecurityAlert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;userId&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="s1"&gt;'require-login'&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="s1"&gt;'ok'&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;Be careful with false alarms. IP addresses change often on mobile networks, so an alert email is usually a better first step than a forced logout.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Protect the signing keys
&lt;/h3&gt;

&lt;p&gt;Anyone who has your signing key can create valid tokens for any user.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generate long, random keys. Do not use a word or a phrase.&lt;/li&gt;
&lt;li&gt;Keep them out of version control. Load them from environment variables or a secret manager such as AWS KMS or HashiCorp Vault.&lt;/li&gt;
&lt;li&gt;Use different keys for each environment.&lt;/li&gt;
&lt;li&gt;Plan for key rotation. Put a key id (&lt;code&gt;kid&lt;/code&gt;) in the token header, so that tokens signed with the previous key still verify until they expire.
&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;const&lt;/span&gt; &lt;span class="n"&gt;keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;JWT_SECRET_V1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;v2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;JWT_SECRET_V2&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;currentKeyId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;JWT_KEY_ID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// for example 'v2'&lt;/span&gt;

&lt;span class="c1"&gt;// Sign new tokens with the current key&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;signingKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;currentKeyId&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Verify with the key named in the token header&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;getVerificationKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kid&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;keys&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;kid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the refresh token is a random string that you look up in the database, it does not need a signing key at all.&lt;/p&gt;




&lt;h2&gt;
  
  
  Advanced notes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  JWT vs. opaque tokens
&lt;/h3&gt;

&lt;p&gt;An opaque token is a random string with no meaning of its own. The server looks it up to find out who it belongs to.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;JWT&lt;/th&gt;
&lt;th&gt;Opaque token&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Content&lt;/td&gt;
&lt;td&gt;Carries its own data&lt;/td&gt;
&lt;td&gt;A random reference&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Verification&lt;/td&gt;
&lt;td&gt;Signature check, no lookup&lt;/td&gt;
&lt;td&gt;Database or cache lookup&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Size&lt;/td&gt;
&lt;td&gt;Larger&lt;/td&gt;
&lt;td&gt;Small&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Revocation&lt;/td&gt;
&lt;td&gt;Hard before it expires&lt;/td&gt;
&lt;td&gt;Immediate&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A common setup uses both: a JWT for the access token, because it is checked on every request, and an opaque string for the refresh token, because it must be easy to revoke.&lt;/p&gt;

&lt;h3&gt;
  
  
  OAuth 2.0
&lt;/h3&gt;

&lt;p&gt;If your users log in through an OAuth 2.0 or OpenID Connect provider, the same two tokens appear. The current recommendations are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use the &lt;strong&gt;Authorization Code flow with PKCE&lt;/strong&gt; for every kind of client: server-side apps, SPAs, and mobile apps.&lt;/li&gt;
&lt;li&gt;Do not use the Implicit flow or the Password grant. Current guidance advises against both.&lt;/li&gt;
&lt;li&gt;Match redirect URIs exactly.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;state&lt;/code&gt; parameter to protect the redirect against CSRF.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  PKCE
&lt;/h3&gt;

&lt;p&gt;PKCE (Proof Key for Code Exchange) protects the authorization code. The client creates a random secret, sends only its hash at the start, and reveals the secret itself when it exchanges the code for tokens. A stolen code is useless without that secret.&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;// A random secret, kept by the client&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;codeVerifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generateRandomString&lt;/span&gt;&lt;span class="p"&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="c1"&gt;// Its SHA-256 hash, sent with the first request&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;codeChallenge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;base64UrlEncode&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;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;codeVerifier&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;params&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;URLSearchParams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;client_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;redirect_uri&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;redirectUri&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;response_type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'code'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;code_challenge&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;codeChallenge&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;code_challenge_method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'S256'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;state&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;authUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`${authEndpoint}?${params}`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h3&gt;
  
  
  Further reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://datatracker.ietf.org/doc/html/rfc6749" rel="noopener noreferrer"&gt;RFC 6749: The OAuth 2.0 Authorization Framework&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://datatracker.ietf.org/doc/html/rfc9700" rel="noopener noreferrer"&gt;RFC 9700: Best Current Practice for OAuth 2.0 Security&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://datatracker.ietf.org/doc/html/rfc7636" rel="noopener noreferrer"&gt;RFC 7636: Proof Key for Code Exchange (PKCE)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://datatracker.ietf.org/doc/html/rfc7519" rel="noopener noreferrer"&gt;RFC 7519: JSON Web Token (JWT)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html" rel="noopener noreferrer"&gt;OWASP Authentication Cheat Sheet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cheatsheetseries.owasp.org/cheatsheets/JSON_Web_Token_for_Java_Cheat_Sheet.html" rel="noopener noreferrer"&gt;OWASP JSON Web Token Cheat Sheet&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;access token&lt;/strong&gt; is short-lived and is sent with every API request.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;refresh token&lt;/strong&gt; is long-lived, is stored where scripts cannot read it, and is sent only to the refresh endpoint.&lt;/li&gt;
&lt;li&gt;Together they limit the damage of a stolen token and keep the user logged in.&lt;/li&gt;
&lt;li&gt;Keep a hashed record of each refresh token, rotate it on every use, and treat a reused token as a sign of theft.&lt;/li&gt;
&lt;li&gt;Give users a way to log out everywhere, and protect the signing keys.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What has your experience with token-based authentication been? If you solved a problem in a different way, share it in the comments.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>security</category>
      <category>authentication</category>
      <category>jwt</category>
    </item>
    <item>
      <title>CSR vs SSR in Next.js</title>
      <dc:creator>Khaled Saeed</dc:creator>
      <pubDate>Fri, 18 Jul 2025 10:02:40 +0000</pubDate>
      <link>https://dev.to/khaledsaeed18/csr-vs-ssr-in-nextjs-34o8</link>
      <guid>https://dev.to/khaledsaeed18/csr-vs-ssr-in-nextjs-34o8</guid>
      <description>&lt;p&gt;When you build an application with Next.js, one of the first decisions is where each page gets rendered: in the browser or on the server. Client-Side Rendering (&lt;strong&gt;CSR&lt;/strong&gt;) and Server-Side Rendering (&lt;strong&gt;SSR&lt;/strong&gt;) both work, and each fits different pages. This guide explains how they differ, shows the code for both, and helps you choose.&lt;/p&gt;

&lt;p&gt;Most examples here use the Pages Router (&lt;code&gt;pages/&lt;/code&gt;), where the difference is easiest to see. There is a section on the App Router (&lt;code&gt;app/&lt;/code&gt;) further down.&lt;/p&gt;

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

&lt;p&gt;Rendering is the process of turning your React components into HTML that the browser can display.&lt;/p&gt;

&lt;p&gt;Next.js supports several rendering methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CSR (Client-Side Rendering)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SSR (Server-Side Rendering)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SSG (Static Site Generation)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ISR (Incremental Static Regeneration)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This post focuses on CSR and SSR.&lt;/p&gt;




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

&lt;p&gt;With &lt;strong&gt;Client-Side Rendering&lt;/strong&gt;, the HTML sent to the browser is minimal. JavaScript then runs in the browser, fetches the data, and builds the content.&lt;/p&gt;

&lt;h3&gt;
  
  
  How CSR works
&lt;/h3&gt;

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




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

&lt;p&gt;With &lt;strong&gt;Server-Side Rendering&lt;/strong&gt;, the server builds the HTML for every request and sends the browser a complete page.&lt;/p&gt;

&lt;h3&gt;
  
  
  How SSR works
&lt;/h3&gt;

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




&lt;h2&gt;
  
  
  Key differences
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Aspect&lt;/th&gt;
&lt;th&gt;CSR&lt;/th&gt;
&lt;th&gt;SSR&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Initial load&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Slower (JavaScript must load first)&lt;/td&gt;
&lt;td&gt;Faster (HTML is ready immediately)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;SEO&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Weaker (content depends on JavaScript)&lt;/td&gt;
&lt;td&gt;Strong (content is in the HTML)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Server load&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Lower&lt;/td&gt;
&lt;td&gt;Higher&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Later navigation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Fast (no page render on the server)&lt;/td&gt;
&lt;td&gt;Needs a server round trip for each page&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;User experience&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Blank or loading page, then content&lt;/td&gt;
&lt;td&gt;Content appears immediately&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Caching&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Easy to cache static assets&lt;/td&gt;
&lt;td&gt;More work to cache well&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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




&lt;h2&gt;
  
  
  Code examples
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Client-Side Rendering
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// pages/csr-example.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'react'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;CSRExample&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="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;setData&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="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="n"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setLoading&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;true&lt;/span&gt;&lt;span class="p"&gt;)&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="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="c1"&gt;// This runs only in the browser&lt;/span&gt;
    &lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/api/data'&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="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="n"&gt;data&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;setData&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="nf"&gt;setLoading&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="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;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;loading&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Loading&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;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&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;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Side&lt;/span&gt; &lt;span class="nc"&gt;Rendered&lt;/span&gt; &lt;span class="nc"&gt;Page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&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;data&lt;/span&gt;&lt;span class="o"&gt;?&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="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nc"&gt;CSRExample&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Server-Side Rendering
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// pages/ssr-example.js&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;SSRExample&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Server&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Side&lt;/span&gt; &lt;span class="nc"&gt;Rendered&lt;/span&gt; &lt;span class="nc"&gt;Page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&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;data&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="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Generated&lt;/span&gt; &lt;span class="n"&gt;at&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="n"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&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="c1"&gt;// This function runs on the server for every request&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getServerSideProps&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;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'https://api.example.com/data'&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;data&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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;props&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="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;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&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;toISOString&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nc"&gt;SSRExample&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Performance comparison
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Time to First Byte (TTFB)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CSR&lt;/strong&gt;: Fast (the server sends a small shell)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SSR&lt;/strong&gt;: Slower (the server has to fetch data and render first)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  First Contentful Paint (FCP)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CSR&lt;/strong&gt;: Slow (waits for JavaScript to run)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SSR&lt;/strong&gt;: Fast (the HTML already has the content)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Time to Interactive (TTI)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CSR&lt;/strong&gt;: Slow (depends on the size of the JavaScript bundle)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SSR&lt;/strong&gt;: Moderate (the page still has to hydrate)&lt;/li&gt;
&lt;/ul&gt;

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




&lt;h3&gt;
  
  
  Mixing both on one page
&lt;/h3&gt;

&lt;p&gt;You do not have to choose one for the whole page. Render the important content on the server and load heavy parts only in the browser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// pages/optimized-ssr.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dynamic&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next/dynamic'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// `ssr: false` skips server rendering for this component.&lt;/span&gt;
&lt;span class="c1"&gt;// It is loaded and rendered only in the browser.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nc"&gt;HeavyComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dynamic&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="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'../components/HeavyComponent'&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;loading&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Loading&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;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&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;ssr&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="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;OptimizedSSR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;criticalData&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Optimized&lt;/span&gt; &lt;span class="no"&gt;SSR&lt;/span&gt; &lt;span class="nc"&gt;Page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Critical&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="n"&gt;criticalData&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="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&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;/* This component loads only in the browser */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;HeavyComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&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;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getServerSideProps&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;// Fetch only the critical data on the server&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'https://api.example.com/critical-data'&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;criticalData&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;criticalData&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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nc"&gt;OptimizedSSR&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  When to use what
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use CSR when
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You are building a dashboard or admin panel&lt;/li&gt;
&lt;li&gt;Users interact with the page a lot&lt;/li&gt;
&lt;li&gt;SEO is not a priority&lt;/li&gt;
&lt;li&gt;You need real-time updates&lt;/li&gt;
&lt;li&gt;The page sits behind a login&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Use SSR when
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;SEO matters&lt;/li&gt;
&lt;li&gt;The first page load has to be fast&lt;/li&gt;
&lt;li&gt;Content changes often&lt;/li&gt;
&lt;li&gt;You are building an e-commerce site&lt;/li&gt;
&lt;li&gt;Links to the page are shared on social media&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The same idea in the App Router
&lt;/h2&gt;

&lt;p&gt;The App Router (&lt;code&gt;app/&lt;/code&gt;) uses different names for the same choice. Components are &lt;strong&gt;Server Components&lt;/strong&gt; by default, and you opt into the browser with &lt;code&gt;'use client'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A page that is rendered on the server for every request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/ssr-example/page.js&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;Page&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;// `no-store` tells Next.js to fetch on every request&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'https://api.example.com/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;cache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'no-store'&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;data&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Server&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Side&lt;/span&gt; &lt;span class="nc"&gt;Rendered&lt;/span&gt; &lt;span class="nc"&gt;Page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&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;data&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="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A page that fetches its data in the browser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/csr-example/page.js&lt;/span&gt;
&lt;span class="s1"&gt;'use client'&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="n"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'react'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;Page&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="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;setData&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="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="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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/api/data'&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="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="n"&gt;setData&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;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;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Loading&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;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&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;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Side&lt;/span&gt; &lt;span class="nc"&gt;Rendered&lt;/span&gt; &lt;span class="nc"&gt;Page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&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;data&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="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is no &lt;code&gt;getServerSideProps&lt;/code&gt; in the App Router. Caching defaults have also changed between Next.js versions, so check the docs for the version you use.&lt;/p&gt;




&lt;h2&gt;
  
  
  Advanced topics
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Dynamic imports for code splitting
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dynamic&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next/dynamic'&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="n"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'react'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nc"&gt;DynamicChart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dynamic&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="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'../components/Chart'&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;loading&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="n"&gt;chart&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;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&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;ssr&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="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nc"&gt;DynamicTable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dynamic&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="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'../components/Table'&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;loading&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="n"&gt;table&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;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&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="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;AdvancedCSR&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="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setView&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="s1"&gt;'chart'&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;nav&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="n"&gt;onClick&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'chart'&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="nc"&gt;Chart&lt;/span&gt; &lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="n"&gt;onClick&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'table'&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="nc"&gt;Table&lt;/span&gt; &lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;nav&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;view&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'chart'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DynamicChart&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;view&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'table'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DynamicTable&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nc"&gt;AdvancedCSR&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Streaming SSR
&lt;/h3&gt;

&lt;p&gt;With streaming, the server sends the page in parts. The fast content arrives first, and slow parts follow when they are ready. This example uses the App Router, where a component can be &lt;code&gt;async&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/streaming/page.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Suspense&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'react'&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="nc"&gt;SlowComponent&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;// Simulate a slow data source&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3000&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Slow&lt;/span&gt; &lt;span class="n"&gt;loading&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;Page&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Streaming&lt;/span&gt; &lt;span class="no"&gt;SSR&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;This&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="n"&gt;loads&lt;/span&gt; &lt;span class="n"&gt;immediately&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Suspense&lt;/span&gt; &lt;span class="n"&gt;fallback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="n"&gt;slow&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="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;SlowComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Suspense&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Streaming only shows on pages that are rendered at request time. A page that Next.js builds ahead of time is already complete when it is served.&lt;/p&gt;




&lt;h2&gt;
  
  
  Best practices
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Choose the rendering strategy per page
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The decision, written as code&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;getRenderingStrategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;requirements&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="n"&gt;requirements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seo&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;requirements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;freshData&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="s1"&gt;'SSR'&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;requirements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;interactivity&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;requirements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seo&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="s1"&gt;'CSR'&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="s1"&gt;'Hybrid'&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;h3&gt;
  
  
  2. Measure your bundle size
&lt;/h3&gt;

&lt;p&gt;Before you optimize, look at what is in your JavaScript bundle. The official analyzer shows it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// next.config.js&lt;/span&gt;
&lt;span class="cm"&gt;/** @type {import('next').NextConfig} */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;nextConfig&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;withBundleAnalyzer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'@next/bundle-analyzer'&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;enabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;ANALYZE&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&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="kd"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;withBundleAnalyzer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nextConfig&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;Run it with &lt;code&gt;ANALYZE=true npm run build&lt;/code&gt;. Then move large, rarely used components behind &lt;code&gt;next/dynamic&lt;/code&gt;, as in the examples above.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Use progressive enhancement
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'react'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;ProgressiveForm&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="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;isClient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setIsClient&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="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="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="nf"&gt;setIsClient&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;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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"email"&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"email"&lt;/span&gt; &lt;span class="n"&gt;required&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;/* Extra features only in the browser */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;isClient&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;AutoComplete&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;RealTimeValidation&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"submit"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Submit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;form&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Handle errors and loading states
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;RobustSSR&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="n"&gt;error&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;error&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ErrorComponent&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;error&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Robust&lt;/span&gt; &lt;span class="no"&gt;SSR&lt;/span&gt; &lt;span class="nc"&gt;Page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&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;data&lt;/span&gt;&lt;span class="o"&gt;?&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="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&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;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getServerSideProps&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;try&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;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'https://api.example.com/data'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;res&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="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="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Failed to fetch data'&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;data&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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;props&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="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&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;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;error&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="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;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Choosing between CSR and SSR is not about picking one for the whole application. It is about what each page needs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use SSR&lt;/strong&gt; for pages where SEO and a fast first load matter&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use CSR&lt;/strong&gt; for highly interactive pages where SEO does not matter&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next.js lets you mix both in the same application, and even on the same page. Profile your application, follow the path your users take, and choose per page.&lt;/p&gt;

&lt;p&gt;Remember: &lt;strong&gt;performance is not only about loading fast. It is about the right content being ready at the right time.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Further reading: &lt;a href="https://nextjs.org/docs/app/getting-started/server-and-client-components" rel="noopener noreferrer"&gt;Server and Client Components&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>nextjs</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Automatically Set Access Tokens in Postman After Login (No More Copy-Paste)</title>
      <dc:creator>Khaled Saeed</dc:creator>
      <pubDate>Mon, 07 Jul 2025 08:44:20 +0000</pubDate>
      <link>https://dev.to/khaledsaeed18/automatically-set-access-tokens-in-postman-after-login-no-more-copy-paste-19mi</link>
      <guid>https://dev.to/khaledsaeed18/automatically-set-access-tokens-in-postman-after-login-no-more-copy-paste-19mi</guid>
      <description>&lt;p&gt;If you work with APIs that use access tokens (like JWTs), you know the routine:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Send a &lt;code&gt;POST /login&lt;/code&gt; request&lt;/li&gt;
&lt;li&gt;Copy the access token from the response&lt;/li&gt;
&lt;li&gt;Paste it into the &lt;code&gt;Authorization&lt;/code&gt; header of every other request&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then the token expires and you do it all again.&lt;/p&gt;

&lt;p&gt;Postman can do this for you. A short script on the login request saves the token into an environment variable, and every other request reads it from there. You log in once and keep working.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you need
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Postman (the desktop app or the web version)&lt;/li&gt;
&lt;li&gt;A login endpoint that returns an access token in its JSON response&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The examples below assume a response shaped like this. Yours may differ, and that is fine: you only need to change one line of the script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"success"&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="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"accessToken"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"eyJhbGciOi..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"refreshToken"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"dGhpcyBpcyBh..."&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 1: Create an environment
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Open &lt;strong&gt;Environments&lt;/strong&gt; in the sidebar and create a new one (for example &lt;code&gt;Local API&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Add a variable called &lt;code&gt;accessToken&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Set its type to &lt;code&gt;secret&lt;/code&gt; and leave the value empty&lt;/li&gt;
&lt;li&gt;Select the environment in the environment dropdown at the top right&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The last step is the one people forget. If no environment is selected, the script has nowhere to save the token.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Add a script to the login request
&lt;/h2&gt;

&lt;p&gt;Open your login request (for example &lt;code&gt;POST /api/v1/auth/login&lt;/code&gt;), go to &lt;strong&gt;Scripts&lt;/strong&gt;, choose &lt;strong&gt;Post-response&lt;/strong&gt;, and add this. In older versions of Postman this tab is called &lt;strong&gt;Tests&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;success&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;res&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;accessToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;pm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"accessToken"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&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;accessToken&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="s2"&gt;"accessToken saved to environment"&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;else&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="s2"&gt;"Could not find accessToken in the response"&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;h3&gt;
  
  
  What the script does
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It parses the JSON response of the login request.&lt;/li&gt;
&lt;li&gt;It checks that the response contains an &lt;code&gt;accessToken&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If it does, it stores the token in the &lt;code&gt;accessToken&lt;/code&gt; environment variable.&lt;/li&gt;
&lt;li&gt;It prints a line to the Postman console, so you can see what happened.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your API returns the token somewhere else, change the path. For a response like &lt;code&gt;{ "token": "..." }&lt;/code&gt;, use &lt;code&gt;res.token&lt;/code&gt; in place of &lt;code&gt;res.data.accessToken&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can save other fields the same way:&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;pm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"refreshToken"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&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;refreshToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;pm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"userId"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&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;user&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;h2&gt;
  
  
  Step 3: Use the token in other requests
&lt;/h2&gt;

&lt;p&gt;Once the token is saved, any request can read it with &lt;code&gt;{{accessToken}}&lt;/code&gt;. There are three ways to do that. The first one is the least work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Option 1: Set it once on the collection
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Open your collection and go to the &lt;strong&gt;Authorization&lt;/strong&gt; tab&lt;/li&gt;
&lt;li&gt;Choose &lt;strong&gt;Bearer Token&lt;/strong&gt; as the auth type&lt;/li&gt;
&lt;li&gt;Set the token to &lt;code&gt;{{accessToken}}&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Every request in the collection uses this by default, because new requests are set to &lt;strong&gt;Inherit auth from parent&lt;/strong&gt;. You never touch the header again.&lt;/p&gt;

&lt;h3&gt;
  
  
  Option 2: Set it on a single request
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;In the request, go to the &lt;strong&gt;Authorization&lt;/strong&gt; tab&lt;/li&gt;
&lt;li&gt;Choose &lt;strong&gt;Bearer Token&lt;/strong&gt; as the auth type&lt;/li&gt;
&lt;li&gt;Set the token to &lt;code&gt;{{accessToken}}&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Option 3: Add the header yourself
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="nc"&gt;Authorization&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bearer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;accessToken&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;Whichever you choose, the token updates after each login and is used wherever &lt;code&gt;{{accessToken}}&lt;/code&gt; appears.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: refresh the token
&lt;/h2&gt;

&lt;p&gt;If your API has a refresh endpoint (for example &lt;code&gt;POST /refresh-token&lt;/code&gt;), you can handle that too.&lt;/p&gt;

&lt;p&gt;Create a request for the refresh call, and give it a &lt;strong&gt;Post-response&lt;/strong&gt; script:&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;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;newAccessToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;pm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"accessToken"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;newAccessToken&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="s2"&gt;"accessToken refreshed"&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;When a request comes back with &lt;code&gt;401&lt;/code&gt;, send the refresh request once and try again. As with the login script, change &lt;code&gt;res.newAccessToken&lt;/code&gt; to match the field your API returns.&lt;/p&gt;

&lt;h2&gt;
  
  
  If it does not work
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;{{accessToken}}&lt;/code&gt; shows as unresolved (red).&lt;/strong&gt; No environment is selected, or the variable has a different name.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The console says it could not find the token.&lt;/strong&gt; Your response has a different shape. Open the response body and change the path in the script.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nothing is printed at all.&lt;/strong&gt; Open the console with &lt;strong&gt;View &amp;gt; Show Postman Console&lt;/strong&gt; and send the login request again. If there is still no output, check that the script is on the &lt;strong&gt;Post-response&lt;/strong&gt; tab of the login request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The request still returns &lt;code&gt;401&lt;/code&gt;.&lt;/strong&gt; Look at the request headers in the console to confirm that &lt;code&gt;Authorization: Bearer ...&lt;/code&gt; was sent.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The login request saves the access token by itself.&lt;/li&gt;
&lt;li&gt;Other requests read it through &lt;code&gt;{{accessToken}}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You no longer copy and paste tokens between tabs.&lt;/li&gt;
&lt;li&gt;The same pattern works for a refresh token.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is a small script, and it removes a step you would otherwise repeat many times a day.&lt;/p&gt;

&lt;p&gt;If you would like a follow-up on refreshing the token automatically before each request, let me know in the comments.&lt;/p&gt;

</description>
      <category>postman</category>
      <category>api</category>
      <category>automation</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
