<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Leejjon on Medium]]></title>
        <description><![CDATA[Stories by Leejjon on Medium]]></description>
        <link>https://medium.com/@leejjon?source=rss-e00e4e1c1655------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*YZMfPzagIboViZ2AIae8ug.png</url>
            <title>Stories by Leejjon on Medium</title>
            <link>https://medium.com/@leejjon?source=rss-e00e4e1c1655------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 18:57:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@leejjon/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The long road that lead me to react-hook-forms, Zod and finally @conform-to/react]]></title>
            <link>https://leejjon.medium.com/the-long-road-that-lead-me-to-react-hook-forms-zod-and-finally-conform-to-react-2ef5c0d5b335?source=rss-e00e4e1c1655------2</link>
            <guid isPermaLink="false">https://medium.com/p/2ef5c0d5b335</guid>
            <category><![CDATA[zod]]></category>
            <category><![CDATA[react-hook-form]]></category>
            <category><![CDATA[conform]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react-router]]></category>
            <dc:creator><![CDATA[Leejjon]]></dc:creator>
            <pubDate>Tue, 10 Nov 2026 15:43:38 GMT</pubDate>
            <atom:updated>2026-11-10T15:55:57.325Z</atom:updated>
            <content:encoded><![CDATA[<p>In this really long blog post I’ll write about various ways you can build a react app that stores a comment. I’ll list requirements and go through a few ways of building the same application. Each time I will point out what is missing and why we move on to the next attempt. We will use: React Router (framework mode), <strong>Zod</strong>, <strong>react-hook-form</strong> and <strong>@conform-to/react</strong>.</p><ul><li>In this post I do not let AI write anything, I do these projects by hand to learn and practice.</li><li>This post is for people who still want to understand code. For people who just use agents without reading code there is not much point in reading on.</li><li>As I do in all my blog posts, I will provide instructions to build an example project from scratch, and you can find every step on GitHub. So you can either follow it or clone a version of the repository to try things out.</li></ul><h3>Why talk about forms?</h3><p>As a full stack developer I work on many different things. I did not use forms for a long time, yet my favorite frontend framework Remix (now React Router) put a lot of focus on <a href="https://reactrouter.com/api/components/Form#form">using forms the right way in its docs</a>. At work some developers added react-hook-forms and I didn’t really understand what it was for. So I decided to fully dive into forms in React. Around that time I saw Kent C Dodds tweet this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/606/1*uHEmUqG-05aIUgY-dexA8g.png" /></figure><p>This kind of made me want to write about it even more. Maybe for him forms is a boring subject because he has done this many times and covers the subject in his React courses already. But for me it’s something worth learning.</p><h3>Prerequisites (if you want to build along)</h3><ul><li>Node.js, I used v24.19.0</li><li>Git</li><li>A terminal that runs bash.</li></ul><h3>What will we build?</h3><p>In this post we will be building a simple form to submit comments.</p><p>Functional requirements:</p><ul><li>It must be possible to enter your name and message.</li><li>There is a submit button that sends the name and message to an API that stores them.</li><li>There should be validations that notify the user if the API failed to store the name and message.</li><li>The form should be accessible.</li><li>The form should be usable when a user has JavaScript disabled.</li></ul><p>My technical choices:</p><ul><li><strong>TypeScript</strong>: I want type safety.</li><li><strong>React</strong>: I prefer TypeScript files to dynamically render <strong>JSX</strong> instead of template files to contain JavaScript or TypeScript like in Angular, Vue or Svelte.</li><li>I want validation logic in one place. Not different validation in the frontend and backend.</li></ul><p>We need a backend to actually store a comment. That rules out a simple React + Vite SPA project. For this post I decided that we <a href="https://reactrouter.com/start/framework/installation">will create a React app using the React Router framework</a> that supports server side rendering and thus provides a backend out of the box:</p><pre>npx create-react-router@latest rr-forms<br></pre><p>I called my project <strong>rr-forms</strong>, but use whatever you want.</p><h3>Chapter One — Using the old classic &lt;form&gt;</h3><p>The <strong>&lt;form&gt;</strong>, <strong>&lt;input&gt;</strong>, <strong>&lt;select&gt;</strong> and <strong>&lt;textarea&gt;</strong> tags <a href="https://en.wikipedia.org/wiki/HTML_form">were added to the HTML standard in 1995</a>. There was no JavaScript to validate inputs on the client. Netscape added a beta version in 1995, and Microsoft shipped a similar version with the name of JScript that next year.</p><p>During this time, validation had to be done on the server. At the time people used the Apache HTTP server or Microsoft IIS. Now we are not going to use those. Let’s try using the old classic form tags with the React Router framework. Yes, we will use React Router framework without React. We can do so using <a href="https://reactrouter.com/how-to/resource-routes">resource routes</a>.</p><p>You can skip this if you want, but I think every developer should build a form without JavaScript at least once to gain understanding of how the web works and used to work. And as a bonus you’ll learn how <a href="https://reactrouter.com/how-to/resource-routes">resource routes</a> work in the React Router framework.</p><p>How do we build a form without React in our newly bootstrapped React Router framework?</p><ul><li>First off, delete the <strong>welcome</strong> directory and it’s contents.</li><li>Change <strong>home.tsx</strong> to <strong>home.ts</strong> and put this code in:</li></ul><pre>import type {Route} from &quot;./+types/home&quot;;<br><br>export async function loader(args: Route.LoaderArgs) {<br>    const commentForm = &quot;&lt;form action=&#39;/&#39; method=&#39;POST&#39;&gt;&quot; +<br>        &quot;&lt;label&gt;Name:&lt;/label&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;input name=&#39;name&#39; /&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;label&gt;Message:&lt;/label&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;textarea name=&#39;message&#39;&gt;&lt;/textarea&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;br /&gt;&quot; +<br>        &quot;&lt;button type=&#39;submit&#39;&gt;Submit&lt;/button&gt;&quot; +<br>        &quot;&lt;/form&gt;&quot;;<br>    return new Response(`&lt;html&gt;${commentForm}&lt;/html&gt;`, {<br>        headers: {&quot;Content-Type&quot;: &quot;text/html&quot;},<br>        status: 200<br>    });<br>}</pre><p>This code simply sends static HTML in a HTTP response. If you get errors in your IDE, run npm run typecheck to fix this.</p><ul><li>In the <strong>routes.ts</strong> file, also change the index to <strong>home.ts</strong> like:</li></ul><pre>import { type RouteConfig, index } from &quot;@react-router/dev/routes&quot;;<br><br>export default [<br>    index(&quot;routes/home.ts&quot;)<br>] satisfies RouteConfig;</pre><p>When you run this code with npm run dev , you can fill in the form and press submit, but you’ll get a 404 error:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*sI0MqtaNnWzwRjeujE10MQ.gif" /></figure><p>Because we haven’t created the backend API. You can find <a href="https://github.com/Leejjon/rr-forms/tree/classic-form">the code so far on GitHub</a>.</p><h3>Configuring the the backend API</h3><p>In the <strong>home.ts</strong> file, add the following action function:</p><pre>import type {Route} from &quot;./+types/home&quot;;<br><br>export async function action({request}: Route.ActionArgs) {<br>    const formData = await request.formData();<br>    const name = formData.get(&quot;name&quot;);<br>    const message = formData.get(&quot;message&quot;);<br>    console.log(`Name=${name} Message=${message}`);<br>    return &quot;Form submitted&quot;;<br>}<br><br>export async function loader(args: Route.LoaderArgs) {<br>    const commentForm = &quot;&lt;form action=&#39;/&#39; method=&#39;POST&#39;&gt;&quot; +<br>        &quot;&lt;label&gt;Name:&lt;/label&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;input name=&#39;name&#39; /&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;label&gt;Message:&lt;/label&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;textarea name=&#39;message&#39;&gt;&lt;/textarea&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;br /&gt;&quot; +<br>        &quot;&lt;button type=&#39;submit&#39;&gt;Submit&lt;/button&gt;&quot; +<br>        &quot;&lt;/form&gt;&quot;;<br>    return new Response(`&lt;html&gt;${commentForm}&lt;/html&gt;`, {<br>        headers: {&quot;Content-Type&quot;: &quot;text/html&quot;},<br>        status: 200<br>    });<br>}</pre><p>This creates a minimal endpoint implementation. All we do right now is log the submitted name and message on the server.</p><p>Run npm run dev to test out your form:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*0JVy9SKid0GIW2LNmqdC8g.gif" /></figure><p>And your server should log:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*C60gmQ_hT0KwkUz4CLlYuQ.png" /></figure><p>You can find <a href="https://github.com/Leejjon/rr-forms/tree/classic-form-with-endpoint">the code so far on GitHub</a>:</p><h3>Adding the simplest possible in-memory db to store comments</h3><p>Normally you would expect to view your comment after posting it. But to display comments we need to store comments after they have been submitted via our forms. I first looked at things like <a href="https://sqlite.org/">SQLite</a> for this, but <a href="https://medium.com/@ravipatel.it/rest-api-crud-operations-using-express-js-and-in-memory-database-0fb0989a708f">this post</a> convinced me to keep things even simpler. We will define an array of comments and export it. This will function as a singleton and the array will be kept in memory for as long as this instance of our backend lives.</p><p>Note that this approach is only for demo purposes and means that comments that are stored will be gone when the server stops running.</p><p>Create a directory called <strong>common</strong> in your <strong>app</strong> folder, and create a file called <strong>comments.ts</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/267/1*arm2UJCjl90aG6AoWJjNoQ.png" /></figure><p>In the <strong>comments.ts</strong> file, we first declare a type for our comments:</p><pre>import {type UUID} from &quot;node:crypto&quot;;<br><br>export type NewComment = {<br>    name: string;<br>    message: string;<br>}<br><br>export type Comment = {<br>    id: UUID;<br>    timestamp: number;<br>    name: string;<br>    message: string;<br>}</pre><p>We add a UUID and timestamp to make sure we can identify and sort the messages (normally I’d combine this and use <a href="https://github.com/uuidjs/uuid#uuidv7options-buffer-offset">time based UUID’s</a>, but JavaScript doesn’t have native support for UUID v7 and let’s not add too many eternal packages now). We will store the timestamp as string, because the Date object in JavaScript doesn’t serialize through json properly.</p><p>Create a directory called <strong>database</strong> in your <strong>app</strong> folder, and create a file called<strong> comments.server.ts</strong> in it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/344/1*_hLSWAo9sK9teiJWARGFag.png" /><figcaption>By making the extension .server.ts, react router will treat it as a server module. This will make sure this code doesn’t end up in a client bundle if you would use React.</figcaption></figure><p>In the <strong>comments.server.ts</strong> file, simply define an array of the <strong>Comment</strong> object type we just created, and export functions to get and modify it:</p><pre>import {type Comment, type NewComment} from &quot;~/common/comments&quot;;<br>import {randomUUID} from &quot;node:crypto&quot;;<br><br>const comments: Comment[] = [];<br><br>export function getComments () {<br>    return [...comments].sort((a, b) =&gt; a.timestamp - b.timestamp);<br>}<br><br>export function addComment(comment: NewComment) {<br>    comments.push({id: randomUUID(), timestamp: Date.now(), name: comment.name, message: comment.message} as Comment);<br>}</pre><p>In the <strong>getComments</strong> function I return a shallow copy that I sort on timestamp, so that the messages will be in the correct order when I render them.</p><h3>Let the endpoint store a comment</h3><p>In the <strong>home.ts</strong> action, modify the action function to store the form data as a <strong>Comment</strong> object with this new <strong>addComment</strong> function:</p><pre>import type {Route} from &quot;./+types/home&quot;;<br>import {addComment, getComments} from &quot;~/database/comments.server&quot;;<br>import type {NewComment} from &quot;~/common/comments&quot;;<br>import {redirect} from &quot;react-router&quot;;<br><br>export async function action({request}: Route.ActionArgs) {<br>    const formData = await request.formData();<br>    const name = formData.get(&quot;name&quot;);<br>    const message = formData.get(&quot;message&quot;);<br>    addComment({name, message} as NewComment);<br>    return redirect(&quot;/&quot;);<br>}<br><br>// The loader etc...</pre><p>Also instead of returning “Form submitted”, let’s just redirect to the page where we came from. That page can then display the comments array that we just updated.</p><h3>Display the comments</h3><p><em>WARNING: This code lacks protection against cross site scripting. NEVER display raw user input on websites.</em></p><p>In the <strong>home.ts</strong> route, call the <strong>getComments()</strong> function and map the Comment objects to some HTML. This way the comments will be nicely displayed on the top of the page:</p><pre>import type {Route} from &quot;./+types/home&quot;;<br>import {getComments} from &quot;~/database/comments.server&quot;;<br><br>// The action...<br><br>export async function loader(args: Route.LoaderArgs) {<br>    const commentsElement = `&lt;div&gt;${getComments().map((comment) =&gt; {<br>        return `&lt;div key=&quot;${comment.id}&quot;&gt;&lt;b&gt;${comment.name}:&lt;/b&gt; ${comment.message}` +<br>            `&lt;br /&gt;&lt;i&gt;Posted at ${new Date(comment.timestamp).toISOString()}&lt;/i&gt;&lt;/div&gt;`<br>    }).join(&#39;&lt;br/&gt;&#39;)}&lt;/div&gt;`;<br><br>    const commentForm = &quot;&lt;form action=&#39;/api/comments&#39; method=&#39;POST&#39;&gt;&quot; +<br>        `${commentsElement}` +<br>        &quot;&lt;br /&gt;&quot; +<br>        &quot;&lt;label&gt;Name:&lt;/label&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;input name=&#39;name&#39; /&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;label&gt;Message:&lt;/label&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;textarea name=&#39;message&#39;&gt;&lt;/textarea&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;br /&gt;&quot; +<br>        &quot;&lt;button type=&#39;submit&#39;&gt;Submit&lt;/button&gt;&quot; +<br>        &quot;&lt;/form&gt;&quot;;<br>    return new Response(`&lt;html&gt;${commentForm}&lt;/html&gt;`, {<br>        headers: {&quot;Content-Type&quot;: &quot;text/html&quot;},<br>        status: 200<br>    });<br>}</pre><p><em>Warning: I do .toISOString() here which means that the timestamp it always displayed in UTC. Displaying a timestamp in the right timezone is mostly handled in JavaScript on the client.</em></p><p>Now run again using npm run dev and test the result!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*R0F2AKxJs77y8B0nyM67_A.gif" /></figure><p>You can find <a href="https://github.com/Leejjon/rr-forms/tree/classic-form-with-in-memory-db">the code so far on GitHub</a>.</p><h3>Server side validation</h3><p>Right now our form has no validation and is very vulnerable to cross site scripting attacks. By injecting HTML and JavaScript in comments, attackers might try to redirect users to dangerous websites or even injecting login pop ups to steal passwords. If you started to do frontend with React, you might not realize that React is protecting you against stuff like this by default.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*Q_4eiLugQrVr_augQQ4GGw.gif" /></figure><p>In general you should never trust data coming from a database, even if the input was filtered. If you do raw server side rendering like we do here, you should probably use some template engines or validation libraries.</p><p>For the scope of this already large blog post we will validate at least the name input, so that it’s not possible to inject any HTML or JavaScript in the name.</p><p>First create a <strong>validation.ts</strong> file in the <strong>common</strong> directory:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/336/1*EARHy8TThd3DGBdfK7Sa6w.png" /></figure><p>In this file we will create our validation using a regular expression (aka Regex) for user names <a href="https://stackoverflow.com/questions/1221985/how-to-validate-a-user-name-with-regex">recommended by a StackOverflow post</a>. It basically checks if the name has characters that are <strong>not</strong> letters, numbers or white-space:</p><pre>const nameMatcher = /^[A-Za-z0-9]+(?:[ _\-][A-Za-z0-9]+)*$/<br><br>export function nameIsValid(comment: string | undefined): boolean {<br>    return comment !== undefined<br>        &amp;&amp; comment.length &gt; 1<br>        &amp;&amp; comment.length &lt;= 20<br>        &amp;&amp; comment?.match(nameMatcher) !== null;<br>}<br></pre><p>This function is exported so we can now use this validation function in the <strong>home.ts</strong> page:</p><pre>import type {Route} from &quot;./+types/home&quot;;<br>import {addComment, getComments} from &quot;~/database/comments.server&quot;;<br>import type {NewComment} from &quot;~/common/comments&quot;;<br>import {redirect} from &quot;react-router&quot;;<br>import {nameIsValid} from &quot;~/common/validation&quot;;<br><br>export async function action({request}: Route.ActionArgs) {<br>    const formData = await request.formData();<br>    const name = formData.get(&quot;name&quot;);<br>    const message = formData.get(&quot;message&quot;);<br><br>    if (nameIsValid(name?.toString())) {<br>        addComment({name, message} as NewComment);<br>        return redirect(&quot;/&quot;);<br>    } else {<br>        return new Response(&quot;Invalid name&quot;, {<br>            status: 400<br>        });<br>    }<br>}<br>// The loader and more</pre><p>I also added checks on the length. I know you can also put length checks in the regular expression, but I’m afraid that I will screw up the regular expression if I try to modify it.</p><p>Endpoints usually return a HTTP 400 (Bad Request) status code when input is invalid. But if you only rely on this, it is bad for the usability. Here I try to put a % in my name to trigger the validation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*wKFLRpztxCyGP2HEhdU67w.gif" /></figure><p>Obviously it is not nice to be redirected away from the form. The user would have to click back and try again.</p><p>You can find <a href="https://github.com/Leejjon/rr-forms/tree/classic-form-ugly-ssr-validation">the code so far on GitHub</a>.</p><h3>Rendering the form again on the server with error messages included</h3><p>From the action function in the <strong>home.ts</strong> file, we can also just render the entire form again, this time with an error behind the name field, if a name is incorrect.</p><pre>import type {Route} from &quot;./+types/home&quot;;<br>import {addComment, getComments} from &quot;~/database/comments.server&quot;;<br>import type {NewComment} from &quot;~/common/comments&quot;;<br>import {redirect} from &quot;react-router&quot;;<br>import {nameIsValid} from &quot;~/common/validation&quot;;<br><br>function createCommentsElements() {<br>    return `&lt;div&gt;${getComments().map((comment) =&gt; {<br>        return `&lt;div key=&quot;${comment.id}&quot;&gt;&lt;b&gt;${comment.name}:&lt;/b&gt; ${comment.message}` +<br>            `&lt;br /&gt;&lt;i&gt;Posted at ${new Date(comment.timestamp).toISOString()}&lt;/i&gt;&lt;/div&gt;`<br>    }).join(&#39;&lt;br/&gt;&#39;)}&lt;/div&gt;`;<br>}<br><br>function createFormElement(nameError?: string) {<br>    const nameErrorLabel = `&lt;label style=&quot;color: red; margin-left: 1em;&quot;&gt;${nameError}&lt;/label&gt;`;<br>    return &quot;&lt;form action=&#39;/&#39; method=&#39;POST&#39;&gt;&quot; +<br>        `${createCommentsElements()}` +<br>        &quot;&lt;br /&gt;&quot; +<br>        &quot;&lt;label&gt;Name:&lt;/label&gt;&lt;br /&gt;&quot; +<br>        `&lt;input name=&#39;name&#39; autocomplete=&#39;off&#39; /&gt;${nameError !== undefined ? nameErrorLabel : &#39;&#39;}&lt;br /&gt;` +<br>        &quot;&lt;label&gt;Message:&lt;/label&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;textarea name=&#39;message&#39;&gt;&lt;/textarea&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;br /&gt;&quot; +<br>        &quot;&lt;button type=&#39;submit&#39;&gt;Submit&lt;/button&gt;&quot; +<br>        &quot;&lt;/form&gt;&quot;;<br>}<br><br>export async function action({request}: Route.ActionArgs) {<br>    const formData = await request.formData();<br>    const name = formData.get(&quot;name&quot;);<br>    const message = formData.get(&quot;message&quot;);<br><br>    if (nameIsValid(name?.toString())) {<br>        addComment({name, message} as NewComment);<br>        return redirect(&quot;/&quot;);<br>    } else {<br>        return new Response(`&lt;html&gt;${createFormElement(&#39;You entered an invalid name.&#39;)}&lt;/html&gt;`, {<br>            headers: {&quot;Content-Type&quot;: &quot;text/html&quot;},<br>            status: 200<br>        });<br>    }<br>}<br><br>export async function loader(args: Route.LoaderArgs) {<br>    return new Response(`&lt;html&gt;${createFormElement()}&lt;/html&gt;`, {<br>        headers: {&quot;Content-Type&quot;: &quot;text/html&quot;},<br>        status: 200<br>    });<br>}</pre><p>Now if the name is not valid, the form will be rendered with the error message ‘You entered an invalid name.’ I did extract two template functions <strong>createCommentElements</strong> and <strong>createFormElement</strong> to prevent that the <strong>action</strong> and<strong> loader</strong> functions would contain the same strings.</p><p>Here is how it looks when I enter a name with an illegal character like %:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*uSSCCzSTl1JM_GG8PajAgw.gif" /></figure><p>You can find <a href="https://github.com/Leejjon/rr-forms/tree/classic-form-render-errors-ssr-validation">the code so far on GitHub</a>.</p><p>This validation works. But there are still a bunch of downsides:</p><ul><li>The user only gets feedback when he clicks the submit button.</li><li>All fields get cleared when one field contains an illegal character.</li></ul><h3>Use native HTML attributes on &lt;input&gt; fields to validate before submitting the &lt;form&gt;</h3><p>To prevent users from entering super long strings in textfields, in 1995 there already was an attribute called <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/maxlength"><strong>maxlength</strong></a> that you could put on <strong>&lt;input&gt;</strong> elements.</p><p>Later in 2003 a couple more were added such as <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/required"><strong>required</strong></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/pattern"><strong>pattern</strong></a> and in 2017 <strong>minlength</strong> was added. This is great because that gives us the ability to validate on the client side.</p><p>So instead of this function:</p><pre>function createFormElement(nameError?: string) {<br>    const nameErrorLabel = `&lt;label style=&quot;color: red; margin-left: 1em;&quot;&gt;${nameError}&lt;/label&gt;`;<br>    return &quot;&lt;form action=&#39;/&#39; method=&#39;POST&#39;&gt;&quot; +<br>        `${createCommentsElements()}` +<br>        &quot;&lt;br /&gt;&quot; +<br>        &quot;&lt;label&gt;Name:&lt;/label&gt;&lt;br /&gt;&quot; +<br>        `&lt;input name=&#39;name&#39; autocomplete=&#39;off&#39; /&gt;${nameError !== undefined ? nameErrorLabel : &#39;&#39;}&lt;br /&gt;` +<br>        &quot;&lt;label&gt;Message:&lt;/label&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;textarea name=&#39;message&#39;&gt;&lt;/textarea&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;br /&gt;&quot; +<br>        &quot;&lt;button type=&#39;submit&#39;&gt;Submit&lt;/button&gt;&quot; +<br>        &quot;&lt;/form&gt;&quot;;<br>}</pre><p>Add <strong>required</strong>, <strong>pattern</strong>, <strong>minLength</strong> and <strong>maxLength</strong> to the name <strong>&lt;input&gt;</strong> element:</p><pre>function createFormElement() {<br>    const nameErrorLabel = `&lt;label style=&quot;color: red; margin-left: 1em;}&quot;&gt;You entered an invalid name.&lt;/label&gt;`;<br>    return &quot;&lt;form action=&#39;/&#39; method=&#39;POST&#39;&gt;&quot; +<br>        `${createCommentsElements()}` +<br>        &quot;&lt;br /&gt;&quot; +<br>        &quot;&lt;label&gt;Name:&lt;/label&gt;&lt;br /&gt;&quot; +<br>        `&lt;input name=&#39;name&#39; required minlength=&#39;1&#39; maxlength=&#39;20&#39; pattern=&#39;^[A-Za-z0-9]+(?:[ _\\-][A-Za-z0-9]+)*$&#39; /&gt;${nameErrorLabel}&lt;br /&gt;` +<br>        &quot;&lt;label&gt;Message:&lt;/label&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;textarea name=&#39;message&#39;&gt;&lt;/textarea&gt;&lt;br /&gt;&quot; +<br>        &quot;&lt;br /&gt;&quot; +<br>        &quot;&lt;button type=&#39;submit&#39;&gt;Submit&lt;/button&gt;&quot; +<br>        &quot;&lt;/form&gt;&quot;;<br>}</pre><p>The behavior is now:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*eT8y6-BDnmJb44bTDmtOqQ.gif" /><figcaption>Note that no network request is happening, this validation is all on the client side.</figcaption></figure><p>While it saved us a request to the server to get to this point, it still only happens when we click submit. So can we already show an error right away when a user types an invalid character? Without JavaScript?</p><p>We can change the display value of the error label with some dynamic css, that we have to set in a<strong> &lt;style&gt;</strong> element in the <strong>&lt;head&gt;</strong> of the document:</p><pre><br>function createFormElement() {<br>    const nameErrorLabel = `&lt;label class=&quot;error&quot;&gt;You entered an invalid name.&lt;/label&gt;`;<br>    // Other logic<br>}<br><br>const headAndStyleElement = &quot;&lt;head&gt;&quot; +<br>    &quot;&lt;style&gt;&quot; +<br>    &quot;input:not(:placeholder-shown):invalid {&quot; +<br>        &quot;border: 2px solid red;&quot; +<br>        &quot;background-color: #ffecec;&quot; +<br>    &quot;}&quot;+<br>    &quot;input:not(:placeholder-shown):invalid + .error {&quot; +<br>        &quot;display: inline;&quot; +<br>        &quot;color: red;&quot; +<br>    &quot;}&quot; +<br>    &quot;.error { display: none; color: red; margin-left: 1em; }&quot; +<br>    &quot;&lt;/style&gt;&quot; +<br>    &quot;&lt;/head&gt;&quot;;<br><br>export async function loader(args: Route.LoaderArgs) {<br>    return new Response(`&lt;html&gt;${headAndStyleElement}${createFormElement()}&lt;/html&gt;`, {<br>        headers: {&quot;Content-Type&quot;: &quot;text/html&quot;},<br>        status: 200<br>    });<br>}</pre><p>Now the form will behave like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*eQmu1EK0tbkY74S_KfDFzw.gif" /></figure><p>This works pretty well, but now it shows as invalid when the name input field is empty. Which is technically correct because the user is not allowed to submit the form with an empty name, but it is ugly.</p><p>Luckily they added a <strong>placeholder</strong> attribute to HTML5 in 2011 that we can add:</p><pre>&lt;input name=&#39;name&#39; required minlength=&#39;1&#39; maxlength=&#39;20&#39; <br>pattern=&#39;^[A-Za-z0-9]+(?:[ _\-][A-Za-z0-9]+)*$&#39; placeholder=&#39;&#39; /&gt;</pre><p>That will remove the error when the field is empty. Now it all looks good!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/1*TVJfQmFNb9SjdsLM_oauEA.png" /></figure><p>We will keep our backend validation for security reasons (people can always bypass the client side validation), but we no longer have to do effort to render errors from the server nicely in the page. Instead we can just throw a HTTP 400 in our action:</p><pre>export async function action({request}: Route.ActionArgs) {<br>    const formData = await request.formData();<br>    const name = formData.get(&quot;name&quot;);<br>    const message = formData.get(&quot;message&quot;);<br><br>    if (nameIsValid(name?.toString())) {<br>        addComment({name, message} as NewComment);<br>        return redirect(&quot;/&quot;);<br>    } else {<br>        return new Response(`Invalid request`, {<br>            status: 400<br>        });<br>    }<br>}</pre><p>I was actually surprised that it was possible to make such a form work fairly well without any JavaScript. And because we don’t use JavaScript, screen readers are actually able to help visually impaired people quite well to enter such forms.</p><p>You can find <a href="https://github.com/Leejjon/rr-forms/tree/classic-form-native-html-validation">the code so far on GitHub.</a></p><p>Its annoying that we have the same validation rules implemented on two different places. Once with native HTML attributes, and written in TypeScript on the server. So if the validation rules of an input field change, you have to update two places.</p><h3>Chapter two —Using React and JavaScript without &lt;form&gt;</h3><p>The primitive <em>Zero-JavaScript</em> approach of the first chapter was obviously not good enough for people to work with, so Facebook invented React. There are many reasons why <strong>React.js</strong> took off, but for me the most important reason was how easy it was to split up UI in components by using <strong>JSX</strong>. I dislike Facebook (now Meta) as a company, but this really is the thing they did right.</p><p>The beauty of JSX for me is that you can write anything you want using TypeScript and render very dynamic HTML for the UI parts. That is great for programmers that need to do UI (which is me).</p><h3>React router with SSR</h3><p>For the next implementation, we will use <strong>React</strong> and write the validations in <strong>TypeScript</strong>. We will not jump to solutions such as <strong>zod</strong> and <strong>react-hook-form</strong> immediately to show what challenges this introduces. If you are coding along with this blog, simply switch back to the master branch and create a new branch using:</p><pre>git checkout main<br>git checkout -b react-programmatically<br>rm -rf app/welcome</pre><p>In the <strong>home.tsx</strong> file, add the following code that defines the same form we built earlier, but this time in <strong>React </strong>using a native fetch call to submit the comment:</p><pre>import type {Route} from &quot;./+types/home&quot;;<br>import type {Comment, NewCommentRequest} from &quot;~/common/comments&quot;;<br>import {type ChangeEvent, useRef, useState} from &quot;react&quot;;<br>import {nameIsValid} from &quot;~/common/validation&quot;;<br>import {addComment, getComments} from &quot;~/database/comments.server&quot;;<br>import {useLoaderData} from &quot;react-router&quot;;<br><br>export async function action({request}: Route.ActionArgs) {<br>    const newComment = await request.json() as NewCommentRequest;<br><br>    if (nameIsValid(newComment.name)) {<br>        addComment(newComment);<br>        return new Response(&quot;Success&quot;, {status: 200});<br>    } else {<br>        return new Response(&quot;Bad request&quot;, {status: 400});<br>    }<br>}<br><br>export async function loader() {<br>    return getComments();<br>}<br><br>export default function Home() {<br>    const comments = useLoaderData&lt;typeof loader&gt;();<br><br>    const [nameInvalid, setNameInvalid] = useState(false);<br>    const nameInputRef = useRef&lt;HTMLInputElement | null&gt;(null);<br>    const messageInputRef = useRef&lt;HTMLTextAreaElement | null&gt;(null);<br><br>    function validateUpdatedName(event: ChangeEvent&lt;HTMLInputElement&gt;) {<br>        if (nameIsValid(event.target.value)) {<br>            setNameInvalid(false);<br>        } else {<br>            setNameInvalid(true);<br>        }<br>    }<br><br>    function submitComment() {<br>        if (!nameIsValid(nameInputRef.current?.value)) {<br>            nameInputRef.current?.focus();<br>            return;<br>        }<br><br>        const commentRequestBody = {<br>            name: nameInputRef.current?.value, message: messageInputRef.current?.value<br>        };<br><br>        fetch(&#39;/?index&#39;, {<br>            method: &quot;POST&quot;,<br>            body: JSON.stringify(commentRequestBody)<br>        }).then(response =&gt; {<br>            if (response.status === 200) {<br>                window.location.reload();<br>            } else {<br>                setNameInvalid(true);<br>            }<br>        });<br>    }<br><br>    return (<br>        &lt;div className=&quot;flex flex-col items-start *:m-1&quot;&gt;<br>            {comments.map((comment) =&gt; {<br>                return (<br>                    &lt;div key={comment.id} className=&quot;flex flex-col&quot;&gt;<br>                        &lt;div&gt;&lt;b&gt;{comment.name}: &lt;/b&gt;&lt;&gt;{comment.message} &lt;/&gt;<br>                        &lt;/div&gt;<br>                        &lt;i&gt;Posted at {new Date(comment.timestamp).toLocaleString()}&lt;/i&gt;<br>                    &lt;/div&gt;<br>                );<br>            })<br>            }<br>            &lt;label&gt;Name:&lt;/label&gt;<br>            &lt;div className=&quot;flex items-center gap-2&quot;&gt;<br>                &lt;input name=&#39;name&#39; ref={nameInputRef} onChange={validateUpdatedName}<br>                       className=&quot;border border-gray-400 rounded px-2 py-1&quot;/&gt;<br>                {nameInvalid &amp;&amp; &lt;label className=&quot;error&quot;&gt;You entered an invalid name.&lt;/label&gt;}<br>            &lt;/div&gt;<br>            &lt;label&gt;Message:&lt;/label&gt;<br>            &lt;textarea name=&#39;message&#39; ref={messageInputRef} className=&quot;border border-gray-400 rounded px-2 py-1&quot;/&gt;<br>            &lt;button<br>                className=&quot;border border-gray-400 rounded bg-gray-100 px-3 py-1 hover:bg-gray-200 active:bg-gray-300&quot;<br>                onClick={submitComment}&gt;Submit<br>            &lt;/button&gt;<br>        &lt;/div&gt;<br>    );<br>}</pre><p>A few things about this new code:</p><ul><li>Tailwind CSS is the default styling option in React Router. I think it is great because it allows us to keep the styling close to the component. I personally dislike it when the styling is hidden in different CSS files.</li><li>The error labels are conditionally rendered based on a <a href="https://react.dev/reference/react/useState"><strong>useState</strong></a> hook instead of the dynamic CSS code that we put in the <strong>&lt;style&gt;</strong> tag in chapter one.</li><li>Our submit button triggers the <strong>submitComment</strong> function. It reads the values of the input fields via two <a href="https://react.dev/reference/react/useRef#manipulating-the-dom-with-a-ref"><strong>useRef</strong></a> hooks <strong>nameInputRef</strong> and <strong>messageInputRef </strong>and sends it to the server. These are tied to the <strong>&lt;input&gt;</strong> and <strong>&lt;textarea&gt;</strong> via <strong>ref</strong> attributes. Before sending it to the server it runs our <strong>nameIsValid</strong> function to validate the name, and exits the function if the name is invalid (and sets focus to the nameInput via the <strong>nameInputRef </strong>if that is the case).</li><li>The name input field has an onChange listener that calls the <strong>validateUpdateName</strong> function that will immediately show an error if you type illegal characters in the name. And clears it again when you correct it.</li><li>We use <a href="https://reactrouter.com/api/hooks/useLoaderData#useloaderdata">a loader function along with the useLoaderData hook</a> to pass all comments in our in-memory database and display them.</li><li>We use an action function to receive the <strong>NewComment</strong> object and store it in our in-memory database.</li></ul><p>You can run it with npm run dev and it should behave like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*OqHLFlcU58MGv6la2y35uw.gif" /></figure><p>One feature React offers us out of the box, is that it protects you against cross site scripting:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*C35OisJxJmPDC1gbalauCg.gif" /></figure><p>You can find <a href="https://github.com/Leejjon/rr-forms/tree/react-programmatically">the code so far on GitHub</a>.</p><p>So this all works, in <strong>TypeScript</strong>. In this React setup we reuse our <strong>nameIsValid</strong> function on the client and server. We could write vitest tests for this function and we could write tests with <a href="https://testing-library.com/docs/react-testing-library/intro/">React Testing Library</a> to verify that validations are displayed properly.</p><p>We now only validate one field. We should write a validation function for the message too, but I leave it out of scope in this blog post. Big forms will force you to write lots of validation functions. Sometimes also where input of one field affects the validation of other fields. To keep complex validation logic clean, Schema validation libraries exist. There are a lot of those out there, but I am using Zod.</p><h3>Using Zod for validation</h3><p>We will replace our<strong> nameIsValid</strong> function with a Zod schema.</p><p>To start with Zod, install it:</p><pre>npm install zod</pre><p>In the <strong>validation.ts</strong> file, throw out the <strong>nameIsValid</strong> function and replace it with:</p><pre>import { z } from &quot;zod&quot;;<br><br>const nameMatcher = /^[A-Za-z0-9]+(?:[ _\-][A-Za-z0-9]+)*$/<br><br>export const CommentSchema = z.object({<br>    name: z.string().regex(nameMatcher).min(1).max(20),<br>    message: z.string()<br>});</pre><p>In the <strong>common/comments.ts</strong> file you can make the definition of the <strong>NewComment</strong> infer it’s type from the <strong>CommentSchema</strong>:</p><pre>import {type UUID} from &quot;node:crypto&quot;;<br>import type {CommentSchema} from &quot;~/common/validation&quot;;<br>import { z } from &quot;zod&quot;;<br><br>export type NewComment = z.infer&lt;typeof CommentSchema&gt;;<br><br>export type Comment = {<br>    id: UUID;<br>    timestamp: number;<br>    name: string;<br>    message: string;<br>}</pre><p>Now in the <strong>action</strong> function on the server side, it’s very easy to start using this schema, because we always want to validate all fields in the schema in one go:</p><pre>import type {Route} from &quot;./+types/home&quot;;<br>import type {NewComment} from &quot;~/common/comments&quot;;<br>import {type ChangeEvent, useRef, useState} from &quot;react&quot;;<br>import {CommentSchema} from &quot;~/common/validation&quot;;<br>import {addComment, getComments} from &quot;~/database/comments.server&quot;;<br>import {useLoaderData} from &quot;react-router&quot;;<br><br>export async function action({request}: Route.ActionArgs) {<br>    const newComment = await request.json() as NewComment;<br><br>    const validationResult = CommentSchema.safeParse(newComment);<br>    if (validationResult.success) {<br>        addComment(newComment);<br>        return new Response(&quot;Success&quot;, {status: 200});<br>    } else {<br>        return new Response(&quot;Bad request&quot;, {status: 400});<br>    }<br>}</pre><p>In the <strong>submitComment()</strong> function it’s the same, when you click on the submit button you want to validate all fields:</p><pre>    function submitComment() {<br>        const validationResult = CommentSchema.safeParse(newComment);<br>        if (!nameIsValid(nameInputRef.current?.value)) {<br>            nameInputRef.current?.focus();<br>            return;<br>        }<br><br>        const commentRequestBody = {<br>            name: nameInputRef.current?.value, message: messageInputRef.current?.value<br>        };<br><br>        fetch(&#39;/?index&#39;, {<br>            method: &quot;POST&quot;,<br>            body: JSON.stringify(commentRequestBody)<br>        }).then(response =&gt; {<br>            if (response.status === 200) {<br>                window.location.reload();<br>            } else {<br>                setNameInvalid(true);<br>            }<br>        });<br>    }</pre><p>We can do the same in the <strong>validateUpdatedName</strong> change listener function:</p><pre>    function validateUpdatedName(event: ChangeEvent&lt;HTMLInputElement&gt;) {<br>        const validationResult = CommentSchema.safeParse({<br>            name: event.target.value,<br>        });<br>        if (!validationResult.success &amp;&amp; validationResult.error.issues.find(<br>            (issue) =&gt; issue.path[0] === &quot;name&quot;<br>        )) {<br>            setNameInvalid(false);<br>        } else {<br>            setNameInvalid(true);<br>        }<br>    }</pre><p>Things will work exactly the same as before when you run npm run dev</p><p>There are some downsides to this approach:</p><ol><li>Managing validation state and refs for every field is a lot of code to manage.</li><li>If you have a “form” with many input fields and want to move the input fields in different components, you have to pass refs through a context or props so that you can pass all values.</li><li>The website won’t work when JavaScript is disabled in the browser.</li><li>Accessibility is less good than with traditional <strong>&lt;form&gt;</strong> tags.</li></ol><p>You can find <a href="https://github.com/Leejjon/rr-forms/tree/react-programatically-with-zod">the code so far on GitHub</a>.</p><h3>Chapter 3 — Don’t map validation errors to your input fields manually! Use react-hook-form</h3><p>For a hobby project that is now eight years old, I was in the same situation as downside number 2 in the list I just mentioned. It took me a lot of effort to write functions like <strong>validateUpdatedName</strong> that would search through the list of ZodIssues and match it with an input field and then show an error there.</p><p>I asked a colleague (hi Ruben!) that did React for most of his career how he dealt with that. He told me he doesn’t write these validation functions at all. He simply feeds a <strong>Zod</strong> schema to the <strong>useForm</strong> hook from the <strong>react-hook-form</strong> library and everything is handled for him. This wasn’t the first time I heard about react-hook-form, frontenders at my job actually used it. I kind of didn’t touch the logic because I didn’t really understood what it did. It was time to learn what it does.</p><p>To use this, first install <strong>react-hook-form</strong> and <strong>@hookform/resolvers</strong> using:</p><pre>npm i react-hook-form @hookform/resolvers --legacy-peer-deps</pre><p>You might not need the --legacy-peer-depsflag, but I needed it because my React Router version depended on a different <strong>valibot</strong> dependency than the <strong>@hookform/resolvers</strong> did.</p><p>In the <strong>home.tsx</strong> file, you can add the following code:</p><pre>import type {Route} from &quot;./+types/home&quot;;<br>import type {NewComment} from &quot;~/common/comments&quot;;<br>import {CommentSchema} from &quot;~/common/validation&quot;;<br>import {addComment, getComments} from &quot;~/database/comments.server&quot;;<br>import {useLoaderData} from &quot;react-router&quot;;<br>import {useForm} from &quot;react-hook-form&quot;;<br>import {zodResolver} from &quot;@hookform/resolvers/zod&quot;;<br><br>export async function action({request}: Route.ActionArgs) {<br>    const newComment = await request.json() as NewComment;<br><br>    const validationResult = CommentSchema.safeParse(newComment);<br>    if (validationResult.success) {<br>        addComment(newComment);<br>        return new Response(&quot;Success&quot;, {status: 200});<br>    } else {<br>        return new Response(&quot;Bad request&quot;, {status: 400});<br>    }<br>}<br><br>export async function loader() {<br>    return getComments();<br>}<br><br>export default function Home() {<br>    const comments = useLoaderData&lt;typeof loader&gt;();<br><br>    const {<br>        register,<br>        handleSubmit,<br>        formState: {errors}<br>    } = useForm&lt;NewComment&gt;({<br>        resolver: zodResolver(CommentSchema),<br>        mode: &quot;onChange&quot;<br>    });<br><br>    function submitComment(newComment: NewComment) {<br>        fetch(&#39;/?index&#39;, {<br>            method: &quot;POST&quot;,<br>            body: JSON.stringify(newComment)<br>        }).then(response =&gt; {<br>            if (response.status === 200) {<br>                window.location.reload();<br>            }<br>        });<br>    }<br><br>    return (<br>        &lt;div className=&quot;flex flex-col items-start *:m-1&quot;&gt;<br>            {comments.map((comment) =&gt; {<br>                return (<br>                    &lt;div key={comment.id} className=&quot;flex flex-col&quot;&gt;<br>                        &lt;div&gt;&lt;b&gt;{comment.name}: &lt;/b&gt;&lt;&gt;{comment.message} &lt;/&gt;<br>                        &lt;/div&gt;<br>                        &lt;i&gt;Posted at {new Date(comment.timestamp).toLocaleString()}&lt;/i&gt;<br>                    &lt;/div&gt;<br>                );<br>            })<br>            }<br>            &lt;label&gt;Name:&lt;/label&gt;<br>            &lt;div className=&quot;flex items-center gap-2&quot;&gt;<br>                &lt;input {...register(&quot;name&quot;)} name=&#39;name&#39; className=&quot;border border-gray-400 rounded px-2 py-1&quot;/&gt;<br>                {errors.name &amp;&amp; &lt;label className=&quot;error&quot;&gt;You entered an invalid name.&lt;/label&gt;}<br>            &lt;/div&gt;<br>            &lt;label&gt;Message:&lt;/label&gt;<br>            &lt;textarea {...register(&quot;message&quot;)} name=&#39;message&#39; className=&quot;border border-gray-400 rounded px-2 py-1&quot;/&gt;<br>            &lt;button<br>                type=&quot;submit&quot;<br>                className=&quot;border border-gray-400 rounded bg-gray-100 px-3 py-1 hover:bg-gray-200 active:bg-gray-300&quot;<br>                onClick={handleSubmit(submitComment)}&gt;Submit<br>            &lt;/button&gt;<br>        &lt;/div&gt;<br>    );<br>}</pre><p>Again, this should NOT affect any functionality. But here is a list of things that changed in the code:</p><ul><li>We removed the <strong>useState</strong> and <strong>useRef</strong> hooks that were needed to manage the name and message fields.</li><li>We use the register function from react-hook-form on the <strong>name</strong> and <strong>message</strong> fields.</li><li>Removed the validation of the <strong>submitComment</strong> function. It now just receives a validated instance of the NewComment type that it can directly send to the server.</li><li>Modified the <strong>onClick</strong> attribute of the button to: onClick={handleSubmit(submitComment}</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QXuSnX_jmFcInMmvacTt7w.gif" /></figure><p>You can find <a href="https://github.com/Leejjon/rr-forms/tree/react-programatically-with-zod-react-hook-form">the code so far on GitHub</a>.</p><h3>Making your React app work without JavaScript</h3><p>For a long time thinking about supporting browsers without JavaScript did not even cross my mind as most React.js apps would be a SPA (Single page app). When Next.js and other frameworks started to offer server side rendered React, most people used it for SEO or to prevent Request Waterfalls.</p><p>It wasn’t until I learned that Remix v2 (which got merged into React Router) really went all in on offering the possibility of writing React that works without JavaScript.</p><p>Even though React Router framework mode can support React rendered UI that works without JavaScript being enabled, I think it might not surprise you that our current implementation breaks when JavaScript is disabled.</p><p>First off it does render all the UI elements and sends it as HTML to the client, so that part does work. But then it doesn’t validate the name and clicking the submit button doesn’t do anything:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1jtDR2i9tP-sW-g-BdWlew.gif" /><figcaption>I am using the Firefox dev tools to disable JavaScript for this website.</figcaption></figure><p>We should at least fix the submit button so that users that disabled JavaScript can at least post comments on our website.</p><h3>Use React Routers built in &lt;Form&gt; component</h3><p>We are not going to repeat the earlier SSR solution and just use a <strong>&lt;form&gt;</strong> to let all of this work without JavaScript. React Router <a href="https://reactrouter.com/api/components/Form">has its own <strong>&lt;Form&gt;</strong> component</a> that works with and without JavaScript. This &lt;Form&gt; is great because it works like a regular &lt;form&gt; before JavaScript is loaded, and once JavaScript is loaded, it will just use a fetch under the hood. This is to avoid navigation if it is not desired to navigate to another url. If you disable JavaScript, the form submit will always navigate to the url defined in the action attribute of the <strong>&lt;Form&gt;</strong> tag. With fast internet you might not notice it if the <strong>action</strong> function immediately redirects back to the form page.</p><p>In the <strong>home.tsx</strong> file put:</p><pre>import type {Route} from &quot;./+types/home&quot;;<br>import type {NewComment} from &quot;~/common/comments&quot;;<br>import {CommentSchema} from &quot;~/common/validation&quot;;<br>import {addComment, getComments} from &quot;~/database/comments.server&quot;;<br>import {Form, useLoaderData} from &quot;react-router&quot;;<br>import {useForm} from &quot;react-hook-form&quot;;<br>import {zodResolver} from &quot;@hookform/resolvers/zod&quot;;<br><br>export async function action({request}: Route.ActionArgs) {<br>    const formData = await request.formData();<br>    const name = formData.get(&quot;name&quot;);<br>    const message = formData.get(&quot;message&quot;);<br><br>    const newComment = {name, message} as NewComment;<br><br>    const validationResult = CommentSchema.safeParse(<br>        newComment<br>    );<br>    if (validationResult.success) {<br>        addComment(newComment);<br>        return new Response(&quot;Success&quot;, {status: 200});<br>    } else {<br>        return new Response(&quot;Bad request&quot;, {status: 400});<br>    }<br>}<br><br>export async function loader() {<br>    return getComments();<br>}<br><br>export default function Home() {<br>    const comments = useLoaderData&lt;typeof loader&gt;();<br><br>    const {<br>        register,<br>        formState: { errors}<br>    } = useForm({<br>        resolver: zodResolver(CommentSchema),<br>        mode: &quot;onChange&quot;<br>    });<br><br>    return (<br>        &lt;Form action=&quot;/?index&quot; method=&quot;POST&quot; className=&quot;flex flex-col items-start *:m-1&quot;&gt;<br>            {comments.map((comment) =&gt; {<br>                return (<br>                    &lt;div key={comment.id} className=&quot;flex flex-col&quot;&gt;<br>                        &lt;div&gt;&lt;b&gt;{comment.name}: &lt;/b&gt;&lt;&gt;{comment.message} &lt;/&gt;<br>                        &lt;/div&gt;<br>                        &lt;i&gt;Posted at {new Date(comment.timestamp).toLocaleString()}&lt;/i&gt;<br>                    &lt;/div&gt;<br>                );<br>            })<br>            }<br>            &lt;label&gt;Name:&lt;/label&gt;<br>            &lt;div className=&quot;flex items-center gap-2&quot;&gt;<br>                &lt;input {...register(&quot;name&quot;)} name=&#39;name&#39;<br>                       className=&quot;border border-gray-400 rounded px-2 py-1&quot;/&gt;<br>                {errors.name &amp;&amp; &lt;label className=&quot;error&quot;&gt;You entered an invalid name.&lt;/label&gt;}<br>            &lt;/div&gt;<br>            &lt;label&gt;Message:&lt;/label&gt;<br>            &lt;textarea {...register(&quot;message&quot;)} name=&#39;message&#39; className=&quot;border border-gray-400 rounded px-2 py-1&quot;/&gt;<br>            &lt;button<br>                type=&quot;submit&quot;<br>                className=&quot;border border-gray-400 rounded bg-gray-100 px-3 py-1 hover:bg-gray-200 active:bg-gray-300&quot;&gt;<br>                Submit<br>            &lt;/button&gt;<br>        &lt;/Form&gt;<br>    );<br>}</pre><p>To summarize the changes made here:</p><ul><li>Removed the JavaScript based <strong>onClick</strong> handler on the Submit button, that was executing a fetch call to store the <strong>NewComment</strong>.</li><li>Add type=&quot;submit&quot; to the Submit button.</li><li>Changed the root div to a <strong>&lt;Form </strong>action=”/?index” method=”POST&quot;<strong>&gt;</strong>. I needed to put /?index in the action attribute. Don’t worry too much about this, <a href="https://reactrouter.com/explanation/index-query-param">it’s a React Router thing</a>.</li><li>In the <strong>action</strong> function on the server, read <a href="https://developer.mozilla.org/en-US/docs/Web/API/FormData">FormData</a> from the request instead of just a JSON object in the request body. Because the <strong>&lt;Form&gt;</strong> in React Router will automatically grab all <strong>&lt;input&gt;</strong> elements inside it and submits them in a <strong>FormData</strong> object.</li></ul><p>Here is a demonstration of how server side React code works with JavaScript disabled:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*uq7rg-IR9v6vQTlt6uDDvw.gif" /><figcaption>Validation still happens when you click on the submit button, but the experience is bad because no error is displayed and the fields are cleared.</figcaption></figure><p>You can find <a href="https://github.com/Leejjon/rr-forms/tree/react-router-form-and-react-hook-form">the code so far on GitHub</a>.</p><p>But wait, with JavaScript disabled the form shows zero validation error. The user might just think the button is broken.</p><p>There are two solutions to display errors (without JavaScript):</p><ol><li>The validation we can solve by adding native HTML attributes such as <strong>required</strong>, <strong>pattern</strong>, <strong>minLength</strong> and <strong>maxLength.</strong> It would have been amazing if <strong>react-hook-form</strong> could somehow read this information from the <strong>Zod</strong> schema. But it does not. We could add them manually, which sucks because then once again we have validation on two places.</li><li>Rendering the form again on the server with the error labels included.</li></ol><p>If would have to use <strong>react-hook-form</strong>, I would probably just put the <strong>required</strong>, <strong>pattern</strong>, <strong>minLength</strong> and <strong>maxLength</strong> attributes in manually and accept that the validation logic is in two places.</p><p>But hear me out.</p><h3>Rendering the form again on the server with error messages included — React Router Edition</h3><p>You already know that preventing invalid input with native HTML attributes can mostly prevent the need for rendering errors on the server (if you read the first part of this blog post). But I still think it’s cool to show how to render error messages on the server again in React Router using the <strong>useActionData</strong> hook. Even if you use native HTML attributes, you can still use this for validations that have to be executed server side, like claiming a username (you have to check for duplicates) or stock when you are trying to order a product (the product might be out of stock when you click order).</p><p>In your <strong>home.tsx</strong>, use this code:</p><pre>import type {Route} from &quot;./+types/home&quot;;<br>import type {NewComment} from &quot;~/common/comments&quot;;<br>import {CommentSchema} from &quot;~/common/validation&quot;;<br>import {addComment, getComments} from &quot;~/database/comments.server&quot;;<br>import {Form, useActionData, useLoaderData} from &quot;react-router&quot;;<br>import {useForm} from &quot;react-hook-form&quot;;<br>import {zodResolver} from &quot;@hookform/resolvers/zod&quot;;<br>import {z} from &quot;zod&quot;;<br><br>type FormSubmitResponse = {<br>    errors: Array&lt;z.core.$ZodIssue&gt;;<br>}<br><br>export async function action({request}: Route.ActionArgs): Promise&lt;FormSubmitResponse&gt; {<br>    const formData = await request.formData();<br>    const name = formData.get(&quot;name&quot;);<br>    const message = formData.get(&quot;message&quot;);<br><br>    const newComment = {name, message} as NewComment;<br><br>    const validationResult = CommentSchema.safeParse(<br>        newComment<br>    );<br>    if (validationResult.success) {<br>        addComment(newComment);<br>        return {errors: []}<br>    } else {<br>        return {errors: validationResult.error.issues}<br>    }<br>}<br><br>export async function loader() {<br>    return getComments();<br>}<br><br>export default function Home() {<br>    const comments = useLoaderData&lt;typeof loader&gt;();<br>    const actionResponse = useActionData&lt;typeof action&gt;();<br><br>    const {<br>        register,<br>        formState: {errors}<br>    } = useForm({<br>        resolver: zodResolver(CommentSchema),<br>        mode: &quot;onChange&quot;<br>    });<br><br>    function actionSaysNameIsInvalid() {<br>        const nameIssue = actionResponse?.errors.find((error) =&gt; error.path[0] === &quot;name&quot;);<br>        return nameIssue !== undefined;<br>    }<br><br>    return (<br>        &lt;Form action=&quot;/?index&quot; method=&quot;POST&quot; className=&quot;flex flex-col items-start *:m-1&quot;&gt;<br>            {comments.map((comment) =&gt; {<br>                return (<br>                    &lt;div key={comment.id} className=&quot;flex flex-col&quot;&gt;<br>                        &lt;div&gt;&lt;b&gt;{comment.name}: &lt;/b&gt;&lt;&gt;{comment.message} &lt;/&gt;&lt;/div&gt;<br>                        &lt;i&gt;Posted at {new Date(comment.timestamp).toLocaleString()}&lt;/i&gt;<br>                    &lt;/div&gt;<br>                );<br>            })<br>            }<br>            &lt;label&gt;Name:&lt;/label&gt;<br>            &lt;div className=&quot;flex items-center gap-2&quot;&gt;<br>                &lt;input {...register(&quot;name&quot;)} name=&#39;name&#39; className=&quot;border border-gray-400 rounded px-2 py-1&quot; /&gt;<br>                {(actionSaysNameIsInvalid() || errors.name) &amp;&amp; &lt;label className=&quot;error&quot;&gt;You entered an invalid name.&lt;/label&gt;}<br>            &lt;/div&gt;<br>            &lt;label&gt;Message:&lt;/label&gt;<br>            &lt;textarea {...register(&quot;message&quot;)} name=&#39;message&#39; className=&quot;border border-gray-400 rounded px-2 py-1&quot;/&gt;<br>            &lt;button<br>                type=&quot;submit&quot;<br>                disabled={!!errors.name}<br>                className=&quot;border border-gray-400 rounded bg-gray-100 px-3 py-1 hover:bg-gray-200 active:bg-gray-300&quot;&gt;<br>                Submit<br>            &lt;/button&gt;<br>        &lt;/Form&gt;<br>    );<br>}</pre><p>In the above code I:</p><ul><li>I updated the action function to return an array of <strong>ZodIssue</strong> objects.</li><li>I added the <strong>useActionData</strong> hook, and wrote a actionSaysNameIsInvalid() function that checks whether the errors contain a <strong>ZodIssue</strong> that is about the name field.</li><li>Based on this function, we render a label with You entered an invalid name. next to the name input field.</li></ul><p>This will once again show server side errors when JavaScript is disabled:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*AS2SfguI-vtkeYJJsRNvBg.gif" /><figcaption>An error is shown, but still annoying that everything is cleared.</figcaption></figure><p>You can find <a href="https://github.com/Leejjon/rr-forms/tree/react-router-form-react-hook-form-with-progressive-enhancement">the code so far on GitHub</a>.</p><p>All fields in our form are cleared when we click the submit button. We solved this in our original SSR solution at the start of this blog, so we can do it here too (but it’s a little bit more complicated).</p><h3>Rerendering your form data when the form refreshes (when JavaScript is disabled)</h3><p>In the <strong>home.tsx</strong> file, update the action and its response type to contain the the <strong>NewComment</strong> object we constructed from the FormData:</p><pre>type FormSubmitResponse = {<br>    data: NewComment;<br>    errors: Array&lt;z.core.$ZodIssue&gt;;<br>}<br><br>export async function action({request}: Route.ActionArgs): Promise&lt;FormSubmitResponse&gt; {<br>    const formData = await request.formData();<br>    const name = formData.get(&quot;name&quot;);<br>    const message = formData.get(&quot;message&quot;);<br><br>    const newComment = {name, message} as NewComment;<br><br>    const validationResult = CommentSchema.safeParse(<br>        newComment<br>    );<br>    if (validationResult.success) {<br>        addComment(newComment);<br>        return {data: {name, message: &quot;&quot;} as NewComment, errors: []}<br>    } else {<br>        return {data: newComment, errors: validationResult.error.issues}<br>    }<br>}</pre><p>In case of a successful comment, we keep the name field filled just in case the user wants to post another comment, but we empty the message field (because spamming the same message is usually not what the user wants, unless he wants attention from a streamer in a Twitch chat). If there are errors, we do send the complete <strong>NewComment</strong> object.</p><p>We can use the actionResponse of the useActionData hook to get the contents of the <strong>NewComment</strong> object. And then put it as defaultValue in our name <strong>&lt;input&gt;</strong> field:</p><pre>&lt;input {...register(&quot;name&quot;)} name=&#39;name&#39; <br>   defaultValue={actionResponse?.data.name ?? &quot;&quot;}<br>   className=&quot;border border-gray-400 rounded px-2 py-1&quot; <br>/&gt;</pre><p>And message <strong>&lt;textarea&gt;:</strong></p><pre>&lt;textarea {...register(&quot;message&quot;)} name=&#39;message&#39; <br>   defaultValue={actionResponse?.data.message ?? &quot;&quot;}<br>   className=&quot;border border-gray-400 rounded px-2 py-1&quot;<br>/&gt;</pre><p>This fixes the issue of empty fields when JavaScript is enabled:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*HldrcT-RGsholGpVnQxITQ.gif" /><figcaption>Pretty cool that React protects us against the cross site scripting attempt (the &lt;script&gt; tags) out of the box.</figcaption></figure><p>You can find <a href="https://github.com/Leejjon/rr-forms/tree/react-router-form-react-hook-form-with-progressive-enhancement-rerender-input">the code so far on GitHub</a>.</p><h3>Disabling the submit button while submitting (using the useNavigation hook)</h3><p>A classic bug in web forms is to not disable the submit button after the user has clicked. In normal browsers with fast internet you don’t notice, but on a slow internet connection it becomes possible for a user to double click on a submit button, and submit the form multiple times accidentally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/902/1*mxGhRckvo-0GjOwPo8SIdQ.gif" /><figcaption>Throttling the browser on GPRS speed to demonstrate the risk of multiple submits.</figcaption></figure><p>It’s not strange for users to click multiple times if they don’t see anything happen. They might wonder if their click registered.</p><p>We can fix this behavior with a little bit of JavaScript. At the top of your function component, place:</p><pre>    const navigation = useNavigation();<br>    const isSubmitting = navigation.state !== &quot;idle&quot; &amp;&amp; navigation.formMethod === &quot;POST&quot; &amp;&amp; navigation.formAction === &quot;/?index&quot;;</pre><p>And in the submit button use this isSubmitting:</p><pre>&lt;button type=&quot;submit&quot; disabled={!!errors.name || isSubmitting}<br>  className=&quot;border border-gray-400 rounded bg-gray-100 px-3 py-1 hover:bg-gray-200 active:bg-gray-300&quot;&gt;<br>  {isSubmitting ? &quot;Submitting...&quot; : &quot;Submit&quot;}<br>&lt;/button&gt;</pre><p>With this in place, users are no longer able able to submit the form twice accidentally. They won’t try anymore either, because the submit button now shows “Submitting…” while the network request loads.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/1*8YUQxFuEtSMd2L_jXWvomg.gif" /><figcaption>I tried to click the button twice as fast as possible, but because it’s disabled it only submits the message once.</figcaption></figure><p>You can find <a href="https://github.com/Leejjon/rr-forms/tree/react-router-form-react-hook-form-with-progressive-enhancement-rerender-input-disabling">the code so far on GitHub</a>.</p><h3>Getting the native attributes to work; by replacing react-hook-form with @conform-to/react</h3><p>As I said twice already: If I would just add the <strong>required</strong>, <strong>pattern</strong>, <strong>minLength</strong> and <strong>maxLength </strong>attributes to our <strong>&lt;input&gt;</strong> and <strong>&lt;textarea&gt;</strong> elements, we would not have to render errors into our JSX on the server. But we would break the technical requirement I defined:</p><ul><li>I want validation logic in one place. Not different validation in the frontend and backend.</li></ul><p>It would be really nice if our @hookform/resolvers library would tell <strong>react-hook-form</strong> to put these HTML attributes on the input elements based on our <strong>Zod</strong> schema. Unfortunately it does not.</p><p>However, there is another library that can. It is called <a href="https://conform.guide/installation">Conform</a>. Let’s uninstall <strong>react-hook-form</strong> and install <strong>Conform</strong>.</p><pre><br>npm uninstall react-hook-form @hookform/resolvers<br>npm install @conform-to/react @conform-to/zod zod</pre><p>Now update the <strong>action</strong> function in <strong>home.tsx</strong> to use the <strong>report</strong> function in <strong>@conform-to/react</strong> to return the submitted form data and the validation errors.</p><pre>import type {Route} from &quot;./+types/home&quot;;<br>import type {NewComment} from &quot;~/common/comments&quot;;<br>import {CommentSchema} from &quot;~/common/validation&quot;;<br>import {addComment, getComments} from &quot;~/database/comments.server&quot;;<br>import {Form, useActionData, useLoaderData, useNavigation} from &quot;react-router&quot;;<br>import {parseSubmission, useForm, report, type SubmissionResult} from &quot;@conform-to/react/future&quot;;<br>import { coerceFormValue, getConstraints } from &#39;@conform-to/zod/v4/future&#39;;<br><br>type FormSubmitResponse = {<br>    data?: NewComment;<br>    validationResult?: SubmissionResult&lt;string[], string | number | boolean | null&gt;<br>}<br><br>export async function action({request}: Route.ActionArgs): Promise&lt;FormSubmitResponse&gt; {<br>    const formData = await request.formData();<br>    const submission = parseSubmission(formData);<br><br>    const validationResult = CommentSchema.safeParse(submission.payload);<br>    if (validationResult.success) {<br>        addComment(validationResult.data);<br>        return { validationResult: report(submission, { targetValue: { ...submission.payload, message: &quot;&quot; }})};<br>    } else {<br>        return { validationResult: report(submission, { error: { issues: validationResult.error.issues} })};<br>    }<br>}</pre><p>Note that I remove the <strong>message</strong> from the payload that we send back in the targetValue. Because if the comment was posted successfully a user likely doesn’t want to post the same comment twice.</p><p>In the function component, we can now use the <strong>useForm</strong> hook:</p><pre><br>const actionData = useActionData&lt;typeof action&gt;();    <br>const {<br>        form, fields<br>    } = useForm(<br>        coerceFormValue(CommentSchema), {<br>            lastResult: actionData?.validationResult, <br>            shouldValidate: &#39;onBlur&#39;, <br>            shouldRevalidate: &#39;onInput&#39;, <br>            constraint: getConstraints(CommentSchema),<br>    });</pre><p>We can connect our existing <strong>&lt;Form&gt;</strong> element to the de-structured <strong>form</strong> value of the <strong>useForm</strong> hook by adding {...form.props} to the <strong>&lt;Form&gt;</strong> element:</p><pre>&lt;Form action=&quot;/?index&quot; method=&quot;POST&quot; {...form.props} noValidate={false} className=&quot;flex flex-col items-start *:m-1&quot;&gt;</pre><p>I also set <strong>noValidate</strong> to false. I noticed that by default, Conform sets <strong>novalidate</strong> on our form. This breaks the native validation we are trying to add so things work without JavaScript enabled. Apparently Kent <a href="https://www.youtube.com/watch?v=Rmuc7QL7TVY">received a question about this</a>. He ran into the same problem when updating one of his courses about forms and he was surprised about <strong>novalidate</strong> being default.</p><p>Now we can simply set the validation attributes based on the de-structured <strong>fields</strong> value of the <strong>useForm</strong> hook:</p><pre>&lt;input id={fields.name.id} name={fields.name.name} defaultValue={fields.name.defaultValue} placeholder=&quot;&quot;<br>       aria-invalid={!fields.name.valid || undefined} aria-describedby={fields.name.ariaDescribedBy}<br>       required={fields.name.required}<br>       minLength={fields.name.minLength}<br>       maxLength={fields.name.maxLength}<br>       pattern={fields.name.pattern}<br>       className=&quot;border border-gray-400 rounded px-2 py-1&quot; /&gt;</pre><p>It is a bit labor intensive to add all these fields, but at least now it grabs all the values from our <strong>Zod</strong> schema. Then we have to add a bit of Tailwind CSS to the <strong>className</strong> attribute of our error label, so that it nicely shows an error (also when JavaScript is disabled):</p><pre>&lt;label className=&quot;hidden [input:not(:placeholder-shown):invalid+&amp;]:inline&quot;&gt;You entered an invalid name.&lt;/label&gt;</pre><p>You also need to update the <strong>&lt;textarea&gt;</strong> and submit <strong>&lt;button&gt;</strong>, as they used references to <strong>react-hook-form</strong> objects that are no longer there:</p><pre>&lt;textarea id={fields.message.id} name={fields.message.name}<br>          defaultValue={actionData?.data?.message ?? &quot;&quot;}<br>          className=&quot;border border-gray-400 rounded px-2 py-1&quot;/&gt;<br>&lt;button<br>    type=&quot;submit&quot;<br>    disabled={isSubmitting}<br>    className=&quot;border border-gray-400 rounded bg-gray-100 px-3 py-1 hover:bg-gray-200 active:bg-gray-300&quot;&gt;<br>    {isSubmitting ? &quot;Submitting...&quot; : &quot;Submit&quot;}<br>&lt;/button&gt;</pre><p>And as you can see, now everything works with and without JavaScript enabled:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/1*BIPJxNapysQeV4MZtQkkFw.gif" /></figure><p>You can find <a href="https://github.com/Leejjon/rr-forms/tree/react-router-form-conform">the final code on GitHub</a>.</p><p>So for me personally, React Router + Zod + Conform seems like the best way to do forms in React. There are some ways throw all attributes in <strong>&lt;input&gt;</strong> tags more efficient, but I’ll leave that for another time. I am personally going to apply react router and <strong>Conform</strong> on one of my projects (<a href="https://blindpool.com/">blindpool.com</a>) to make it work without JavaScript and brag about it on LinkedIn. While at work we probably stay with <strong>react-hook-form</strong>. I learned a lot writing this, I hope you learned something from reading this!</p><h3>Follow me for more!</h3><p>If you enjoyed this blog post, please follow me on <a href="https://leejjon.medium.com/">Medium</a>, <a href="https://twitter.com/Leejjon_net">X</a>, <a href="https://bsky.app/profile/leejjon.bsky.social">BlueSky</a> or <a href="https://www.linkedin.com/in/leon-liefting-4b511a29/">LinkedIN</a>.</p><p>I post very few articles per year and I do take my time to put good working code examples along with my posts (unlike the many people who spam Medium with AI generated content and hope to generate passive income out of it). Following me hopefully boosts the reach of my posts.</p><p>If there are any mistakes in my blog, let me know!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2ef5c0d5b335" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building flexible breadcrumbs in a Next.js app using the App Router]]></title>
            <link>https://leejjon.medium.com/building-flexible-breadcrumbs-in-a-next-js-app-using-the-app-router-48720e833cb5?source=rss-e00e4e1c1655------2</link>
            <guid isPermaLink="false">https://medium.com/p/48720e833cb5</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[react-server-component]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Leejjon]]></dc:creator>
            <pubDate>Wed, 18 Feb 2026 14:26:11 GMT</pubDate>
            <atom:updated>2026-03-29T00:10:58.149Z</atom:updated>
            <content:encoded><![CDATA[<p>Building flexible breadcrumbs doesn’t sound too spectacular, does it? It can get complex quickly when using the App Router in Next.js.</p><p>Read this blog post if you want:</p><ul><li>To read about my experience with using Next.js and the App Router at the retail company I work for.</li><li>To build (or just see) a Game of Thrones code example demonstrate the problems (and solutions) I ran into when building breadcrumbs functionality.</li><li>To be a smart ass and point out that I am using it wrong (which is very possible). Feedback is always appreciated.</li></ul><h3>What do you even mean with breadcrumbs?</h3><p>There are no stupid questions. With breadcrumbs I mean the navigation links at the top of a page that show your position within the website’s hierarchy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*MtA37oIX43D7JL4iXpCGFQ.png" /><figcaption>The breadcrumbs on Google Drive.</figcaption></figure><h3>Putting a Game of Thrones characters web app together</h3><p>I am using the <a href="https://anapioficeandfire.com/">An Api Of Ice And Fire</a> API for some test data. The following calls in specific:</p><pre>https://www.anapioficeandfire.com/api/characters<br>https://www.anapioficeandfire.com/api/characters/1</pre><p>I created an overview page where you can see a list of the characters with pagination buttons (previous &amp; next). On each of those characters you can click to go to a character details page and see their full information.</p><p>You can either follow my guide to build this entire thing from scratch or just browse <a href="https://github.com/Leejjon/nextjs-app-router-got-breadcrumbs/tree/breadcrumbs-client-component">the source code on GitHub</a>.</p><h3>Setting up Next.js</h3><p>I am using the latest Next.js (16.1.1 at the time of writing). You can generate a project with (if you have <a href="https://nodejs.org/en">Node.js</a>):</p><pre>npx create-next-app@latest</pre><p>You can run npm run dev to see if your application works at <a href="http://localhost:3000/">http://localhost:3000</a></p><p>In the <strong>app</strong> folder, create a directory called <strong>characters</strong> and a <strong>page.tsx</strong> file inside it, like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/419/1*iuSL8hC_6WLaXdXHZ508qA.png" /></figure><p>Replace the default content in the page.tsx (in the app folder) with this code:</p><pre>export default function Home() {<br>    return (<br>        &lt;&gt;In this web app you can browse through the &#39;A Song of Ice and Fire&#39; API.&lt;/&gt;<br>    );<br>}</pre><h3>Fetching data from the API</h3><p>For this blog post I am going to keep it simple and just use the fetch function that is built into JavaScript. <strong>Warning:</strong> If you make a real app that relies on an external API like this, use caching. You should probably at least use <a href="https://tanstack.com/query/latest">Tanstack Query</a>.</p><p>In the <strong>page.tsx</strong> (inside the characters folder) put the following code:</p><pre>import Link from &#39;next/link&#39;;<br><br>type SearchParams = {<br>    page: number;<br>    pageSize: number;<br>};<br><br>type Character = {<br>    url: string;<br>    name: string;<br>    gender: string;<br>    titles: string[];<br>    aliases: string[];<br>};<br><br>export default async function Characters({searchParams}: { searchParams: Promise&lt;SearchParams&gt; }) {<br>    const {page, pageSize} = await searchParams;<br><br>    const response = await fetch(<br>        `https://www.anapioficeandfire.com/api/characters?page=${page ?? 1}&amp;pageSize=${pageSize ?? 5}`,<br>    );<br><br>    const enablePreviousButton = (page ? page &gt; 1 : false);<br><br>    if (response.ok) {<br>        const characters = await response.json() as Character[];<br><br>        return (<br>            &lt;div className=&quot;grid grid-cols-12 min-h-screen grid-rows-[auto_1fr]&quot;&gt;<br>                &lt;div id=&quot;header&quot; className=&quot;col-span-12 border-b-2 border-black&quot;&gt;<br>                    &lt;h1 className=&quot;text-6xl font-bold text-center m-2&quot;&gt;My Game of Thrones character app&lt;/h1&gt;<br>                &lt;/div&gt;<br>                &lt;div id=&quot;navbar&quot; className=&quot;p-2 border-r-2 border-black col-span-2 flex flex-col gap-2&quot;&gt;<br>                    &lt;Link href=&quot;/&quot;&gt;Home&lt;/Link&gt;<br>                    &lt;Link href=&quot;/books&quot;&gt;Books&lt;/Link&gt;<br>                    &lt;Link href=&quot;/characters&quot;&gt;Characters&lt;/Link&gt;<br>                    &lt;Link href=&quot;/houses&quot;&gt;Houses&lt;/Link&gt;<br>                &lt;/div&gt;<br>                &lt;div id=&quot;content&quot; className=&quot;col-span-10 p-2&quot;&gt;<br>                    &lt;h2 className=&quot;text-2xl font-bold&quot;&gt;Characters&lt;/h2&gt;<br>                    &lt;p className=&quot;mb-3&quot;&gt;A list of all characters:&lt;/p&gt;<br>                    {characters.map((character) =&gt; {<br>                        const id = character.url.split(&#39;/&#39;).at(-1);<br>                        return (<br>                            &lt;div key={`character-${id}`} className=&quot;border-black border-2 rounded-md mb-4 p-2 w-150&quot;&gt;<br>                                &lt;p&gt;Name: {character.name}&lt;/p&gt;<br>                                &lt;p&gt;Gender: {character.gender}&lt;/p&gt;<br>                                &lt;p&gt;Culture: {character.aliases.length &gt; 0 ? character.aliases[0] : &#39;None&#39;}&lt;/p&gt;<br>                            &lt;/div&gt;<br>                        );<br>                    })}<br>                    &lt;div className=&quot;w-150 text-center&quot;&gt;<br>                        &lt;Link<br>                            className=&quot;bg-gray-300 border-1 border-black rounded-md p-2 m-1 w-20&quot;<br>                            style={{<br>                                pointerEvents: enablePreviousButton ? &quot;auto&quot; : &quot;none&quot;,<br>                            }}<br>                            href={enablePreviousButton ? `/characters?page=${page - 1}` : &quot;/characters&quot;}&gt;Previous<br>                        &lt;/Link&gt;<br>                        &lt;Link type=&quot;button&quot;<br>                              className=&quot;bg-gray-300 border-1 border-black rounded-md p-2 m-1 w-20&quot;<br>                              href={page ? `/characters?page=${+page + 1}` : &quot;/characters?page=2&quot;}&gt;Next<br>                        &lt;/Link&gt;<br>                    &lt;/div&gt;<br>                &lt;/div&gt;<br>            &lt;/div&gt;<br>        );<br>    } else {<br>        return (<br>            &lt;div&gt;Could not load characters.&lt;/div&gt;<br>        );<br>    }<br>}</pre><p>Don’t mind my tailwind styling too much. I’ve tried to make the UI readable without making the examples too big.</p><p>Run npm run dev and visit http://localhost:3000/characters and browse through the characters with the Next and Previous buttons:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e1YEIrlE8VkCLQFk8ujEkw.png" /></figure><h3>Extracting the header and navbar to layout.tsx</h3><p>Before making a second page that shows a characters details, we want to extract the header and navbar to the layout.tsx file <a href="https://nextjs.org/docs/app/getting-started/layouts-and-pages">as is recommended by the Next.js documentation</a>. This <strong>layout.tsx</strong> mechanism is nice for de-duplication of code, but is part of the reason that makes creating flexible breadcrumbs difficult.</p><p>Change your <strong>layout.tsx</strong> to:</p><pre>import type {Metadata} from &quot;next&quot;;<br>import &quot;./globals.css&quot;;<br>import Link from &#39;next/link&#39;;<br>import {ReactNode} from &quot;react&quot;;<br><br>export const metadata: Metadata = {<br>    title: &quot;My Game of Thrones character app&quot;,<br>    description: &quot;Generated by create next app&quot;,<br>};<br><br>export default function RootLayout({<br>   children,<br>}: Readonly&lt;{<br>    children: ReactNode;<br>}&gt;) {<br>    return (<br>        &lt;html lang=&quot;en&quot;&gt;<br>        &lt;body&gt;<br>        &lt;div className=&quot;grid grid-cols-12 min-h-screen grid-rows-[auto_1fr]&quot;&gt;<br>            &lt;div id=&quot;header&quot; className=&quot;col-span-12 border-b-2 border-black&quot;&gt;<br>                &lt;h1 className=&quot;text-6xl font-bold text-center m-2&quot;&gt;My Game of Thrones character app&lt;/h1&gt;<br>            &lt;/div&gt;<br>            &lt;div id=&quot;navbar&quot; className=&quot;p-2 border-r-2 border-black col-span-2 flex flex-col gap-2&quot;&gt;<br>                &lt;Link href=&quot;/&quot;&gt;Home&lt;/Link&gt;                <br>                &lt;Link href=&quot;/books&quot;&gt;Books&lt;/Link&gt;<br>                &lt;Link href=&quot;/characters&quot;&gt;Characters&lt;/Link&gt;<br>            &lt;/div&gt;<br>            &lt;div id=&quot;content&quot; className=&quot;col-span-10 p-2&quot;&gt;<br>                {children}<br>            &lt;/div&gt;<br>        &lt;/div&gt;<br>        &lt;/body&gt;<br>        &lt;/html&gt;<br>    );<br>}</pre><p>Now the <strong>header</strong> and <strong>navbar</strong> of the page will be wrapped on every <strong>page.tsx</strong> we create in the <strong>app</strong> folder. So we can remove the <strong>header</strong> and <strong>navbar</strong> from the <strong>/characters/page.tsx</strong> file:</p><pre>import Link from &#39;next/link&#39;;<br><br>type SearchParams = {<br>    page: number;<br>    pageSize: number;<br>};<br><br>type Character = {<br>    url: string;<br>    name: string;<br>    gender: string;<br>    titles: string[];<br>    aliases: string[];<br>};<br><br>export default async function Characters({searchParams}: { searchParams: Promise&lt;SearchParams&gt; }) {<br>    const {page, pageSize} = await searchParams;<br><br>    const response = await fetch(<br>        `https://www.anapioficeandfire.com/api/characters?page=${page ?? 1}&amp;pageSize=${pageSize ?? 5}`,<br>    );<br><br>    const enablePreviousButton = (page ? page &gt; 1 : false);<br><br>    if (response.ok) {<br>        const characters = await response.json() as Character[];<br><br>        return (<br>            &lt;&gt;<br>                &lt;h2 className=&quot;text-2xl font-bold&quot;&gt;Characters&lt;/h2&gt;<br>                &lt;p className=&quot;mb-3&quot;&gt;A list of all characters:&lt;/p&gt;<br>                {characters.map((character) =&gt; {<br>                    const id = character.url.split(&#39;/&#39;).at(-1);<br>                    return (<br>                        &lt;div key={`character-${id}`} className=&quot;border-black border-2 rounded-md mb-4 p-2 w-150&quot;&gt;<br>                            &lt;p&gt;Name: {character.name}&lt;/p&gt;<br>                            &lt;p&gt;Gender: {character.gender}&lt;/p&gt;<br>                            &lt;p&gt;Culture: {character.aliases.length &gt; 0 ? character.aliases[0] : &#39;None&#39;}&lt;/p&gt;<br>                        &lt;/div&gt;<br>                    );<br>                })}<br>                &lt;div className=&quot;w-150 text-center&quot;&gt;<br>                    &lt;Link<br>                        className=&quot;bg-gray-300 border-1 border-black rounded-md p-2 m-1 w-20&quot;<br>                        style={{<br>                            pointerEvents: enablePreviousButton ? &quot;auto&quot; : &quot;none&quot;,<br>                        }}<br>                        href={enablePreviousButton ? `/characters?page=${page - 1}` : &quot;/characters&quot;}&gt;Previous<br>                    &lt;/Link&gt;<br>                    &lt;Link type=&quot;button&quot;<br>                          className=&quot;bg-gray-300 border-1 border-black rounded-md p-2 m-1 w-20&quot;<br>                          href={page ? `/characters?page=${+page + 1}` : &quot;/characters?page=2&quot;}&gt;Next<br>                    &lt;/Link&gt;<br>                &lt;/div&gt;<br>            &lt;/&gt;<br>        );<br>    } else {<br>        return (<br>            &lt;&gt;Could not load characters.&lt;/&gt;<br>        );<br>    }<br>}</pre><p>Everything should still work.</p><h3>Creating the character details page</h3><p>I want to make it possible to click on one of the characters, and go to the a page that displays all of the character data. Create a new <strong>page.tsx</strong> in a folder named [id]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*xKdCRyV02-8HbTffF_vqmA.png" /></figure><p>Insert the following code:</p><pre>import Link from &quot;next/link&quot;;<br><br>type CharacterPageProps = {<br>    params: {<br>        id: string;<br>    };<br>};<br><br>type CharacterDetails = {<br>    url: string;<br>    name: string;<br>    gender: string;<br>    culture: string;<br>    born: string;<br>    died: &quot;&quot;;<br>    titles: string[];<br>    aliases: string[];<br>    father: string;<br>    mother: string;<br>    spouse: string;<br>    allegiances: string[];<br>    books: string[];<br>    povBooks: string[];<br>    tvSeries: string[];<br>    playedBy: string[];<br>};<br><br>export default async function CharacterPage(props: CharacterPageProps) {<br>    const {id} = await props.params;<br>    const response = await fetch(<br>        `https://www.anapioficeandfire.com/api/characters/${id}`,<br>    );<br><br>    if (response.ok) {<br>        const character = await response.json() as CharacterDetails;<br>        const id = character.url.split(&#39;/&#39;).at(-1);<br>        return (<br>            &lt;&gt;<br>                &lt;h2 className=&quot;text-2xl font-bold&quot;&gt;{character.name}&lt;/h2&gt;<br>                &lt;p&gt;&lt;b&gt;Gender:&lt;/b&gt; {character.gender}&lt;/p&gt;<br>                &lt;p&gt;&lt;b&gt;Culture:&lt;/b&gt; {character.culture}&lt;/p&gt;<br>                &lt;p&gt;&lt;b&gt;Born:&lt;/b&gt; {character.born}&lt;/p&gt;<br>                &lt;p&gt;&lt;b&gt;Died:&lt;/b&gt; {character.died}&lt;/p&gt;<br>                &lt;p&gt;&lt;b&gt;Titles:&lt;/b&gt;&lt;/p&gt;<br>                &lt;ul&gt;<br>                    {character.titles.map((title) =&gt; &lt;li key={`title${title}`}&gt;- {title}&lt;/li&gt;)}<br>                &lt;/ul&gt;<br>                &lt;p&gt;&lt;b&gt;Aliases:&lt;/b&gt;&lt;/p&gt;<br>                &lt;ul&gt;<br>                    {character.aliases.map((alias) =&gt; &lt;li key={`alias${alias}`}&gt;- {alias}&lt;/li&gt;)}<br>                &lt;/ul&gt;<br>                &lt;p&gt;&lt;b&gt;Father:&lt;/b&gt; {character.father.split(&#39;/&#39;).at(-1)}&lt;/p&gt;<br>                &lt;p&gt;&lt;b&gt;Mother:&lt;/b&gt; {character.mother.split(&#39;/&#39;).at(-1)}&lt;/p&gt;<br>                &lt;p&gt;&lt;b&gt;Spouse:&lt;/b&gt; {character.spouse.split(&#39;/&#39;).at(-1)}&lt;/p&gt;<br>                &lt;p&gt;&lt;b&gt;Allegiances:&lt;/b&gt;&lt;/p&gt;<br>                &lt;ul&gt;<br>                    {character.allegiances.map((allegiance) =&gt; &lt;li key={`allegiance${allegiance.split(&#39;/&#39;).at(-1)}`}&gt;- {allegiance.split(&#39;/&#39;).at(-1)}&lt;/li&gt;)}<br>                &lt;/ul&gt;<br>                &lt;p&gt;&lt;b&gt;Books:&lt;/b&gt;&lt;/p&gt;<br>                &lt;ul&gt;<br>                    {character.books.map((book) =&gt; &lt;li key={`book${book.split(&#39;/&#39;).at(-1)}`}&gt;- {book.split(&#39;/&#39;).at(-1)}&lt;/li&gt;)}<br>                &lt;/ul&gt;<br>                &lt;p&gt;&lt;b&gt;Pov books:&lt;/b&gt;&lt;/p&gt;<br>                &lt;ul&gt;<br>                    {character.povBooks.map((povBook) =&gt; &lt;li key={`povBook${povBook.split(&#39;/&#39;).at(-1)}`}&gt;- {povBook.split(&#39;/&#39;).at(-1)}&lt;/li&gt;)}<br>                &lt;/ul&gt;<br>                &lt;p&gt;&lt;b&gt;Tv series:&lt;/b&gt;&lt;/p&gt;<br>                &lt;ul&gt;<br>                    {character.tvSeries.map((tvSeries) =&gt; &lt;li key={`tvSeries${tvSeries}`}&gt;- {tvSeries}&lt;/li&gt;)}<br>                &lt;/ul&gt;<br>                &lt;p&gt;&lt;b&gt;Played by:&lt;/b&gt;&lt;/p&gt;<br>                &lt;ul&gt;<br>                    {character.playedBy.map((playedBy) =&gt; &lt;li key={`playedBy${playedBy}`}&gt;- {playedBy}&lt;/li&gt;)}<br>                &lt;/ul&gt;<br>                &lt;br/&gt;<br>                &lt;Link type=&quot;button&quot;<br>                      className=&quot;bg-gray-300 border-1 border-black rounded-md p-2 m-1 w-40&quot;<br>                      href=&quot;../characters&quot;&gt;Back to characters<br>                &lt;/Link&gt;<br>            &lt;/&gt;<br>        );<br>    } else {<br>        return (<br>            &lt;&gt;Could not load character. Go back to &lt;Link href=&quot;/characters&quot;&gt;character&lt;/Link&gt;s.&lt;/&gt;<br>        );<br>    }<br>}</pre><p>And now on the characters page, add a Link of the type “button” to browse to the character details page:</p><pre>&lt;div key={`character-${id}`} className=&quot;border-black border-2 rounded-md mb-4 p-2 w-150&quot;&gt;<br>    &lt;p&gt;Name: {character.name}&lt;/p&gt;<br>    &lt;p&gt;Gender: {character.gender}&lt;/p&gt;<br>    &lt;p&gt;Culture: {character.aliases.length &gt; 0 ? character.aliases[0] : &#39;None&#39;}&lt;/p&gt;<br>    &lt;Link type=&quot;button&quot; href={`/characters/${id}`} className=&quot;bg-gray-300 border-1 border-black rounded-md p-1 my-1 w-25 inline-block&quot;&gt;View details&lt;/Link&gt;<br>&lt;/div&gt;</pre><p>It now should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zd7p_cHvKT3GmsTn2yjrIg.gif" /></figure><h3>Adding breadcrumbs everywhere at once</h3><p>Imagine that you want to add breadcrumbs to a page, to make it a little bit easier to figure out for the user where he is located on your website. We could put code for that on every page, but that would be quite labor intensive (even with AI it’s a lot of extra code to maintain).</p><p>We could try to build a breadcrumbs mechanism in the <strong>layout.tsx</strong> file so it is automatically there on every page. An example:</p><p><a href="https://example.com/hello/world"><strong>https://example.com/hello/world</strong></a> would have a path of <strong>/hello/world</strong></p><p>This should result in two breadcrumbs:</p><ul><li>“hello” linking to /hello</li><li>“world” linking to /hello/world</li></ul><p>Before continuing, it’s good to make a commit/tag/branch of your working version so far. Because I’m going to go through two implementations.</p><h3>Generating breadcrumbs on the server</h3><p>By default the <strong>layout.tsx</strong> is a server component. And a server component does not know the <strong>pathname</strong> (just so you know, the pathname is this part of the url: https://example.com<strong>/pathname</strong>) that it is on by default. You can work around this by defining a <strong>proxy.ts</strong> file in the root of the project (formerly known as <strong>middleware.ts</strong>, they changed it in Next.js 16):</p><pre>import {NextResponse} from &quot;next/server&quot;;<br><br>export function proxy(request: Request) {<br>    const url = new URL(request.url);<br><br>    const response = NextResponse.next();<br>    response.headers.set(&#39;x-url&#39;, url.pathname);<br>    return response;<br>}</pre><p>So let’s create a <strong>server-breadcrumbs.tsx</strong> server component (put it in a components folder) that we can put in the <strong>layout.tsx</strong> to display breadcrumbs based on the path of the url:</p><pre>&#39;use server&#39;;<br><br>import {headers} from &#39;next/headers&#39;;<br>import Link from &quot;next/link&quot;;<br>import {Fragment} from &quot;react&quot;;<br><br>export default async function ServerBreadcrumbs() {<br>    const headersList = await headers();<br>    const path = headersList.get(&#39;x-url&#39;) || &#39;&#39;;<br>    const pathSegments = path.split(&#39;/&#39;).filter(segment =&gt; segment !== &#39;&#39;);<br>    return (<br>        &lt;div className=&quot;mb-2&quot;&gt;<br>            {path === &#39;/&#39; &amp;&amp; &lt;Link href=&quot;/&quot; key=&quot;breadcrumb0&quot;&gt;Home&lt;/Link&gt;}<br>            {pathSegments.map((segment, index) =&gt; {<br>                const renderBreadcrumbSeparation = index &gt; 0 &amp;&amp; &#39; -&gt; &#39;;<br>                const url = `/${pathSegments.slice(0, index + 1).join(&#39;/&#39;)}`;<br>                return (<br>                    &lt;Fragment key={`breadcrumb${index + 1}`}&gt;<br>                        {renderBreadcrumbSeparation}<br>                        &lt;Link href={url}&gt;<br>                             &lt;span className=&quot;capitalize&quot;&gt;{segment}&lt;/span&gt;<br>                        &lt;/Link&gt;<br>                    &lt;/Fragment&gt;<br>                );<br>            })}<br>        &lt;/div&gt;<br>    );<br>}</pre><p>Add this new <strong>server-breadcrumbs.tsx </strong>component to the <strong>layout.tsx</strong> file, just above the {children} prop in which the content of the actual pages will be loaded:</p><pre>&lt;div id=&quot;content&quot; className=&quot;col-span-10 p-2&quot;&gt;<br>    &lt;ServerBreadcrumbs/&gt;<br>    {children}<br>&lt;/div&gt;</pre><p>Initially, this looks good when doing a first time visit or hard refresh on a web page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*etwaXeApq7HOItDM1xrF0Q.gif" /></figure><p>But as you can see, it generates the breadcrumbs once, and then only updates the page content when links are being clicked. The breadcrumbs keep displaying the first page that was loaded.</p><p>If you look at <a href="https://nextjs.org/docs/app/api-reference/file-conventions/layout">the docs of the <strong>layout.tsx</strong> in Next.js</a> you see that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/745/1*v5nfZaB6cRPHKlolW72CNA.png" /><figcaption>From <a href="https://nextjs.org/docs/app/api-reference/file-conventions/layout#request-object">the official Next.js docs</a>.</figcaption></figure><p>I have a tried a bunch of (stupid but interesting) things to fix this behaviour. (many were recommended by LLM’s), but none of these worked:</p><ul><li>Using a <strong>template.tsx file</strong> instead of layout.tsx because <a href="https://nextjs.org/docs/app/api-reference/file-conventions/template#behavior">the Next.js template docs</a> said it was an alternative to the <strong>layout.tsx </strong>and that it could refresh.</li><li>Using the <strong>unstable_noStore</strong> function from “next/cache”. By not caching the <strong>server-breadcrumbs.tsx</strong>, a LLM hoped it would refresh on every navigation. But all server components under <strong>layout.tsx</strong> are not refreshed upon navigation actions, because <strong>layout.tsx</strong> does not refresh..</li><li>Wrapping the <strong>server-breadcrumbs.tsx</strong> file in a client component (and passing the value from the usePathname() hook as prop. It got me: Error: Server Functions cannot be called during initial render. This would create a fetch waterfall. Try to use a Server Component to pass dataa to Client Components instead. You can only pass server components to client components via slots. But even if you try this, a client component cannot interact with this slot.</li></ul><p>You could probably solve this by:</p><ul><li>Just put the breadcrumb component in every <strong>page.tsx </strong>and remove it from the <strong>layout.tsx</strong>.</li><li>Or disable all server component caching and make sure <strong>layout.tsx</strong> is refreshed every time a client side navigation happens you could get it to work, but that is quite expensive for your bandwidth.</li></ul><p>So I gave up and turned it into a client component.</p><h3>Generate breadcrumbs on the client</h3><p>There is simply a <strong>usePathname()</strong> hook built into Next.js to be used in client components. Create a <strong>client-breadcrumbs.tsx</strong> file in a components folder like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*BwGrEK9zvdhbPHpgRYuy2Q.png" /></figure><p>The implementation of <strong>client-breadcrumbs.tsx</strong> is extremely similar to the server variant:</p><pre>&#39;use client&#39;;<br><br>import {usePathname} from &quot;next/navigation&quot;;<br>import Link from &quot;next/link&quot;;<br>import {Fragment} from &quot;react&quot;;<br><br>export function ClientBreadcrumbs() {<br>    const pathname = usePathname();<br>    const pathSegments = pathname.split(&#39;/&#39;).filter(segment =&gt; segment !== &#39;&#39;);<br>    return (<br>        &lt;div className=&quot;mb-2&quot;&gt;<br>            &lt;Link href=&quot;/&quot; key=&quot;breadcrumb0&quot;&gt;Home&lt;/Link&gt;{&#39; -&gt; &#39;}<br>            {pathSegments.map((segment, index) =&gt; {<br>                const renderBreadcrumbSeparation = index &gt; 0 &amp;&amp; &#39; -&gt; &#39;;<br>                const url = `/${pathSegments.slice(0, index + 1).join(&#39;/&#39;)}`;<br>                return (<br>                    &lt;Fragment key={`breadcrumb${index + 1}`}&gt;<br>                        {renderBreadcrumbSeparation}<br>                        &lt;Link href={url}&gt;<br>                            &lt;span className=&quot;capitalize&quot;&gt;{segment}&lt;/span&gt;<br>                        &lt;/Link&gt;<br>                    &lt;/Fragment&gt;<br>                );<br>            })}<br>        &lt;/div&gt;<br>    );<br>}</pre><p>This one just works fine:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*13DekTsEiYB2sVsVsACPNQ.gif" /></figure><p>This was fairly easy. Just plain React.</p><h3>Showing actual character names rather than id’s</h3><p>You probably noticed that with our current implementation, the breadcrumbs for the character detail page with <a href="http://localhost:3000/characters/16">http://localhost:3000/characters/16</a> show up as:</p><p><a href="http://localhost:3000/characters">Characters</a> -&gt; <a href="http://localhost:3000/characters/16">16</a></p><p>No user will understand that by 16 we actually mean Margaery Tyrell. So it would be better if in this case we will show:</p><p><a href="http://localhost:3000/characters">Characters</a> -&gt; <a href="http://localhost:3000/characters/16">Margaery Tyrell</a></p><p>But that is not how the AnApiOfIceAndFire API works. The designers of that API gave every character in Game of Thrones a unique number that you can query the character details on. Maybe you think it would have been better if they would have use the character name as a primary key instead of a number.</p><p>But in Game of Thrones names are not necessarily unique. For example there are a lot of characters that go by the name of <strong>Walder Frey.</strong></p><h3>Dynamic overrides</h3><p>So how can we override the breadcrumbs in the case of the <strong>/app/characters/[id]/page.tsx</strong>? Let’s visualize what we want:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/674/1*XazcNPX-MlDDhW0D_bNa-Q.png" /></figure><p>We could add props to our <strong>client-breadcrumbs.tsx</strong> to allow overrides:</p><pre>&#39;use client&#39;;<br><br>import {usePathname} from &quot;next/navigation&quot;;<br>import Link from &quot;next/link&quot;;<br>import {Fragment} from &quot;react&quot;;<br><br>export type ClientBreadcrumbsProps = {<br>    breadcrumbLabelOverrides: string[];<br>};<br><br>export function ClientBreadcrumbs({breadcrumbLabelOverrides}: ClientBreadcrumbsProps) {<br>    const pathname = usePathname();<br>    const pathSegments = pathname.split(&#39;/&#39;).filter(segment =&gt; segment !== &#39;&#39;);<br>    return (<br>        &lt;div className=&quot;mb-2&quot;&gt;<br>            {pathname === &#39;/&#39; &amp;&amp; &lt;Link href=&quot;/&quot; key=&quot;breadcrumb0&quot;&gt;Home&lt;/Link&gt;}<br>            {pathSegments.map((segment, index) =&gt; {<br>                const renderBreadcrumbSeparation = index &gt; 0 &amp;&amp; &#39; -&gt; &#39;;<br>                const labelOverride: string | undefined = breadcrumbLabelOverrides[index];<br>                const label = labelOverride || segment;<br>                const url = `/${pathSegments.slice(0, index + 1).join(&#39;/&#39;)}`;<br>                return (<br>                    &lt;Fragment key={`breadcrumb${index + 1}`}&gt;<br>                        {renderBreadcrumbSeparation}<br>                        &lt;Link href={url}&gt;<br>                            &lt;span className=&quot;capitalize&quot;&gt;{label}&lt;/span&gt;<br>                        &lt;/Link&gt;<br>                    &lt;/Fragment&gt;<br>                );<br>            })}<br>        &lt;/div&gt;<br>    );<br>}</pre><p>But how do we ever pass this labeloverride as props? The layout.tsx file doesn’t know.</p><p>After puzzling on this for some time I came up with this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/1*5eky37WEHDB5QiY5MBA38g.png" /></figure><p>Okay these pictures work better in a PowerPoint where you can let all steps appear one by one. But try to read them in order. I will agree that this solution made me doubt whether server components are the future of frontend. If you need this amount of complexity just to have some breadcrumb functionality it makes you think.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*ELknelSdC-vDFzfN" /></figure><p>Like me, you probably need server side rendering and things like streaming or are already too far into Next.js that you will simply have to deal with this complexity. So we might as well own it.</p><p>Let’s implement this little “design”.</p><h3>Creating the client component that will contain the breadcrumbs and content</h3><p>We will put an extra client component between the <strong>client-breadcrumbs.tsx</strong> and <strong>layout.tsx</strong>. Why do we need such client component?</p><p>Well if you cannot pass props from one component to another, (in this case because the <strong>layout.tsx</strong> doesn’t know which <strong>page.tsx</strong> it is loading), you fall back to React context. And React Context only works in client components, so we can’t put it in <strong>layout.tsx</strong> itself.</p><p>Create a new component in the components folder called <strong>breadcrumbs-content-wrapper.tsx</strong>:</p><pre>&#39;use client&#39;;<br><br>import {createContext, Dispatch, PropsWithChildren, SetStateAction, useState} from &quot;react&quot;;<br>import {ClientBreadcrumbs} from &quot;@/components/client-breadcrumbs&quot;;<br><br>export type SetBreadcrumbLabelOverrides = {<br>    setOverrides: Dispatch&lt;SetStateAction&lt;string[]&gt;&gt;<br>}<br><br>export const BreadcrumbContext = createContext&lt;SetBreadcrumbLabelOverrides | undefined&gt;(undefined);<br><br>export default function BreadcrumbsContentWrapper({children}: PropsWithChildren) {<br>    const [customBreadcrumbs, setCustomBreadcrumbs] = useState&lt;string[]&gt;([]);<br>    return (<br>        &lt;div id=&quot;content&quot; className=&quot;col-span-10 p-2&quot;&gt;<br>            &lt;ClientBreadcrumbs breadcrumbLabelOverrides={customBreadcrumbs}/&gt;<br>            &lt;BreadcrumbContext.Provider value={{setOverrides: setCustomBreadcrumbs}}&gt;<br>                {children}<br>            &lt;/BreadcrumbContext.Provider&gt;<br>        &lt;/div&gt;<br>    );<br>}</pre><p>In the <strong>layout.tsx</strong>, start using the <strong>breadcrumbs-content-wrapper.tsx</strong>:</p><pre>import type {Metadata} from &quot;next&quot;;<br>import &quot;./globals.css&quot;;<br>import Link from &#39;next/link&#39;;<br>import {ReactNode} from &quot;react&quot;;<br>import BreadcrumbsContentWrapper from &quot;@/components/breadcrumbs-content-wrapper&quot;;<br><br>export const metadata: Metadata = {<br>    title: &quot;My Game of Thrones character app&quot;,<br>    description: &quot;Generated by create next app&quot;,<br>};<br><br>export default function RootLayout({<br>   children,<br>}: Readonly&lt;{<br>    children: ReactNode;<br>}&gt;) {<br>    return (<br>        &lt;html lang=&quot;en&quot;&gt;<br>        &lt;body&gt;<br>        &lt;div className=&quot;grid grid-cols-12 min-h-screen grid-rows-[auto_1fr]&quot;&gt;<br>            &lt;div id=&quot;header&quot; className=&quot;col-span-12 border-b-2 border-black&quot;&gt;<br>                &lt;h1 className=&quot;text-6xl font-bold text-center m-2&quot;&gt;My Game of Thrones character app&lt;/h1&gt;<br>            &lt;/div&gt;<br>            &lt;div id=&quot;navbar&quot; className=&quot;p-2 border-r-2 border-black col-span-2 flex flex-col gap-2&quot;&gt;<br>                &lt;Link href=&quot;/&quot;&gt;Home&lt;/Link&gt;<br>                &lt;Link href=&quot;/books&quot;&gt;Books&lt;/Link&gt;<br>                &lt;Link href=&quot;/characters&quot;&gt;Characters&lt;/Link&gt;<br>                &lt;Link href=&quot;/books&quot;&gt;Houses&lt;/Link&gt;<br>            &lt;/div&gt;<br>            &lt;BreadcrumbsContentWrapper&gt;<br>                {children}<br>            &lt;/BreadcrumbsContentWrapper&gt;<br>        &lt;/div&gt;<br>        &lt;/body&gt;<br>        &lt;/html&gt;<br>    );<br>}</pre><p>Now your app should work as before, in the next step we will start overriding breadcrumbs for <strong>/app/characters/[id]/page.tsx</strong>.</p><h3>Actually overriding the breadcrumbs</h3><p>Create <strong>custom-breadcrumbs.tsx</strong> in the components folder:</p><pre>&#39;use client&#39;;<br><br>import {PropsWithChildren, useContext, useEffect} from &quot;react&quot;;<br>import {BreadcrumbContext} from &quot;@/components/breadcrumbs-content-wrapper&quot;;<br><br>export default function CustomBreadcrumbs({children, labelOverrides}: PropsWithChildren &amp; {<br>    labelOverrides: string[];<br>}) {<br>    const breadcrumbContext = useContext(BreadcrumbContext);<br><br>    useEffect(() =&gt; {<br>        breadcrumbContext?.setOverrides(labelOverrides);<br><br>        // To make sure the override disappears when a user navigates to another page.<br>        return () =&gt; {<br>            breadcrumbContext?.setOverrides([]);<br>        }<br>    }, [labelOverrides, breadcrumbContext]);<br>}</pre><p>This is basically another wrapper, which you need to use in <strong>/app/characters/[id]/page.tsx:</strong></p><pre>import Link from &quot;next/link&quot;;<br>import CustomBreadcrumbs from &quot;@/components/custom-breadcrumbs&quot;;<br><br>type CharacterPageProps = {<br>    params: {<br>        id: string;<br>    };<br>};<br><br>type CharacterDetails = {<br>    url: string;<br>    name: string;<br>    gender: string;<br>    culture: string;<br>    born: string;<br>    died: &quot;&quot;;<br>    titles: string[];<br>    aliases: string[];<br>    father: string;<br>    mother: string;<br>    spouse: string;<br>    allegiances: string[];<br>    books: string[];<br>    povBooks: string[];<br>    tvSeries: string[];<br>    playedBy: string[];<br>};<br><br>export default async function CharacterPage(props: CharacterPageProps) {<br>    const {id} = await props.params;<br>    const response = await fetch(<br>        `https://www.anapioficeandfire.com/api/characters/${id}`,<br>    );<br><br>    if (response.ok) {<br>        const character = await response.json() as CharacterDetails;<br>        const id = character.url.split(&#39;/&#39;).at(-1);<br>        return (<br>            &lt;CustomBreadcrumbs labelOverrides={[&#39;Characters&#39;, character.name]}&gt;<br>                &lt;h2 className=&quot;text-2xl font-bold&quot;&gt;{character.name}&lt;/h2&gt;<br>                &lt;p&gt;&lt;b&gt;Gender:&lt;/b&gt; {character.gender}&lt;/p&gt;<br>                &lt;p&gt;&lt;b&gt;Culture:&lt;/b&gt; {character.culture}&lt;/p&gt;<br>                &lt;p&gt;&lt;b&gt;Born:&lt;/b&gt; {character.born}&lt;/p&gt;<br>                &lt;p&gt;&lt;b&gt;Died:&lt;/b&gt; {character.died}&lt;/p&gt;<br>                &lt;p&gt;&lt;b&gt;Titles:&lt;/b&gt;&lt;/p&gt;<br>                &lt;ul&gt;<br>                    {character.titles.map((title) =&gt; &lt;li key={`title${title}`}&gt;- {title}&lt;/li&gt;)}<br>                &lt;/ul&gt;<br>                &lt;p&gt;&lt;b&gt;Aliases:&lt;/b&gt;&lt;/p&gt;<br>                &lt;ul&gt;<br>                    {character.aliases.map((alias) =&gt; &lt;li key={`alias${alias}`}&gt;- {alias}&lt;/li&gt;)}<br>                &lt;/ul&gt;<br>                &lt;p&gt;&lt;b&gt;Father:&lt;/b&gt; {character.father.split(&#39;/&#39;).at(-1)}&lt;/p&gt;<br>                &lt;p&gt;&lt;b&gt;Mother:&lt;/b&gt; {character.mother.split(&#39;/&#39;).at(-1)}&lt;/p&gt;<br>                &lt;p&gt;&lt;b&gt;Spouse:&lt;/b&gt; {character.spouse.split(&#39;/&#39;).at(-1)}&lt;/p&gt;<br>                &lt;p&gt;&lt;b&gt;Allegiances:&lt;/b&gt;&lt;/p&gt;<br>                &lt;ul&gt;<br>                    {character.allegiances.map((allegiance) =&gt; &lt;li key={`allegiance${allegiance.split(&#39;/&#39;).at(-1)}`}&gt;- {allegiance.split(&#39;/&#39;).at(-1)}&lt;/li&gt;)}<br>                &lt;/ul&gt;<br>                &lt;p&gt;&lt;b&gt;Books:&lt;/b&gt;&lt;/p&gt;<br>                &lt;ul&gt;<br>                    {character.books.map((book) =&gt; &lt;li key={`book${book.split(&#39;/&#39;).at(-1)}`}&gt;- {book.split(&#39;/&#39;).at(-1)}&lt;/li&gt;)}<br>                &lt;/ul&gt;<br>                &lt;p&gt;&lt;b&gt;Pov books:&lt;/b&gt;&lt;/p&gt;<br>                &lt;ul&gt;<br>                    {character.povBooks.map((povBook) =&gt; &lt;li key={`povBook${povBook.split(&#39;/&#39;).at(-1)}`}&gt;- {povBook.split(&#39;/&#39;).at(-1)}&lt;/li&gt;)}<br>                &lt;/ul&gt;<br>                &lt;p&gt;&lt;b&gt;Tv series:&lt;/b&gt;&lt;/p&gt;<br>                &lt;ul&gt;<br>                    {character.tvSeries.map((tvSeries) =&gt; &lt;li key={`tvSeries${tvSeries}`}&gt;- {tvSeries}&lt;/li&gt;)}<br>                &lt;/ul&gt;<br>                &lt;p&gt;&lt;b&gt;Played by:&lt;/b&gt;&lt;/p&gt;<br>                &lt;ul&gt;<br>                    {character.playedBy.map((playedBy) =&gt; &lt;li key={`playedBy${playedBy}`}&gt;- {playedBy}&lt;/li&gt;)}<br>                &lt;/ul&gt;<br>                &lt;br/&gt;<br>                &lt;Link type=&quot;button&quot;<br>                      className=&quot;bg-gray-300 border-1 border-black rounded-md p-2 m-1 w-40&quot;<br>                      href=&quot;../characters&quot;&gt;Back to characters<br>                &lt;/Link&gt;<br>            &lt;/CustomBreadcrumbs&gt;<br>        );<br>    } else {<br>        return (<br>            &lt;&gt;Could not load character. Go back to &lt;Link href=&quot;/characters&quot;&gt;character&lt;/Link&gt;s.&lt;/&gt;<br>        );<br>    }<br>}</pre><p>Now simply run your application using npm run dev again and verify that you can see the actual name. Please note that some characters do not have a name available in the database of the AnApiOfIceAndFire API. In such case the solution falls back to showing the number.</p><p>Here is how it should look:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WnmPLUfgYcnn48WovjLJ7Q.png" /></figure><h3>Follow me for more!</h3><p>The source code can <a href="https://github.com/Leejjon/nextjs-app-router-got-breadcrumbs/tree/breadcrumbs-client-component">be found in this GitHub project</a>. If you enjoyed this blog post, please follow me on <a href="https://leejjon.medium.com/">Medium</a>, <a href="https://twitter.com/Leejjon_net">X</a> or <a href="https://www.linkedin.com/in/leon-liefting-4b511a29/">LinkedIN</a>.</p><p>I post very few articles per year and I do take my time to put good working code examples along with my posts (unlike the many people who spam Medium with AI generated content and hope to generate passive income out of it). Following me hopefully boosts the reach of my posts.</p><p>If there are any mistakes in my blog, let me know!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48720e833cb5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Four ways to do CRUD on SQL databases in a Kotlin Spring Boot Application]]></title>
            <link>https://leejjon.medium.com/four-ways-to-do-crud-on-sql-databases-in-a-kotlin-spring-boot-application-2706e58922dd?source=rss-e00e4e1c1655------2</link>
            <guid isPermaLink="false">https://medium.com/p/2706e58922dd</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[exposed]]></category>
            <category><![CDATA[hibernate-jpa]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Leejjon]]></dc:creator>
            <pubDate>Sun, 09 Feb 2025 01:04:14 GMT</pubDate>
            <atom:updated>2025-04-10T21:24:44.976Z</atom:updated>
            <content:encoded><![CDATA[<p>In the last few years, two interview assignments required me to build a CRUD REST API with a SQL database behind it. The Java / Kotlin ecosystem has many ways to do this, so I decided to work out four ways in Kotlin for comparison. I provide <a href="https://github.com/Leejjon/ways-to-crud/">working code samples for every implementation on GitHub</a> that you can use.</p><h3>My first Kotlin blog</h3><p>This will be my first blog post on Kotlin. I have been working with Kotlin professionally for over three years now and I’m starting to feel more confident writing about it. In the first two years my Kotlin was pretty Java-ish as we used Spring Boot with libraries that were written in Java.</p><p>I mostly liked Kotlin for:</p><ul><li>Immutability by default</li><li>The way Kotlin handles null</li><li>Being less verbose in general</li></ul><p>Another one of Kotlin’s features is <a href="https://kotlinlang.org/docs/type-safe-builders.html">the ability to write DSL’s</a>. I did not use that and most likely you don’t need to write a DSL often. But it has been great to use libraries that provide DSLs that I can use.</p><h3>A Kotlin DSL example</h3><p>We were using Rest Assured to execute REST calls in our integration tests. And it started to offer a Kotlin DSL. Let’s compare two examples:</p><p>Rest Assured in Java (example from <a href="https://code.haleby.se/2019/09/06/rest-assured-in-kotlin/">code.haleby.se</a>):</p><pre>List cities = given()<br>  .queryParam(&quot;deleted&quot;, false)<br>  .when()<br>  .get(&quot;/users&quot;)<br>  .then()<br>  .statusCode(200)<br>  .body(&quot;size()&quot;, is(3))<br>  .body(&quot;name.any { it == &#39;Ervin Howell&#39; }&quot;, is(true))<br>  .body(&quot;find { user -&gt; user.username == &#39;Antonette&#39; }.company.name&quot;, equalTo(&quot;Romaguera-Jacobson&quot;)<br>  .extract()<br>  .path(&quot;address.city&quot;);</pre><p>Rest Assured using the Kotlin DSL (another example from <a href="https://code.haleby.se/2019/09/06/rest-assured-in-kotlin/">code.haleby.se</a>):</p><pre>val cities : List = Given {<br>        queryParam(&quot;deleted&quot;, false)<br>} When {<br>	get(&quot;/users&quot;)<br>} Then {<br>	statusCode(200)<br>	body(&quot;size()&quot;, is(3))<br>	body(&quot;name.any { it == &#39;Ervin Howell&#39; }&quot;, is(true))<br>	body(&quot;find { user -&gt; user.username == &#39;Antonette&#39; }.company.name&quot;, equalTo(&quot;Romaguera-Jacobson&quot;))<br>} Extract {<br>	path(&quot;address.city&quot;)<br>}</pre><p>You might think that syntax sugar like this doesn’t matter that much, but it can simplify long chains of methods. Especially when <a href="https://www.geeksforgeeks.org/method-chaining-in-java-with-examples/">method chaining</a> is used to create complex database queries.</p><h3>Querying databases for a living</h3><p>For the assignment that I had to do for my last job interview I needed to build a REST API with CRUD functionality (with some tricky things that had to be solved with transactions).</p><p>I implemented the assignment using plain SQL with the Spring JdbcClient. The interviewer liked my SQL knowledge but asked why I didn’t use an ORM like JPA / Hibernate. In my case it was because I personally like to stay as close to SQL as possible when doing queries, and JPA tries to abstract it away. He said that they used <a href="https://github.com/JetBrains/Exposed">the Exposed framework</a> that provided a nice Kotlin DSL to write SQL queries. I had not heard of Exposed before. I did know jOOQ had something similar, but never used it.</p><p>I got hired. After working with it I think the Exposed library is a great example of how the Kotlin DSL feature can make code easier. It felt more natural than working with Hibernate at earlier jobs.</p><p>Whether Exposed is better than using the JdbcClient, JPA / Hibernate and <a href="https://www.jooq.org/">jOOQ</a> depends on your needs. I will show how to build simple CRUD functionality with all four approaches, so you can compare and decide for yourself!</p><h3>What are we going to build?</h3><p>I’m trying to keep it as simple as possible. An API with CRUD (Create Read Update Delete) functionality for storing “persons”. I will insert some known football players as test data.</p><p>Technically we will be using:</p><ul><li>Kotlin</li><li>Gradle (with Kotlin DSL)</li><li>Spring boot</li><li>The H2 in-memory database</li><li>JUnit with Rest Assured</li><li>Spring JdbcClient</li><li>JPA (Hibernate)</li><li><a href="https://github.com/JetBrains/Exposed">Jetbrains Exposed</a></li><li><a href="https://www.jooq.org/">JOOQ</a></li></ul><p>I drew a picture of what our example will look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/1*ZCUeYag3wgrR1TEch6GsgA.png" /></figure><p>This is quite a bit of logic already. So here are some things I’m leaving out of this blog post because they make our “simple” example too complicated:</p><ul><li>Authentication</li><li>Validation</li><li>Joins (dynamic WHERE clauses are a good enough example to demonstrate how query builder libraries work)</li><li>Hexagonal / Ports and Adapters architecture (I don’t want to define my model multiple times and write lots of mappers)</li></ul><h3>Using the JdbcClient from the Spring boot starter</h3><p>Before you learn any other SQL builder or ORM you should probably start with the <strong>JdbcClient</strong> from the <strong>spring-boot-starter-jdbc</strong> module.</p><p>Let’s start by generating a project with the Spring Initializer:</p><ul><li>Go to <a href="https://start.spring.io/">https://start.spring.io</a></li><li>Select <strong>Gradle — Kotlin</strong> as project (why would anyone choose Groovy?).</li><li>Select <strong>Kotlin</strong> as language.</li><li>Enter whatever package name you want to use and name your project.</li><li>Click <strong>Add dependencies</strong> on the right and add <strong>Spring Data JDBC</strong> and the <strong>H2 Database</strong>.</li><li>Unzip it on your local machine and open the project with IntelliJ or whatever IDE you use.</li></ul><p>In the service we will build we will:</p><ul><li>Use jackson for JSON parsing.</li><li>Use the springdoc library generate an OpenAPI spec and to have a nice page to test our rest calls easily.</li><li>Use Rest Assured with the fancy Kotlin syntax.</li><li>The <a href="https://github.com/oshai/kotlin-logging">kotlin-logging</a> library because the internet told me that this was the easiest way to set up logging.</li></ul><p>In order to use these things we need to add a few dependencies to the <strong>build.gradle.kts</strong>:</p><pre>implementation(&quot;org.springframework.boot:spring-boot-starter-web&quot;)<br> implementation(&quot;com.fasterxml.jackson.module:jackson-module-kotlin&quot;)<br> implementation(&quot;org.springframework.boot:spring-boot-starter-jdbc&quot;)<br> implementation(&quot;org.jetbrains.kotlin:kotlin-reflect&quot;)<br> // https://springdoc.org/#general-overview<br> implementation(&quot;org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0&quot;)<br> implementation(&quot;io.github.oshai:kotlin-logging-jvm:7.0.0&quot;)<br><br> runtimeOnly(&quot;com.h2database:h2&quot;)<br> testImplementation(&quot;org.springframework.boot:spring-boot-starter-test&quot;)<br> testImplementation(&quot;org.jetbrains.kotlin:kotlin-test-junit5&quot;)<br> testRuntimeOnly(&quot;org.junit.platform:junit-platform-launcher&quot;)<br> testImplementation (&quot;io.rest-assured:rest-assured:5.5.0&quot;)<br> testImplementation (&quot;io.rest-assured:kotlin-extensions:5.5.0&quot;)</pre><p>You can now build your project using your IDE or on the command line with:</p><pre># This should only be needed once<br>chmod +x gradlew<br><br>./gradlew build</pre><h3>Configure the H2 connection</h3><p>Add the following properties to your <strong>application.properties</strong> file:</p><pre>spring.application.name=ways-to-crud<br>spring.datasource.url=jdbc:h2:mem:testdb<br>spring.datasource.driverClassName=org.h2.Driver<br>spring.datasource.username=sa<br>spring.datasource.password=password<br><br># Without this stupid line the Springdoc UI doesn&#39;t work.<br>server.servlet.context-path=/</pre><p>Create a <strong>schema.sql</strong> file in your <strong>src/main/resources</strong> folder with the following SQL to build a table to store “PERSONS”:</p><pre>CREATE TABLE IF NOT EXISTS PERSON(<br>    ID INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,<br>    FULL_NAME VARCHAR(50) NOT NULL,<br>    DATE_OF_BIRTH DATE NOT NULL,<br>    HEIGHT_IN_METERS DOUBLE NOT NULL<br>);</pre><p>This creates a table in which we can store… persons! Now we have to add some persons to this table. Create a <strong>data.sql</strong> file in your <strong>src/main/resources </strong>folder with the following SQL:</p><pre>INSERT INTO PERSON(FULL_NAME, DATE_OF_BIRTH, HEIGHT_IN_METERS) VALUES (&#39;Lionel Messi&#39;, DATE &#39;1987-06-24&#39;, 1.70);<br>INSERT INTO PERSON(FULL_NAME, DATE_OF_BIRTH, HEIGHT_IN_METERS) VALUES (&#39;Christiano Ronaldo&#39;, DATE &#39;1985-02-05&#39;, 1.87);</pre><p>The above code inserts two known football players to our person table.</p><h3>Creating a model</h3><p>Create a package called <strong>model</strong> with a <strong>Persons.kt</strong> file with two data classes:</p><pre>package net.leejjon.crud.model<br><br>import java.time.LocalDate<br><br>data class Persons(<br>    val persons: List&lt;Person&gt;<br>)<br><br>data class Person(<br>    val id: Int,<br>    val fullName: String,<br>    val dateOfBirth: LocalDate,<br>    val heightInMeters: Double<br>)<br><br>data class NewPerson(<br>    // No ID because that cannot be changed as ID&#39;s are generated by the db.<br>    val fullName: String,<br>    val dateOfBirth: LocalDate,<br>    val heightInMeters: Double<br>)</pre><h3>Creating a database service</h3><p>Create a package called <strong>database</strong> with a <strong>PersonRowMapper.kt</strong> file:</p><pre>package net.leejjon.crud.database<br><br>import net.leejjon.crud.model.Person<br>import org.springframework.jdbc.core.RowMapper<br>import java.sql.ResultSet<br><br>class PersonRowMapper : RowMapper&lt;Person&gt; {<br>    override fun mapRow(rs: ResultSet, rowNum: Int): Person {<br>        return Person(<br>            rs.getInt(&quot;ID&quot;),<br>            rs.getString(&quot;FULL_NAME&quot;),<br>            rs.getDate(&quot;DATE_OF_BIRTH&quot;).toLocalDate(),<br>            rs.getDouble(&quot;HEIGHT_IN_METERS&quot;)<br>        )<br>    }<br>}</pre><p>Create a file called <strong>DbService.kt</strong> in the <strong>database</strong> package:</p><pre>package net.leejjon.crud.database<br><br>import io.github.oshai.kotlinlogging.KotlinLogging<br>import net.leejjon.crud.model.NewPerson<br>import net.leejjon.crud.model.Person<br>import org.springframework.http.HttpStatusCode<br>import org.springframework.jdbc.core.simple.JdbcClient<br>import org.springframework.jdbc.support.GeneratedKeyHolder<br>import org.springframework.jdbc.support.KeyHolder<br>import org.springframework.stereotype.Service<br>import org.springframework.web.server.ResponseStatusException<br>import java.time.LocalDate<br><br>@Service<br>class DbService(<br>    private val jdbcClient: JdbcClient<br>) {<br>    private val logger = KotlinLogging.logger {}<br>    fun getPersonsFromDb(): List&lt;Person&gt; =<br>        jdbcClient.sql(&quot;SELECT * FROM PERSON&quot;)<br>            .query(PersonRowMapper())<br>            .list()<br><br>    fun getPerson(id: Int): java.util.Optional&lt;Person&gt; =<br>        jdbcClient.sql(&quot;SELECT * FROM PERSON WHERE ID = ?&quot;)<br>            .params(id)<br>            .query(PersonRowMapper())<br>            .optional()<br><br>    fun createPerson(person: NewPerson): Person? {<br>        val keyHolder: KeyHolder = GeneratedKeyHolder()<br>        val update =<br>            jdbcClient.sql(&quot;INSERT INTO PERSON(FULL_NAME, DATE_OF_BIRTH, HEIGHT_IN_METERS) VALUES (?, ?, ?)&quot;)<br>                .params(person.fullName, person.dateOfBirth, person.heightInMeters)<br>                .update(keyHolder)<br><br>        if (update == 1) {<br>            val potentiallyCreatedPerson = getPerson(keyHolder.getKeyAs(Integer::class.java)?.toInt()!!)<br><br>            return if (potentiallyCreatedPerson.isPresent) {<br>                potentiallyCreatedPerson.get()<br>            } else {<br>                throw ResponseStatusException(HttpStatusCode.valueOf(404))<br>            }<br>        } else {<br>            logger.error { &quot;Unable to create person $person&quot; }<br>            throw ResponseStatusException(HttpStatusCode.valueOf(500))<br>        }<br>    }<br><br>    fun deletePerson(id: Int) {<br>        val update = try {<br>            jdbcClient.sql(&quot;DELETE FROM PERSON WHERE ID = ?&quot;)<br>                .params(id)<br>                .update()<br>        } catch (e: Exception) {<br>            logger.error(e) { &quot;Unable to delete person due to error with the query or connection&quot; }<br>            throw ResponseStatusException(HttpStatusCode.valueOf(500))<br>        }<br>        if (update == 1) {<br>            return<br>        } else {<br>            logger.error { &quot;Could not find person with id $id&quot; }<br>            throw ResponseStatusException(HttpStatusCode.valueOf(404))<br>        }<br>    }<br><br>    fun updatePerson(person: Person): Person {<br>        val update = try {<br>            jdbcClient.sql(&quot;UPDATE PERSON SET FULL_NAME = ?, DATE_OF_BIRTH = ?, HEIGHT_IN_METERS = ? WHERE ID = ?&quot;)<br>                .params(person.fullName, person.dateOfBirth, person.heightInMeters, person.id)<br>                .update()<br>        } catch (e: Exception) {<br>            logger.error(e) { &quot;Unable to delete person due to error with the query or connection&quot; }<br>            throw ResponseStatusException(HttpStatusCode.valueOf(500))<br>        }<br>        if (update == 1) {<br>            val personFromDb = getPerson(person.id)<br>            if (personFromDb.isPresent) {<br>                return personFromDb.get()<br>            } else {<br>                logger.error { &quot;Could not update person with id $person.id&quot; }<br>                throw ResponseStatusException(HttpStatusCode.valueOf(404))<br>            }<br>        } else {<br>            logger.error { &quot;Could not update person with id $person.id&quot; }<br>            throw ResponseStatusException(HttpStatusCode.valueOf(404))<br>        }<br>    }<br>}</pre><p>You can see I am just putting SQL queries in the JdbcClient and have question marks as placeholders for variables.</p><h3>Adding the REST API</h3><p>Create package <strong>controllers</strong> and create a file named <strong>PersonController.kt </strong>in it:</p><pre>package net.leejjon.crud.controllers<br><br>import io.swagger.v3.oas.annotations.responses.ApiResponse<br>import io.swagger.v3.oas.annotations.responses.ApiResponses<br>import net.leejjon.crud.database.DbService<br>import net.leejjon.crud.model.NewPerson<br>import net.leejjon.crud.model.Person<br>import net.leejjon.crud.model.Persons<br>import org.springframework.http.ResponseEntity<br>import org.springframework.web.bind.annotation.DeleteMapping<br>import org.springframework.web.bind.annotation.GetMapping<br>import org.springframework.web.bind.annotation.PathVariable<br>import org.springframework.web.bind.annotation.PostMapping<br>import org.springframework.web.bind.annotation.PutMapping<br>import org.springframework.web.bind.annotation.RequestBody<br>import org.springframework.web.bind.annotation.RestController<br><br>@RestController(&quot;/v1&quot;)<br>class PersonController(<br>    private val dbService: DbService,<br>) {<br>    @GetMapping(&quot;/v1/persons&quot;)<br>    fun listPersons(): ResponseEntity&lt;Persons&gt; {<br>        return ResponseEntity.ok(Persons(dbService.getPersonsFromDb()))<br>    }<br><br>    @ApiResponses(value =<br>        [<br>            ApiResponse(responseCode = &quot;200&quot;),<br>            ApiResponse(responseCode = &quot;404&quot;, description = &quot;User not found&quot;),<br>            ApiResponse(responseCode = &quot;500&quot;, description = &quot;Internal Server Error&quot;)<br>        ]<br>    )<br>    @GetMapping(&quot;/v1/persons/{id}&quot;)<br>    fun getPerson(<br>        @PathVariable<br>        id: Int<br>    ): ResponseEntity&lt;Person&gt; {<br>        val potentiallyExistingPerson = dbService.getPerson(id)<br>        return if (potentiallyExistingPerson.isPresent) {<br>            ResponseEntity.ok(potentiallyExistingPerson.get())<br>        } else {<br>            ResponseEntity.notFound().build()<br>        }<br>    }<br><br>    @ApiResponses(value =<br>        [<br>            ApiResponse(responseCode = &quot;200&quot;),<br>            ApiResponse(responseCode = &quot;500&quot;, description = &quot;Internal Server Error&quot;)<br>        ]<br>    )<br>    @PostMapping(&quot;/v1/persons&quot;)<br>    fun createPerson(@RequestBody person: NewPerson): ResponseEntity&lt;Person&gt; {<br>        val createdPerson = dbService.createPerson(person)<br>        return ResponseEntity.ok().body(createdPerson)<br>    }<br><br>    @ApiResponses(value =<br>        [<br>            ApiResponse(responseCode = &quot;200&quot;),<br>            ApiResponse(responseCode = &quot;404&quot;, description = &quot;User not found&quot;),<br>            ApiResponse(responseCode = &quot;500&quot;, description = &quot;Internal Server Error&quot;)<br>        ]<br>    )<br>    @DeleteMapping(&quot;/v1/persons/{id}&quot;)<br>    fun deletePerson(<br>        @PathVariable id: Int<br>    ) {<br>        dbService.deletePerson(id)<br>    }<br><br>    @PutMapping(&quot;/v1/persons&quot;)<br>    fun updatePerson(<br>        @RequestBody person: Person<br>    ): ResponseEntity&lt;Person&gt; = ResponseEntity.ok(dbService.updatePerson(person))<br><br>    @PatchMapping(&quot;/v1/persons/{id}&quot;)<br>    fun updatePerson(@PathVariable id: Int, @RequestBody updatedFields: Map&lt;String, Any?&gt;): ResponseEntity&lt;Person&gt; {<br>        val updatedPerson = dbService.updatePersonAttributes(id, updatedFields)<br>        return ResponseEntity.ok(updatedPerson)<br>    }<br>}</pre><h3>Testing the endpoint manually</h3><p>You can run the spring boot application now in your IDE or via the command line:</p><pre>./gradlew bootRun</pre><p>Since we added the Springdocs dependency, we can open the Swagger UI:</p><p><a href="http://localhost:8080/swagger-ui/index.html">http://localhost:8080/swagger-ui/index.html</a></p><p>You should be able to do the calls from that interface.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xufjcnMkHpcOXeAWRjoAtA.png" /></figure><h3>Testing the endpoints automatically</h3><p>Let’s be good citizens and write some tests. Normally I start with unit tests. But with CRUD functionality on a SQL database I prefer using integration tests, because the logic is mostly in the SQL queries. If you mock the JdbcClient there is little logic that we would actually test. And if we would ever switch to other methods of database querying (like Hibernate or Jetbrains Exposed), we would have to completely rewrite our unit tests.</p><p>Create a file called <strong>PersonsApiIntegrationTests.kt</strong> in the src/test/kotlin folder and copy and paste these beautiful tests that I wrote with rest assured:</p><pre>package net.leejjon.crud<br><br>import io.restassured.http.ContentType<br>import io.restassured.module.kotlin.extensions.Extract<br>import org.junit.jupiter.api.Test<br>import io.restassured.module.kotlin.extensions.Given<br>import io.restassured.module.kotlin.extensions.Then<br>import io.restassured.module.kotlin.extensions.When<br>import io.restassured.specification.RequestSpecification<br>import net.leejjon.crud.model.Person<br>import net.leejjon.crud.model.Persons<br>import org.assertj.core.api.Assertions.assertThat<br>import org.junit.jupiter.api.BeforeEach<br>import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase<br>import org.springframework.boot.test.context.SpringBootTest<br>import org.springframework.boot.test.web.server.LocalServerPort<br>import org.springframework.test.annotation.DirtiesContext<br>import java.time.LocalDate<br><br>@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = [WaysToCrudApplication::class])<br>// Thank stackoverflow for telling me to put these annotations: https://stackoverflow.com/questions/34617152/how-to-re-create-database-before-each-test-in-spring<br>@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)<br>@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY)<br>class PersonsApiIntegrationTests {<br><br>    private lateinit var requestSpecification: RequestSpecification<br><br>    @LocalServerPort<br>    private val localPort: Int = 0<br><br>    @BeforeEach<br>    fun setup() {<br>        requestSpecification =<br>            Given {<br>                baseUri(&quot;http://localhost&quot;)<br>                port(localPort)<br>                log().all()<br>                response().log().all()<br>                contentType(ContentType.JSON)<br>                accept(ContentType.JSON)<br>            }<br>    }<br><br><br>    @Test<br>    fun `Verify that the GET request on the v1 persons endpoint returns Messi and Ronaldo`() {<br>        val response = Given {<br>            spec(requestSpecification)<br>        } When {<br>            get(&quot;/v1/persons&quot;)<br>        } Then {<br>            statusCode(200)<br>        } Extract {<br>            body().`as`(Persons::class.java)<br>        }<br><br>        assertThat(response.persons).hasSize(2)<br>        assertMessi(response.persons.first())<br>        assertRonaldo(response.persons.last())<br>    }<br><br>    @Test<br>    fun `Verify that the GET request on the v1 single person returns Messi`() {<br>        val response = Given {<br>            spec(requestSpecification)<br>        } When {<br>            get(&quot;/v1/persons/1&quot;)<br>        } Then {<br>            statusCode(200)<br>        } Extract {<br>            body().`as`(Person::class.java)<br>        }<br><br>        assertMessi(response)<br>    }<br><br>    @Test<br>    fun `Verify that the POST request on the v1 persons endpoint stores the user and returns 200`() {<br>        val response = Given {<br>            spec(requestSpecification)<br>        } When {<br>            body(<br>                &quot;&quot;&quot;{<br>                  &quot;fullName&quot;: &quot;$NEYMAR_NAME&quot;,<br>                  &quot;dateOfBirth&quot;: &quot;$NEYMAR_DATE_OF_BIRTH&quot;,<br>                  &quot;heightInMeters&quot;: $NEYMAR_HEIGHT<br>                }&quot;&quot;&quot;<br>            )<br>            post(&quot;/v1/persons&quot;)<br>        } Then {<br>            statusCode(200)<br>        } Extract {<br>            body().`as`(Person::class.java)<br>        }<br><br>        assertNeymar(response)<br>    }<br><br>    @Test<br>    fun `Verify that the DELETE request on the v1 persons endpoint deletes the user and returns 200`() {<br>        Given {<br>            spec(requestSpecification)<br>        } When {<br>            delete(&quot;/v1/persons/2&quot;)<br>        } Then {<br>            statusCode(200)<br>        }<br><br>        // Verify if there is one less person<br>        val response = Given {<br>            spec(requestSpecification)<br>        } When {<br>            get(&quot;/v1/persons&quot;)<br>        } Then {<br>            statusCode(200)<br>        } Extract {<br>            body().`as`(Persons::class.java)<br>        }<br><br>        assertThat(response.persons).hasSize(1)<br>        assertMessi(response.persons.first())<br>    }<br><br>    @Test<br>    fun `Verify that the PUT request on the v1 persons endpoint updates the user and returns 200`() {<br>        val response = Given {<br>            spec(requestSpecification)<br>        } When {<br>            // Update ronaldo to neymar<br>            body(&quot;&quot;&quot;<br>                {<br>                  &quot;id&quot;: &quot;2&quot;,<br>                  &quot;fullName&quot;: &quot;$NEYMAR_NAME&quot;,<br>                  &quot;dateOfBirth&quot;: &quot;$NEYMAR_DATE_OF_BIRTH&quot;,<br>                  &quot;heightInMeters&quot;: $NEYMAR_HEIGHT<br>                }<br>            &quot;&quot;&quot;.trimIndent())<br>            put(&quot;/v1/persons&quot;)<br>        } Then {<br>            statusCode(200)<br>        } Extract {<br>            body().`as`(Person::class.java)<br>        }<br>        assertNeymar(response, 2)<br>    }<br><br>    @Test<br>    fun `Verify that the PATCH request on the v1 persons endpoint only updates the height if the request contains just the height`() {<br>        val response = Given {<br>            spec(requestSpecification)<br>        } When {<br>            // Let&#39;s give Ronaldo Messi&#39;s height<br>            body(&quot;&quot;&quot;<br>                {<br>                    &quot;heightInMeters&quot;: $MESSI_HEIGHT<br>                }<br>            &quot;&quot;&quot;.trimIndent())<br>            patch(&quot;/v1/persons/1&quot;)<br>        } Then {<br>            statusCode(200)<br>        } Extract {<br>            body().`as`(Person::class.java)<br>        }<br><br>        assertRonaldo(response, MESSI_HEIGHT)<br>    }<br><br>    private fun assertMessi(messi: Person) {<br>        assertThat(messi.fullName).isEqualTo(MESSI_NAME)<br>        assertThat(messi.heightInMeters).isEqualTo(MESSI_HEIGHT)<br>        assertThat(messi.dateOfBirth).isEqualTo(MESSI_DATE_OF_BIRTH)<br>    }<br><br>    private fun assertRonaldo(ronaldo: Person, height: Double = RONALDO_HEIGHT) {<br>        assertThat(ronaldo.fullName).isEqualTo(RONALDO_NAME)<br>        assertThat(ronaldo.heightInMeters).isEqualTo(height)<br>        assertThat(ronaldo.dateOfBirth).isEqualTo(RONALDO_DATE_OF_BIRTH)<br>    }<br><br>    private fun assertNeymar(neymar: Person, id: Int = 3) {<br>        assertThat(neymar.id).isEqualTo(id)<br>        assertThat(neymar.fullName).isEqualTo(NEYMAR_NAME)<br>        assertThat(neymar.heightInMeters).isEqualTo(NEYMAR_HEIGHT)<br>        assertThat(neymar.dateOfBirth).isEqualTo(NEYMAR_DATE_OF_BIRTH)<br>    }<br><br>    companion object {<br>        val MESSI_NAME = &quot;Lionel Messi&quot;<br>        val MESSI_HEIGHT = 1.70<br>        val MESSI_DATE_OF_BIRTH = LocalDate.of(1987, 6, 24)<br><br>        val RONALDO_NAME = &quot;Christiano Ronaldo&quot;<br>        val RONALDO_HEIGHT = 1.87<br>        val RONALDO_DATE_OF_BIRTH = LocalDate.of(1985, 2, 5)<br><br>        val NEYMAR_NAME = &quot;Neymar Júnior&quot;<br>        val NEYMAR_HEIGHT = 1.75<br>        val NEYMAR_DATE_OF_BIRTH = LocalDate.of(1992, 2, 5)<br>    }<br>}</pre><p>You can run this test from your IDE or via the command line:</p><pre>./gradlew test</pre><p>You’ll see that all tests succeed except for the test that does a PATCH request.</p><h3>What about the PATCH endpoint?</h3><p>The PUT endpoint above technically updates the person and thus counts as the U in CRUD. But it is a bit stupid that with the PUT endpoint you need to provide the entire person object, you cannot simply update one field.</p><p>In typical CRUD applications such as content management systems you need partial updates. This behavior gets awkward with the JdbcClient.</p><p>Instead of one update query you could split the following line:</p><pre>jdbcClient.sql(&quot;UPDATE PERSON SET FULL_NAME = ?, DATE_OF_BIRTH = ?, HEIGHT_IN_METERS = ? WHERE ID = ?&quot;)<br>                .params(person.fullName, person.dateOfBirth, person.heightInMeters, person.id)<br>                .update()</pre><p>Up into three conditional queries:</p><pre>if (person.name != null) {<br>    jdbcClient.sql(&quot;UPDATE PERSON SET FULL_NAME = ? WHERE ID = ?&quot;)<br>        .params(person.name, person.id)<br>        .update()<br>}<br>if (person.dateOfBirth != null) {<br>    jdbcClient.sql(&quot;UPDATE PERSON SET DATE_OF_BIRTH = ? WHERE ID = ?&quot;)<br>        .params(person.dateOfBirth, person.id)<br>        .update()<br>}<br>if (person.heightInMeters != null) {<br>    jdbcClient.sql(&quot;UPDATE PERSON SET HEIGHT_IN_METERS = ? WHERE id = ?&quot;)<br>        .params(person.heightInMeters, person.id)<br>        .update()<br>}</pre><p>This has the downside that it potentially runs three queries instead of one. You could also build a dynamic efficient query with string concatenation, which I wouldn’t recommend.</p><p>One solution that might be acceptable but inefficient, is to retrieve the entire person object and then update one field, and store it again:</p><pre>package net.leejjon.crud.database<br><br>import io.github.oshai.kotlinlogging.KotlinLogging<br>import net.leejjon.crud.model.NewPerson<br>import net.leejjon.crud.model.Person<br>import org.springframework.http.HttpStatusCode<br>import org.springframework.jdbc.core.simple.JdbcClient<br>import org.springframework.jdbc.support.GeneratedKeyHolder<br>import org.springframework.jdbc.support.KeyHolder<br>import org.springframework.stereotype.Service<br>import org.springframework.web.server.ResponseStatusException<br>import java.time.LocalDate<br>import java.time.format.DateTimeParseException<br><br>@Service<br>class DbService(<br>    private val jdbcClient: JdbcClient<br>) {<br>    private val logger = KotlinLogging.logger {}<br>   <br>    // OTHER METHODS<br><br>    fun getPerson(id: Int): java.util.Optional&lt;Person&gt; =<br>        jdbcClient.sql(&quot;SELECT * FROM PERSON WHERE ID = ?&quot;)<br>            .params(id)<br>            .query(PersonRowMapper())<br>            .optional()<br><br>    fun updatePerson(person: Person): Person {<br>        val update = try {<br>            jdbcClient.sql(&quot;UPDATE PERSON SET FULL_NAME = ?, DATE_OF_BIRTH = ?, HEIGHT_IN_METERS = ? WHERE ID = ?&quot;)<br>                .params(person.fullName, person.dateOfBirth, person.heightInMeters, person.id)<br>                .update()<br>        } catch (e: Exception) {<br>            logger.error(e) { &quot;Unable to delete person due to error with the query or connection&quot; }<br>            throw ResponseStatusException(HttpStatusCode.valueOf(500))<br>        }<br>        if (update == 1) {<br>            val personFromDb = getPerson(person.id)<br>            if (personFromDb.isPresent) {<br>                return personFromDb.get()<br>            } else {<br>                logger.error { &quot;Could not update person with id $person.id&quot; }<br>                throw ResponseStatusException(HttpStatusCode.valueOf(404))<br>            }<br>        } else {<br>            logger.error { &quot;Could not update person with id $person.id&quot; }<br>            throw ResponseStatusException(HttpStatusCode.valueOf(404))<br>        }<br>    }<br><br>    fun updatePersonAttributes(personId: Int, updatedFields: Map&lt;String, Any?&gt;): Person {<br>        val person = getPerson(personId)<br>        if (person.isPresent) {<br>            val updatedPerson = person.get().copy(<br>                fullName = updatedFields[&quot;fullName&quot;] as? String ?: person.get().fullName,<br>                dateOfBirth = parseLocalDate(updatedFields[&quot;dateOfBirth&quot;] as? String) ?: person.get().dateOfBirth,<br>                heightInMeters = updatedFields[&quot;heightInMeters&quot;] as? Double ?: person.get().heightInMeters<br>            )<br>            return updatePerson(updatedPerson)<br>        } else {<br>            logger.error { &quot;Could not update person with id $person.id&quot; }<br>            throw ResponseStatusException(HttpStatusCode.valueOf(404))<br>        }<br>    }<br><br>    private fun parseLocalDate(date: String?): LocalDate? {<br>        if (date == null) return null<br>        return try {<br>            LocalDate.parse(date)<br>        } catch (e: DateTimeParseException) {<br>            null<br>        }<br>    }<br>}</pre><p>In most cases this will be fine, but this implementation is inefficient because it does two queries, first retrieving the current data, and then an update. <a href="https://github.com/Leejjon/ways-to-crud/tree/main">The full code example can be found on GitHub</a>.</p><p>If you need efficient partial updates, I advise you to look to other solutions.</p><h3>Pros VS Cons of Spring JDBC</h3><p>Advantages of using Spring JDBC:</p><ul><li>No magic. The query you write is being executed on the database.</li></ul><p>Disadvantages of using Spring JDBC:</p><ul><li>You need to write RowMappers to put the database data into Java or Kotlin classes.</li><li>For UPDATES where you ONLY want to update a few specific fields, you need to make your query dynamic with string concatenation OR write lots of very similar queries. <em>Warning: string concatenation can lead to </em><a href="https://www.w3schools.com/sql/sql_injection.asp"><em>SQL Injection vulnerabilities</em></a><em>.</em></li><li>If you have queries that read data and you want FILTER fields on certain columns that the end user selects, you need to make your query dynamic with string concatenation OR write lots of similar queries. <em>Warning: string concatenation can lead to </em><a href="https://www.w3schools.com/sql/sql_injection.asp"><em>SQL Injection vulnerabilities</em></a><em>.</em></li></ul><h3>Less SQL, more Java with JPA (and thus Hibernate)</h3><p><a href="https://hibernate.org/">Hibernate is a library that makes it easy to map Java classes to SQL database tables)</a>. When I first used it I was excited. But then saw people without SQL knowledge do crazy stuff in Java code that could be done way more efficient in SQL. Java later introduced JPA (Java Persistence API) of which Hibernate became the default implementation in Spring.</p><p>Hibernate and JPA are a powerful tool that can solve the partial update problem we encountered when using the JdbcClient. However, when Kotlin was still relatively new there were some problems like:</p><p><a href="https://blog.kotlin-academy.com/kotlin-and-jpa-a-good-match-or-a-recipe-for-failure-e52718d93b4f">Kotlin and JPA — a good match or a recipe for failure?</a></p><p>But in 2025 it mostly works fine. Let’s update the code we have so far to use JPA. Or if you don’t want to code along, look at the <a href="https://github.com/Leejjon/ways-to-crud/tree/hibernate-jpa">hibernate-jpa branch on GitHub</a>.</p><h3>Configuring JPA</h3><p>In the <strong>build.gradle.kts:</strong></p><ul><li>Add the JPA plugin for kotlin to the plugins section:</li></ul><pre>plugins {<br> kotlin(&quot;jvm&quot;) version &quot;1.9.24&quot;<br> kotlin(&quot;plugin.spring&quot;) version &quot;1.9.24&quot;<br> kotlin(&quot;plugin.jpa&quot;) version &quot;1.9.24&quot; // You need to add this line<br> id(&quot;org.springframework.boot&quot;) version &quot;3.3.2&quot;<br> id(&quot;io.spring.dependency-management&quot;) version &quot;1.1.6&quot;<br>}</pre><ul><li>Replace the <strong>spring-boot-starter-jdbc</strong> with the <strong>spring-boot-starter-jpa</strong>:</li></ul><pre>//  implementation(&quot;org.springframework.boot:spring-boot-starter-jdbc&quot;)<br>    implementation(&quot;org.springframework.boot:spring-boot-starter-data-jpa&quot;)</pre><p>In the application.properties file, add the following properties:</p><pre># Automatically convert camelcase attributes to snakecase database columns in the PersonEntity.<br># https://stackoverflow.com/questions/50858718/how-to-make-spring-data-jpa-by-default-naming-from-camelcase-to-snake-case-u<br>spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy<br>spring.jpa.hibernate.ddl-auto=none<br>spring.jpa.defer-datasource-initialization=true<br><br></pre><p>The <strong>spring.jpa.hibernate.ddl-auto</strong> property is an interesting one, you can let Hibernate generate your tables based on your Kotlin classes. However as we already have a working database with the <strong>schema.sql</strong> script let’s disable it and keep the existing tables.</p><p>In the your main class that starts your Spring Boot Application (in my case it was named WaysToCrudApplication.kt) you should add the <strong>@EnableJPARepositories</strong> annotation:</p><pre>package net.leejjon.crud<br><br>import io.swagger.v3.oas.annotations.OpenAPIDefinition<br>import org.springframework.boot.autoconfigure.SpringBootApplication<br>import org.springframework.boot.runApplication<br>import org.springframework.data.jpa.repository.config.EnableJpaRepositories<br><br>@SpringBootApplication<br>@OpenAPIDefinition<br>@EnableJpaRepositories(<br> basePackages = [<br>  &quot;net.leejjon.crud.database&quot;<br> ]<br>)<br>class WaysToCrudApplication</pre><h3>Creating a JPA Entity object</h3><p>In the JDBC solution we had the <strong>PersonRowMapper</strong> to give back a Person object. Delete that file. We are going to create a <strong>PersonEntity.kt</strong> class:</p><pre>package net.leejjon.crud.database<br><br>import jakarta.persistence.Entity<br>import jakarta.persistence.GeneratedValue<br>import jakarta.persistence.GenerationType<br>import jakarta.persistence.Id<br>import net.leejjon.crud.model.NewPerson<br>import net.leejjon.crud.model.Person<br>import java.time.LocalDate<br><br>@Entity(name = &quot;PERSON&quot;)<br>class PersonEntity(<br>    var fullName: String,<br>    var dateOfBirth: LocalDate,<br>    var heightInMeters: Double<br>) {<br>    @Id<br>    @GeneratedValue(strategy = GenerationType.IDENTITY)<br>    val id: Int? = null<br>}<br><br>fun NewPerson.toPersonEntity() = PersonEntity(<br>    this.fullName,<br>    this.dateOfBirth,<br>    this.heightInMeters<br>)<br><br>fun Person.toPersonEntity() = PersonEntity(<br>    this.fullName,<br>    this.dateOfBirth,<br>    this.heightInMeters<br>)<br><br>fun PersonEntity.toPerson(): Person = Person(<br>    this.id!!,<br>    this.fullName,<br>    this.dateOfBirth,<br>    this.heightInMeters<br>)</pre><p>I added some extension functions to map the existing <strong>Person</strong> and <strong>NewPerson</strong> class to a <strong>PersonEntity</strong> and back.</p><h3>Creating a JPA Repository</h3><p>This is actually very simple.</p><pre>package net.leejjon.crud.database<br><br>import org.springframework.data.repository.CrudRepository<br>import org.springframework.stereotype.Repository<br><br>@Repository<br>interface PersonRepository : CrudRepository&lt;PersonEntity, Int&gt;</pre><h3>Updating the DbService</h3><p>In your <strong>DbService.kt</strong> file you need to use the <strong>PersonRepository</strong> to implement the CRUD scenarios:</p><pre>package net.leejjon.crud.database<br><br>import net.leejjon.crud.model.NewPerson<br>import net.leejjon.crud.model.Person<br>import org.springframework.stereotype.Service<br>import java.util.*<br>import kotlin.jvm.optionals.getOrNull<br><br>@Service<br>class DbService(<br>    private val personRepository: PersonRepository<br>) {<br>    fun getPersonsFromDb(): List&lt;Person&gt; =<br>        personRepository.findAll().toList().map { it.toPerson() }<br><br>    fun getPerson(id: Int): Person? =<br>        personRepository.findById(id).getOrNull()?.toPerson()<br><br>    fun createPerson(person: NewPerson): Person {<br>        return personRepository.save(person.toPersonEntity()).toPerson()<br>    }<br><br>    fun deletePerson(id: Int) {<br>        personRepository.deleteById(id)<br>    }<br><br>    fun updatePerson(person: Person): Person? {<br>        val existingPersonOptional: Optional&lt;PersonEntity&gt; = personRepository.findById(person.id)<br><br>        return if (existingPersonOptional.isPresent) {<br>            val existingPerson = existingPersonOptional.get()<br>            existingPerson.fullName = person.fullName<br>            existingPerson.dateOfBirth = person.dateOfBirth<br>            existingPerson.heightInMeters = person.heightInMeters<br>            personRepository.save(existingPerson).toPerson()<br>        } else {<br>            null<br>        }<br>    }<br>}</pre><p>As you can see there are no more SQL queries in this class. We don’t have to change anything in the integration tests (Nice!). So run them again:</p><pre>./gradlew test</pre><h3>Implementing the PATCH endpoint</h3><p>With the JDBC implementation I explained that it is not suited for writing or constructing queries. We can use the Criteria API. For that we need access to the EntityManager. Create a second repository called <strong>PersonCriteriaRepository.kt</strong> in the database package. We pass the entitymanager through the constructor:</p><pre>package net.leejjon.crud.database<br><br>import jakarta.persistence.EntityManager<br>import jakarta.persistence.PersistenceContext<br>import jakarta.transaction.Transactional<br>import org.springframework.http.HttpStatusCode<br>import org.springframework.stereotype.Repository<br>import org.springframework.web.server.ResponseStatusException<br>import java.time.LocalDate<br><br>@Repository<br>class PersonCriteriaRepository(<br>    @PersistenceContext<br>    val em: EntityManager<br>) {<br><br>    @Transactional<br>    fun updatePersonAttributes(personId: Int, updatedFields: Map&lt;String, Any?&gt;) {<br>        if (updatedFields.isNotEmpty() &amp;&amp; !updatedFields.containsValue(null)) {<br>            val cb = em.criteriaBuilder<br>            val cu = cb.createCriteriaUpdate(PersonEntity::class.java)<br>            val person = cu.from(PersonEntity::class.java)<br>            for (fieldToUpdate in updatedFields.entries.iterator()) {<br>                val castedFieldToUpdateValue = when (fieldToUpdate.key) {<br>                    &quot;fullName&quot; -&gt; fieldToUpdate.value as String<br>                    &quot;dateOfBirth&quot; -&gt; LocalDate.parse(fieldToUpdate.value as String)<br>                    &quot;heightInMeters&quot; -&gt; fieldToUpdate.value as Double<br>                    else -&gt; throw ResponseStatusException(HttpStatusCode.valueOf(400))<br>                }<br><br>                cu.set(fieldToUpdate.key, castedFieldToUpdateValue)<br>            }<br>            cu.where(cb.equal(person.get&lt;Int&gt;(&quot;id&quot;), personId))<br><br>            em.createQuery(cu).executeUpdate()<br>        } else {<br>            throw ResponseStatusException(HttpStatusCode.valueOf(400))<br>        }<br>    }<br>}</pre><p>This method constructs an update query where we dynamically update fields that are passed in the updatedFields map. The values in this map are of the type Any to support all kinds of attributes, so we cast the attribute to a class based on the attribute name before we insert it to the update criteria object.</p><p>In the <strong>DbService.kt</strong> you need to pass the new <strong>PersonCriteriaRepository.kt</strong> and add the following function:</p><pre>package net.leejjon.crud.database<br><br>import net.leejjon.crud.model.NewPerson<br>import net.leejjon.crud.model.Person<br>import org.springframework.stereotype.Service<br>import java.util.*<br>import kotlin.jvm.optionals.getOrNull<br><br>@Service<br>class DbService(<br>    private val personRepository: PersonRepository,<br>    private val personCriteriaRepository: PersonCriteriaRepository<br>) {<br>   // Other methods<br>    fun updatePersonAttributes(personId: Int, updatedFields: Map&lt;String, Any?&gt;): Person? {<br>        personCriteriaRepository.updatePersonAttributes(personId, updatedFields)<br>        val updatedPerson = personRepository.findById(personId)<br>        return if (updatedPerson.isPresent) {<br>            updatedPerson.get().toPerson()<br>        } else {<br>            null<br>        }<br>    }<br>}</pre><p>This calls the method to update the person attributes, and retrieve the entire updated person via the existing personRepository afterwards.</p><p>Now in the <strong>PersonController.kt</strong>, add the patch endpoint:</p><pre>@PatchMapping(&quot;/v1/persons/{id}&quot;)<br>fun updatePerson(@PathVariable id: Int, @RequestBody updatedFields: Map&lt;String, Any?&gt;): ResponseEntity&lt;Person&gt; {<br>    val updatedPerson = dbService.updatePersonAttributes(id, updatedFields)<br>    return if (updatedPerson == null) {<br>        ResponseEntity.notFound().build()<br>    } else {<br>        ResponseEntity.ok(updatedPerson)<br>    }<br>}</pre><p>Now the PATCH endpoint should work. Run the tests using ./gradlew test to verify if everything works.</p><p>Again, you can find the full code <a href="https://github.com/Leejjon/ways-to-crud/tree/hibernate-jpa">here</a>.</p><h3>Pros VS Cons of JPA</h3><p>Advantages of using JPA:</p><ul><li>It makes it easy to map database tables to Kotlin classes.</li><li>It provides a safer way to construct dynamic queries.</li><li>It’s a mature project.</li></ul><p>Disadvantages of using JPA:</p><ul><li>You can’t use immutable Kotlin data classes as Entity. You need to use mutable classes.</li><li>The criteria builder and it’s classes are typically Java. Not everything is immutable.</li></ul><h3>The (JetBrains) Exposed framework</h3><p><a href="https://github.com/JetBrains/Exposed">Exposed</a> calls itself an ORM framework and lightweight SQL library at the same time. The main feature here is that this is a Kotlin library, so it uses all the nice Kotlin syntax sugar.</p><p>In the <strong>build.gradle.kts</strong> add the exposed spring boot starter:</p><pre>// implementation(&quot;org.springframework.boot:spring-boot-starter-jdbc&quot;)<br>implementation(&quot;org.jetbrains.exposed:exposed-spring-boot-starter:0.58.0&quot;)<br>implementation(&quot;org.jetbrains.exposed:exposed-java-time:0.58.0&quot;)</pre><p>In your main class with the @SpringBootApplication annotation, add the <strong>@ImportAutoConfiguration</strong> like below to make Exposed work:</p><pre>package net.leejjon.crud<br><br>import io.swagger.v3.oas.annotations.OpenAPIDefinition<br>import org.jetbrains.exposed.spring.autoconfigure.ExposedAutoConfiguration<br>import org.springframework.boot.autoconfigure.ImportAutoConfiguration<br>import org.springframework.boot.autoconfigure.SpringBootApplication<br>import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration<br>import org.springframework.boot.runApplication<br><br>@SpringBootApplication<br>@OpenAPIDefinition<br>@ImportAutoConfiguration(<br>    value = [ExposedAutoConfiguration::class],<br>    exclude = [DataSourceTransactionManagerAutoConfiguration::class]<br>)<br>class WaysToCrudApplication<br><br>fun main(args: Array&lt;String&gt;) {<br>    runApplication&lt;WaysToCrudApplication&gt;(*args)<br>}</pre><p><a href="https://github.com/JetBrains/Exposed/tree/main/samples/exposed-spring">https://github.com/JetBrains/Exposed/tree/main/samples/exposed-spring</a></p><h3>The Exposed version of PersonEntity.kt</h3><p>You have to create a <strong>PersonEntity.kt</strong> file like we did with the JPA approach, but this time it looks like this:</p><pre>package net.leejjon.crud.database<br><br>import net.leejjon.crud.model.Person<br>import org.jetbrains.exposed.dao.id.IntIdTable<br>import org.jetbrains.exposed.sql.ResultRow<br>import org.jetbrains.exposed.sql.javatime.date<br><br>object PersonEntity : IntIdTable(&quot;PERSON&quot;) {<br>    val fullName = varchar(&quot;FULL_NAME&quot;, 50)<br>    val dateOfBirth = date(&quot;DATE_OF_BIRTH&quot;)<br>    val heightInMeters = double(&quot;HEIGHT_IN_METERS&quot;)<br>}<br><br>fun ResultRow.toPerson(): Person = Person(<br>    id = this[PersonEntity.id].value,<br>    fullName = this[PersonEntity.fullName],<br>    dateOfBirth = this[PersonEntity.dateOfBirth],<br>    heightInMeters = this[PersonEntity.heightInMeters]<br>)</pre><p>The <strong>DbService.kt</strong> is implemented like this:</p><pre>package net.leejjon.crud.database<br><br>import io.github.oshai.kotlinlogging.KotlinLogging<br>import net.leejjon.crud.model.NewPerson<br>import net.leejjon.crud.model.Person<br>import org.jetbrains.exposed.sql.*<br>import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq<br>import org.jetbrains.exposed.sql.transactions.transaction<br>import org.springframework.http.HttpStatusCode<br>import org.springframework.stereotype.Service<br>import org.springframework.web.server.ResponseStatusException<br>import java.time.LocalDate<br><br>@Service<br>class DbService {<br>    private val logger = KotlinLogging.logger {}<br>    fun getPersonsFromDb(): List&lt;Person&gt; = transaction {<br>        PersonEntity.selectAll().map {<br>                row -&gt; row.toPerson()<br>        }<br>    }<br><br>    fun getPerson(id: Int): Person? = transaction {<br>        PersonEntity.selectAll().where { PersonEntity.id.eq(id) }.firstOrNull()?.toPerson()<br>    }<br><br>    fun createPerson(person: NewPerson): Person? = transaction {<br>        PersonEntity.insert {<br>            it[fullName] = person.fullName<br>            it[dateOfBirth] = person.dateOfBirth<br>            it[heightInMeters] = person.heightInMeters<br>        }.resultedValues!!.firstOrNull()?.toPerson()<br>    }<br><br>    fun deletePerson(id: Int) = transaction {<br>        val update = PersonEntity.deleteWhere { PersonEntity.id.eq(id) }<br>        if (update != 1) {<br>            logger.error { &quot;Could not find person with id $id&quot; }<br>            throw ResponseStatusException(HttpStatusCode.valueOf(404))<br>        }<br>    }<br><br>    fun updatePerson(person: Person): Person = transaction {<br>        val update = PersonEntity.update({ PersonEntity.id.eq(person.id) }) {<br>            it[fullName] = person.fullName<br>            it[dateOfBirth] = person.dateOfBirth<br>            it[heightInMeters] = person.heightInMeters<br>        }<br><br>        if (update != 1) {<br>            logger.error { &quot;Could not find person with id ${person.id}&quot; }<br>            throw ResponseStatusException(HttpStatusCode.valueOf(404))<br>        } else {<br>            val updatedPersonFromDb = getPerson(person.id)<br>            if (updatedPersonFromDb != null) {<br>                return@transaction updatedPersonFromDb<br>            } else {<br>                logger.error { &quot;Could not update person with id $person.id&quot; }<br>                throw ResponseStatusException(HttpStatusCode.valueOf(500))<br>            }<br>        }<br>    }<br><br>    fun updatePersonAttributes(personId: Int, updatedFields: Map&lt;String, Any?&gt;): Person = transaction {<br>        val filterColumnAndValue = updatedFields.entries.map {<br>            return@map when (it.key) {<br>                &quot;fullName&quot; -&gt; Pair(PersonEntity.fullName, it.value as String)<br>                &quot;dateOfBirth&quot; -&gt; Pair(PersonEntity.dateOfBirth, LocalDate.parse(it.value as String))<br>                &quot;heightInMeters&quot; -&gt; Pair(PersonEntity.heightInMeters, it.value as Double)<br>                else -&gt; throw ResponseStatusException(HttpStatusCode.valueOf(400))<br>            }<br>        }<br><br>        val update = PersonEntity.update({<br>            PersonEntity.id.eq(personId)<br>        }) {<br>            filterColumnAndValue.forEach { (column, value) -&gt;<br>                it[column as Column&lt;Any&gt;] = value<br>            }<br>        }<br><br>        if (update != 1) {<br>            logger.error { &quot;Could not find person with id ${personId}&quot; }<br>            throw ResponseStatusException(HttpStatusCode.valueOf(404))<br>        } else {<br>            val updatedPersonFromDb = getPerson(personId)<br>            if (updatedPersonFromDb != null) {<br>                return@transaction updatedPersonFromDb<br>            } else {<br>                logger.error { &quot;Could not update person with id $personId&quot; }<br>                throw ResponseStatusException(HttpStatusCode.valueOf(500))<br>            }<br>        }<br>    }<br>}</pre><p>Run the <strong>PersonsApiIntegrationTests.kt</strong> file again to see that our queries created with Exposed work well.</p><p>Check out <a href="https://github.com/Leejjon/ways-to-crud/tree/exposed">the full code of the Exposed version of this CRUD service on GitHub</a>.</p><h3>Pros VS Cons of Exposed</h3><p>Advantages of using Exposed:</p><ul><li>Exposed lets you write code that is closer to SQL. No annotations that can feel like magic.</li><li>Exposed is a Kotlin library. This means classes will be immutable and it has a fancy DSL for crafting queries.</li><li>Exposed is backed by Jetbrains.</li></ul><p>Disadvantages of using Exposed:</p><ul><li>I don’t think you can use it from Java easily.</li><li>It is newer technology and therefore less mature than Jdbc and JPA.</li></ul><p>I personally had a pretty good first production experience with Exposed and I will be using Exposed in future projects.</p><h3>Java Object Oriented Querying, also known as jOOQ</h3><p>I think I learned about jOOQ on Twitter. Lukas Eder used to post weird SQL things he discovered when implementing parts of jOOQ (unfortunately I think he deleted his “X” account). I feel I just had to include jOOQ as an option in this blog post because of the following difference:</p><p>Hibernate and Exposed require you to write classes that match with database tables, jOOQ does it the other way around. You can let jOOQ take a look at either SQL files or have it connect to a running database and generate Java classes that you can use to query the database.</p><p>The documentation can be found on <a href="https://www.jooq.org/">https://www.jooq.org/</a>.</p><h3>The licensing</h3><p>You can use jOOQ for free with Open Source databases. For commercial databases it costs money. The jOOQ founder <a href="https://blog.jooq.org/with-commercial-licensing-invest-in-innovation-not-protection/">explained how their licensing works in this blog post</a>. Don’t forget to include <a href="https://www.jooq.org/doc/latest/manual/copyright/">the correct license</a> files in your project if you are going to put it online.</p><h3>Adding jOOQ to your project</h3><p>Add the org.jooq.jooq-codegen-gradle plugin to the plugins section of your <strong>build.gradle.kts</strong>:</p><pre>plugins {<br>    kotlin(&quot;jvm&quot;) version &quot;1.9.24&quot;<br>    kotlin(&quot;plugin.spring&quot;) version &quot;1.9.24&quot;<br>    id(&quot;org.springframework.boot&quot;) version &quot;3.3.2&quot;<br>    id(&quot;io.spring.dependency-management&quot;) version &quot;1.1.6&quot;<br>    id(&quot;org.jooq.jooq-codegen-gradle&quot;) version &quot;3.19.18&quot;<br>}</pre><p>You need to add a bunch of jOOQ dependencies and the <strong>spring-boot-starter-jooq</strong>:</p><pre>dependencies {<br>    implementation(&quot;org.springframework.boot:spring-boot-starter-web&quot;)<br>    implementation(&quot;com.fasterxml.jackson.module:jackson-module-kotlin&quot;)<br>    implementation(&quot;org.springframework.boot:spring-boot-starter-jooq&quot;)<br>    implementation(&quot;org.jetbrains.kotlin:kotlin-reflect&quot;)<br>    // https://springdoc.org/#general-overview<br>    implementation(&quot;org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0&quot;)<br>    implementation(&quot;io.github.oshai:kotlin-logging-jvm:7.0.0&quot;)<br>    implementation(&quot;org.jooq:jooq:3.19.18&quot;)<br>    implementation(&quot;org.jooq:jooq-meta-extensions:3.19.18&quot;)<br><br>    runtimeOnly(&quot;com.h2database:h2&quot;)<br>    jooqCodegen(&quot;org.jooq:jooq-meta-extensions:3.19.18&quot;)<br><br>    testImplementation(&quot;org.springframework.boot:spring-boot-starter-test&quot;)<br>    testImplementation(&quot;org.jetbrains.kotlin:kotlin-test-junit5&quot;)<br>    testRuntimeOnly(&quot;org.junit.platform:junit-platform-launcher&quot;)<br>    testImplementation (&quot;io.rest-assured:rest-assured:5.5.0&quot;)<br>    testImplementation (&quot;io.rest-assured:kotlin-extensions:5.5.0&quot;)<br>}</pre><h3>jOOQ code generation</h3><p>As I said earlier, jOOQ generates Java classes from the SQL definition of your tables. We have defined our database structure in the <strong>schema.sql</strong> file. We need to tell the jOOQ codegeneration plugin to use this same file. In your <strong>build.gradle.kts</strong> file add:</p><pre>jooq {<br>    configuration {<br>       generator {<br>          name = &quot;org.jooq.codegen.JavaGenerator&quot;<br>          database {<br>             name = &quot;org.jooq.meta.extensions.ddl.DDLDatabase&quot;<br>             properties {<br><br>                // Specify the location of your SQL script.<br>                // You may use ant-style file matching, e.g. /path/**/to/*.sql<br>                //<br>                // Where:<br>                // - ** matches any directory subtree<br>                // - * matches any number of characters in a directory / file name<br>                // - ? matches a single character in a directory / file name<br>                property {<br>                   key = &quot;scripts&quot;<br>                   value = &quot;src/main/resources/schema.sql&quot;<br>                }<br><br>                // The sort order of the scripts within a directory, where:<br>                //<br>                // - semantic: sorts versions, e.g. v-3.10.0 is after v-3.9.0 (default)<br>                // - alphanumeric: sorts strings, e.g. v-3.10.0 is before v-3.9.0<br>                // - flyway: sorts files the same way as flyway does<br>                // - none: doesn&#39;t sort directory contents after fetching them from the directory<br>                property {<br>                   key = &quot;sort&quot;<br>                   value = &quot;semantic&quot;<br>                }<br><br>                // The default schema for unqualified objects:<br>                //<br>                // - public: all unqualified objects are located in the PUBLIC (upper case) schema<br>                // - none: all unqualified objects are located in the default schema (default)<br>                //<br>                // This configuration can be overridden with the schema mapping feature<br>                property {<br>                   key = &quot;unqualifiedSchema&quot;<br>                   value = &quot;none&quot;<br>                }<br><br>                // The default name case for unquoted objects:<br>                //<br>                // - as_is: unquoted object names are kept unquoted<br>                // - upper: unquoted object names are turned into upper case (most databases)<br>                // - lower: unquoted object names are turned into lower case (e.g. PostgreSQL)<br>                property {<br>                   key = &quot;defaultNameCase&quot;<br>                   value = &quot;as_is&quot;<br>                }<br>             }<br>          }<br>          target {<br>             packageName = &quot;net.leejjon.crud.database.model&quot;<br>             directory = &quot;$buildDir/generated-src/jooq&quot;<br>          }<br>       }<br>    }<br>}</pre><p>We can try out the code generation by running:</p><pre>./gradlew jooqCodegen</pre><p>If succesful, you can find the generated code in your build folder:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hXceTuU2sNNNTbfZHpam4g.png" /></figure><p>To use these generated classes in your Kotlin code, we need to mark this <strong>generated-src/jooq</strong> folder as a source directory. We can do that by adding the following code in the <strong>build.gradle.kts</strong> file:</p><pre>java {<br>    toolchain {<br>       languageVersion = JavaLanguageVersion.of(21)<br>    }<br>    sourceSets[&quot;main&quot;].java {<br>       // Add the generated jOOQ source folder to the main source set<br>       srcDir(&quot;$buildDir/generated-src/jooq&quot;)<br>    }<br>}</pre><p>This should make it possible for your <strong>DbService.kt</strong> to import the generated files:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kc659v522PfSueSd3PbuGg.png" /><figcaption>Because both my Person class and the one jOOQ generated are named Person, I used an <a href="https://medium.com/@erickpranata/kotlin-import-alias-3f2831295902">import alias</a> <strong>PersonTable</strong> to be able to use it easily.</figcaption></figure><p>One more thing we should add is the following lines to the <strong>build.gradle.kts</strong> to make sure the jooqCodegen command is ran automatically before compiling the Kotlin code (otherwise everybody that clones your repo and runs ./gradlew build will have their build fail due to the jOOQ code not being generated):</p><pre>tasks.named(&quot;compileKotlin&quot;) {<br>    dependsOn(&quot;jooqCodegen&quot;) <br>}</pre><h3>Configuring jOOQ in Spring</h3><p>Before we can do anything with the imported classes, we first need to autowire the <strong>DslContext</strong> into our <strong>DbService.kt</strong>. Therefore we need to define some beans. You could put them in a class with the @Configuration annotation, however I just thew it in my application starter class called <strong>WaysToCrudApplication.kt</strong>:</p><pre>package net.leejjon.crud<br><br>import io.swagger.v3.oas.annotations.OpenAPIDefinition<br>import net.leejjon.crud.database.ExceptionTranslator<br>import org.jooq.SQLDialect<br>import org.jooq.impl.DataSourceConnectionProvider<br>import org.jooq.impl.DefaultConfiguration<br>import org.jooq.impl.DefaultDSLContext<br>import org.jooq.impl.DefaultExecuteListenerProvider<br>import org.springframework.beans.factory.annotation.Autowired<br>import org.springframework.boot.autoconfigure.SpringBootApplication<br>import org.springframework.boot.runApplication<br>import org.springframework.context.annotation.Bean<br>import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy<br>import org.springframework.transaction.annotation.EnableTransactionManagement<br>import javax.sql.DataSource<br><br>@SpringBootApplication<br>@EnableTransactionManagement<br>@OpenAPIDefinition<br>class WaysToCrudApplication {<br><br> @Autowired<br> lateinit var dataSource: DataSource<br><br> @Bean<br> fun connectionProvider(): DataSourceConnectionProvider {<br>  return DataSourceConnectionProvider(TransactionAwareDataSourceProxy(dataSource))<br> }<br><br> @Bean<br> fun exceptionTransformer(): ExceptionTranslator {<br>  return ExceptionTranslator()<br> }<br><br> @Bean<br> fun dsl(): DefaultDSLContext {<br>  return DefaultDSLContext(configuration())<br> }<br><br> @Bean<br> fun configuration(): DefaultConfiguration {<br>  val jooqConfiguration = DefaultConfiguration()<br>  jooqConfiguration.set(connectionProvider())<br>  jooqConfiguration.set(DefaultExecuteListenerProvider(exceptionTransformer()))<br><br>  val dialect = SQLDialect.valueOf(&quot;H2&quot;)<br>  jooqConfiguration.set(dialect)<br><br>  return jooqConfiguration<br> }<br>}<br><br>fun main(args: Array&lt;String&gt;) {<br> runApplication&lt;WaysToCrudApplication&gt;(*args)<br>}</pre><p>With the jOOQ configuration set up, update the <strong>DbService.kt</strong> file:</p><pre>package net.leejjon.crud.database<br><br>import io.github.oshai.kotlinlogging.KotlinLogging<br>import net.leejjon.crud.model.NewPerson<br>import net.leejjon.crud.model.Person<br>import org.jooq.DSLContext<br>import org.springframework.beans.factory.annotation.Autowired<br><br>import org.springframework.http.HttpStatusCode<br>import org.springframework.stereotype.Service<br>import org.springframework.transaction.annotation.Transactional<br>import org.springframework.web.server.ResponseStatusException<br>import java.time.LocalDate<br><br>import net.leejjon.crud.database.model.tables.Person as PersonTable<br><br>@Service<br>class DbService {<br>    private val logger = KotlinLogging.logger {}<br><br>    @Autowired<br>    lateinit var dsl: DSLContext<br><br>    fun getPersonsFromDb(): List&lt;Person&gt; {<br>        val persons = dsl.select().from(PersonTable.PERSON).fetch()<br>        return persons.map {<br>            Person(<br>                it.get(PersonTable.PERSON.ID),<br>                it.get(PersonTable.PERSON.FULL_NAME),<br>                it.get(PersonTable.PERSON.DATE_OF_BIRTH),<br>                it.get(PersonTable.PERSON.HEIGHT_IN_METERS)<br>            )<br>        }<br>    }<br><br>    fun getPerson(id: Int): Person? {<br>        val person = dsl.select().from(PersonTable.PERSON)<br>            .where(PersonTable.PERSON.ID.eq(id))<br>            .fetchOne()<br>        return if (person != null) Person(<br>            person.get(PersonTable.PERSON.ID),<br>            person.get(PersonTable.PERSON.FULL_NAME),<br>            person.get(PersonTable.PERSON.DATE_OF_BIRTH),<br>            person.get(PersonTable.PERSON.HEIGHT_IN_METERS)<br>        ) else null<br>    }<br><br>    @Transactional<br>    fun createPerson(person: NewPerson): Person? {<br>        val createdPerson = dsl.insertInto(PersonTable.PERSON)<br>            .set(PersonTable.PERSON.FULL_NAME, person.fullName)<br>            .set(PersonTable.PERSON.DATE_OF_BIRTH, person.dateOfBirth)<br>            .set(PersonTable.PERSON.HEIGHT_IN_METERS, person.heightInMeters)<br>            .returningResult(PersonTable.PERSON.ID)<br>            .fetchOne()<br>        if (createdPerson != null) {<br>            val createdPersonId = createdPerson.into(Integer::class.java)<br>            return getPerson(createdPersonId.toInt())<br>        } else {<br>            logger.error { &quot;Unable to create person $person&quot; }<br>            throw ResponseStatusException(HttpStatusCode.valueOf(500))<br>        }<br>    }<br><br>    fun deletePerson(id: Int) {<br>        val deletedRecord = dsl.delete(PersonTable.PERSON).where(PersonTable.PERSON.ID.eq(id)).execute()<br><br>        if (deletedRecord == 0) {<br>            logger.error { &quot;Could not find person with id $id&quot; }<br>            throw ResponseStatusException(HttpStatusCode.valueOf(404))<br>        }<br>    }<br><br>    @Transactional<br>    fun updatePerson(person: Person): Person {<br>        val updatedRecords = dsl.update(PersonTable.PERSON)<br>            .set(PersonTable.PERSON.FULL_NAME, person.fullName)<br>            .set(PersonTable.PERSON.DATE_OF_BIRTH, person.dateOfBirth)<br>            .set(PersonTable.PERSON.HEIGHT_IN_METERS, person.heightInMeters)<br>            .where(PersonTable.PERSON.ID.eq(person.id))<br>            .execute()<br>        if (updatedRecords == 1) {<br>            return getPerson(person.id)!!<br>        } else {<br>            logger.error { &quot;Could not find person with id ${person.id}&quot; }<br>            throw ResponseStatusException(HttpStatusCode.valueOf(404))<br>        }<br>    }<br><br>    @Transactional<br>    fun updatePersonAttributes(personId: Int, updatedFields: Map&lt;String, Any?&gt;): Person {<br>        // Stack overflow told me to use the UpdateSetStep.set(Record) function<br>        // https://stackoverflow.com/questions/42923028/jooq-dynamic-set<br>        val recordWithWhereStatements = dsl.newRecord(PersonTable.PERSON)<br><br>        for (fieldToUpdate in updatedFields.entries.iterator()) {<br>            when (fieldToUpdate.key) {<br>                &quot;fullName&quot; -&gt; recordWithWhereStatements.set(PersonTable.PERSON.FULL_NAME, fieldToUpdate.value as String)<br>                &quot;dateOfBirth&quot; -&gt; recordWithWhereStatements.set(<br>                    PersonTable.PERSON.DATE_OF_BIRTH,<br>                    LocalDate.parse(fieldToUpdate.value as String)<br>                )<br><br>                &quot;heightInMeters&quot; -&gt; recordWithWhereStatements.set(<br>                    PersonTable.PERSON.HEIGHT_IN_METERS,<br>                    fieldToUpdate.value as Double<br>                )<br><br>                else -&gt; throw ResponseStatusException(HttpStatusCode.valueOf(400))<br>            }<br>        }<br><br>        val updatedRecords =<br>            dsl.update(PersonTable.PERSON)<br>                .set(recordWithWhereStatements)<br>                .where(PersonTable.PERSON.ID.eq(personId))<br>                .execute()<br>        if (updatedRecords == 1) {<br>            return getPerson(personId)!!<br>        } else {<br>            logger.error { &quot;Could not find person with id ${personId}&quot; }<br>            throw ResponseStatusException(HttpStatusCode.valueOf(404))<br>        }<br>    }<br>}</pre><p>As you can see it looks a lot like SQL. Run the integration tests again and everything should work. The final jOOQ code can be found <a href="https://github.com/Leejjon/ways-to-crud/tree/jooq">here on GitHub</a>.</p><h3>Pros and Cons of jOOQ</h3><p>Advantages of using jOOQ:</p><ul><li>It reads like SQL queries. I like this DSL.</li><li>It works with Java.</li><li>You can generate models easily from existing databases.</li><li>Open source (except for the tests)</li></ul><p>Disadvantages of using jOOQ:</p><ul><li>If you want to use it together with commercial databases, <a href="https://www.jooq.org/legal/licensing">you need to buy a license</a>.</li></ul><h3>Follow me for more!</h3><p>The source code of all four approaches can <a href="https://github.com/Leejjon/ways-to-crud/">be found in this GitHub project</a> (there is a branch per implementation). If you enjoyed this blog post, please follow me on <a href="https://leejjon.medium.com/">Medium</a>, <a href="https://twitter.com/Leejjon_net">X</a> or <a href="https://www.linkedin.com/in/leon-liefting-4b511a29/">LinkedIN</a>.</p><p>I recently reached 100+ followers on Medium. Thank you all for that. I only post a few articles per year and I do take my time to put good working code examples along with my posts (unlike the many people who spam Medium with AI generated content and hope to generate passive income out of it).</p><p>If there are any mistakes in my blog, let me know!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2706e58922dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Streaming with Remix on Google Cloud Platform (App Engine Flex & Cloud Run)]]></title>
            <link>https://leejjon.medium.com/streaming-with-remix-on-google-cloud-platform-app-engine-flex-cloud-run-4e3e16b8c68d?source=rss-e00e4e1c1655------2</link>
            <guid isPermaLink="false">https://medium.com/p/4e3e16b8c68d</guid>
            <category><![CDATA[remix]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[streaming]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <dc:creator><![CDATA[Leejjon]]></dc:creator>
            <pubDate>Sun, 09 Jun 2024 22:54:40 GMT</pubDate>
            <atom:updated>2024-06-13T20:40:03.583Z</atom:updated>
            <content:encoded><![CDATA[<p>There are many blog posts that talk about the benefits that HTTP Streaming brings in React.js apps. But I have not seen one show how to get a Remix app to stream responses on Google Cloud Platform. How do you know if your host even supports streaming? How do you see that your deployed app is streaming instead of sending a buffered request? If you’re looking for that information, read on!</p><h3>Why am I researching this?</h3><p>After I figured out how to migrate my React App that was created with the Create-React-app tool to a Remix app (which I wrote about in <a href="https://medium.com/@leejjon/migrating-an-create-react-app-project-that-uses-react-router-to-remix-b52b4b6f6456">this blog post</a>), I ran into a “problem” that forced me to reconsider what service I used to host my React app.</p><p>I always want to be able to use the latest and greatest features. One of <a href="https://react.dev/blog/2022/03/29/react-v18">the new features in React 18</a> was that the React frameworks could use HTTP streaming on the server to send data to the client and use Suspense to use this data in your React components (<a href="https://blog.logrocket.com/streaming-ssr-with-react-18/">here’s a good link</a> if you don’t fully follow what I mean in the line above).</p><p><a href="https://remix.run/">The Remix framework</a> uses HTTP steaming to prevent request waterfalls that Single Page Apps tend to have when fetching data. <a href="https://www.youtube.com/watch?v=95B8mnhzoCM">This older “When to Fetch” talk</a> from <a href="https://x.com/ryanflorence">Ryan Florence</a> (he works on React Router and Remix) does an amazing job on explaining this request waterfall problem and how Remix can simplify data loading and makes it very fast.</p><p>That talk really sold me on using Remix. But this warning in <a href="https://remix.run/docs/en/main/guides/streaming">the streaming documentation from Remix</a> made me wonder if my host even supported it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/787/1*gPWSj2UxCqD7Y3uvzT_6mA.png" /></figure><h3>How do you know which hosting providers support HTTP streaming?</h3><p>There isn’t really a list of supported platforms in the Remix documentation. Which makes sense, there are an insane amount of possible hosting providers around the world. It’s not their job to keep track of who supports HTTP streaming and who doesn’t. You will have to read the documentation of your hosting provider. Or if it is unclear, test it!</p><p>My hosting provider is Google Cloud Platform, and I’m running my React application on the Node.js runtime of App Engine Standard.</p><p>Unfortunately, <a href="https://cloud.google.com/appengine/docs/standard/how-requests-are-handled?tab=node.js#top">the App Engine Standard docs</a> write:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/895/1*dAmWbd5Ta4S8CXGnQpIgvQ.png" /></figure><h3>My alternatives in Google Cloud Platform</h3><p>Call me a vendor locked in loser if you want, but I’m looking to stay on GCP if possible. Otherwise I would have to migrate my Google Datastore database and domains to something else and I’m used to the nice Logging and CI/CD tools that GCP offers out of the box.</p><ul><li>App Engine Flexible is a more custom version of App Engine. <a href="https://cloud.google.com/appengine/docs/flexible/how-requests-are-handled?tab=node.js#x-accel-buffering">Their documentation does mention a setting</a> that could make it “send bytes directly to the client”. After testing I found it does do HTTP streaming. This is great because I can run App Engine Flex services next to my App Engine Standard services behind the same domain. The downside is that you always have to have one instance of App Engine Flex running 24/7. App Engine Flex is a lot more expensive than App Engine Standard for this reason. App Engine Standard only boots up instances if there is traffic (right now I pay between 10–15$ each month for a hobby app with very low traffic).</li><li>Cloud Run is another serverless option in Google Cloud Platform. The streaming capabilities of Cloud Run were a bit unclear. There were some StackOverflow posts that said that Cloud Run could not do HTTP streaming. Then in <a href="https://cloud.google.com/blog/products/serverless/cloud-run-now-supports-http-grpc-server-streaming">this announcement from Google</a> the title seems to suggest that HTTP and gRPC streaming are both supported, but then the article only talks about using gRPC. After testing I found that it actually does support HTTP streaming.</li><li>Firebase App Hosting. Actually last month <a href="https://firebase.blog/posts/2024/05/introducing-app-hosting/">Google ancounced Angular and Next.js hosting in Firebase</a>. This blog posts actually mentions streaming: <strong>App Hosting also supports advanced framework features that optimize your app’s performance, like streaming for Next.js and defer (lazy loading) for Angular.</strong> <br>This sounds great but it is unfortunate that they don’t support Remix. I could use Next.js instead. But I am a bit skeptical here because the whole Firebase part of Google Cloud always felt like they bought Firebase and never properly integrated it with the rest of Google Cloud. It feels like a mess (and apparently <a href="https://www.scmagazine.com/news/google-firebase-may-have-exposed-125m-records-from-misconfigurations">is one</a>) and I’ve been burned in similar fashion by AWS that promised to support Next.js on AWS Amplify before (it took them over a year to support Next.js 12). So I am a bit scared to use Next.js on any place that is not <a href="https://vercel.com/">Vercel</a>. <strong>[Out of scope]</strong></li><li>GKE is Google’s Managed Kubernetes service. It could definitely do HTTP streaming if I managed to set things up to do so. I don’t even consider it because I think Kubernetes is too much for my use case. <br><strong>[Out of scope]</strong> <br>It reminds me of this tweet:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*1nCDdaoPIAHVon5PmCnKUw.png" /></figure><p>Other developer friendly options would be <a href="https://vercel.com/">Vercel</a> and <a href="https://fly.io/">Fly.io</a> (where <a href="https://x.com/kentcdodds">Kent C Dodds</a> runs his <a href="https://www.epicweb.dev/">Epic stack</a>), but for the scope of this blog post I’ll leave those out. I would not recommend AWS (I say that as <a href="https://www.credly.com/badges/e6e8193b-d04e-4da8-af11-dd85bd1ce42e">an AWS Certified Developer</a>) unless you’re very experienced with stuff like AWS ECS.</p><p><strong>TLDR: We are only going to cover App Engine Flex and Cloud Run in this post.</strong></p><h3>Test with an example app that streams!</h3><p>Ryan Florence didn’t put <a href="https://www.youtube.com/watch?v=95B8mnhzoCM">the Pokemon streaming example from his talk</a> on his public GitHub account. So let’s build our own Pokemon streaming project (view <a href="https://github.com/Leejjon/remix-vite-with-streaming/">the result on GitHub</a>).</p><p>Requirements:</p><ul><li>A bash terminal (I am on Ubuntu 22.04.4)</li><li>Git (probably)</li><li>Modern versions of Node.js and NPM (I’m on Node 20.9.0)</li></ul><p>Requirements if you want to deploy the App we are building to Google Cloud Platform</p><ul><li>Install the <a href="https://cloud.google.com/sdk/docs/install">Google Cloud CLI</a></li><li>A Google Cloud account set up (go to cloud.google.com and click on start free).</li><li>Create a Google Cloud project. You need to have the following services enabled in API &amp; Services: App Engine, Cloud Build API, Cloud Run Admin API, Cloud Logging API &amp; Cloud Monitoring API.</li><li>VS Code with the Gemini Code Assist + Google Cloud Code extension</li><li>Docker (only if you want to deploy to Cloud Run and don’t want to leave the building to Cloud Build)</li><li>It might be useful to follow the interactive tutorials for <a href="https://cloud.google.com/appengine/docs/standard/nodejs/building-app/creating-project">App Engine Standard</a>, <a href="https://cloud.google.com/appengine/docs/flexible/nodejs/create-app">App Engine Flex</a> and <a href="https://cloud.google.com/run/docs/quickstarts/build-and-deploy/deploy-nodejs-service?hl=en">Cloud run</a> so that you get an idea.</li></ul><h3>Step one: Create a remix project:</h3><p>In a terminal, run the following command and follow the prompts (giving it a name etc). I named my project <strong>remix-vite-with-streaming</strong>:</p><pre>npx create-remix@latest</pre><h3>Step two: Collect all Pokemon and put them in a file:</h3><ul><li>Go into the browser and visit this url: <br><a href="https://pokeapi.co/api/v2/pokemon?limit=151">https://pokeapi.co/api/v2/pokemon?limit=151</a></li><li>Copy the json string with all 151 Pokemon. In Firefox I click on raw data and then press the copy button.</li><li>Create a file <strong>pokemon.json</strong> in the app folder of your project and paste the contents copied in step two into it.</li></ul><h3>Step three: Streaming the Pokemon into your React component:</h3><p>According to <a href="https://remix.run/docs/en/main/guides/streaming">the Remix documentation</a>, streaming is enabled by default. So any loader that responds with JSON is being streamed. Even if your code isn’t optimized for it. Here is some code that loads the contents of our pokemon.json file, sends it to the client and displays the pokemon names in the JSX. Paste the snippet below in the <strong>_index.tsx</strong> file,</p><pre>import { json, useLoaderData } from &quot;@remix-run/react&quot;;<br><br>import pokemonJsonAsAny from &quot;../pokemon.json&quot;;<br><br>type PokemonNameAndUrl = {name: string, url: string};<br>type PokemonJsonType = {results: Array&lt;PokemonNameAndUrl&gt;};<br><br>export async function loader() {<br>    const pokemonJson = pokemonJsonAsAny as PokemonJsonType;<br>    return json(pokemonJson);<br>}<br><br>export default function Pokemon() {<br>    const data = useLoaderData&lt;typeof loader&gt;();runtime: nodejs20<br><br>    return (<br>        &lt;&gt;<br>            {data.results.map((pokemon, index) =&gt; {<br>                return (<br>                    &lt;p key={index}&gt;{++index} - {pokemon.name}&lt;/p&gt;<br>                );<br>            })}<br>        &lt;/&gt;<br>    );<br>}</pre><p>If you now test this in the browser, the data shows up immediately:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/789/1*9f4fbIjnmVp29v6caTcQLQ.gif" /></figure><p>This is not how streaming works. Even if this response is streamed using HTTP Streaming, the React Component will just wait for the loader to be done before rendering.</p><p>To make sure the React Component can already start rendering before the response is streamed, we need to use <strong>defer</strong> and <strong>Suspense</strong>:</p><pre>import { Await, useLoaderData } from &quot;@remix-run/react&quot;;<br>import { defer } from &quot;@remix-run/node&quot;;<br>import { Suspense } from &quot;react&quot;;<br><br>type PokemonNameAndUrl = { name: string, url: string };<br>type PokemonJsonType = { results: Array&lt;PokemonNameAndUrl&gt; };<br><br>async function awaitPokemon(): Promise&lt;PokemonJsonType&gt; {<br>    const pokemonJsonAsAny = await import(&quot;../pokemon.json&quot;);<br>    const pokemonJson = pokemonJsonAsAny as PokemonJsonType;<br>    return pokemonJson;<br>}<br><br>export async function loader() {<br>    const pokemonResult = awaitPokemon();<br>    return defer({ pokemonResult });<br>}<br><br>export default function Pokemon() {<br>    const { pokemonResult } = useLoaderData&lt;typeof loader&gt;();<br><br>    return (<br>        &lt;Suspense fallback={&lt;p&gt;loading&lt;/p&gt;}&gt;<br>            &lt;Await resolve={pokemonResult}&gt;<br>                {(pokemonResult) =&gt; (<br>                    &lt;ul&gt;<br>                        {pokemonResult.results.map((pokemon, index) =&gt; {<br>                            return (<br>                                &lt;p key={index}&gt;{++index} - {pokemon.name}&lt;/p&gt;<br>                            );<br>                        })}<br>                    &lt;/ul&gt;<br>                )}<br>            &lt;/Await&gt;<br>        &lt;/Suspense&gt;<br>    );<br>}</pre><p>Now our client component isn’t blocked anymore! Even though the React Component could set itself up before the stream was done, you probably wouldn’t notice a difference.</p><p>If this file contained 151 billion Pokemon instead of 151, our screen would probably say <strong>&lt;p&gt;loading&lt;/p&gt;</strong> for quite a while before in one *POP* all data would be displayed.</p><h4>But is it Streaming?</h4><p>I thought that I would be able to see whether it was streamed or not in the Response headers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/380/1*CI8Z1DdK0GuNIyfl_OeIVA.png" /></figure><p>At first I thought this was proof that it was streaming. My non streaming React App didn’t have these headers. In my head it made sense that:</p><ul><li>The <strong>Transfer-Encoding</strong> is <strong>chunked</strong>, because the streamed response would be received in chunks rather than one big file.</li><li>The <strong>Connection</strong> had the <strong>keep-alive</strong> value, as with streaming I would expect it to reuse the same connection to transfer the chunks.</li></ul><p>After testing I found out that it is also possible for a server to stream a response, but not have these response headers. This made me want to give my example project even more obvious streaming functionality that makes it incredibly obvious when it does and does not stream.</p><p>To make the streaming effects more visible we can do two things:</p><ul><li>Load more data. For example more Pokemon and maybe more attributes of them like move sets in the game.</li><li>Hack in some artificial delays with code.</li></ul><h4>Loading each Pokemon a little more delayed</h4><p>We are going to add some delays to make it look like we are streaming data. Open up the <strong>_index.tsx</strong> file and paste the following code:</p><pre>import { defer } from &quot;@remix-run/node&quot;;<br>import { Await, useLoaderData } from &quot;@remix-run/react&quot;;<br>import pokemonJsonAsAny from &quot;../pokemon.json&quot;;<br>import { Suspense } from &quot;react&quot;;<br><br>type PokemonResponse = {id: number, name: string};<br>type PokemonNameAndUrl = {name: string, url: string};<br>type PokemonJsonType = {results: Array&lt;PokemonNameAndUrl&gt;};<br><br>function delay(ms: number) {<br>    return new Promise( resolve =&gt; setTimeout(resolve, ms) );<br>}<br><br>async function loadPokemon(pokemonId: number, pokemon: PokemonNameAndUrl): Promise&lt;PokemonResponse&gt; {<br>    // This way every pokemon loads a little longer.<br>    await delay(pokemonId * 100);<br><br>    return {id: pokemonId, name: pokemon.name} as PokemonResponse;<br>}<br><br>export async function loader() {<br>    const pokemonJson = pokemonJsonAsAny as PokemonJsonType;<br>    const pokemonList = pokemonJson.results.map((value, index) =&gt; {<br>        return loadPokemon(++index, value);<br>    });<br><br>    return defer({...pokemonList});<br>}<br><br>export default function Pokemon() {<br>    const data = useLoaderData&lt;typeof loader&gt;();<br><br>    const suspenseList = Object.values(data).map((value, index) =&gt; {<br>        return (<br>            &lt;Suspense key={index} fallback={&lt;p&gt;loading&lt;/p&gt;}&gt;<br>                &lt;Await resolve={value} errorElement={&lt;p&gt;Error&lt;/p&gt;}&gt;<br>                    {(response) =&gt; (<br>                        &lt;p&gt;{response.id} - {response.name}&lt;/p&gt;<br>                    )}<br>                &lt;/Await&gt;<br>            &lt;/Suspense&gt;<br>        );<br>    });<br>    <br>    return (<br>        &lt;&gt;<br>            &lt;div&gt;All first gen pokemon:&lt;/div&gt;<br>            {suspenseList}<br>        &lt;/&gt;<br>    );<br>}</pre><p>But when you try to run this code you’ll see it streams but crashes after the 50th Pokemon due to a timeout:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*RgLs9JBzve5xqI-wgSmlug.gif" /></figure><p>We can solve this by changing a line in the <strong>entry.server.tsx</strong> file. By default this file is hidden in Remix, you can enable it by running command in the terminal (at the root of your project):</p><pre>npx remix reveal</pre><p>Now that you see the <strong>entry.server.tsx</strong> file, change the value of the ABORT_DELAY const from 5_000 to 20_000.</p><h3>Streaming from App Engine Flex</h3><p>Deploying to Flex is just as easy as it is to deploy to Standard. You don’t need to create a Dockerfile. In the root of you project you create a new file called <strong>app.yaml</strong> which you fill with the following configuration:</p><pre>runtime: nodejs<br>runtime_config:<br>  operating_system: &quot;ubuntu22&quot;<br>  runtime_version: &quot;22&quot;<br>service: default<br>env: flex<br>instance_class: F1<br>entrypoint: npm run start<br>handlers:<br>  - url: &#39;.*&#39;<br>    script: auto<br>    secure: always<br>automatic_scaling:<br>  min_num_instances: 1<br>  max_num_instances: 2</pre><p>That is (almost) all you need to deploy a Remix app. If you already have run the gcloud init and selected your Google Cloud project, you can deploy with:</p><pre>gloud app deploy</pre><p>Unfortunately App Engine does HTTP response buffering by default, so you’ll notice that the request is waiting for 15 seconds until the last Pokemon is loaded (this GIF is a little sped up to reduce the size) before it sends the entire request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mMLpgTL13rRFCGBz2XKCyg.gif" /></figure><p>A moderator on the Remix Discord channel told me that hosts that don’t support streaming will try to buffer the whole response and send it all at once.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t5dTll76_kJ0ZwePzEo6Cw.png" /></figure><p>If we want to stream responses on App Engine flex, we need to disable buffering by adding the following response header in the <strong>entry.server.tsx </strong>on line 125:</p><pre>responseHeaders.set(&quot;X-Accel-Buffering&quot;, &quot;no&quot;);</pre><p>Deploy again and you’ll see that it streams fine (even though this GIF file looks a little laggy, on my screen it looked more fluent):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dxpPxRxc3Zghv-2Df6zhxw.gif" /></figure><p>You probably want to delete this version if you actually deployed, otherwise you’re going to get expensive bills. Either delete the entire Google Cloud Project or:</p><ul><li>Deploy a version to app engine standard.</li><li>Migrate traffic to this version.</li><li>Delete the App Engine Flexible version.</li></ul><h3>Streaming from Cloud Run</h3><p>Cloud Run is even simpler in the sense that you don’t need an app.yaml and you don’t need to enable any settings to disable buffering. Another great thing is that Cloud Run doesn’t require you to have a server up 24/7. It will just start an instance when needed, and kill it when it is not in use for a while. <a href="https://cloud.google.com/run/docs/about-instance-autoscaling">Their docs</a> state:</p><p><em>In Cloud Run, each </em><a href="https://cloud.google.com/run/docs/resource-model#revisions"><em>revision</em></a><em> is automatically scaled to the number of instances needed to handle all incoming requests, events, or CPU utilization.</em></p><p><em>When a revision does not receive any traffic, by default it is scaled in to zero instances.</em></p><p>Open the your Remix project in VS Code. If you have set up the Cloud Code plugin and are logged in with your Google Account, you can click on the project in the left bottom of your screen and execute the <strong>Deploy to Cloud Run</strong> action:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i71Yxd6phscDT8_98vzlSg.png" /></figure><p>You can just use all default settings and click on deploy. It will create a new service in Cloud Run:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WP2C4L3ZTxOMRc4cEk6ILw.png" /></figure><p>And as you’ll notice Cloud Run streams just fine as well:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1001/1*HpBpN3HmSGHugtMVLbLtOg.png" /></figure><p>Happy streaming! Now obviously this is a very artificial (but hopefully very clear) example.</p><p>Update: I found out that Ryan Florence uploaded <a href="https://www.youtube.com/watch?v=StZt-WrI2to">an even better video to explain how to use streaming to make calls effective</a>.</p><h3>Follow me for more!</h3><p>The final source code can <a href="https://github.com/Leejjon/remix-with-routing">be found in this GitHub project</a>. If you enjoyed this blog post, please follow me here on <a href="https://leejjon.medium.com/">Medium</a>, <a href="https://twitter.com/Leejjon_net">X</a> or <a href="https://www.linkedin.com/in/leon-liefting-4b511a29/">LinkedIN</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4e3e16b8c68d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Migrating a Create-React-App project to Remix]]></title>
            <link>https://leejjon.medium.com/migrating-an-create-react-app-project-that-uses-react-router-to-remix-b52b4b6f6456?source=rss-e00e4e1c1655------2</link>
            <guid isPermaLink="false">https://medium.com/p/b52b4b6f6456</guid>
            <category><![CDATA[create-react-app]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[remix]]></category>
            <dc:creator><![CDATA[Leejjon]]></dc:creator>
            <pubDate>Sun, 10 Dec 2023 23:45:04 GMT</pubDate>
            <atom:updated>2024-06-09T22:23:44.363Z</atom:updated>
            <content:encoded><![CDATA[<p>Create-react-app has been unmaintained for over two years. I’ve been doing research for a while now what I should use to run and build my React App instead.</p><p>UPDATE: This guide converts the app from a SPA (Single page app) hosted on an Express.js server to Remix which has a default server included. By default Remix does some server side rendering. Which is great for SEO and performance. If you want to ditch Create-React-App but still want to output static HTML and JavaScript so you can host it on a CDN, Remix now has <a href="https://remix.run/docs/en/main/future/spa-mode">SPA mode.</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*nhF3uOCmcv40m9bsogr28w.png" /></figure><h3>My “old” stack</h3><p>Read <a href="https://medium.com/@leejjon/sharing-code-between-frontend-and-backend-in-a-full-stack-typescript-app-1c8711c40304">this post</a> if you want more details on my existing stack. TLDR I have been using:</p><ul><li><a href="https://nodejs.org/en">Node.js</a> — JavaScript runtime environment</li><li><a href="https://expressjs.com/">Express.js</a> — Fast, unopinionated, minimalist web framework for Node.js</li><li><a href="https://www.typescriptlang.org/">TypeScript</a> — TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.</li><li><a href="https://create-react-app.dev/">Create-react-app</a> — Set up a React web app by running one command.</li><li><a href="https://webpack.js.org/">Webpack </a>— A bundler.</li><li><a href="https://cloud.google.com/appengine?hl=en">App Engine</a> — A service to easily run Node.js (or other) applications on Google Cloud Platform</li></ul><p>I selected this stack in 2018 with the knowledge that I had.</p><h3>Create-react-app eject</h3><p>CRA (the abbreviation for create-react-app) was the recommended way according to <a href="https://legacy.reactjs.org/docs/create-a-new-react-app.html">the official documentation</a> to bootstrap simple React projects. CRA projects have scripts in the <strong>package.json</strong> to build and run your react app:</p><pre>&quot;scripts&quot;: {<br>  &quot;start&quot;: &quot;react-scripts start&quot;,<br>  &quot;build&quot;: &quot;react-scripts build&quot;,<br>  &quot;test&quot;: &quot;react-scripts test&quot;,<br>  &quot;eject&quot;: &quot;react-scripts eject&quot;<br>}</pre><p>For running and building React the CRA scripts used <a href="https://webpack.js.org/">Webpack</a>, <a href="https://babeljs.io/">Babel</a> and more under the hood. As a CRA user you were not supposed to alter any of those settings. This made sure the team that maintained CRA could update their Webpack / Babel configuration and users could follow easily by updating the version of the react-scripts dependency.</p><p>For those that somehow needed to alter their Webpack / Babel settings, CRA offered an eject script. This would generate the webpack and babel configuration files that CRA used along with some scripts to use them. After ejecting, the user would have to update and maintain those by himself.</p><p>You can eject by running npm run eject on a CRA project.</p><p>It generates a bunch of scripts and dependencies in your project. <a href="https://github.com/Leejjon/cra-eject/pull/1/files">Here is a full list of files generated</a> after eject. I will believe the maintainers had a good reason for discontinuing CRA, so I am not going to go against their take and maintain all of that by myself!</p><h3>Continuing to build on my React Router configuration</h3><p>My current React application bootstrapped with create-react-app is using React Router for the routing in my Single Page App. I’m mostly very happy with <a href="https://github.com/Leejjon/Blindpool/blob/0bc65fbae3ed5b57251507bbbbbfb32b7cac0fb6/blindpool-client/src/App.tsx">my routes defined in JSX</a>.</p><p>By following <a href="https://twitter.com/mjackson">Michael Jackson</a> (the co-author of React Router, not the King of Pop) on X and conferences I was introduced to the Remix framework. Remix is a full stack framework for React applications. It is built on top of the foundations of React Router.</p><p>I hoped migrating to Remix would be a smooth transition with React Router already handling my routes.</p><p>One of the advantages of using Remix is that it can run on multiple HTTP servers. It’s not even tied to Node.js. You can read more on all possibilities <a href="https://remix.run/docs/en/main/discussion/runtimes">in the Remix documentation</a>. When looking at this I think Remix gives me more flexibility if I ever need to switch to another cloud provider.</p><h3>Why don’t you use Next.js like everybody is doing now?</h3><p>Next.js would definitely be a good base for my application. I have worked for about two years on a Next.js project. If you deploy it on Vercel it really works like a charm.</p><p>The main reasons were:</p><ul><li>The struggle I’ve had to run Next.js outside of Vercel.</li><li>I already know Next.js, and want to try something new.</li><li>Because <a href="https://www.epicweb.dev/why-i-wont-use-nextjs">Kent C Dodds says</a> it follows web standards more closely.</li></ul><h3>The reason I even used an Express.js server</h3><p>I had an Expres.js application running on Google App Engine. My first thought was to serve Remix from this server and use <a href="https://remix.run/docs/en/main/other-api/adapter#creating-an-adapter">the Express.js adapter for Remix</a>. But the Expess.js template for Remix didn’t have the server code in TypeScript. I did try to convert it, but ran into trouble with HMR (Webpack and Remix in one project required to have two tsconfig.json files, and that makes the setup complex).</p><p>I was using Express.js for two things:</p><ul><li>To make sure every URL (example.com/page) would point to the <strong>index.html</strong> of the single page web app built with create-react-app. Then React Router would load the right content on the client.</li><li>Set the correct page title, page description and other meta tags on the server side so that the Google bot would be able to index my web app properly on Google (Google bots often fail to render JavaScript).</li></ul><p>The book <strong>Full Stack Development with Remix</strong> by <strong>Andre Landgraf </strong>(this isn’t an ad, I just thought this book was really good) taught me that above points can be done in Remix directly, without needing Express.js.</p><p>So we’re ditching Express.js and running the default Remix template (for Node.js).</p><h3>A test CRA + React Router project to migrate</h3><p>Lets quickly create a TypeScript project generated by Create-React-App that uses React Router for routing. Here are the bash commands to do so (or <a href="https://github.com/Leejjon/cra-with-router">clone it from here</a>):</p><pre># Create the <br>npx create-react-app cra-with-router --template typescript<br>cd cra-with-router<br>npm i react-router-dom @types/react-router-dom react-helmet-async<br>mkdir src/pages<br>touch src/pages/Home.tsx<br>touch src/pages/News.tsx<br>touch src/pages/About.tsx</pre><p>In <strong>Home.tsx</strong> put the following code:</p><pre>import {Helmet} from &quot;react-helmet-async&quot;;<br><br>function Home() {<br>    const title = &quot;Home&quot;;<br>    const description = &quot;Home sweet home.&quot;;<br>    return (<br>        &lt;&gt;<br>            &lt;Helmet&gt;<br>                &lt;title&gt;{title}&lt;/title&gt;<br>                &lt;meta name=&quot;description&quot; content={description}/&gt;<br>                &lt;meta property=&quot;og:title&quot; content={title}/&gt;<br>                &lt;meta property=&quot;og:description&quot; content={description}/&gt;<br>            &lt;/Helmet&gt;<br>            &lt;div&gt;{description}&lt;/div&gt;<br>        &lt;/&gt;<br>    );<br>}<br><br>export default Home;</pre><p>You might not have heard about the <a href="https://www.npmjs.com/package/react-helmet-async">react-helmet-async</a> package. It’s a fork of the original react-helmet package that doesn’t receive updates anymore. I used it to make sure the page title and meta tags are correct for every page and that these are updated when a user goes to another page.</p><p>In <strong>News.tsx</strong> put the following code:</p><pre>import {Helmet} from &quot;react-helmet-async&quot;;<br><br>function News() {<br>    const title = &quot;News&quot;;<br>    const description = &quot;The latest news.&quot;;<br>    return (<br>        &lt;&gt;<br>            &lt;Helmet&gt;<br>                &lt;title&gt;{title}&lt;/title&gt;<br>                &lt;meta name=&quot;description&quot; content={description}/&gt;<br>                &lt;meta property=&quot;og:title&quot; content={title}/&gt;<br>                &lt;meta property=&quot;og:description&quot; content={description}/&gt;<br>            &lt;/Helmet&gt;<br>            &lt;div&gt;The latest news.&lt;/div&gt;<br>        &lt;/&gt;<br>    );<br>}<br><br>export default News;</pre><p>In About.tsx put the following code:</p><pre>import {Helmet} from &quot;react-helmet-async&quot;;<br><br>function About() {<br>    const title = &quot;About&quot;;<br>    const description = &quot;About us.&quot;;<br>    return (<br>        &lt;&gt;<br>            &lt;Helmet&gt;<br>                &lt;title&gt;{title}&lt;/title&gt;<br>                &lt;meta name=&quot;description&quot; content={description}/&gt;<br>                &lt;meta property=&quot;og:title&quot; content={title}/&gt;<br>                &lt;meta property=&quot;og:description&quot; content={description}/&gt;<br>            &lt;/Helmet&gt;<br>            &lt;div&gt;{description}&lt;/div&gt;<br>        &lt;/&gt;<br>    );<br>}<br><br>export default About;</pre><p>And finally replace the code in the <strong>App.tsx</strong> with:</p><pre>import React, {lazy, Suspense} from &#39;react&#39;;<br>import {Link, Route, Routes} from &#39;react-router-dom&#39;;<br>import {BrowserRouter} from &#39;react-router-dom&#39;;<br>import &#39;./App.css&#39;;<br><br>const Home = lazy(() =&gt; import(&quot;./pages/Home&quot;));<br>const News = lazy(() =&gt; import(&quot;./pages/News&quot;));<br>const About = lazy(() =&gt; import(&quot;./pages/About&quot;));<br><br>function App() {<br>    return (<br>        &lt;div className=&quot;App&quot;&gt;<br>            &lt;Suspense fallback={&lt;Loader/&gt;}&gt;<br>                &lt;BrowserRouter&gt;<br>                    &lt;Link id=&quot;linkToHome&quot; to=&quot;/&quot;&gt;<br>                        Home<br>                    &lt;/Link&gt;&lt;br/&gt;<br>                    &lt;Link id=&quot;linkToNews&quot; to=&quot;/news&quot;&gt;<br>                        News<br>                    &lt;/Link&gt;&lt;br/&gt;<br>                    &lt;Link id=&quot;linkToAbout&quot; to=&quot;/about&quot;&gt;<br>                        About<br>                    &lt;/Link&gt;<br>                    &lt;Routes&gt;<br>                        &lt;Route path=&quot;/&quot; element={&lt;Home/&gt;}/&gt;<br>                        &lt;Route path=&quot;/news&quot; element={&lt;News/&gt;}/&gt;<br>                        &lt;Route path=&quot;/about&quot; element={&lt;About/&gt;}/&gt;<br>                    &lt;/Routes&gt;<br>                &lt;/BrowserRouter&gt;<br>            &lt;/Suspense&gt;<br>        &lt;/div&gt;<br>    );<br>}<br><br>function Loader() {<br>    return (<br>        &lt;div&gt;Loading...&lt;/div&gt;<br>    );<br>}<br><br>export default App;</pre><p>Now run the app with: <strong>npm run start</strong> and visit <strong>localhost:3000</strong> in your browser. The pages are <a href="https://react.dev/reference/react/lazy">initialized lazily using React Suspense</a>. This means we will only load it when you visit it the first time. After that you can navigate between these three pages without the browser doing any calls to the server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/946/1*bNDx0X41xX1lxH8OiIjlEA.png" /><figcaption>You can see the page title and meta title and descriptions are filled properly by react-helmet-async.</figcaption></figure><p>Now even though <strong>react-helmet-async</strong> sets the page title and description properly, the Google search bot would not index the CRA version of my app properly.</p><p>I solved it in the Express.js <a href="https://blog.logrocket.com/adding-dynamic-meta-tags-react-app-without-ssr/">like this guide explains</a>. Which isn’t ideal because it means every time you add a page in your React application you need to update your Express.js server too.</p><h3>Building the same in Remix</h3><p>Generate a Remix project:</p><pre>npx create-remix@latest</pre><p>You’ll get a project that looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/377/1*bWStomRx4CXxavDcy7VtDA.png" /></figure><p>Run it with <strong>npm run dev</strong> and you can see the app work in your browser:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*rzI8KvgOg2xIYqRmX9563A.png" /></figure><p>If you look into the <strong>root.tsx</strong> you’ll notice it doesn’t have any routes. One of my mistakes was that I started to add routes in there like we did in the <strong>cra-with-routing</strong> project, but you should not. Remix generates the route configurations by looking at the files in the routes folder.</p><p>If we want to see what the routes look like of this current Remix app, run:</p><pre>npx remix routes</pre><p>The output will be:</p><pre>&lt;Routes&gt;<br>  &lt;Route file=&quot;root.tsx&quot;&gt;<br>    &lt;Route index file=&quot;routes/_index.tsx&quot; /&gt;<br>  &lt;/Route&gt;<br>&lt;/Routes&gt;</pre><p>That looks a bit like React Router code.</p><h3>Moving our content over to the Remix app</h3><p>Paste the following JSX code (basically the <strong>Home.tsx</strong> of our previous cra-with-routing project without the helmet part) in the <strong>_index.tsx</strong> file (which is the default component):</p><pre>export default function Index() {<br>  return (<br>      &lt;div&gt;Home sweet home.&lt;/div&gt;<br>  );<br>}</pre><p>If you look in your browser, it is now showing our contents.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/1*lc0et9lRHOp0oUEgIHJq7w.png" /></figure><p>Now copy and paste the <strong>About.tsx</strong> and <strong>News.tsx</strong> files to the routes folder in our the new <strong>remix-with-routing</strong> project. You can remove the &lt;Helmet&gt; tag. These routes will start working (without even restarting the dev server):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/951/1*FsDup5zdbHFRBKAgz6v_aA.png" /></figure><p>If we run the <strong>npx remix routes</strong> command again, you can see that the routes now look more like what we had in the <strong>cra-with-routing</strong> project:</p><pre>&lt;Routes&gt;<br>  &lt;Route file=&quot;root.tsx&quot;&gt;<br>    &lt;Route index file=&quot;routes/_index.tsx&quot; /&gt;<br>    &lt;Route path=&quot;About&quot; file=&quot;routes/About.tsx&quot; /&gt;<br>    &lt;Route path=&quot;News&quot; file=&quot;routes/News.tsx&quot; /&gt;<br>  &lt;/Route&gt;<br>&lt;/Routes&gt;</pre><h3>Adding the navigation links</h3><p>In Remix you don’t use the React Router’s Link components. Remix has it’s own Link component, which you can use to add navigation links just above the &lt;Outlet/&gt; element:</p><pre>import { cssBundleHref } from &quot;@remix-run/css-bundle&quot;;<br>import type { LinksFunction } from &quot;@remix-run/node&quot;;<br>import {<br>  Link,<br>  Links,<br>  LiveReload,<br>  Meta,<br>  Outlet,<br>  Scripts,<br>  ScrollRestoration,<br>} from &quot;@remix-run/react&quot;;<br><br>export const links: LinksFunction = () =&gt; [<br>  ...(cssBundleHref ? [{ rel: &quot;stylesheet&quot;, href: cssBundleHref }] : []),<br>];<br><br>export default function App() {<br>  return (<br>    &lt;html lang=&quot;en&quot;&gt;<br>      &lt;head&gt;<br>        &lt;meta charSet=&quot;utf-8&quot; /&gt;<br>        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot; /&gt;<br>        &lt;Meta /&gt;<br>        &lt;Links /&gt;<br>      &lt;/head&gt;<br>      &lt;body&gt;<br>        &lt;Link to=&quot;/&quot;&gt;Home&lt;/Link&gt;|<br>        &lt;Link to=&quot;/news&quot;&gt;News&lt;/Link&gt;|<br>        &lt;Link to=&quot;/about&quot;&gt;About&lt;/Link&gt;<br>        &lt;Outlet /&gt;<br>        &lt;ScrollRestoration /&gt;<br>        &lt;Scripts /&gt;<br>        &lt;LiveReload /&gt;<br>      &lt;/body&gt;<br>    &lt;/html&gt;<br>  );<br>}</pre><p>The <strong>&lt;Outlet&gt;</strong> element is important. It renders whatever component in the routes folder matches the current route. If you delete it none of the components in the routes folder will be rendered.</p><p>With these changes, the Remix app now looks the same as the application made with CRA:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nj7i8Br4xAO0UGzVj5tI0g.png" /></figure><h3>Adding correct page titles</h3><p>Unlike fixing the page titles and meta tags in Express.js, in Remix you can simply add a <strong>MetaFunction</strong> to the pages.</p><p><strong>Home.tsx</strong>:</p><pre>import type { MetaFunction } from &quot;@remix-run/node&quot;;<br><br>const title = &quot;Home&quot;;<br>const description = &quot;Home sweet home.&quot;<br><br>export const meta: MetaFunction = () =&gt; {<br>  return [<br>    { title: title },<br>    { name: &quot;description&quot;, content: description },<br>  ];<br>};<br><br>export default function Index() {<br>  return (<br>      &lt;div&gt;{description}&lt;/div&gt;<br>  );<br>}</pre><p><strong>News.tsx</strong>:</p><pre>import type { MetaFunction } from &quot;@remix-run/node&quot;;<br><br>const title = &quot;News&quot;;<br>const description = &quot;The latest news.&quot;<br><br>export const meta: MetaFunction = () =&gt; {<br>    return [<br>        { title: title },<br>        { name: &quot;description&quot;, content: description },<br>    ];<br>};<br><br>function News() {<br>    return (<br>        &lt;div&gt;{description}&lt;/div&gt;<br>    );<br>}<br><br>export default News;</pre><p><strong>About.tsx</strong>:</p><pre>import type { MetaFunction } from &quot;@remix-run/node&quot;;<br><br>const title = &quot;About&quot;;<br>const description = &quot;About us.&quot;<br><br>export const meta: MetaFunction = () =&gt; {<br>    return [<br>        { title: title },<br>        { name: &quot;description&quot;, content: description },<br>    ];<br>};<br><br>function About() {<br>    return (<br>        &lt;div&gt;{description}&lt;/div&gt;<br>    );<br>}<br><br>export default About;</pre><p>And now we have titles and descriptions that are rendered on the server:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*WSoGJgAKh9wL2-lJRQB_8A.png" /></figure><p>You can test that the meta tags are there before React is being initialized with curl:</p><pre>curl localhost:3000/news</pre><p>You should see the correct title.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*33_K3kXQ89zFUsnyKyTmFg.png" /></figure><p>The MetaFunction returns an array, so you can easily add as many tags to the head element as you like.</p><h3>Using translations in Remix</h3><p>My original project created with Create-React-App had translations set up using the <a href="https://www.npmjs.com/package/react-i18next">react-i18next</a> library. My web app had two domains, a Dutch one and an English one. Based on the domain that the user was requesting from, I’d load the correct translations.</p><p>Let’s add this to our CRA project (or download this branch) to show how we can migrate this functionality to Remix. In the cra-with-router project, run:</p><pre>npm i i18next react-i18next</pre><p>Now create a file called i18n.ts and put the following content in:</p><pre>import i18n from &quot;i18next&quot;;<br>import {initReactI18next} from &quot;react-i18next&quot;;<br><br>// English is loaded unless the user comes from the Dutch domain.<br>const locale = window.location.hostname.endsWith(&#39;mysite.nl&#39;) ? &#39;nl&#39; : &#39;en&#39;;<br><br>export const resources = {<br>    en: {<br>        translation: {<br>            DESCRIPTION_ABOUT: &quot;About us.&quot;,<br>            DESCRIPTION_HOME: &quot;Home sweet home.&quot;,<br>            DESCRIPTION_NEWS: &quot;The latest news.&quot;,<br>            LOADING: &quot;Loading...&quot;,<br>            TITLE_ABOUT: &quot;About&quot;,<br>            TITLE_HOME: &quot;Home&quot;,<br>            TITLE_NEWS: &quot;News&quot;,<br>        }<br>    },<br>    nl: {<br>        translation: {<br>            DESCRIPTION_ABOUT: &quot;Over ons.&quot;,<br>            DESCRIPTION_HOME: &quot;Fijn thuis.&quot;,<br>            DESCRIPTION_NEWS: &quot;Het laatste niews.&quot;,<br>            LOADING: &quot;Laden...&quot;,<br>            TITLE_ABOUT: &quot;Over ons&quot;,<br>            TITLE_HOME: &quot;Thuis&quot;,<br>            TITLE_NEWS: &quot;News&quot;,<br>        }<br>    }<br>}<br><br>i18n<br>    .use(initReactI18next) // passes i18n down to react-i18next<br>    .init({<br>        resources,<br>        lng: locale,<br>        returnNull: false,<br>        keySeparator: false, // we do not use keys in form messages.welcome<br>        interpolation: {<br>            escapeValue: false // react already safes from xss<br>        }<br>    });</pre><p>In the <strong>index.tsx</strong> file, add this import to make sure the above code is executed:</p><pre>import &#39;./locales/i18n&#39;;</pre><p>Now in the <strong>App.tsx</strong> we can use the <strong>useTranslation</strong> hook to grab the correct translation on runtime.</p><pre>import React, {lazy, Suspense} from &#39;react&#39;;<br>import {Link, Route, Routes} from &#39;react-router-dom&#39;;<br>import {BrowserRouter} from &#39;react-router-dom&#39;;<br>import {HelmetProvider} from &quot;react-helmet-async&quot;;<br>import &#39;./App.css&#39;;<br>import {useTranslation} from &quot;react-i18next&quot;;<br><br>const Home = lazy(() =&gt; import(&quot;./pages/Home&quot;));<br>const News = lazy(() =&gt; import(&quot;./pages/News&quot;));<br>const About = lazy(() =&gt; import(&quot;./pages/About&quot;));<br><br>function App() {<br>    const {t} = useTranslation();<br>    return (<br>        &lt;HelmetProvider&gt;<br>            &lt;div className=&quot;App&quot;&gt;<br>                &lt;Suspense fallback={&lt;Loader/&gt;}&gt;<br>                    &lt;BrowserRouter&gt;<br>                        &lt;Link id=&quot;linkToHome&quot; to=&quot;/&quot;&gt;<br>                            {t(&quot;TITLE_HOME&quot;)}<br>                        &lt;/Link&gt;&lt;br/&gt;<br>                        &lt;Link id=&quot;linkToNews&quot; to=&quot;/news&quot;&gt;<br>                            {t(&quot;TITLE_NEWS&quot;)}<br>                        &lt;/Link&gt;&lt;br/&gt;<br>                        &lt;Link id=&quot;linkToAbout&quot; to=&quot;/about&quot;&gt;<br>                            {t(&quot;TITLE_ABOUT&quot;)}<br>                        &lt;/Link&gt;<br>                        &lt;Routes&gt;<br>                            &lt;Route path=&quot;/&quot; element={&lt;Home/&gt;}/&gt;<br>                            &lt;Route path=&quot;/news&quot; element={&lt;News/&gt;}/&gt;<br>                            &lt;Route path=&quot;/about&quot; element={&lt;About/&gt;}/&gt;<br>                        &lt;/Routes&gt;<br>                    &lt;/BrowserRouter&gt;<br>                &lt;/Suspense&gt;<br>            &lt;/div&gt;<br>        &lt;/HelmetProvider&gt;<br>    );<br>}<br><br>function Loader() {<br>    const {t} = useTranslation();<br>    return (<br>        &lt;div&gt;{t(&quot;LOADING&quot;)}&lt;/div&gt;<br>    );<br>}<br><br>export default App;</pre><p>In the <strong>Home.tsx</strong>, <strong>News.tsx</strong> and <strong>About.tsx</strong> pages you can also use the <strong>useTranslation</strong> hook:</p><pre>import {Helmet} from &quot;react-helmet-async&quot;;<br>import {useTranslation} from &quot;react-i18next&quot;;<br><br>function Home() {<br>    const {t} = useTranslation();<br>    return (<br>        &lt;&gt;<br>            &lt;Helmet&gt;<br>                &lt;title&gt;{t(&quot;TITLE_HOME&quot;)}&lt;/title&gt;<br>                &lt;meta name=&quot;description&quot; content={t(&quot;DESCRIPTION_HOME&quot;)}/&gt;<br>                &lt;meta property=&quot;og:title&quot; content={t(&quot;TITLE_HOME&quot;)}/&gt;<br>                &lt;meta property=&quot;og:description&quot; content={t(&quot;DESCRIPTION_HOME&quot;)}/&gt;<br>            &lt;/Helmet&gt;<br>            &lt;div&gt;{t(&quot;DESCRIPTION_HOME&quot;)}&lt;/div&gt;<br>        &lt;/&gt;<br>    );<br>}<br><br>export default Home;</pre><p>You can run the project with npm start and see the translations work.</p><p>The downside of these translations in React is that when a Web crawler (like the Googlebot) downloads the page and sometimes doesn’t render JavaScript.</p><p>You can try to download your page with curl to see what a Web crawler that doesn’t support JavaScript would see and index.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*1zbmTQ6rgE4IhkeZf-TUkw.png" /></figure><p>As you can see, there are no translations to be found in the index.html</p><h3>Using the i18next library in Remix</h3><p>In the CRA project, we had one translations file for all React components that was downloaded on the first page visit. Then the useTranslation hook would be used to load and use translations in your React component.</p><p>Since Remix renders so much on the server, you can’t just use the <strong>useTranslation</strong> hook in Remix components.</p><p>Sergioxda has made the <a href="https://www.npmjs.com/package/remix-i18next">remix-i18next</a> package (that uses i18n and react-i18next under the hood) that can get the useTranslation hook to work in Remix components.</p><p>Remix-i18next also specifies how to put translations in components that are rendered on the server side:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/871/1*fi4MhzJ3NPIahJKv0G6DPw.png" /></figure><p>After some of trial and error I figured out how to use these translations from loaders, in both the page and the MetaFunction. See <a href="https://github.com/Leejjon/remix-with-routing/commit/0c7b6203b350aea0bf847f2fdf9ef55b68cee312">this PR</a> to see my attempt at using remix-i18next for translations in my server side rendered components.</p><p>I felt that three depencencies (i18n, react-i18next and remix-i18next</p><h3>Building our own solution for translations in loaders</h3><p>Create a file called i18n.ts in which you put the actual translations (a little bit like in the Create-React-App project:</p><pre><br>type TranslationPairs = {[key: string]: string};<br><br>export const resources: { [language: string]: { translation: TranslationPairs } } = {<br>    en: {<br>        translation: {<br>            DESCRIPTION_ABOUT: &quot;About us.&quot;,<br>            DESCRIPTION_HOME: &quot;Home sweet home.&quot;,<br>            DESCRIPTION_NEWS: &quot;The latest news.&quot;,<br>            LOADING: &quot;Loading...&quot;,<br>            TITLE_ABOUT: &quot;About&quot;,<br>            TITLE_HOME: &quot;Home&quot;,<br>            TITLE_NEWS: &quot;News&quot;,<br>        }<br>    },<br>    nl: {<br>        translation: {<br>            DESCRIPTION_ABOUT: &quot;Over ons.&quot;,<br>            DESCRIPTION_HOME: &quot;Fijn thuis.&quot;,<br>            DESCRIPTION_NEWS: &quot;Het laatste niews.&quot;,<br>            LOADING: &quot;Laden...&quot;,<br>            TITLE_ABOUT: &quot;Over ons&quot;,<br>            TITLE_HOME: &quot;Thuis&quot;,<br>            TITLE_NEWS: &quot;News&quot;,<br>        }<br>    }<br>};<br><br>export function getCountryCode(request: Request) {<br>    const host = request.headers.get(&quot;Host&quot;);<br>    return host &amp;&amp; host.endsWith(&quot;mysite.nl&quot;) ? &quot;nl&quot; : &quot;en&quot;;<br>}<br><br>function translate(countryCode: string, key: string) {<br>    for (const country in resources) {<br>        if (country === countryCode) {<br>            const translations = resources[countryCode].translation;<br>            for (const translationKey in translations) {<br>                if (translationKey == key) {<br>                    return translations[translationKey];<br>                }<br>            }<br>            return `Translation key ${key} is missing.`<br>        }<br>    }<br>    return `${countryCode} is not a supported language.`;<br>}<br><br>export function getTranslator(countryCode: string) {<br>    return (key: string) =&gt; translate(countryCode, key);<br>}</pre><h3>Passing translations from a loader</h3><p>Now we’ve set up all this configuration, let’s use translations for the links in our <strong>root.tsx</strong> page. To use them we have to add a loader:</p><pre>import { cssBundleHref } from &quot;@remix-run/css-bundle&quot;;<br>import {json, type LinksFunction, type LoaderFunctionArgs} from &quot;@remix-run/node&quot;;<br>import {<br>  Link,<br>  Links,<br>  LiveReload,<br>  Meta,<br>  Outlet,<br>  Scripts,<br>  ScrollRestoration,<br>  useLoaderData,<br>} from &quot;@remix-run/react&quot;;<br>import {getCountryCode, getTranslator} from &quot;~/i18n&quot;;<br><br>export async function loader({request}: LoaderFunctionArgs) {<br>  const t = getTranslator(getCountryCode(request));<br>  const TITLE_ABOUT = t(&quot;TITLE_ABOUT&quot;);<br>  const TITLE_HOME = t(&quot;TITLE_HOME&quot;);<br>  const TITLE_NEWS = t(&quot;TITLE_NEWS&quot;);<br>  const headers = { &quot;Cache-Control&quot;: &quot;max-age=86400&quot; }; // One day<br><br>  return json({<br>    TITLE_ABOUT, TITLE_HOME, TITLE_NEWS<br>  }, {headers});<br>}<br><br>export const links: LinksFunction = () =&gt; [<br>  ...(cssBundleHref ? [{ rel: &quot;stylesheet&quot;, href: cssBundleHref }] : []),<br>];<br><br>export default function App() {<br>  const {<br>    TITLE_ABOUT,<br>    TITLE_HOME,<br>    TITLE_NEWS<br>  } = useLoaderData&lt;typeof loader&gt;();<br><br>  return (<br>    &lt;html lang=&quot;en&quot;&gt;<br>      &lt;head&gt;<br>        &lt;meta charSet=&quot;utf-8&quot; /&gt;<br>        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot; /&gt;<br>        &lt;Meta /&gt;<br>        &lt;Links /&gt;<br>      &lt;/head&gt;<br>      &lt;body&gt;<br>        &lt;Link to=&quot;/&quot;&gt;{TITLE_HOME}&lt;/Link&gt;|<br>        &lt;Link to=&quot;/news&quot;&gt;{TITLE_NEWS}&lt;/Link&gt;|<br>        &lt;Link to=&quot;/about&quot;&gt;{TITLE_ABOUT}&lt;/Link&gt;<br>        &lt;Outlet /&gt;<br>        &lt;ScrollRestoration /&gt;<br>        &lt;Scripts /&gt;<br>        &lt;LiveReload /&gt;<br>      &lt;/body&gt;<br>    &lt;/html&gt;<br>  );<br>}</pre><p>You might notice that if you run this code, the translations are downloaded in a GET call (due to the loader mechanism):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*atrraZk0tVRYFV1VMy4L1g.png" /><figcaption>I added a Cache-Control header in the loader that tells the browser to cache the GET call for a day. This should reduce traffic a lot.</figcaption></figure><p>Let’s also add a loader in the pages (<strong>_index.tsx</strong>, <strong>About.tsx</strong> and <strong>News.tsx</strong>) page to let the pages use the translations in their <strong>content</strong> and <strong>meta headers.</strong></p><p><strong>_index.tsx</strong>:</p><pre>import { json, type LoaderFunctionArgs, type MetaFunction } from &quot;@remix-run/node&quot;;<br>import { useLoaderData } from &quot;@remix-run/react&quot;;<br>import {getCountryCode, getTranslator} from &quot;~/i18n&quot;;<br><br>export async function loader({request}: LoaderFunctionArgs) {<br>  const t = getTranslator(getCountryCode(request));<br>  const TITLE_HOME = t(&quot;TITLE_HOME&quot;);<br>  const DESCRIPTION_HOME = t(&quot;DESCRIPTION_HOME&quot;);<br><br>  const headers = { &quot;Cache-Control&quot;: &quot;max-age=86400&quot; }; // One day<br><br>  return json({<br>    DESCRIPTION_HOME, TITLE_HOME<br>  }, {headers});<br>}<br><br>export const meta: MetaFunction&lt;typeof loader&gt; = ({data}) =&gt; {<br>  if (data) {<br>    const {DESCRIPTION_HOME, TITLE_HOME} = data;<br>    return [<br>      { title: TITLE_HOME },<br>      { name: &quot;description&quot;, content: DESCRIPTION_HOME },<br>    ];<br>  } else {<br>    return [<br>      { title: &quot;Couldn&#39;t load the translations.&quot; }<br>    ];<br>  }<br>};<br><br>export default function Index() {<br>  const {DESCRIPTION_HOME} = useLoaderData&lt;typeof loader&gt;();<br>  return (<br>      &lt;div&gt;{DESCRIPTION_HOME}&lt;/div&gt;<br>  );<br>}</pre><p><strong>About.tsx</strong>:</p><pre>import { json, type MetaFunction, type LoaderFunctionArgs } from &quot;@remix-run/node&quot;;<br>import { useLoaderData } from &quot;@remix-run/react&quot;;<br>import {getCountryCode, getTranslator} from &quot;~/i18n&quot;;<br><br>export async function loader({ request }: LoaderFunctionArgs) {<br>    console.log(new URL(request.url).pathname);<br>    const t = getTranslator(getCountryCode(request));<br>    const TITLE_ABOUT = t(&quot;TITLE_ABOUT&quot;);<br>    const DESCRIPTION_ABOUT = t(&quot;DESCRIPTION_ABOUT&quot;);<br><br>    const headers = { &quot;Cache-Control&quot;: &quot;max-age=86400&quot; }; // One day<br><br>    return json({<br>        DESCRIPTION_ABOUT, TITLE_ABOUT<br>    }, { headers });<br>}<br><br>export const meta: MetaFunction&lt;typeof loader&gt; = ({ data }) =&gt; {<br>    if (data) {<br>        const {DESCRIPTION_ABOUT, TITLE_ABOUT} = data;<br>        return [<br>            { title: TITLE_ABOUT },<br>            { name: &quot;description&quot;, content: DESCRIPTION_ABOUT },<br>        ];<br>    } else {<br>        return [<br>            { title: &quot;Couldn&#39;t load the translations.&quot; }<br>        ];<br>    }<br>};<br><br>function About() {<br>    const {DESCRIPTION_ABOUT} = useLoaderData&lt;typeof loader&gt;();<br>    return (<br>        &lt;div&gt;{DESCRIPTION_ABOUT}&lt;/div&gt;<br>    );<br>}<br><br>export default About;</pre><p><strong>News.tsx</strong>:</p><pre>import {json, type LoaderFunctionArgs, type MetaFunction} from &quot;@remix-run/node&quot;;<br>import {useLoaderData} from &quot;@remix-run/react&quot;;<br>import {getCountryCode, getTranslator} from &quot;~/i18n&quot;;<br><br>export async function loader({request}: LoaderFunctionArgs) {<br>    const t = getTranslator(getCountryCode(request));<br>    const TITLE_NEWS = t(&quot;TITLE_NEWS&quot;);<br>    const DESCRIPTION_NEWS = t(&quot;DESCRIPTION_NEWS&quot;);<br><br>    const headers = {&quot;Cache-Control&quot;: &quot;max-age=86400&quot;}; // One day<br><br>    return json({<br>        DESCRIPTION_NEWS, TITLE_NEWS<br>    }, {headers});<br>}<br><br>export const meta: MetaFunction&lt;typeof loader&gt; = ({data}) =&gt; {<br>    if (data) {<br>        const {TITLE_NEWS, DESCRIPTION_NEWS} = data;<br>        return [<br>            {title: TITLE_NEWS},<br>            {name: &quot;description&quot;, content: DESCRIPTION_NEWS},<br>        ];<br>    } else {<br>        return [<br>            {title: &quot;Couldn&#39;t load the translations.&quot;}<br>        ];<br>    }<br>};<br><br>function News() {<br>    const {DESCRIPTION_NEWS} = useLoaderData&lt;typeof loader&gt;();<br>    return (<br>        &lt;div&gt;{DESCRIPTION_NEWS}&lt;/div&gt;<br>    );<br>}<br><br>export default News;</pre><p>If you now run the remix project with npm run dev you will notice the translations will be correct on all pages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uWBO8fa9uyV2MXOLvA44nw.png" /><figcaption>The GET calls to obtain the translations will be cached properly.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/885/1*Jxw3bkuyf7-gNgg3DuzQGA.png" /><figcaption>The response of the GET call.</figcaption></figure><p>Done! I’d love to see feedback on my translations approach.</p><h3>Follow me for more!</h3><p>The final source code can be found <a href="https://github.com/Leejjon/remix-with-routing">in this GitHub project</a>. If you enjoyed this blog post, please follow me here on <a href="https://leejjon.medium.com/">Medium</a>, <a href="https://twitter.com/Leejjon_net">X</a> or <a href="https://www.linkedin.com/in/leon-liefting-4b511a29/">LinkedIN</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b52b4b6f6456" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Define a Node.js App Engine application in Terraform]]></title>
            <link>https://leejjon.medium.com/define-a-node-js-app-engine-application-in-terraform-7a4d5e0fd32f?source=rss-e00e4e1c1655------2</link>
            <guid isPermaLink="false">https://medium.com/p/7a4d5e0fd32f</guid>
            <category><![CDATA[bash]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[google-app-engine]]></category>
            <dc:creator><![CDATA[Leejjon]]></dc:creator>
            <pubDate>Fri, 01 Sep 2023 23:59:25 GMT</pubDate>
            <atom:updated>2024-06-09T22:24:35.290Z</atom:updated>
            <content:encoded><![CDATA[<h3>What are we going to do</h3><p>Create a Node.js hello world application and deploy it on App Engine with Terraform. We will create the project from scratch and supply every command you need to run along the way.</p><h3>What made me want to write this?</h3><p>With <strong>App Engine</strong> it has been extremely easy to deploy web apps in the cloud. In my opinion it has been a lot easier than configuring <strong>AWS Beanstalk</strong> projects or even <strong>AWS Lambda</strong>. It’s really nice that you don’t even need to know about docker. Only <strong>Vercel</strong> gets close on developer friendliness level.</p><p>You can deploy your first application using the great interactive tutorials or using the <a href="https://cloud.google.com/sdk/gcloud">gcloud CLI</a>. If you start connecting your application with other Google Cloud Platform services, it’s easy to do it by hand (in the Google Cloud Console web UI).</p><p>As time passes it becomes more difficult to remember all the things you’ve configured. Especially databases. I was already learning to use Terraform on AWS for my job, so I tried to define my App Engine project and other required resources in Terraform.</p><p>There are currently not a lot of guides to define App Engine projects in Terraform besides the official Terraform docs (<a href="https://vercel.com/guides/integrating-terraform-with-vercel">Vercel does this a lot better</a>). And those that I found were not detailed at all and mostly targeting the App Engine runtimes that I do not use (Go / Python).</p><p>After many hours of reading the docs and reverse engineering Terraform configurations from random projects on GitHub that use App Engine and Terraform, I managed to create a project that deploys new versions nicely.</p><h3>Requirements</h3><ul><li>You need a <a href="https://cloud.google.com/">Google Cloud account</a> with billing set up (you can use the free trial credit you get when signing up).</li><li>You need to have <a href="https://cloud.google.com/sdk">the Google Cloud SDK</a> installed.</li><li>You need to have <a href="https://www.terraform.io/">Terraform</a> installed.</li><li>A bash terminal. I’m using Ubuntu 22.04 but Mac OS is fine too and even Windows has a subsystem for Linux nowadays.</li></ul><h3>Create a project in Google cloud</h3><p>Go to <a href="https://console.cloud.google.com/">https://console.cloud.google.com</a> and create a new project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*DLonBnoNLGAH2Nr8sj6WKQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*E00iLedHYe1fxvlDUzuCrA.png" /></figure><p>Warning: If you pick a Project Name that is already being used as a Project ID by somebody else, Google Cloud will will be setting it to something random as <strong>custom-program-394415</strong>. Click on the blue <strong>EDIT</strong> button to give it a better name.</p><h3>Instructions</h3><p>Create a project folder, with a <strong>Terraform</strong> folder that contains a <strong>main.tf</strong> file:</p><pre>mkdir ts-appengine-terraform<br>cd ts-appengine-terraform<br>mkdir terraform<br>cd terraform<br>touch main.tf</pre><p>Paste the following content in the <strong>main.tf </strong>and fill in the <strong>project id</strong> and bucket name placeholders:</p><pre>terraform {<br>  required_providers {<br>    google = {<br>      source  = &quot;hashicorp/google&quot;<br>      version = &quot;4.51.0&quot;<br>    }<br>  }<br>  # backend &quot;gcs&quot; {<br>  #   bucket = &quot;&lt;your_bucket_name&gt;&quot;<br>  #   prefix = &quot;ts-appengine-terraform/state&quot;<br>  # }<br>}<br><br>provider &quot;google&quot; {<br>  project = var.project_name<br>  region  = &quot;us-central1&quot;<br>}<br><br>resource &quot;google_storage_bucket&quot; &quot;terraform_state&quot; {<br>  name          = &quot;&lt;your_bucket_name&gt;&quot;<br>  force_destroy = false<br>  location      = &quot;US&quot;<br>  storage_class = &quot;STANDARD&quot;<br>  versioning {<br>    enabled = true<br>  }<br>}</pre><p>Now create a <strong>variables.tf</strong> file in which we will put our project name:</p><pre>variable &quot;project_name&quot; {<br>  type = string<br>  default = &quot;&lt;your project id here&gt;&quot;<br>}</pre><h3>Initializing gcloud and Terraform</h3><ul><li>Run: gcloud init</li><li>Choose initialize (or re-initialize) the configuration</li><li>Click enter project id</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/705/1*NNTTk_pmYCA6K3PlV8BhrA.png" /></figure><p>Now that <strong>gcloud</strong> has been initialized, we can let <strong>Terraform</strong> use the same authentication connection if we enable <strong>User Default Credentials</strong> as described <a href="https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#authentication">in the Terraform docs on Authentication</a>. You can run:</p><pre>gcloud auth application-default login --project your_project_id</pre><p>Now into your terraform folder, run:</p><pre>terraform init<br>terraform plan</pre><p>If all seems well, we can run:</p><pre>terraform apply</pre><p>You can check in the cloud console if Terraform managed to create the bucket:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WZ3YE3Dar9urg0-ZjwDgTw.png" /></figure><h3>Storing our state file in the cloud (optional)</h3><p>We now created infrastructure from our local terraform script. Terraform stored a .<strong>tfstate</strong> file on our local machine to keep track of the infrastructure it manages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*_diNO54Kv_ctHTPTpiOKXg.png" /></figure><p>I’ve <a href="https://stackoverflow.com/questions/38486335/should-i-commit-tfstate-files-to-git">read</a> that it’s bad practice to commit <strong>tfstate</strong> files in Git because they might contain passwords to database connections.</p><p>The upcoming instructions will explain how to store the tfstate file in the storage bucket we just created in the cloud. This is also <a href="https://cloud.google.com/docs/terraform/resource-management/store-state">explained in the Google Cloud docs</a>.</p><p>Remove the comments of the <strong>backend “gcs”</strong> block in the <strong>main.tf</strong> and make sure it points to your bucket:</p><pre>terraform {<br>  required_providers {<br>    google = {<br>      source  = &quot;hashicorp/google&quot;<br>      version = &quot;4.51.0&quot;<br>    }<br>  }<br>  backend &quot;gcs&quot; {<br>    bucket = &quot;ts-appengine-terraform-tfstate&quot;<br>    prefix = &quot;ts-appengine-terraform/state&quot;<br>  }<br>}<br><br>provider &quot;google&quot; {<br>  project = var.project_name<br>  region  = &quot;us-central1&quot;<br>}<br><br>resource &quot;google_storage_bucket&quot; &quot;terraform_state&quot; {<br>  name          = &quot;ts-appengine-terraform-tfstate&quot;<br>  force_destroy = false<br>  location      = &quot;US&quot;<br>  storage_class = &quot;STANDARD&quot;<br>  versioning {<br>    enabled = true<br>  }<br>}</pre><p>Now we have to re-run the init command to change the tfstate file to the bucket. You’ll get a prompt about importing the local Terraform state into the bucket.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*hvtV3bxHkCiBbl8-M79o9g.png" /></figure><p>After this you can run a terraform apply and it should tell you nothing changed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1019/1*CLnK6MhnqX6GOhevTRtoFw.png" /></figure><h3>Adding an App Engine application</h3><p>Put the following code in the <strong>main.tf</strong>:</p><pre>resource &quot;google_app_engine_application&quot; &quot;ts-appengine-app&quot; {<br>  project     = var.project_name<br>  location_id = &quot;us-central&quot;<br>}<br><br>resource &quot;google_app_engine_application_url_dispatch_rules&quot; &quot;ts-appengine-app-dispatch-rules&quot; {<br>  dispatch_rules {<br>    domain = &quot;*&quot;<br>    path = &quot;/*&quot;<br>    service = &quot;default&quot;<br>  }<br>}</pre><p>Running <strong>terraform</strong> <strong>apply</strong> will create an App Engine project with no deployments yet.</p><h3>Creating the simplest Node.js application</h3><p>Run the following commands:</p><pre># Go back to your project folder (outside the terraform folder)<br>cd ..<br><br># Create a folder for the Node.js app<br>mkdir app<br>touch index.js<br>npm init<br>npm install express</pre><p>Paste the following code in your newly created <strong>index.js</strong> file:</p><pre>&#39;use strict&#39;;<br><br>const express = require(&#39;express&#39;);<br>const app = express();<br><br>app.use((req, res) =&gt; {<br>    res.status(200).send(&#39;Hello, world!&#39;);<br>});<br>// Start the server<br>const PORT = process.env.PORT || 8080;<br>app.listen(PORT, () =&gt; {<br>    console.log(`App listening on port ${PORT}`);<br>    console.log(&#39;Press Ctrl+C to quit.&#39;);<br>});</pre><p>And make sure your script start script looks like this:</p><pre>{<br>  &quot;name&quot;: &quot;runtime&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;node index.js&quot;,<br>    &quot;gcp-build&quot;: &quot;&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;dependencies&quot;: {<br>    &quot;express&quot;: &quot;^4.18.2&quot;<br>  }<br>}</pre><p>You can try running locally with npm start and browsing to <strong>localhost:8080</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/1*gInZddtpTRPZPdeNrPctOA.png" /></figure><h3>Deploying the application code via gcloud</h3><p>You can choose to only have the definition of the app engine application and resources that it needs in Terraform, but still deploy the actual code with the gcloud command (which could be ran from a cloud build pipeline).</p><p>For this we need to specify a few things in an <strong>app.yaml</strong> file in the app folder:</p><pre>runtime: nodejs20<br>service: default<br>env: standard<br>instance_class: F1<br>handlers:<br>  - url: &#39;.*&#39;<br>    script: auto<br>    secure: always<br>automatic_scaling:<br>  min_idle_instances: automatic<br>  max_idle_instances: automatic<br>  min_pending_latency: automatic<br>  max_pending_latency: automatic</pre><p>It works like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*Md9cevjpVU_lvJaw6VVLqA.png" /></figure><p>It results in a version being deployed in App Engine:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oPYLlw6iM3oHTVwFa_hezQ.png" /></figure><p>And when we click the version link we see that our Hello World! application runs fine:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*EB8dfiIJ0ywn7_JoR6-AjQ.png" /></figure><h3>Deploying the application code via Terraform</h3><p><a href="https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/app_engine_standard_app_version">According to the Terraform docs</a>, you can define App Engine versions in Terraform, so let’s try that out. You can remove the <strong>app.yaml</strong> file (and the auto-generated <strong>.gcloudignore</strong>) we just created.</p><p>First we need to zip our code. Apparently the <strong>gcloud app deploy</strong> command zips files and stores them in a storage bucket under the hood, before creating an actual App Engine version.</p><p>So first we need Terraform to create a zip file and a storage bucket by adding the following to the <strong>main.tf</strong>:</p><pre>resource &quot;google_storage_bucket&quot; &quot;app&quot; {<br>  name          = &quot;${var.project_name}-${random_id.app.hex}&quot;<br>  location      = &quot;US&quot;<br>  force_destroy = true<br>  versioning {<br>    enabled = true<br>  }<br>}<br><br>resource &quot;random_id&quot; &quot;app&quot; {<br>  byte_length = 8<br>}<br><br>data &quot;archive_file&quot; &quot;function_dist&quot; {<br>  type        = &quot;zip&quot;<br>  source_dir  = &quot;../app&quot;<br>  output_path = &quot;../app/app.zip&quot;<br>}<br><br>resource &quot;google_storage_bucket_object&quot; &quot;app&quot; {<br>  name   = &quot;app.zip&quot;<br>  source = data.archive_file.function_dist.output_path<br>  bucket = google_storage_bucket.app.name<br>}</pre><p>You can run <strong>terraform apply</strong> and verify in the cloud console that indeed a zip file ended up in a new storage bucket:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aujZYYdnnD7d1ZQcy-J9CA.png" /></figure><p>Now we can add an app version to the <strong>main.tf</strong> file:</p><pre>resource &quot;google_app_engine_standard_app_version&quot; &quot;latest_version&quot; {<br><br>  version_id = var.deployment_version<br>  service    = &quot;default&quot;<br>  runtime    = &quot;nodejs20&quot;<br><br>  entrypoint {<br>    shell = &quot;node index.js&quot;<br>  }<br><br>  deployment {<br>    zip {<br>      source_url = &quot;https://storage.googleapis.com/${google_storage_bucket.app.name}/${google_storage_bucket_object.app.name}&quot;<br>    }<br>  }<br><br>  instance_class = &quot;F1&quot;<br><br>  automatic_scaling {<br>    max_concurrent_requests = 10<br>    min_idle_instances      = 1<br>    max_idle_instances      = 3<br>    min_pending_latency     = &quot;1s&quot;<br>    max_pending_latency     = &quot;5s&quot;<br>    standard_scheduler_settings {<br>      target_cpu_utilization        = 0.5<br>      target_throughput_utilization = 0.75<br>      min_instances                 = 0<br>      max_instances                 = 4<br>    }<br>  }<br>  noop_on_destroy = true<br>  delete_service_on_destroy = true<br>}</pre><p>I recommend adding a variable called <strong>deployment_version</strong> to the <strong>variables.tf</strong> so we can make this dynamic later:</p><pre>variable &quot;deployment_version&quot; {<br>  type = string<br>}</pre><p>You can run the following command to deploy a new version <strong>2</strong>:</p><pre>terraform apply -var=&quot;deployment_version=2&quot;</pre><p>You’ll notice that this has created a second deployment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8oAvluNAx-D-H88pcP4ktg.png" /></figure><p>But something is wrong, the traffic wasn’t automatically promoted to the new version. The previous <strong>gcloud app deploy</strong> command automatically points all traffic to the new App Engine version (unless you use the --no-promoteflag).</p><p>I looked in the Terraform docs and there does not seem to find a way to set something like “promote: true” in the definition of a <strong>google_app_engine_standard_app_version</strong> resource.</p><p>They do have a <a href="https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/app_engine_service_split_traffic">traffic splitting definition</a>, but that only works if you specify both the old and new App Engine versions in the <strong>main.tf</strong> file. I don’t want to add a new App Engine version to the <strong>main.tf</strong> file every time I do a new deployment (that would cause spam in the git commit history).</p><h3>Combining Terraform and gcloud</h3><p>The gcloud command has <a href="https://cloud.google.com/sdk/gcloud/reference/app/services/set-traffic">a separate command</a> to point traffic to a certain App Engine version. So I guess we’ll have to glue <strong>Terraform</strong> and <strong>gcloud</strong> commands together with some <strong>bash</strong>.</p><p>In the root of the project, create a <strong>scripts</strong> folder. In there create a <strong>deploy.sh </strong>script. Apply chmod +x deploy.shon the file to be able to run the script. This bash code below will:</p><ul><li>Generate a version id based on a timestamp</li><li>Run Terraform to deploy a new App Engine version</li><li>Point traffic towards that version using gcloud</li></ul><pre>cd ../app || exit<br>npm install<br><br>dateString=$(date +%s)<br><br>cd ../terraform || exit<br>terraform apply -auto-approve -var=&quot;deployment_version=${dateString}&quot;<br><br>cd ../scripts || exit<br><br># Point 100% of the traffic to the new version<br>gcloud app services set-traffic default --splits=$dateString=1 --quiet</pre><p>You can find <a href="https://github.com/Leejjon/ts-appengine-terraform">the full code that includes this script on GitHub</a>.</p><p>Every time you run this script a new App Engine version is created, without deleting the old one. This is nice because if you discover something is wrong in your new version, you can simply point the traffic back to an old version that did work well.</p><h3>Conclusion</h3><p>Personally, I will use Terraform to define my App Engine application, database and any other services that my application depends on. But I will not use the <strong>google_app_engine_standard_app_version</strong> resources.</p><p>I will simply keep using the gcloud app deploy --version=x command for deploying code changes in my Node.js project. The reason here is that app engine can have multiple services. If I want to make a change in only one service, I can just do that without deploying every service. Each service could then have a separate CI/CD pipeline that only deploys that service using gcloud.</p><p>Now you know how to run the Node.js app, you can start adding TypeScript.</p><ul><li>Thank you for reading!</li><li>Leave a comment if you have questions.</li><li>Follow me on <a href="https://leejjon.medium.com/">Medium</a>/<a href="https://twitter.com/Leejjon_net">Twitter</a>/<a href="https://www.linkedin.com/in/leon-liefting-4b511a29/">LinkedIn</a> if you want to read more original programming content.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a4d5e0fd32f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Use Terraform to create an AWS Lambda function that runs your TypeScript code]]></title>
            <link>https://leejjon.medium.com/use-terraform-to-create-an-aws-lambda-function-that-runs-your-typescript-code-b805db667a93?source=rss-e00e4e1c1655------2</link>
            <guid isPermaLink="false">https://medium.com/p/b805db667a93</guid>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[bash]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Leejjon]]></dc:creator>
            <pubDate>Tue, 27 Jun 2023 23:42:09 GMT</pubDate>
            <atom:updated>2024-05-26T20:24:54.863Z</atom:updated>
            <content:encoded><![CDATA[<p>In this guide, I will show you how to quickly set up infrastructure needed to deploy <strong>TypeScript</strong> code to an <strong>AWS Lambda function</strong>.</p><p>There are times when you just want to run some code in the cloud. The fastest way usually is to “click” your way around in your cloud provider until you have something that works. Unfortunately, if you create your Lambda function in the <strong>AWS Management Console</strong>, it creates a lot of resources that you won’t know about. This makes it difficult to troubleshoot what you just configured and even harder to clean up the resources after you are done. Because most likely you were clicking around to try if something works.</p><p>Rather than doing “<strong>clickOps</strong>” you should define your infrastructure as code. There are a few possible ways to do this on AWS:</p><ul><li><strong>AWS Cloudformation</strong> — the Infra as Code solution that AWS offers. It uses <strong>JSON</strong> or <strong>YAML</strong> files to define AWS resources.</li><li><strong>AWS CDK</strong> — a library to write Infra as Code in your favourite languages such as <strong>TypeScript</strong>, Python, Java or Kotlin. In the end, it just generates one or more <strong>CloudFormation</strong> stack(s).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*jKU3RfpUChi7XFoi9YWnbA.jpeg" /></figure><ul><li><strong>AWS SAM</strong> — The Serverless Application Model is an extension of CloudFormation.</li><li><strong>Serverless framework</strong> is a framework for AWS Lambda.</li><li><strong>Terraform</strong> — The vendor agnostic Infra as Code solution by HashiCorp. It uses .tf files with the HCL (HashiCorp Configuration Language) syntax to define resources.</li></ul><h3>Why I am picking Terraform?</h3><p>As an AWS Certified Developer — Associate, I have worked a lot with CloudFormation and more recently the CDK. While they work, these skills are only ever useful on AWS. Fortunately I’m not married to AWS. As a freelancer I will work with whatever cloud my customer wants me to use. It could be AWS, Azure, Google Cloud Platform or more specialized platforms like Vercel. By learning how to use Terraform, I hope it becomes easier for me to write infrastructure as code on whatever cloud I need to use.</p><p>Besides being cloud agnostic, Terraform seems to create/update/delete resources faster than CloudFormation does and has a more concise syntax.</p><h3>What are we building?</h3><p>We are building a Terraform script that deploys a lambda function that returns a “Hello world!” message in JSON. The function will be written in TypeScript. We won’t set up full blown CI/CD, but if you know how to deploy all infrastructure from your terminal, it’s very easy to do the same in a CI/CD pipeline.</p><h3>What do you need?</h3><ul><li><a href="https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli">Install terraform</a> (I’m using Ubuntu Linux 22.04.2 LTS).</li><li><a href="https://aws.amazon.com/cli/">Install the AWS CLI</a> and configure it to point to an active AWS account. You need to have an IAM user with programmatic access.</li><li>Recommended: Use any Jetbrains IDE with the Terraform and HCL plugin to have autocompletion in your terraform files. There is also one for Visual Studio Code.</li></ul><h3>Let’s start</h3><p>Create a project folder with two subfolders:</p><ul><li>terraform (create a subfolder <strong>src</strong> in here, you’ll need it later)</li><li>lambda</li></ul><p>In the terraform folder, create a <strong>main.tf</strong> file in a folder and copy this content in it:</p><pre>terraform {<br>  required_providers {<br>    aws = {<br>      source  = &quot;hashicorp/aws&quot;<br>      version = &quot;~&gt; 4.47.0&quot;<br>    }<br>  }<br>#  backend &quot;s3&quot; {<br>#    bucket = &quot;&lt;your unique bucket name&gt;&quot;<br>#    key    = &quot;my_lambda/terraform.tfstate&quot;<br>#    region = &quot;eu-central-1&quot;<br>#  }<br>}<br><br>provider &quot;aws&quot; {<br>  region = &quot;eu-central-1&quot;<br>}<br><br>resource &quot;aws_s3_bucket&quot; &quot;terraform_state&quot; {<br>  bucket = &quot;&lt;globally unique bucket name&gt;&quot;<br><br>  # Prevent accidental deletion of this S3 bucket<br>  lifecycle {<br>    prevent_destroy = true<br>  }<br>}</pre><p>Replace the <strong>&lt;globally unique bucket name&gt;</strong> with a unique name for your S3 bucket to store your terraform state file.</p><p>Run:</p><pre>terraform init<br>terraform apply</pre><p>After the apply command your bucket should be created in AWS. Check if it is there, mine is called <strong>bucket-for-terraform-state-ts-lambda</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xjl3YWCEhAz3qqITpZePag.png" /></figure><p>Now you can uncomment the following code in <strong>main.tf </strong>(by removing the #’s) and enter the name of your bucket to start storing state:</p><pre>#  backend &quot;s3&quot; {<br>#    bucket = &quot;&lt;your unique bucket name&gt;&quot;<br>#    key    = &quot;my_lambda/terraform.tfstate&quot;<br>#    region = &quot;eu-central-1&quot;<br>#  }</pre><p>Re-run <strong>terraform init</strong> again and enter <strong>yes</strong> at the prompt.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*jFSsxOkmtNDz30mdm3rS5A.png" /></figure><p>After this step you should see a <strong>my_lambda folder</strong> that contains a <strong>terraform.tfstate</strong> file in your S3 bucket:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*gwzieAg1ijk3xwAjP49abA.png" /></figure><p>Now you can run <strong>terraform plan</strong> and <strong>terraform apply. </strong>(technically nothing changed in AWS yet, but from now on your can let a team member work on your project and use this same statefile).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1019/1*CySA221pg4g21jky2dVALg.png" /></figure><h3>Adding the Lambda function resource</h3><p>Before we can create a Lambda function, we first need to create a role in the <strong>main.tf</strong>:</p><pre>resource &quot;aws_iam_role&quot; &quot;ts_lambda_role&quot; {<br>  name               = &quot;ts_lambda-role&quot;<br>  assume_role_policy = jsonencode({<br>    Version   = &quot;2012-10-17&quot;<br>    Statement = [<br>      {<br>        Action    = &quot;sts:AssumeRole&quot;<br>        Effect    = &quot;Allow&quot;<br>        Principal = {<br>          Service = &quot;lambda.amazonaws.com&quot;<br>        }<br>      }<br>    ]<br>  })<br>}</pre><p>Next, add the Lambda function:</p><pre>resource &quot;aws_lambda_function&quot; &quot;ts_lambda&quot; {<br>  filename      = &quot;src/lambda_function_${var.lambdasVersion}.zip&quot;<br>  function_name = &quot;ts_lambda&quot;<br>  role          = aws_iam_role.ts_lambda_role.arn<br>  handler       = &quot;index.handler&quot;<br>  runtime       = &quot;nodejs18.x&quot;<br>  memory_size   = 1024<br>  timeout       = 300<br>}</pre><p>You’ll notice that there is the ${var.lambdasVersion} variable will give an error in your IDE.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*JCjzOzyJJf30bUz8gJWsPw.png" /></figure><p>In order to resolve this variable, I created a second Terraform file called <strong>input.tf</strong> in the same folder with the following content:</p><pre>variable &quot;lambdasVersion&quot; {<br>  type        = string<br>  description = &quot;version of the lambdas zip on S3&quot;<br>}</pre><h3>The code that the Lambda function needs to run</h3><p>Before we can deploy a lambda, we need to write some TypeScript. Open up a terminal and cd into the lambda folder of your project. Execute the following commands:</p><pre><br>cd yourproject/lambda<br># Create a package.json, you can use all defaults<br>npm init<br># Install typescript<br>npm i typescript -D<br># Generate a tsconfig.json file<br>tsc --init<br># Create a src folder<br>mkdir src<br># Create an index.ts file<br>touch src/index.ts</pre><p>In the <strong>index.ts</strong> file, paste this code (don’t hate me on the <strong>any</strong> type, we’ll fix that later):</p><pre>export const handler = async (<br>    event: any<br>): Promise&lt;any&gt; =&gt; {<br>    const message = &quot;Hello World!&quot;;<br>    console.log(`Returning ${message}`);<br>    return {<br>        statusCode: 200,<br>        body: JSON.stringify(message)<br>    }<br>}</pre><p>In the <strong>package.json</strong> you generated, add this <strong>build</strong> command in the <strong>scripts</strong>:</p><pre>{<br>  &quot;name&quot;: &quot;lambda&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;,<br>    &quot;build&quot;: &quot;tsc&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;devDependencies&quot;: {<br>    &quot;typescript&quot;: &quot;^5.1.3&quot;<br>  }<br>}</pre><h3>Writing a deploy script</h3><p>The ugly part of AWS Lambda (and other AWS services such as Beanstalk) is that it’s not able to just grab an npm project and run <strong>npm start</strong>. That’s why in the above package.json I don’t specify a start script.</p><p>It works with zip files like we live in the year 2000. We can create a <strong>deploy.sh</strong> file in our terraform folder to automate the creation of a zip file:</p><pre>function deploy {<br>  # Generate a version number based on a date timestamp so that it&#39;s unique<br>  TIMESTAMP=$(date +%Y%m%d%H%M%S)<br>  cd ../lambda/ &amp;&amp; \<br>  # Run the npm commands to transpile the TypeScript to JavaScript<br>  npm i &amp;&amp; \<br>  npm run build &amp;&amp; \<br>  npm prune --production &amp;&amp;\<br>  # Create a dist folder and copy only the js files to dist.<br>  # AWS Lambda does not have a use for a package.json or typescript files on runtime.<br>  mkdir dist &amp;&amp;\<br>  cp -r ./src/*.js dist/ &amp;&amp;\<br>  cp -r ./node_modules dist/ &amp;&amp;\<br>  cd dist &amp;&amp;\<br>  find . -name &quot;*.zip&quot; -type f -delete &amp;&amp; \<br>  # Zip everything in the dist folder and<br>  zip -r ../../terraform/zips/lambda_function_&quot;$TIMESTAMP&quot;.zip . &amp;&amp; \<br>  cd .. &amp;&amp; rm -rf dist &amp;&amp;\<br>  cd ../terraform &amp;&amp; \<br>  terraform plan -input=false -var lambdasVersion=&quot;$TIMESTAMP&quot; -out=./tfplan &amp;&amp; \<br>  terraform apply -input=false ./tfplan<br>}<br><br>deploy</pre><p>To run this script:</p><pre># Give the script permission to run (you only need to do this once)<br>chmod +x deploy.sh<br># To run the script<br>./deploy.sh</pre><p>Once the script has run, check out your AWS Management console to see if the Lambda function was actually created and has the correct code in your generated <strong>index.js</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*Nm96utooq51tycpplWuqfg.png" /></figure><h3>Running the Lambda</h3><p>To quickly test your Lambda you can create a test event by clicking on the blue test button. It will open a window to create a test event. You cannot define such a test event in Terraform according to <a href="https://stackoverflow.com/questions/60329800/how-can-i-create-a-lambda-event-template-via-terraform">StackOverflow</a>, so just create one via the AWS Management Console. Run the lambda with your new event by pressing the blue test button:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dQ_IPU_cfM1dgjgc-WFTBg.png" /></figure><p>The code of this first Lambda can be found on <a href="https://github.com/Leejjon/ts-lambda-terraform/tree/1.0">GitHub</a>.</p><h3>Pushing the logs to CloudWatch</h3><p>You can view the logs of your Lambda function in CloudWatch. The <strong>View CloudWatch logs</strong> button brings you right there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0p5H6t4_cynCsi4TMeiwGQ.png" /></figure><p>But wait, if you click that button it says it cannot find a log group!?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*dyDobxBkaBeTkoYD-hap8Q.png" /></figure><p>This is because we haven’t created one. To make a log group we need to add the following resources to your <strong>main.tf</strong> file:</p><pre><br>resource &quot;aws_cloudwatch_log_group&quot; &quot;ts_lambda_loggroup&quot; {<br>  name              = &quot;/aws/lambda/${aws_lambda_function.ts_lambda.function_name}&quot;<br>  retention_in_days = 3<br>}<br><br>data &quot;aws_iam_policy_document&quot; &quot;ts_lambda_policy&quot; {<br>  statement {<br>    actions = [<br>      &quot;logs:CreateLogStream&quot;,<br>      &quot;logs:PutLogEvents&quot;<br>    ]<br>    resources = [<br>      aws_cloudwatch_log_group.ts_lambda_loggroup.arn,<br>      &quot;${aws_cloudwatch_log_group.ts_lambda_loggroup.arn}:*&quot;<br>    ]<br>  }<br>}<br><br>resource &quot;aws_iam_role_policy&quot; &quot;ts_lambda_role_policy&quot; {<br>  policy = data.aws_iam_policy_document.ts_lambda_policy.json<br>  role   = aws_iam_role.ts_lambda_role.id<br>  name   = &quot;my-lambda-policy&quot;<br>}</pre><p>The first block is the log group itself. The second block is a policy document which contains the permissions that the Lambda function needs to be able to store logs in CloudWatch. The third block assigns the policy document to the role.</p><p>Run <strong>./deploy.sh</strong> again and you’ll see that the log group now exists when you click on the <strong>View CloudWatch logs </strong>button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*EGnw5Us5Ey3yb2qt3WakhQ.png" /></figure><p>If you haven’t ran the Lambda since, there will be no log streams. So go to the Lambda function and click on the blue Test button to run it. A log stream will appear that shows the console.log message from your <strong>index.ts</strong> file along with some information on how many milliseconds your lambda took to finish.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E1QQJfzE-dZ5-JKRroFmWQ.png" /></figure><p>The code up to this point can be found <a href="https://github.com/Leejjon/ts-lambda-terraform/tree/1.1">here on GitHub</a>.</p><h3>Triggering the Lambda via a public URL</h3><p>While it’s easy to do quick tests with the blue test button, you most likely want to trigger your Lambda function by calling an HTTP endpoint.</p><p>If you’re going to build an entire API, you might want to <a href="https://aws.amazon.com/api-gateway/">define an API gateway</a>. For a single HTTP endpoint, there is something simpler called <a href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html">a Lambda function URL</a>. Let’s define one for our function in the <strong>main.tf</strong>:</p><pre>resource &quot;aws_lambda_function_url&quot; &quot;ts_lambda_funtion_url&quot; {<br>  function_name      = aws_lambda_function.ts_lambda.id<br>  authorization_type = &quot;NONE&quot;<br>}</pre><p>After you run the <strong>./deploy.sh</strong> script again you should see the Function URL appear in the AWS Management Console. Click on it to execute it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jY0uVmz1mQtsRXzwo-SYPw.png" /></figure><p><a href="https://github.com/Leejjon/ts-lambda-terraform/tree/1.2">Here</a> you can find the source code up to this point.</p><p>While you can visit the Function URL in your browser, the Lambda doesn’t allow to be fetched from JavaScript by default. You can see this if you create an <strong>index.html</strong> file with the following content:</p><pre>&lt;html&gt;<br>&lt;head&gt;&lt;title&gt;Test to fetch data from Lambda&lt;/title&gt;&lt;/head&gt;<br>&lt;body&gt;<br>&lt;script&gt;<br>    fetch(&quot;https://dentg3vcgg27xxqzzu5wifohga0jmvfd.lambda-url.eu-central-1.on.aws/&quot;).then(response =&gt; console.log(response.status))<br>&lt;/script&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>If you open this in a browser you’ll see the CORS (Cross-Origin Resource Sharing) policy error in the console:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/909/1*U8FOUieYh7bjHyCByw3nJA.png" /></figure><p>We can allow requests from all origins in our <strong>main.tf</strong>:</p><pre>resource &quot;aws_lambda_function_url&quot; &quot;ts_lambda_funtion_url&quot; {<br>  function_name      = aws_lambda_function.ts_lambda.id<br>  authorization_type = &quot;NONE&quot;<br>  cors {<br>    allow_origins = [&quot;*&quot;]<br>  }<br>}</pre><p>Once you run the <strong>./deploy.sh</strong> script you can refresh your <strong>index.html</strong> file in your browser you see that it starts working:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/917/1*6iN-6xO0bJZZlg1Y6AMkrQ.png" /></figure><h3>Fixing the types</h3><p>Now why would we want to use TypeScript if our <strong>index.ts</strong> contains the <strong>any</strong> type on both the incoming request event and the response object?</p><pre>export const handler = async (<br>    event: any<br>): Promise&lt;any&gt; =&gt; {<br>    const message = &quot;Hello World!&quot;;<br>    console.log(`Returning ${message}`);<br>    return {<br>        statusCode: 200,<br>        body: JSON.stringify(message)<br>    }<br>}</pre><p>You can read <a href="https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html#event-types">in the official AWS Lambda documentation</a> that they haven’t created type definitions for AWS Lambda events. There is an open source library that contains types for most of the events. You can install it with:</p><pre>npm install -D @types/aws-lambda</pre><p>Even with this library installed, the structure of the event parameter depends on how the Lambda is triggered. We just triggered our lambda via the Function URL. <a href="https://stackoverflow.com/questions/76445727/aws-lambda-function-url-event-type">According to StackOverflow</a> a Function URL gives an event with the APIGatewayProxyEventV2 type. The response object will be a APIGatewayProxyResultV2&lt;T&gt; type. Let’s apply the types:</p><pre>import {APIGatewayProxyEventV2, APIGatewayProxyResultV2} from &quot;aws-lambda&quot;;<br><br>export const handler = async (<br>    event: APIGatewayProxyEventV2<br>): Promise&lt;APIGatewayProxyResultV2&lt;string&gt;&gt; =&gt; {<br>    const message = &quot;Hello World!&quot;;<br>    console.log(`The event: ${JSON.stringify(event)}`);<br>    console.log(`Returning ${message}`);<br>    return {<br>        statusCode: 200,<br>        body: JSON.stringify(message),<br>    } as APIGatewayProxyResultV2&lt;string&gt;;<br>}</pre><p>Thanks to the types, we now have code completion of what is expected in the event:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*ctd6LiNXJwYdCqfHUl5zZg.png" /></figure><p>I added a log line to see the full event. When I called the URL from the browser I saw the following event in the CloudWatch logs:</p><pre>INFO The event: <br>{<br>    &quot;version&quot;: &quot;2.0&quot;,<br>    &quot;routeKey&quot;: &quot;$default&quot;,<br>    &quot;rawPath&quot;: &quot;/&quot;,<br>    &quot;rawQueryString&quot;: &quot;&quot;,<br>    &quot;headers&quot;: {<br>        &quot;sec-fetch-mode&quot;: &quot;navigate&quot;,<br>        &quot;x-amzn-tls-version&quot;: &quot;TLSv1.2&quot;,<br>        &quot;sec-fetch-site&quot;: &quot;none&quot;,<br>        &quot;accept-language&quot;: &quot;en-US,en;q=0.5&quot;,<br>        &quot;x-forwarded-proto&quot;: &quot;https&quot;,<br>        &quot;invitation&quot;: &quot;felix&quot;,<br>        &quot;x-forwarded-port&quot;: &quot;443&quot;,<br>        &quot;x-forwarded-for&quot;: &quot;2a02:a45f:5e20:1:5ba6:8963:aecb:99d5&quot;,<br>        &quot;sec-fetch-user&quot;: &quot;?1&quot;,<br>        &quot;accept&quot;: &quot;text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8&quot;,<br>        &quot;x-amzn-tls-cipher-suite&quot;: &quot;ECDHE-RSA-AES128-GCM-SHA256&quot;,<br>        &quot;x-amzn-trace-id&quot;: &quot;Root=1-649b68d4-7289361a13bc41416a14e044&quot;,<br>        &quot;host&quot;: &quot;dentg3vcgg27xxqzzu5wifohga0jmvfd.lambda-url.eu-central-1.on.aws&quot;,<br>        &quot;upgrade-insecure-requests&quot;: &quot;1&quot;,<br>        &quot;accept-encoding&quot;: &quot;gzip, deflate, br&quot;,<br>        &quot;sec-fetch-dest&quot;: &quot;document&quot;,<br>        &quot;user-agent&quot;: &quot;Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/113.0&quot;<br>    },<br>    &quot;requestContext&quot;: {<br>        &quot;accountId&quot;: &quot;anonymous&quot;,<br>        &quot;apiId&quot;: &quot;dentg3vcgg27xxqzzu5wifohga0jmvfd&quot;,<br>        &quot;domainName&quot;: &quot;dentg3vcgg27xxqzzu5wifohga0jmvfd.lambda-url.eu-central-1.on.aws&quot;,<br>        &quot;domainPrefix&quot;: &quot;dentg3vcgg27xxqzzu5wifohga0jmvfd&quot;,<br>        &quot;http&quot;: {<br>            &quot;method&quot;: &quot;GET&quot;,<br>            &quot;path&quot;: &quot;/&quot;,<br>            &quot;protocol&quot;: &quot;HTTP/1.1&quot;,<br>            &quot;sourceIp&quot;: &quot;2a02:a45f:5e20:1:5ba6:8963:aecb:99d5&quot;,<br>            &quot;userAgent&quot;: &quot;Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/113.0&quot;<br>        },<br>        &quot;requestId&quot;: &quot;cc36778c-7c91-422e-81a0-691fc5f66a4d&quot;,<br>        &quot;routeKey&quot;: &quot;$default&quot;,<br>        &quot;stage&quot;: &quot;$default&quot;,<br>        &quot;time&quot;: &quot;27/Jun/2023:22:55:16 +0000&quot;,<br>        &quot;timeEpoch&quot;: 1687906516726<br>    },<br>    &quot;isBase64Encoded&quot;: false<br>}</pre><p>If you define the APIGatewayProxyEventV2 type you have to make sure you’ll only invoke your Lambda from the Function URL. If you for example try again from the blue test button, the event will be:</p><pre>INFO The event: {&quot;key1&quot;:&quot;value1&quot;,&quot;key2&quot;:&quot;value2&quot;,&quot;key3&quot;:&quot;value3&quot;} </pre><p>Of course you can update the test event to contain an event that matches the type that our Lambda now expects.</p><h3>My conclusions</h3><ul><li>Once you have set up Terraform with a state file in S3, it’s very fast and easy to modify the infrastructure.</li><li>Lambda is good to quickly host a simple function as stateless back-end.</li><li>It’s unfortunate that you have to <strong>zip</strong> the files in your application, so you need a little bash scripting to create a .zip file and hand it to <strong>Terraform</strong>.</li><li>Even though it’s not maintained by AWS, they do recommend <a href="https://www.npmjs.com/package/@types/aws-lambda">the <strong>@types/aws-lambda</strong> library</a> to give types to your requests and responses.</li></ul><p>You can find <a href="https://github.com/Leejjon/ts-lambda-terraform">the final code on my GitHub</a>.</p><ul><li>Thank you for reading!</li><li>Leave a comment if you have questions.</li><li>Follow me on <a href="https://leejjon.medium.com/">Medium</a>/<a href="https://twitter.com/Leejjon_net">Twitter</a>/<a href="https://www.linkedin.com/in/leon-liefting-4b511a29/">LinkedIn</a> if you want to read more original programming content.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b805db667a93" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I stopped using the useEffect hook to fetch data. React Query works, but is it easier?]]></title>
            <link>https://leejjon.medium.com/react-typescript-i-stopped-using-the-useeffect-hook-to-fetch-data-f830c1186b71?source=rss-e00e4e1c1655------2</link>
            <guid isPermaLink="false">https://medium.com/p/f830c1186b71</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[useeffect]]></category>
            <category><![CDATA[react-router]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[react-query]]></category>
            <dc:creator><![CDATA[Leejjon]]></dc:creator>
            <pubDate>Sat, 29 Apr 2023 22:16:49 GMT</pubDate>
            <atom:updated>2023-05-10T21:17:48.038Z</atom:updated>
            <content:encoded><![CDATA[<p>Just like many, when I started to build React.js web apps I used the <strong>useEffect</strong> hook to fetch data <a href="https://legacy.reactjs.org/docs/hooks-faq.html#how-can-i-do-data-fetching-with-hooks">because the (now old) documentation</a> said so.</p><p>A few months ago I stumbled on this article below about the <strong>useEffect</strong> hook and data fetching:</p><p><a href="https://articles.wesionary.team/why-useeffect-is-a-bad-place-to-make-api-calls-98a606735c1c">Why useEffect is a bad place to make API calls</a></p><p>That article has valid points. I could immediately jump to its conclusion and switch to <strong>React Query</strong>, Vercel’s <strong>SWR</strong> or the <strong>Apollo</strong> HTTP client.</p><p>However, it’s usually a good learning experience to not jump to solutions. Instead you should first encounter the problems, isolate them in a hello world project, understand them and look for the best solution. If you jump to a framework that promises lots of solutions for problems you’re not even sure about whether you’re going to have them, you might add a lot complexity.</p><p>So read on if you want to take a look at the problem(s) with me!</p><h3>My starting point</h3><p>Here is an example of a React App created with <a href="https://create-react-app.dev/">create-react-app</a>. It is fetching football matches from <a href="https://www.football-data.org/"><em>api.football-data.org</em></a><em> </em>with the <strong>useEffect</strong> hook and display them in a React component:</p><pre>import React, {useEffect, useState} from &#39;react&#39;;<br>import &#39;./App.css&#39;;<br>import {Match} from &quot;./model/Match&quot;;<br>import {getMatchesFromApi} from &quot;./api/ApiCall&quot;;<br><br>function App() {<br>  const [error, setError] = useState&lt;string | undefined&gt;(undefined);<br>  const [matches, setMatches] = useState&lt;Array&lt;Match&gt;&gt;([]);<br>  useEffect(() =&gt; {<br>    if (matches.length === 0) {<br>      getMatchesFromApi(2021)<br>          .then(matches =&gt; setMatches(matches))<br>          .catch((e) =&gt; setError(&quot;Could not fetch the matches.&quot;));<br>    }<br>  }, [matches, setMatches]);<br>  if (error) {<br>    return (<br>        &lt;div className=&quot;App&quot;&gt;{error}&lt;/div&gt;<br>    );<br>  }<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;ul&gt;<br>        {matches.map((match, index) =&gt; {<br>          return (<br>              &lt;li key={index}&gt;<br>                &lt;p&gt;{match.homeTeam.name}&amp;nbsp;-&amp;nbsp;{match.awayTeam.name}&lt;/p&gt;<br>              &lt;/li&gt;<br>          );<br>        })}<br>      &lt;/ul&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default App;</pre><p>This is how the app works when you run it locally:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*O9PwbxjbSVLWXJVS8xRoLg.png" /><figcaption>You can try this code locally if you clone it from <a href="https://github.com/Leejjon/simplest-fetch-in-useeffect">GitHub</a>. You will need to create a .env.local file and put your own (free) api key from <a href="https://www.football-data.org/">football-data.org</a> in there.</figcaption></figure><p>This code has a few small problems that I could initially live with for hobby apps:</p><ul><li>If you run this locally with strict mode on (default in create-react-app) it will fetch the data twice. You can work around it by removing the &lt;React.StrictMode&gt; tags in your index.tsx file.</li><li>It needs some more code to show a spinner while the data is fetching.</li><li>It only starts fetching the data when the component has loaded.</li></ul><p>Regarding that last thing, let’s take a look at the performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VfMWxjVPQGZqo5-2iaKh8g.png" /></figure><p>In the waterfall from the Chrome screenshot, we see that the React app takes under 400ms to download<strong>.</strong> The content of the DOM is only loaded after ~550ms. That means it takes less than 200ms to start up React and all our components. Then it takes another ~800 ms to load the matches from the football API.</p><h3>Will React Query fetch data faster?</h3><p>I decided replace the <strong>useEffect</strong> hook with the <strong>useQuery</strong> hook from <strong>React Query</strong>, as it seems React Query is being recommended a lot. I’ve <a href="https://github.com/Leejjon/simplest-fetch-in-useeffect/tree/add-reactquery-to-first-example">prepared a separate branch of the same code</a>, that uses this <strong>useQuery</strong> hook instead.</p><p>In the <strong>index.tsx</strong> I’m adding a few things:</p><pre>import React from &#39;react&#39;;<br>import ReactDOM from &#39;react-dom/client&#39;;<br>import &#39;./index.css&#39;;<br>import App from &#39;./App&#39;;<br>import reportWebVitals from &#39;./reportWebVitals&#39;;<br>import {QueryClient, QueryClientProvider} from &quot;@tanstack/react-query&quot;;<br>import {getMatchesFromApi} from &quot;./api/ApiCall&quot;;<br><br>const matchesQueryClient = new QueryClient();<br><br>export const matchesQuery = () =&gt; (<br>    {<br>        queryKey: [&quot;matches&quot;],<br>        queryFn: async () =&gt; { return await getMatchesFromApi(2021)},<br>    }<br>);<br><br>const root = ReactDOM.createRoot(<br>  document.getElementById(&#39;root&#39;) as HTMLElement<br>);<br>root.render(<br>  &lt;React.StrictMode&gt;<br>      &lt;QueryClientProvider client={matchesQueryClient}&gt;<br>        &lt;App /&gt;<br>      &lt;/QueryClientProvider&gt;<br>  &lt;/React.StrictMode&gt;<br>);<br><br>reportWebVitals();</pre><p>And in the <strong>App.tsx</strong> we replace the <strong>useEffect</strong> hook with a <strong>useQuery</strong> hook:</p><pre>import React from &#39;react&#39;;<br>import &#39;./App.css&#39;;<br>import {Match} from &quot;./model/Match&quot;;<br>import {matchesQuery} from &quot;./index&quot;;<br>import {useQuery} from &quot;@tanstack/react-query&quot;;<br><br>function App() {<br>  const {data} = useQuery({<br>    ...matchesQuery(),<br>    refetchOnWindowFocus: false,<br>    refetchOnMount: false,<br>    cacheTime: 5000,<br>    staleTime: 4000,<br>    retry: false,<br>  });<br>  const matches: Array&lt;Match&gt; = data ?? [];<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br><br>        &lt;ul&gt;<br>          {matches.map((match, index) =&gt; {<br>            return (<br>                &lt;li key={index}&gt;<br>                  &lt;p&gt;{match.homeTeam.name}&amp;nbsp;-&amp;nbsp;{match.awayTeam.name}&lt;/p&gt;<br>                &lt;/li&gt;<br>            );<br>          })}<br>        &lt;/ul&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default App;</pre><p>When we run this thing, it works as expected. However the entire call takes even longer!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W-Mu-QoPNw-Ycgswn7z5eg.png" /></figure><p>I’m not sure if adding React Query to our application increased the bundle size and thus slows things down. Let me know in the comments if you think there is another reason.</p><p>My conclusion for now is:</p><ul><li>React Query does not fetches data faster than if you would do it from a <strong>useEffect</strong> hook. It might even use the <strong>useEffec</strong>t hook under the hood.</li><li>Use React Query if you need the extra features it offers for data fetching, such as caching.</li></ul><h3>Fetching data based on react-router routes</h3><p>The above app is simple. It has one page that always fetches the matches data. Most web apps have multiple pages and use a routing library like React Router. In the real world different pages would usually fetch different data. I prepared <a href="https://github.com/Leejjon/simplest-fetch-in-useeffect/tree/withrouter/src">a branch with multiple pages and the routes defined with react router</a>.</p><p>Let’s create the following routes:</p><ul><li><strong>/</strong> The home page</li><li><strong>/matches</strong> A page that displays matches</li><li><strong>/matches2</strong> Another page that displays matches</li></ul><p>We need to move the matches logic to a new file called <strong>Matches.tsx </strong>in a pages folder:</p><pre>import {Match} from &quot;../model/Match&quot;;<br>import React, {useEffect} from &quot;react&quot;;<br>import {getMatchesFromApi} from &quot;../api/ApiCall&quot;;<br><br>export interface MatchesProps {<br>    matches: Array&lt;Match&gt;;<br>    setMatches: (matches: Array&lt;Match&gt;) =&gt; void;<br>}<br><br>function Matches({matches, setMatches}: MatchesProps) {<br>    useEffect(() =&gt; {<br>        if (matches.length === 0) {<br>            getMatchesFromApi(2021)<br>                .then(matches =&gt; {<br>                   setMatches(matches);<br>                   setError(undefined);<br>                })<br>                .catch((e) =&gt; setError(&quot;Could not fetch the matches.&quot;));<br>        }<br>    }, [matches, setMatches]);<br><br>    if (error) {<br>      return (<br>          &lt;div className=&quot;App&quot;&gt;{error}&lt;/div&gt;<br>      );<br>    }<br><br>    return (<br>        &lt;ul&gt;<br>            {matches.map((match, index) =&gt; {<br>                return (<br>                    &lt;li key={index}&gt;<br>                        &lt;p&gt;{match.homeTeam.name}&amp;nbsp;-&amp;nbsp;{match.awayTeam.name}&lt;/p&gt;<br>                    &lt;/li&gt;<br>                );<br>            })}<br>        &lt;/ul&gt;<br>    );<br>}<br><br>export default Matches;</pre><p>Simply duplicate this <strong>Matches.tsx</strong> file to a <strong>Matches2.tsx</strong> file with almost identical code. Also create a nice <strong>Home.tsx</strong>:</p><pre>function Home() {<br>    return (<br>        &lt;div&gt;Home sweet home.&lt;/div&gt;<br>    );<br>}<br><br>export default Home;</pre><p>After installing react router with npm i react-router-dom @types/react-router-dom we can define the routes in <strong>App.tsx</strong>:</p><pre>import React, {useState} from &#39;react&#39;;<br>import &#39;./App.css&#39;;<br>import {Match} from &quot;./model/Match&quot;;<br>import {BrowserRouter, Link, Route, Routes} from &quot;react-router-dom&quot;;<br>import Home from &quot;./pages/Home&quot;;<br>import Matches from &quot;./pages/Matches&quot;;<br>import Matches2 from &quot;./pages/Matches2&quot;;<br><br>function App() {<br>  const [error, setError] = useState&lt;string | undefined&gt;(undefined);<br>  const [matches, setMatches] = useState&lt;Array&lt;Match&gt;&gt;([]);<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;BrowserRouter&gt;<br>        &lt;Link to=&quot;/&quot;&gt;Home&lt;/Link&gt;&lt;br/&gt;<br>        &lt;Link to=&quot;/matches&quot;&gt;Matches&lt;/Link&gt;&lt;br/&gt;<br>        &lt;Link to=&quot;/matches2&quot;&gt;Matches2&lt;/Link&gt;&lt;br/&gt;<br>        &lt;Routes&gt;<br>          &lt;Route path=&quot;/&quot; element={&lt;Home/&gt;} /&gt;<br>          &lt;Route path=&quot;/matches&quot; element={<br>            &lt;Matches matches={matches} setMatches={setMatches} error={error} setError={setError}/&gt;<br>          }/&gt;<br>          &lt;Route path=&quot;/matches2&quot; element={<br>            &lt;Matches2 matches={matches} setMatches={setMatches} error={error} setError={setError}/&gt;<br>          }/&gt;<br>        &lt;/Routes&gt;<br>      &lt;/BrowserRouter&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default App;</pre><p>Going to the home page no longer fetches the matches:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vldsGiG72TCBhKW3TpDRxA.png" /></figure><p>Navigating to /matches or /matches2 does trigger the fetch. If we navigate between them, no re-fetch is happening (which is nice and efficient).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cjb0xb-rHETEijv82DcSPw.png" /><figcaption>The above performance is from when you do a hard refresh on the matches route. You can clone <a href="https://github.com/Leejjon/simplest-fetch-in-useeffect/tree/withrouter">this version of the code here</a></figcaption></figure><h3>React Router loaders</h3><p><a href="https://articles.wesionary.team/why-useeffect-is-a-bad-place-to-make-api-calls-98a606735c1c">The article I mentioned above</a> has a short mention about using React Router <strong>loaders</strong> instead of using the <strong>useEffect</strong> hook.</p><p><a href="https://reactrouter.com/en/dev/route/loader">The React Router loader docs</a> mention:</p><p><em>Each route can define a “loader” function to provide data to the route element before it renders.</em></p><p>Does this mean we can increase performance by letting a loader do the fetching before our element renders?</p><p>At first glance it looks as if you can simply add loaders to the Routes in the JSX:</p><pre>import React from &#39;react&#39;;<br>import &#39;./App.css&#39;;<br>import {BrowserRouter, Link, Route, Routes} from &quot;react-router-dom&quot;;<br>import Home from &quot;./pages/Home&quot;;<br>import Matches from &quot;./pages/Matches&quot;;<br>import Matches2 from &quot;./pages/Matches2&quot;;<br>import {getMatchesFromApi} from &quot;./api/ApiCall&quot;;<br><br>function App() {<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;BrowserRouter&gt;<br>        &lt;Link to=&quot;/&quot;&gt;Home&lt;/Link&gt;&lt;br/&gt;<br>        &lt;Link to=&quot;/matches&quot;&gt;Matches&lt;/Link&gt;&lt;br/&gt;<br>        &lt;Link to=&quot;/matches2&quot;&gt;Matches2&lt;/Link&gt;&lt;br/&gt;<br>        &lt;Routes&gt;<br>          &lt;Route path=&quot;/&quot; element={&lt;Home/&gt;} /&gt;<br>          &lt;Route path=&quot;/matches&quot; element={&lt;Matches /&gt;} <br>             loader={ async () =&gt; { return await getMatchesFromApi(2021)} } /&gt;<br>          &lt;Route path=&quot;/matches2&quot; element={&lt;Matches2 /&gt;} <br>             loader={ async () =&gt; { return await getMatchesFromApi(2021)} } /&gt;<br>        &lt;/Routes&gt;<br>      &lt;/BrowserRouter&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default App;</pre><p>In the Matches component you should be able to use the <strong>useLoaderData</strong> hook to retrieve the data from the loader:</p><pre>import {Match} from &quot;../model/Match&quot;;<br>import React from &quot;react&quot;;<br>import {useLoaderData} from &quot;react-router-dom&quot;;<br><br>function Matches() {<br>    const matches: Array&lt;Match&gt; = useLoaderData() as Array&lt;Match&gt;;<br><br>    return (<br>        &lt;ul&gt;<br>            {matches.map((match, index) =&gt; {<br>                return (<br>                    &lt;li key={index}&gt;<br>                        &lt;p&gt;{match.homeTeam.name}&amp;nbsp;-&amp;nbsp;{match.awayTeam.name}&lt;/p&gt;<br>                    &lt;/li&gt;<br>                );<br>            })}<br>        &lt;/ul&gt;<br>    );<br>}<br><br>export default Matches;</pre><p>Unfortunately, if you try to run <a href="https://github.com/Leejjon/simplest-fetch-in-useeffect/tree/withrouter-loader-in-jsx">this code</a> you’ll get the error <strong>useLoaderData must be used within a data router</strong>, as explained in <a href="https://stackoverflow.com/questions/73979282/useloaderdata-must-be-used-within-a-data-router">this StackOverflow post</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t8RlIOpdbxNPz2dOwXG-gg.png" /></figure><p>To solve this, we have to use the <strong>createBrowserRouter</strong> function to define our Routes. This can be quite an overhaul if you have a lot of routes, let’s apply it on this test project so you get an idea.</p><p>Start with defining the router in <strong>index.tsx</strong> file:</p><pre>import React from &#39;react&#39;;<br>import ReactDOM from &#39;react-dom/client&#39;;<br>import &#39;./index.css&#39;;<br>import reportWebVitals from &#39;./reportWebVitals&#39;;<br>import {createBrowserRouter, RouterProvider} from &quot;react-router-dom&quot;;<br>import App from &quot;./App&quot;;<br>import Matches from &quot;./pages/Matches&quot;;<br>import Matches2 from &quot;./pages/Matches2&quot;;<br>import {getMatchesFromApi} from &quot;./api/ApiCall&quot;;<br><br>const router = createBrowserRouter([<br>    {<br>        path: &quot;/&quot;,<br>        element: &lt;App/&gt;,<br>        children: [<br>            {<br>                index: true,<br>                element: &lt;Home/&gt;<br>            },<br>            {<br>                path: &quot;/matches&quot;,<br>                element: &lt;Matches/&gt;,<br>                loader: async () =&gt; { return await getMatchesFromApi(2021)}<br>            },<br>            {<br>                path: &quot;/matches2&quot;,<br>                element: &lt;Matches2 /&gt;,<br>                loader: async () =&gt; { return await getMatchesFromApi(2021)}<br>            }<br>        ]<br>    }<br>]);<br><br>const root = ReactDOM.createRoot(<br>  document.getElementById(&#39;root&#39;) as HTMLElement<br>);<br><br>root.render(<br>  &lt;React.StrictMode&gt;<br>    &lt;RouterProvider router={router} /&gt;<br>  &lt;/React.StrictMode&gt;<br>);<br><br>reportWebVitals();</pre><p>Our <strong>App.tsx</strong> file will still contain the navigation Link elements, and also an <strong>&lt;Outlet/&gt;</strong> element in which the loaded page content will be loaded:</p><pre>import React from &#39;react&#39;;<br>import &#39;./App.css&#39;;<br>import {Link, Outlet} from &quot;react-router-dom&quot;;<br><br>function App() {<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;Link to=&quot;/&quot;&gt;Home&lt;/Link&gt;&lt;br/&gt;<br>      &lt;Link to=&quot;/matches&quot;&gt;Matches&lt;/Link&gt;&lt;br/&gt;<br>      &lt;Link to=&quot;/matches2&quot;&gt;Matches2&lt;/Link&gt;&lt;br/&gt;<br>      &lt;Outlet/&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default App;</pre><p>Now run the application again (find <a href="https://github.com/Leejjon/simplest-fetch-in-useeffect/tree/withrouterandloader-in-code">this version of the code here</a>):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4fLNRtGensQTtdZmE-XkOQ.png" /></figure><h3>After using loaders</h3><p>By fetching data inside the react router loaders we have successfully removed the <strong>useEffect</strong> clauses. And our code works, but the performance didn’t improve and we’ve lost a bit of control.</p><p>Pro’s:</p><ul><li>No more duplicate fetching in strict mode locally.</li><li>The routes are defined in code, which could be a little bit more flexible. If you prefer to define your routes in JSX and still use loaders, you can use <a href="https://reactrouter.com/en/dev/utils/create-routes-from-elements">the createRoutesFromElements function</a>.</li></ul><p>Con’s:</p><ul><li>We’ve had to refactor our entire routing, and performance didn’t improve. <strong>It might still improve your performance</strong> if you have very heavy rendering (due to many nested components), but in this simple example, it’s <strong>premature optimization</strong>.</li><li>We’ve lost the simple caching our <strong>useEffect</strong> &amp; <strong>useState</strong> solution offered. Which means that browsing from /matches to /matches2 will do a re-fetch of the matches data.</li><li>We have to do an unsafe cast from the <strong>useLoaderData</strong> hook:</li></ul><pre>const matches: Array&lt;Match&gt; = useLoaderData() as Array&lt;Match&gt;;</pre><p>If you look at the declaration in the react-router types, you’ll see that the hook doesn’t have a way to use the return type from the loader in the <strong>useLoaderData</strong> hook:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1020/1*29oqcbJkHG2E7TAzydEXvQ.png" /></figure><h3>Improving it with React Query</h3><p>I’d suggest to use React Query instead of passing raw fetch functions to React Router loaders.</p><p>The author of React Query has <a href="https://tkdodo.eu/blog/react-query-meets-react-router">written a great guide to use React Query with React Router loaders</a>, but I’ll show how to apply it on our example anyway. And of course, you can find <a href="https://github.com/Leejjon/simplest-fetch-in-useeffect/tree/withrouterloaderandreactquery/src">the code on GitHub in a separate branch</a>.</p><p>In the <strong>index.tsx</strong>, we need to define our query client, the query and the query loader:</p><pre>import React from &#39;react&#39;;<br>import ReactDOM from &#39;react-dom/client&#39;;<br>import &#39;./index.css&#39;;<br>import reportWebVitals from &#39;./reportWebVitals&#39;;<br>import {createBrowserRouter, RouterProvider} from &quot;react-router-dom&quot;;<br>import App from &quot;./App&quot;;<br>import Matches from &quot;./pages/Matches&quot;;<br>import Matches2 from &quot;./pages/Matches2&quot;;<br>import {getMatchesFromApi} from &quot;./api/ApiCall&quot;;<br>import {QueryClient, QueryClientProvider} from &quot;@tanstack/react-query&quot;;<br>import {Match} from &quot;./model/Match&quot;;<br>import Home from &quot;./pages/Home&quot;;<br><br>const queryClient = new QueryClient();<br><br>export const matchesQuery = () =&gt; (<br>    {<br>        queryKey: [&quot;matches&quot;],<br>        queryFn: async () =&gt; { return await getMatchesFromApi(2021)},<br>    }<br>);<br><br>const matchesQueryLoader = async (queryClient: QueryClient): Promise&lt;Array&lt;Match&gt;&gt; =&gt; {<br>    return await queryClient.ensureQueryData(matchesQuery());<br>};<br><br>const router = createBrowserRouter([<br>    {<br>        path: &quot;/&quot;,<br>        element: &lt;App/&gt;,<br>        children: [<br>            {<br>                index: true,<br>                element: &lt;Home/&gt;<br>            },<br>            {<br>                path: &quot;/matches&quot;,<br>                element: &lt;Matches/&gt;,<br>                // If there is an error in the loader, load the page anyway because it can render without data.<br>                errorElement: &lt;Matches/&gt;,<br>                loader: async () =&gt; { return matchesQueryLoader(queryClient) }<br>            },<br>            {<br>                path: &quot;/matches2&quot;,<br>                element: &lt;Matches2 /&gt;,<br>                errorElement: &lt;Matches2 /&gt;,<br>                loader: async () =&gt; { return matchesQueryLoader(queryClient)}<br>            }<br>        ]<br>    }<br>]);<br><br>const root = ReactDOM.createRoot(<br>  document.getElementById(&#39;root&#39;) as HTMLElement<br>);<br><br>root.render(<br>  &lt;React.StrictMode&gt;<br>      &lt;QueryClientProvider client={queryClient}&gt;<br>        &lt;RouterProvider router={router} /&gt;<br>      &lt;/QueryClientProvider&gt;<br>  &lt;/React.StrictMode&gt;<br>);<br><br>reportWebVitals();</pre><p>In our <strong>Matches.tsx</strong> and <strong>Matches2.tsx</strong> add the <strong>useQuery</strong> hook to get the matches data:</p><pre>import {Match} from &quot;../model/Match&quot;;<br>import React from &quot;react&quot;;<br>import {useQuery} from &quot;@tanstack/react-query&quot;;<br>import {matchesQuery} from &quot;../index&quot;;<br><br>function Matches() {<br>    const {data, isError, isPaused} = useQuery({<br>        ...matchesQuery(),<br>        refetchOnWindowFocus: false,<br>        refetchOnMount: false,<br>        cacheTime: 5000,<br>        staleTime: 4000,<br>        retry: false,<br>    });<br>    const matches: Array&lt;Match&gt; = data ?? [];<br><br>    // We use isPaused because if there is no internet react query will be paused automatically.<br>    if (isError || isPaused) {<br>        return (<br>            &lt;div className=&quot;App&quot;&gt;Could not load matches.&lt;/div&gt;<br>        );<br>    }<br><br>    return (<br>        &lt;ul&gt;<br>            {matches.map((match, index) =&gt; {<br>                return (<br>                    &lt;li key={index}&gt;<br>                        &lt;p&gt;{match.homeTeam.name}&amp;nbsp;-&amp;nbsp;{match.awayTeam.name}&lt;/p&gt;<br>                    &lt;/li&gt;<br>                );<br>            })}<br>        &lt;/ul&gt;<br>    );<br>}<br><br>export default Matches;</pre><p>If we try this code, we see that it works nicely and doesn’t refresh if you navigate between the Matches and Matches2 pages. But the performance still stays around the same.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K3ZSPeuRf4ynHoTh1RiViA.png" /></figure><p>Even though this mix of React Router and React Query works in the end, I kind of dislike the way my code looks now. I feel that <a href="https://github.com/Leejjon/simplest-fetch-in-useeffect/tree/withrouter">the original react router code</a> was really easy to read for somebody who never used React Router before:</p><pre>import React, {useState} from &#39;react&#39;;<br>import &#39;./App.css&#39;;<br>import {Match} from &quot;./model/Match&quot;;<br>import {BrowserRouter, Link, Route, Routes} from &quot;react-router-dom&quot;;<br>import Home from &quot;./pages/Home&quot;;<br>import Matches from &quot;./pages/Matches&quot;;<br>import Matches2 from &quot;./pages/Matches2&quot;;<br><br>function App() {<br>  const [matches, setMatches] = useState&lt;Array&lt;Match&gt;&gt;([]);<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;BrowserRouter&gt;<br>        &lt;Link to=&quot;/&quot;&gt;Home&lt;/Link&gt;&lt;br/&gt;<br>        &lt;Link to=&quot;/matches&quot;&gt;Matches&lt;/Link&gt;&lt;br/&gt;<br>        &lt;Link to=&quot;/matches2&quot;&gt;Matches2&lt;/Link&gt;&lt;br/&gt;<br>        &lt;Routes&gt;<br>          &lt;Route path=&quot;/&quot; element={&lt;Home/&gt;} /&gt;<br>          &lt;Route path=&quot;/matches&quot; element={&lt;Matches matches={matches} setMatches={setMatches}/&gt;}/&gt;<br>          &lt;Route path=&quot;/matches2&quot; element={&lt;Matches2 matches={matches} setMatches={setMatches}/&gt;}/&gt;<br>        &lt;/Routes&gt;<br>      &lt;/BrowserRouter&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default App;</pre><p>If a new programmer would work on my <a href="https://github.com/Leejjon/simplest-fetch-in-useeffect/tree/withrouterloaderandreactquery">final code example</a> he/she would need to understand:</p><ul><li>What the <strong>RouterProvider</strong> and <strong>QueryClientProvider</strong> contexts do.</li><li>What the <strong>createBrowserRouter</strong> function does, and the concept of loaders.</li><li>What the <strong>&lt;Outlet/&gt;</strong> element does.</li><li>Basically my entire blog post.</li></ul><p>I feel like that is more than just understanding the limits of the <strong>useEffect</strong> hook.</p><h3>Removing the loaders</h3><p>Since the react router loaders force us to refactor lots of code and didn’t improve performance, let’s ditch the loaders:</p><p><strong>index.tsx:</strong></p><pre>import React from &#39;react&#39;;<br>import ReactDOM from &#39;react-dom/client&#39;;<br>import &#39;./index.css&#39;;<br>import App from &#39;./App&#39;;<br>import reportWebVitals from &#39;./reportWebVitals&#39;;<br>import {QueryClient, QueryClientProvider} from &quot;@tanstack/react-query&quot;;<br><br>const matchesQueryClient = new QueryClient();<br><br>const root = ReactDOM.createRoot(<br>    document.getElementById(&#39;root&#39;) as HTMLElement<br>);<br>root.render(<br>    &lt;React.StrictMode&gt;<br>        &lt;QueryClientProvider client={matchesQueryClient}&gt;<br>            &lt;App/&gt;<br>        &lt;/QueryClientProvider&gt;<br>    &lt;/React.StrictMode&gt;<br>);<br><br>reportWebVitals();</pre><p><strong>App.tsx</strong>:</p><pre>import React from &#39;react&#39;;<br>import &#39;./App.css&#39;;<br>import {BrowserRouter, Link, Route, Routes} from &quot;react-router-dom&quot;;<br>import Home from &quot;./pages/Home&quot;;<br>import Matches from &quot;./pages/Matches&quot;;<br>import Matches2 from &quot;./pages/Matches2&quot;;<br><br>function App() {<br>    return (<br>        &lt;div className=&quot;App&quot;&gt;<br>            &lt;BrowserRouter&gt;<br>                &lt;Link to=&quot;/&quot;&gt;Home&lt;/Link&gt;&lt;br/&gt;<br>                &lt;Link to=&quot;/matches&quot;&gt;Matches&lt;/Link&gt;&lt;br/&gt;<br>                &lt;Link to=&quot;/matches2&quot;&gt;Matches2&lt;/Link&gt;&lt;br/&gt;<br>                &lt;Routes&gt;<br>                    &lt;Route path=&quot;/&quot; element={&lt;Home/&gt;}/&gt;<br>                    &lt;Route path=&quot;/matches&quot; element={<br>                        &lt;Matches/&gt;<br>                    }/&gt;<br>                    &lt;Route path=&quot;/matches2&quot; element={<br>                        &lt;Matches2/&gt;<br>                    }/&gt;<br>                &lt;/Routes&gt;<br>            &lt;/BrowserRouter&gt;<br>        &lt;/div&gt;<br>    );<br>}<br><br>export default App;</pre><p><strong>Matches.tsx (and Matches2.tsx)</strong>:</p><pre>import {Match} from &quot;../model/Match&quot;;<br>import React from &quot;react&quot;;<br>import {useQuery} from &quot;@tanstack/react-query&quot;;<br>import {matchesQuery} from &quot;../queries/MatchesQuery&quot;;<br><br>function Matches() {<br>    const {data, isError, isPaused} = useQuery({<br>        ...matchesQuery()<br>    });<br>    const matches: Array&lt;Match&gt; = data ?? [];<br><br>    // We use isPaused because if there is no internet react query will be paused automatically.<br>    if (isError || isPaused) {<br>        return (<br>            &lt;div className=&quot;App&quot;&gt;Could not load matches.&lt;/div&gt;<br>        );<br>    }<br><br>    return (<br>        &lt;ul&gt;<br>            {matches.map((match, index) =&gt; {<br>                return (<br>                    &lt;li key={index}&gt;<br>                        &lt;p&gt;{match.homeTeam.name}&amp;nbsp;-&amp;nbsp;{match.awayTeam.name}&lt;/p&gt;<br>                    &lt;/li&gt;<br>                );<br>            })}<br>        &lt;/ul&gt;<br>    );<br>}</pre><p>Let’s look at the performance when you do a hard refresh (control + shift + R) on the <strong>/matches</strong> route:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*04tzUrtxh-40oyhkmr_-IQ.png" /></figure><p>I’d say good enough! You can find this version of the code <a href="https://github.com/Leejjon/simplest-fetch-in-useeffect/tree/withrouterandquery">here</a>.</p><h3>What else can we do?</h3><p>I think most people suggest to go for server side rendering if you want to speed things up . Even though that could slow down your time to first byte.</p><p>There are a couple of things I’d recommend you to look into:</p><ul><li>Watch <a href="https://www.youtube.com/watch?v=95B8mnhzoCM">this video</a> from Ryan Florence (a co-author of React Router and Remix) about data fetching, react router and remix.</li><li><a href="https://www.developerway.com/posts/how-to-fetch-data-in-react">Read this article</a> for more explanations on the subject of data fetching.</li><li>Read <a href="https://www.reddit.com/r/reactjs/comments/vi6q6f/comment/iddrjue/">Dan Abramov’s reddit post here</a> on fetching in useEffect and what you should use instead.</li></ul><p>I’m probably going to look into situational server side rendering (most likely Remix or Next.js) to make my performance better. But then I need to re-evaluate my infrastructure choices, and that falls outside the scope of this blog post.</p><ul><li>Thank you for reading!</li><li>Leave a comment if you have questions.</li><li>Follow me on <a href="https://leejjon.medium.com/">Medium</a>/<a href="https://twitter.com/Leejjon_net">Twitter</a>/<a href="https://www.linkedin.com/in/leon-liefting-4b511a29/">LinkedIn</a> if you want to read more original programming content.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f830c1186b71" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sharing code between frontend and backend in a Full Stack TypeScript app]]></title>
            <link>https://leejjon.medium.com/sharing-code-between-frontend-and-backend-in-a-full-stack-typescript-app-1c8711c40304?source=rss-e00e4e1c1655------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c8711c40304</guid>
            <dc:creator><![CDATA[Leejjon]]></dc:creator>
            <pubDate>Sun, 20 Nov 2022 20:30:12 GMT</pubDate>
            <atom:updated>2023-04-05T14:17:15.614Z</atom:updated>
            <content:encoded><![CDATA[<p>In this post we’ll create a React.js frontend with <a href="https://create-react-app.dev/">create-react-app</a> and host it on a Node.js server (I have done this earlier <a href="https://medium.com/@leejjon/create-a-react-app-served-by-express-js-node-js-and-add-typescript-33705be3ceda">in this post</a>). This time we will let this server also handle an API call which the React.js frontend will fetch. Everything will be in TypeScript.</p><p>We will validate the request and responses both on the frontend and backend. I have been using <strong>class-transformer</strong> and <strong>class-validator</strong> packages for this. This meant I had identical request and response objects on both the client and server. With <strong>npm workspaces</strong>, we can put the request and response objects in a shared package and re-use them.</p><p><strong>Requirements:</strong></p><ul><li><a href="https://nodejs.org/en/">Node.js 16</a> or higher (I’m still on Node 16 because that is currently the latest Node version that works with <a href="https://cloud.google.com/appengine/docs/standard/nodejs/runtime">App Engine</a>)</li><li>NPM, should be included in Node.js</li><li>NPX, can be installed with npm install -g npx</li></ul><h3>Why I would pick TypeScript over Java/Kotlin on simple back-ends (skip if you don’t care about Java)</h3><p>I’ve been doing Java development over 10 years now. I like Java and Kotlin. At work I often see that people just build Spring boot REST services that only send some JSON to a frontend.</p><p>The Jackson library is a good effort to make JSON parsing possible in Java, but it’s still a hassle to create lots of Java classes with Jackson annotations. In TypeScript it’s very easy to parse JSON.</p><p>To make React frontends work with non TypeScript backends, you can write an OpenApi spec and generate Java or Kotlin code on the server side, and generate TypeScript on the client side. Frontend and backend developers can agree on the OpenAPI and both implement their part.</p><p>Both on the frontend and backend you want to validate incoming requests. On the frontend you want to validate your request to avoid sending unnecessary invalid requests to your backend, and to warn a user on incorrect input even before he has pressed submit. And on the backend you want to validate anyway because hackers might craft invalid requests. If you use Java and TypeScript, you have to write these validations twice.</p><p>If you use TypeScript on both ends, you can create a common package with classes to define your API and validation functions. Another advantage is that you can just let one TypeScript developer build both the frontend and backend parts of new functionality.</p><h3>Creating the React App</h3><p>We will create a full stack application and I will supply all commands needed create and run both the frontend and the backend parts.</p><p>Start by creating a root directory:</p><pre>mkdir full-stack-app<br>cd full-stack-app<br>git init</pre><p>Inside this folder, we are going to create a react project and a node.js server project. For react I use <a href="https://create-react-app.dev/">create-react-app</a>. This is the official easy way to set up React, however <a href="https://github.com/facebook/create-react-app/issues/11180">one of the maintainers admits that it’s not your best option for serious systems on production</a>. He mentions <a href="https://nextjs.org/">Next.js</a>, <a href="https://vitejs.dev/">Vite.js</a> and <a href="https://www.gatsbyjs.com/">Gatsby</a> as more serious options.</p><p>I personally like to explore how far I can go “the easy way”. When you run into problems, you can search for a solution that solves your problem. If you jump to <a href="https://nextjs.org/">Next.js</a>, <a href="https://vitejs.dev/">Vite.js</a> or <a href="https://www.gatsbyjs.com/">Gatsby</a> directly you might drown in their “solutions” for problems you don’t even have yet.</p><p>Let’s create the React App and run it using the following commands:</p><pre>npx create-react-app frontend --template typescript<br>cd frontend<br><br>npm install class-transformer class-validator @types/testing-library__jest-dom<br>npm start</pre><p>Test on <strong>localhost:3000</strong> if you see a working React app. If so, adjust the <strong>App.tsx</strong> to this:</p><pre>import React, {ChangeEvent, useState} from &#39;react&#39;;<br>import &#39;./App.css&#39;;<br>import {Length, IsString, validate} from &quot;class-validator&quot;;<br>import {plainToClass} from &quot;class-transformer&quot;;<br><br>class ResponseBody {<br>    @Length(1,100)<br>    @IsString()<br>    message: string;<br>    constructor(message: string) {<br>        this.message = message;<br>    }<br>}<br><br>class RequestBody {<br>    @Length(1, 30)<br>    @IsString()<br>    name: string;<br><br>    constructor(name: string) {<br>        this.name = name;<br>    }<br>}<br><br>function App() {<br>    const responseBody: ResponseBody = {message: &quot;No response yet&quot;};<br>    const [yourName, setYourName] = useState(&quot;Your name&quot;);<br>    const [response, setResponse] = useState&lt;ResponseBody&gt;(responseBody);<br><br>    async function handleJsonFromApi(json: any) {<br>        let body = plainToClass(ResponseBody, json as Object);<br>        let validationErrors = await validate(body);<br>        if (validationErrors.length &gt; 0) {<br>            setResponse({message: `The server retrieved an object with ${validationErrors.length} validation errors.`})<br>        } else {<br>            setResponse(body);<br>        }<br>    }<br><br>    async function fetchApi() {<br>        const requestBody: RequestBody = new RequestBody(yourName);<br><br>        const validationErrors = await validate(requestBody);<br>        if (validationErrors.length &gt; 0) {<br>            setResponse({message: `The name contains ${validationErrors.length} validation errors.`})<br>        } else {<br>            try {<br>                const response = await fetch(&#39;http://localhost:8080/api/&#39;, {<br>                    headers: {&#39;Accept&#39;: &#39;application/json&#39;, &#39;Content-Type&#39;: &#39;application/json&#39;},<br>                    method: &quot;POST&quot;,<br>                    body: JSON.stringify(requestBody)<br>                });<br><br>                if (response.status === 200) {<br>                    response.json().then(handleJsonFromApi);<br>                } else {<br>                    setResponse({message: &quot;The server denied our request.&quot;})<br>                }<br>            } catch (e) {<br>                setResponse({message: &quot;Failed fetching from the API&quot;});<br>            }<br>        }<br><br>    }<br><br>    return (<br>        &lt;div className=&quot;App&quot;&gt;<br>            &lt;label&gt;Name&lt;/label&gt;&lt;br/&gt;<br>            &lt;input id=&quot;name&quot; type=&quot;text&quot; value={yourName} onChange={(changeEvent: ChangeEvent&lt;HTMLInputElement&gt;) =&gt; {<br>                setYourName(changeEvent.target.value)<br>            }}/&gt;<br>            &lt;br/&gt;&lt;br/&gt;<br>            &lt;button onClick={() =&gt; fetchApi()}&gt;Call API&lt;/button&gt;<br>            &lt;br/&gt;&lt;br/&gt;<br>            &lt;textarea readOnly={true} style={{height: &quot;200px&quot;}} value={response.message}&gt;&lt;/textarea&gt;<br>        &lt;/div&gt;<br>    );<br>}<br><br>export default App;</pre><p>You might get warned that you need to enable experimental decorators due to the decorators in class-validator:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DCejKzaL8aU1W-K1dq6BqQ.png" /></figure><p>Add the <strong>experimentalDecorators</strong> flag to the compilerOptions in your <strong>tsconfig.json</strong> file:</p><pre>{<br>  &quot;compilerOptions&quot;: {<br>    &quot;experimentalDecorators&quot;: true,<br>    ......<br>  }<br>  .....<br>}</pre><h3>What App did we just build?</h3><p>It’s a greeting app. The user enters his name, the frontend sends it to the backend and the backend will return a greeting.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/795/1*n3JFQmXyALymikZB01m7wg.png" /><figcaption>We did not create a backend yet, so our request will fail :(</figcaption></figure><p>Run the following command to build the react-app:</p><pre>npm run build</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/637/1*uUKlEHTNhZIBa4hjTfWHVQ.png" /></figure><h3>Creating the Node.js backend</h3><p>We will use <a href="https://expressjs.com/">Express.js</a> on the Node.js backend. Run the following commands to create the backend:</p><pre># Move out of the frontend folder<br>cd ..<br>mkdir backend<br>cd backend<br>npm init -y<br>npm install express cors class-transformer class-validator<br>npm install @types/express @types/cors @types/node typescript ts-loader ts-node webpack webpack-cli webpack-node-externals copy-webpack-plugin -D</pre><p>You need to make some changes to the package.json:</p><pre>{<br>  &quot;name&quot;: &quot;backend&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;node dist/index.js&quot;,<br>    &quot;build&quot;: &quot;webpack&quot;<br>  },<br>  &quot;keywords&quot;: [],<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;dependencies&quot;: {<br>......<br>  },<br>  &quot;devDependencies&quot;: {<br>........<br>  }<br>}</pre><p>Create a tsconfig.json:</p><pre>{<br>  &quot;compilerOptions&quot;: {<br>    &quot;experimentalDecorators&quot;: true,<br>    &quot;allowSyntheticDefaultImports&quot;: true,<br>    &quot;esModuleInterop&quot;: true // If you disable this, you&#39;ll get an error:<br>    // TypeError: (0 , s.default) is not a function<br>  },<br>  &quot;include&quot;: [&quot;**/*.ts&quot;],<br>  &quot;exclude&quot;: [&quot;node_modules&quot;],<br>  &quot;target&quot;: &quot;es2017&quot;,<br>  &quot;module&quot;: &quot;commonjs&quot;,<br>  &quot;lib&quot;: [&quot;es2017&quot;]<br>}</pre><p>We need to create a <strong>webpack.config.ts</strong> file:</p><pre>// Name this &quot;path2&quot; to not clash with the path variable in the index.ts<br>const path2 = require(&quot;path&quot;);<br>const nodeExternals = require(&quot;webpack-node-externals&quot;);<br>const copyFiles = require(&#39;copy-webpack-plugin&#39;);<br><br>module.exports = {<br>    entry: &#39;./index.ts&#39;,<br>    target: &quot;node&quot;,<br>    mode: &quot;production&quot;,<br>    externals: [nodeExternals()],<br>    resolve: {<br>        extensions: [&#39;.ts&#39;]<br>    },<br>    module: {<br>        rules: [<br>            // all files with a `.ts` extension will be handled by `ts-loader`<br>            { test: /\.ts$/, loader: &#39;ts-loader&#39;, exclude: /node_modules/}<br>        ]<br>    },<br>    plugins: [new copyFiles({ patterns: [{ from: &#39;../frontend/build&#39;, to: &#39;build&#39;}]})],<br>    output: {<br>        path: path2.resolve(__dirname, &#39;dist&#39;),<br>        filename: &#39;index.js&#39;<br>    },<br>};</pre><p>The copyFiles plugin will copy the frontend code into the backend, so that our Node.js code can serve it.</p><p>Finally create the <strong>index.ts</strong> file:</p><pre>import express, {NextFunction, Request, RequestHandler, Response} from &quot;express&quot;;<br>import {Length, IsString} from &quot;class-validator&quot;;<br>import cors from &quot;cors&quot;;<br>import {plainToClass} from &quot;class-transformer&quot;;<br><br>const path = require(&#39;path&#39;);<br>const app = express();<br><br>class RequestBody {<br>    @Length(1, 30)<br>    @IsString()<br>    name: string;<br><br>    constructor(name: string) {<br>        this.name = name;<br>    }<br>}<br><br>class ResponseBody {<br>    @IsString()<br>    message: string;<br>    constructor(message: string) {<br>        this.message = message;<br>    }<br>}<br><br>// Enable cors to be able to reach the backend on localhost:8080 while running React.js in dev mode on localhost:3000<br>// You might want to disbale this on production.<br>app.use(cors());<br>app.use(express.json() as RequestHandler);<br><br>app.post(&#39;/api&#39;, async function(req: Request, res: Response) {<br>    let body = plainToClass(RequestBody, req.body as Object);<br>    let validationErrors = await validate(body);<br>    if (validationErrors.length == 0) {<br>        const responseBody: ResponseBody = new ResponseBody(&quot;Hello, &quot; + body.name);<br>        res.contentType(&#39;application/json&#39;);<br>        res.status(200);<br>        res.send(responseBody);<br>    } else {<br>        res.sendStatus(400);<br>    }<br>});<br><br>// This code makes sure that any request that does not matches a static file<br>// in the build folder, will just serve index.html. Client side routing is<br>// going to make sure that the correct content will be loaded.<br>app.use((req: Request, res: Response, next: NextFunction) =&gt; {<br>    if (/(.ico|.js|.css|.jpg|.png|.map)$/i.test(req.path)) {<br>        next();<br>    } else {<br>        res.header(&#39;Cache-Control&#39;, &#39;private, no-cache, no-store, must-revalidate&#39;);<br>        res.header(&#39;Expires&#39;, &#39;-1&#39;);<br>        res.header(&#39;Pragma&#39;, &#39;no-cache&#39;);<br>        res.sendFile(path.join(__dirname, &#39;build&#39;, &#39;index.html&#39;));<br>    }<br>});<br><br>app.use(express.static(path.join(__dirname, &#39;build&#39;)));<br><br>// Start the server<br>const PORT = process.env.PORT || 8080;<br>app.listen(PORT, () =&gt; {<br>    console.log(`App listening on port ${PORT}`);<br>    console.log(&#39;Press Ctrl+C to quit.&#39;);<br>});</pre><p>Summary of what the code does:</p><ul><li>If the url is /api the backend grabs the <strong>RequestBody</strong> from the request and returns a <strong>ResponseBody</strong>.</li><li>If the url is index.html or any other route that is not going to resolve to a static file, the backend loads the index.html page (the react-app). If you want a more thorough explanation about the how and why, <a href="https://medium.com/@leejjon/create-a-react-app-served-by-express-js-node-js-and-add-typescript-33705be3ceda">read this post</a>.</li><li>If the request matches any file name in the build folder, the backend will simply serve this file. This goes for pictures, fonts, the favicon etc.</li></ul><p>To run the backend:</p><ul><li>Run npm run build in the frontend folder.</li><li>Run npm run build in the backend folder.</li><li>Run npm start in the backend folder.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*drXkwYm2A1s-lGtzeV1IMQ.png" /><figcaption>The React.js App is being served by the backend</figcaption></figure><p>Now the @Length(1, 30) decorator should enforce that the name we enter has at least one character, and 30 characters max. We can quickly test if the validation works by not entering a name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/691/1*j6ZBvcXMVf0vi_xjETzJhg.png" /><figcaption>Pressing the button triggers the validation on the frontend.</figcaption></figure><p>Run this curl command to test the backend validation directly with an empty name.</p><pre>curl -H &#39;Content-Type: application/json&#39; -d &#39;{&quot;name&quot;: &quot;&quot;}&#39; -X POST http://localhost:8080/api</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8dt5BwP_BbDsDeW2tjyqzQ.png" /><figcaption>We can see that the server also applies the same validation and rejects the request.</figcaption></figure><h3>Getting rid of the duplicated classes</h3><p>If we want to share the <strong>RequestBody</strong> and <strong>ResponseBody</strong> classes across the frontend and backend, we need a common package. We could make an npm package with these two classes, and publish it on npmjs.com, and install it using: <strong>npm install our-common-package</strong></p><p>This would be a bit too much because:</p><ul><li>Maybe we want to keep our code closed source.</li><li>It would force us to start versioning our common package.</li><li>We only need it in two projects.</li></ul><p>You could use <strong>npm link</strong> (explained in <a href="https://medium.com/dailyjs/how-to-use-npm-link-7375b6219557">this article</a>) to create symbolic link on on the common package in your node_modules that points to a local folder. However everyone cloning your repository would then need to run the npm link command on the right folder before it can compile and run the software.</p><p>Luckily, <strong>npm</strong> has somewhat copied the <strong>workspaces</strong> concept from their competitor, <strong>yarn</strong>. This works since npm 7. Npm workspaces do roughly the same as the <strong>npm link</strong> command can achieve, but the advantage is that the symbolic links are automatically set up if you run <strong>npm install</strong> in the root of the project.</p><h3>Creating a “common” workspace</h3><p>To use <strong>npm workspaces</strong>, we need a <strong>package.json</strong> in the root of the project:</p><pre>{<br>  &quot;name&quot;: &quot;full-stack-app&quot;,<br>  &quot;private&quot;: true,<br>  &quot;workspaces&quot;: [&quot;common&quot;]<br>}</pre><p>Let’s create a common folder in which we put the shared code:</p><pre># Move out of the backend folder<br>cd ..<br><br>mkdir common<br>cd common<br>npm init -y<br>npm i class-validator<br>npm i typescript @types/node -D</pre><p>Now change the highlighted things in the <strong>package.json</strong> to make sure the common module plays nice with the npm workspace. We use <strong>tsc</strong> to compile the TypeScript:</p><pre>{<br><strong>  &quot;private&quot;: true,<br>  &quot;scripts&quot;: {<br>    &quot;build&quot;: &quot;tsc -b&quot;<br>  },</strong><br>  &quot;dependencies&quot;: {<br>    &quot;class-validator&quot;: &quot;^0.13.2&quot;<br>  },<br>  &quot;devDependencies&quot;: {<br>    &quot;@types/node&quot;: &quot;^18.11.18&quot;,<br>    &quot;typescript&quot;: &quot;^4.9.3&quot;<br>  }<br>}</pre><p>Create a <strong>tsconfig.json</strong>:</p><pre>{<br>  &quot;compilerOptions&quot;: {<br>    &quot;experimentalDecorators&quot;: true,<br>    &quot;declaration&quot;: true<br>  },<br>  &quot;include&quot;: [<br>    &quot;**/*.ts&quot;<br>  ],<br>  &quot;exclude&quot;: [<br>    &quot;node_modules&quot;<br>  ],<br>  &quot;target&quot;: &quot;es2017&quot;,<br>  &quot;module&quot;: &quot;commonjs&quot;,<br>  &quot;lib&quot;: [<br>    &quot;es2017&quot;<br>  ]<br>}</pre><p>Now we can create an <strong>index.ts</strong> file with the classes:</p><pre>import {IsString, Length} from &quot;class-validator&quot;;<br><br>export class ResponseBody {<br>    @Length(1,100)<br>    @IsString()<br>    message: string;<br>    constructor(message: string) {<br>        this.message = message;<br>    }<br>}<br><br>export class RequestBody {<br>    @Length(1, 30)<br>    @IsString()<br>    name: string;<br>    constructor(name: string) {<br>        this.name = name;<br>    }<br>}</pre><p>Note: the <a href="https://github.com/typestack/class-validator">class-validator</a> library has a lot more handy decorators for validation, so use them instead of writing custom validation code!</p><p>Finally build our new <strong>common</strong> package:</p><pre>npm run build</pre><h3>The project structure</h3><p>Go back to the root of the project and run:</p><pre>npm install</pre><p>Your project structure should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*jj16d66BNsw47FP_cRJ0DQ.png" /><figcaption>The small arrow icon on the common folder shows that the npm install command at the root made a symbolic link based on the workspaces definition.</figcaption></figure><h3>Updating the frontend</h3><p>In the <strong>App.tsx</strong>, remove the RequestBody and ResponseBody classes and add the following import:</p><pre>import React, {ChangeEvent, useState} from &#39;react&#39;;<br>import &#39;./App.css&#39;;<br>import {validate} from &quot;class-validator&quot;;<br>import {plainToClass} from &quot;class-transformer&quot;;<br>import {RequestBody, ResponseBody} from &quot;common&quot;;<br><br>function App() {<br>    const responseBody: ResponseBody = {message: &quot;No response yet&quot;};<br>    const [yourName, setYourName] = useState(&quot;Your name&quot;);<br>    const [response, setResponse] = useState&lt;ResponseBody&gt;(responseBody);<br><br>    async function handleJsonFromApi(json: any) {<br>        let body = plainToClass(ResponseBody, json as Object);<br>        let validationErrors = await validate(body);<br>        if (validationErrors.length &gt; 0) {<br>            setResponse({message: `The server retrieved an object with ${validationErrors.length} validation errors.`})<br>        } else {<br>            setResponse(body);<br>        }<br>    }<br><br>    async function fetchApi() {<br>        const requestBody: RequestBody = new RequestBody(yourName);<br><br>        const validationErrors = await validate(requestBody);<br>        if (validationErrors.length &gt; 0) {<br>            setResponse({message: `The name contains ${validationErrors.length} validation errors.`})<br>        } else {<br>            try {<br>                const response = await fetch(&#39;http://localhost:8080/api/&#39;, {<br>                    headers: {&#39;Accept&#39;: &#39;application/json&#39;, &#39;Content-Type&#39;: &#39;application/json&#39;},<br>                    method: &quot;POST&quot;,<br>                    body: JSON.stringify(requestBody)<br>                });<br><br>                if (response.status === 200) {<br>                    response.json().then(handleJsonFromApi);<br>                } else {<br>                    setResponse({message: &quot;The server denied our request.&quot;})<br>                }<br>            } catch (e) {<br>                setResponse({message: &quot;Failed fetching from the API&quot;});<br>            }<br>        }<br><br>    }<br><br>    return (<br>        &lt;div className=&quot;App&quot;&gt;<br>            &lt;label&gt;Name&lt;/label&gt;&lt;br/&gt;<br>            &lt;input id=&quot;name&quot; type=&quot;text&quot; value={yourName} onChange={(changeEvent: ChangeEvent&lt;HTMLInputElement&gt;) =&gt; {<br>                setYourName(changeEvent.target.value)<br>            }}/&gt;<br>            &lt;br/&gt;&lt;br/&gt;<br>            &lt;button onClick={() =&gt; fetchApi()}&gt;Call API&lt;/button&gt;<br>            &lt;br/&gt;&lt;br/&gt;<br>            &lt;textarea readOnly={true} style={{height: &quot;200px&quot;}} value={response.message}&gt;&lt;/textarea&gt;<br>        &lt;/div&gt;<br>    );<br>}<br><br>export default App;</pre><p>After this make sure you run another build:</p><pre>npm run build</pre><h3>Updating the backend</h3><p>In the <strong>index.ts</strong> of the backend, replace the RequestBody and ResponseBody classes and add the import:</p><pre>import express, {NextFunction, Request, RequestHandler, Response} from &quot;express&quot;;<br>import cors from &quot;cors&quot;;<br>import {plainToClass} from &quot;class-transformer&quot;;<br>import {RequestBody, ResponseBody} from &quot;common&quot;;<br>import {validate} from &quot;class-validator&quot;;<br><br>const path = require(&#39;path&#39;);<br>const app = express();<br><br>// Enable cors to be able to reach the backend on localhost:8080 while running React.js in dev mode on localhost:3000<br>// You might want to disbale this on production.<br>app.use(cors());<br>app.use(express.json() as RequestHandler);<br><br>app.post(&#39;/api&#39;, async function(req: Request, res: Response) {<br>    let body = plainToClass(RequestBody, req.body as Object);<br>    let validationErrors = await validate(body);<br>    if (validationErrors.length == 0) {<br>        const responseBody: ResponseBody = new ResponseBody(&quot;Hello, &quot; + body.name);<br>        res.contentType(&#39;application/json&#39;);<br>        res.status(200);<br>        res.send(responseBody);<br>    } else {<br>        res.sendStatus(400);<br>    }<br>});<br><br>// This code makes sure that any request that does not matches a static file<br>// in the build folder, will just serve index.html. Client side routing is<br>// going to make sure that the correct content will be loaded.<br>app.use((req: Request, res: Response, next: NextFunction) =&gt; {<br>    if (/(.ico|.js|.css|.jpg|.png|.map)$/i.test(req.path)) {<br>        next();<br>    } else {<br>        res.header(&#39;Cache-Control&#39;, &#39;private, no-cache, no-store, must-revalidate&#39;);<br>        res.header(&#39;Expires&#39;, &#39;-1&#39;);<br>        res.header(&#39;Pragma&#39;, &#39;no-cache&#39;);<br>        res.sendFile(path.join(__dirname, &#39;build&#39;, &#39;index.html&#39;));<br>    }<br>});<br><br>app.use(express.static(path.join(__dirname, &#39;build&#39;)));<br><br>// Start the server<br>const PORT = process.env.PORT || 8080;<br>app.listen(PORT, () =&gt; {<br>    console.log(`App listening on port ${PORT}`);<br>    console.log(&#39;Press Ctrl+C to quit.&#39;);<br>});</pre><p>Also build the backend again, and then run it to test the code:</p><pre>npm run build<br>npm start</pre><p>The result should be identical:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*LLfs4myF9l8J_v_dgiPDYA.png" /><figcaption>You might want to also try the empty name validation, and the backend validation with the curl command again.</figcaption></figure><h3>How is it even possible that these imports work!?</h3><p>Coming from a Java background it feels really weird that these imports are now being resolved, when the <strong>frontend</strong> and <strong>backend</strong> do not have <strong>common</strong> listed as a dependency in their package.json.</p><p>Then again, the <strong>npm link</strong> command also <a href="https://stackoverflow.com/questions/28664866/npm-link-save-does-not-update-dependencies-on-my-package-json">didn’t modify any package.json files either</a>. By defining <strong>npm workspaces</strong> in a root package.json, you’ll automatically set symbolic links in the <strong>node_modules</strong> folder when you run <strong>npm install</strong>. On compile / runtime it doesn’t matter what’s in the package.json files, all that matters is that your dependencies are in the node_modules folder.</p><p>The full final source code can be found <a href="https://github.com/Leejjon/full-stack-app">in this GitHub project</a>. If you enjoyed this blog post, please follow me here on <a href="https://leejjon.medium.com/">Medium</a>, <a href="https://twitter.com/Leejjon_net">Twitter</a> or <a href="https://www.linkedin.com/in/leon-liefting-4b511a29/">LinkedIN</a>.</p><p>PS: If you are going to continue building on the backend, you might want to enable the HotModuleReplacement in Webpack as I’ve shown <a href="https://medium.com/@leejjon/create-a-react-app-served-by-express-js-node-js-and-add-typescript-33705be3ceda">in my post from last year</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c8711c40304" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Handling Exceptions and Error responses in Java Rest Services]]></title>
            <link>https://leejjon.medium.com/handling-exceptions-and-error-responses-in-java-rest-services-afda273c9d2f?source=rss-e00e4e1c1655------2</link>
            <guid isPermaLink="false">https://medium.com/p/afda273c9d2f</guid>
            <category><![CDATA[lombok]]></category>
            <category><![CDATA[java-exception-handling]]></category>
            <category><![CDATA[spring]]></category>
            <dc:creator><![CDATA[Leejjon]]></dc:creator>
            <pubDate>Sun, 10 Apr 2022 16:19:36 GMT</pubDate>
            <atom:updated>2022-06-08T05:19:36.315Z</atom:updated>
            <content:encoded><![CDATA[<p>I often see presentations about best practices to detect bugs early on and prevent them. Obviously preventing is better than fixing. However, things can always go wrong. Even if your code is perfect the following things can happen:</p><ul><li>The client can do invalid requests</li><li>Network problems</li><li>Hardware failures</li><li>The database might have incorrect data</li></ul><p>In this post I will talk about handling errors. Handling errors nicely will inform users that something went wrong and that they should try again or wait until things have been fixed. If the errors are properly logged the developers can investigate and fix the problems faster.</p><h3>Error handling in Java</h3><p>If you know a bit of Java you’ll probably think of <strong>try-catch</strong> statements to handle exceptions. It’s important to know that Java has <strong>Checked</strong> and <strong>Unchecked</strong> exceptions. You can read more about Java Exceptions in <a href="https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html">the Oracle docs on Exceptions</a>.</p><p>Here is a bit of Java code where I run two methods from the main thread. One can possibly throw a checked SQLException, while the other can throw a UncheckedException (in this code, the chance for it to happen is 50%):</p><pre>import java.sql.SQLException;<br>import java.util.Random;<br><br>public class Main {<br>    public static void main(String[] args) {<br>        try {<br>            <em>checkedException</em>();<br>            <em>unCheckedException</em>();<br>        } catch (SQLException | NullPointerException e) {<br>            e.printStackTrace();<br>        }<br>    }<br><br>    private static void checkedException() <strong>throws SQLException</strong> {<br>        if (new Random().nextBoolean()) {<br>            throw new SQLException();<br>        }<br>    }<br><br>    private static void unCheckedException() {<br>        if (new Random().nextBoolean()) {<br>            throw new NullPointerException();<br>        }<br>    }<br>}</pre><p>As you can see the checked exception needs a throws declaration, while the unchecked exception doesn’t. I used to like checked exceptions when I started to learn Java as it gave me information on what could go wrong in my code. While there are situations where this is useful, most Java REST services nowadays only use unchecked exceptions. I hope to make clear why in the next section.</p><h3>Catching errors at the system boundaries</h3><p>It’s only useful to catch an Exception in a method if the method can still fulfill its purpose. Let’s say we have a system like the design below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/599/1*7BQs5nOo0owwTC3YDQkfhw.png" /></figure><p>The database service tries to execute an SQL query but it fails. This happens a lot in the real world. The query could have a syntax error, or attempt to retrieve data from a table that no longer exists.</p><p>We could put a try catch statement in the database service, but what needs to happen to nicely handle this error? Probably the controller needs to decide to send an HTTP 500 response. The database service is not able to do that, and neither does the business logic. So let’s put the <strong>try catch</strong> statement in the controller.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/599/1*zAyd_pQ8lMC1YjNUNAlagg.png" /></figure><p>Here is how that looks in some simplified example code:</p><pre>import java.sql.SQLException;<br>import java.util.Random;<br><br>public class Main {<br>    public static void main(String[] args) {<br>         BusinessLogic businessLogic = new BusinessLogic(new DatabaseService());<br>         Controller controller = new Controller(businessLogic);<br>         controller.callApi();<br>    }<br><br>    static class <strong>Controller</strong> {<br>        private final BusinessLogic businessLogic;<br>        public Controller(BusinessLogic businessLogic) {<br>            this.businessLogic = businessLogic;<br>        }<br>        <br>        public String callApi() {<br>            <strong>try {</strong><br>                businessLogic.doBusinessLogic();<br>                return &quot;200&quot;;<br>            <strong>} catch (Exception e) {</strong><br>                e.printStackTrace();<br>                return &quot;500&quot;;<br>            <strong>}</strong><br>        }<br>    }<br><br>    static class <strong>BusinessLogic</strong> {<br>        private final DatabaseService databaseService;<br>        public BusinessLogic(DatabaseService databaseService) {<br>            this.databaseService = databaseService;<br>        }<br>        <br>        public void doBusinessLogic() <strong>throws SQLException</strong> {<br>            databaseService.executeQuery(true);<br>        }<br>    }<br><br>    static class <strong>DatabaseService</strong> {<br>        public void runQuery(boolean extraParam)<br>          <strong>throws SQLException</strong> {<br>          Connection con = DriverManager.<em>getConnection</em>(<br>            &quot;jdbc:hsqldb:mem:testdb;DB_CLOSE_DELAY=-1&quot;<br>          ); <em>// Some simple in memory db</em></pre><pre><em>          </em>String sql = &quot;select * from records where extraParam = ?&quot;;<br>          PreparedStatement ps = con.prepareStatement(sql);<br>          ps.setString(1, Boolean.<em>toString</em>(extraParam));<br>          ps.executeQuery();<br>          con.close();<br>       }<br>    }<br>}</pre><h3>Getting rid of the checked exception</h3><p>See how the BusinessLogic class now has a “throws SQLException” declaration on the doBusinessLogic method? It does nothing with this SQLException, it just gets thrown to the controller. Yet it still makes the code depend on the java.sql package.</p><p>Your business logic ultimately shouldn’t care if you use a SQL database, NoSQL database or another REST API to fetch the data from. So it’s definitely not nice that this checked SQLException causes the Business logic to know about the <strong>java.sql</strong> package.</p><p>We can solve this by using the <strong>@SneakyThrows</strong> annotation from <a href="https://projectlombok.org/">the Lombok library</a>:</p><pre><strong>import lombok.SneakyThrows;</strong></pre><pre>.... controller code</pre><pre>static class BusinessLogic {<br>    private final DatabaseService databaseService;<br>    public BusinessLogic(DatabaseService databaseService) {<br>        this.databaseService = databaseService;<br>    }<br><br>    public void doBusinessLogic() {<br>        databaseService.executeQuery(new Random().nextBoolean());<br>    }<br>}<br><br>static class DatabaseService {<br>    <strong>@SneakyThrows</strong><br>    public void runQuery(boolean extraParam) {<br>        Connection con = DriverManager.<em>getConnection</em>(<br>                &quot;jdbc:hsqldb:mem:testdb;DB_CLOSE_DELAY=-1&quot;<br>        ); <em>// Some simple in memory db<br>        </em>String sql = &quot;select * from records where extraParam = ?&quot;;<br>        PreparedStatement ps = con.prepareStatement(sql);<br>        ps.setString(1, Boolean.<em>toString</em>(extraParam));<br>        ps.executeQuery();<br>        con.close();<br>    }<br>}</pre><p>When the <strong>executeQuery</strong> method is annotated with <strong>@SneakyThrows,</strong> it is no longer forced by the compiler to put <strong>throws SQLException</strong> behind the consuming methods.</p><p>Luckily, @SneakyThrows is only needed in very few places. If you use the Spring framework (like 90% of the Java backends use), you will most likely use the JdbcTemplate instead of the original JDBC classes. The JdbcTemplate already catches all SqlExceptions and rethrows them as unchecked exceptions.</p><h3>Catching exceptions in a real Spring Controller</h3><p>The above examples are not real “controllers”. Here is an example of catching Exceptions in a RestController using Spring:</p><pre>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.http.HttpStatus;<br>import org.springframework.http.ResponseEntity;<br>import org.springframework.web.bind.annotation.GetMapping;<br>import org.springframework.web.bind.annotation.RestController;<br><br>@RestController<br>public class GetSomethingController {<br>    private final BusinessLogic businessLogic;<br><br>    @Autowired<br>    public GetSomethingController(BusinessLogic businessLogic) {<br>        this.businessLogic = businessLogic;<br>    }<br><br>    @GetMapping(&quot;/&quot;)<br>    public ResponseEntity&lt;String&gt; getSomething() {<br><strong>        try {</strong><br>            <em>// This might throw an exception...<br>            </em>businessLogic.doBusinessLogic();<br>            return ResponseEntity.<em>ok</em>(&quot;Hello&quot;);<br><strong>        } catch (Exception e) {<br>            <em>// You should log the exception here!<br>            </em>return ResponseEntity<br>                    .<em>status</em>(HttpStatus.<em>INTERNAL_SERVER_ERROR</em>)<br>                    .body(&quot;Error&quot;);<br>        }</strong><br>    }<br>}</pre><p>Now we are catching any exception at the system boundary right? Great! Well this is usually not how Exception handling in Spring works.</p><h3>Using the @ExceptionHandler annotation</h3><p>See this this Spring boot <a href="https://www.baeldung.com/spring-controller-vs-restcontroller">example code from Baeldung</a>:</p><pre>@RestController<br>@RequestMapping(&quot;books-rest&quot;)<br>public class SimpleBookRestController {<br>    <br>    @GetMapping(&quot;/{id}&quot;, produces = &quot;application/json&quot;)<br>    public Book getBook(@PathVariable int id) {<br>        return findBookById(id);<br>    }<br>}</pre><p>They have no ResponseEntity objects in the return types, and they don’t have have try catch statements in the controller either. We could add the try catch, but with the return type <strong>Book</strong> we can’t return a ResponseEntity object with an INTERNAL_ERROR (500) HTTP status.</p><p>We can modify our Spring example to leave out the ResponseEntity class, and throw a <strong>ResponseStatusException</strong> (available since Spring 5) to make Spring respond with a 500 response:</p><pre>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.http.HttpStatus;<br>import org.springframework.web.bind.annotation.GetMapping;<br>import org.springframework.web.bind.annotation.RestController;<br>import org.springframework.web.server.ResponseStatusException;<br><br>@RestController<br>public class GetSomethingController {<br>    private final BusinessLogic businessLogic;<br><br>    @Autowired<br>    public GetSomethingController(BusinessLogic businessLogic) {<br>        this.businessLogic = businessLogic;<br>    }<br><br>    @GetMapping(&quot;/&quot;)<br>    public <strong>String</strong> getSomething() {<br>        try {<br>            <em>// This might throw an exception...<br>            </em>businessLogic.doBusinessLogic();<br>            return &quot;Hello&quot;;<br>        } catch (Exception e) {<br>            <em>// You should log the exception here!<br></em><strong><em>            </em>throw new ResponseStatusException(HttpStatus.<em>INTERNAL_SERVER_ERROR</em>, &quot;Error&quot;);</strong><br>        }<br>    }<br>}</pre><p>However this still clutters your controller code. Especially if your controller has 10 methods that all have this try catch pattern. Instead we can define a method just for handling Exceptions and annotate it with the <strong>@ExceptionHandler</strong> annotation.</p><pre>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.http.HttpStatus;<br>import org.springframework.web.bind.annotation.ExceptionHandler;<br>import org.springframework.web.bind.annotation.GetMapping;<br>import org.springframework.web.bind.annotation.ResponseStatus;<br>import org.springframework.web.bind.annotation.RestController;<br><br>@RestController<br>public class GetSomethingController {<br>    private final BusinessLogic businessLogic;<br><br>    @Autowired<br>    public GetSomethingController(BusinessLogic businessLogic) {<br>        this.businessLogic = businessLogic;<br>    }<br><br>    @GetMapping(&quot;/&quot;)<br>    public String getSomething() {<br>        businessLogic.doBusinessLogic();<br>        return &quot;Hello&quot;;<br>    }<br><br><strong>    @ExceptionHandler(Exception.class)<br>    @ResponseStatus(HttpStatus.<em>INTERNAL_SERVER_ERROR</em>)<br>    public String handleUnexpectedErrors(Exception e) {<br>        <em>// You should log the exception here!<br>        </em>return &quot;Error&quot;;<br>    }</strong><br>}</pre><p>Such a method handles exceptions that occur in all GET/POST/PUT (or any other) mappings in the controller. You could even define a global one that applies to all controllers.</p><h3>Adding logging</h3><p>My previous code snippets already gave it away, we should put logging in the Exception handler. I’m using the <strong>@Slf4j</strong> <a href="https://projectlombok.org/features/log">annotation from Lombok</a> for logging:</p><pre><strong>import lombok.extern.slf4j.Slf4j;</strong><br>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.http.HttpStatus;<br>import org.springframework.web.bind.annotation.ExceptionHandler;<br>import org.springframework.web.bind.annotation.GetMapping;<br>import org.springframework.web.bind.annotation.ResponseStatus;<br>import org.springframework.web.bind.annotation.RestController;<br><br>import javax.servlet.http.HttpServletRequest;<br><br><strong>@Slf4j</strong><br>@RestController<br>public class GetSomethingController {<br>    private final BusinessLogic businessLogic;<br><br>    @Autowired<br>    public GetSomethingController(BusinessLogic businessLogic) {<br>        this.businessLogic = businessLogic;<br>    }<br><br>    @GetMapping(&quot;/&quot;)<br>    public String getSomething() {<br>        businessLogic.doBusinessLogic();<br>        return &quot;Hello&quot;;<br>    }<br><br>    @ExceptionHandler(Exception.class)<br>    @ResponseStatus(HttpStatus.<em>INTERNAL_SERVER_ERROR</em>)<br>    public String handleUnexpectedErrors(<strong>HttpServletRequest req,</strong> Exception e) {<br>        <strong><em>log</em>.error(&quot;Unexpected error occurred on request: &quot; + req.getServletPath(), e);</strong><br>        return &quot;Error&quot;;<br>    }<br>}</pre><p>If you now run your project with mvn spring-boot:run you should see exceptions in your logs.</p><h3>Why logging on the boundaries sometimes isn’t enough…</h3><p>I know I just talked you into catching and logging the exceptions on the system boundaries. But sometimes to troubleshoot you need more. You might want to log the values of certain query parameters when a database query fails. If we try to log at the system boundary (the controller), we don’t have these parameters anymore! So we must do this in the database service itself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*0uc1f2MvP-6yckFh2RrDKA.png" /></figure><p>Here is my DatabaseService.java that uses the JdbcTemplate of the Spring framework. This query will fail in my project simply because there is no “records” table.</p><pre>import lombok.extern.slf4j.Slf4j;<br>import org.springframework.jdbc.core.JdbcTemplate;<br>import org.springframework.jdbc.core.RowMapper;<br>import org.springframework.stereotype.Service;<br><br>import java.sql.ResultSet;<br>import java.sql.SQLException;<br>import java.util.List;<br><br>@Service<br>@Slf4j<br>public class DatabaseService {<br>    private JdbcTemplate jdbcTemplate;<br><br>    public DatabaseService(JdbcTemplate jdbcTemplate) {<br>        this.jdbcTemplate = jdbcTemplate;<br>    }<br><br>    public void runQuery(boolean param) {<br>        String sql = &quot;select * from records where param = ?&quot;;<br>        List&lt;String&gt; ids = jdbcTemplate.query(sql, <br>         new RowMapper&lt;String&gt;() {<br>            @Override<br>            @SneakyThrows<br>            public String mapRow(ResultSet rs, int rowNum) {<br>                return rs.getString(&quot;id&quot;);<br>            }<br>        }, param);<br><br>        <em>log</em>.info(&quot;We retrieved &quot; + ids.size() + &quot; records.&quot;);<br>    }<br>}</pre><p>When running this you’ll see that actually a SQLSyntaxErrorException (extends SQLException and thus is a checked exception) is happening. The spring JDBC wraps it in a BadSqlGrammarException, which is a runtime exception so we don’t have to put @SneakyThrows above the runQuery method.</p><p>Let’s alter the code to log the error and the parameter:</p><pre>import lombok.SneakyThrows;<br>import lombok.extern.slf4j.Slf4j;<br>import org.springframework.dao.DataAccessException;<br>import org.springframework.jdbc.core.JdbcTemplate;<br>import org.springframework.jdbc.core.RowMapper;<br>import org.springframework.stereotype.Service;<br><br>import java.sql.ResultSet;<br>import java.util.List;<br><br>@Service<br>@Slf4j<br>public class DatabaseService {<br>    private JdbcTemplate jdbcTemplate;<br><br>    public DatabaseService(JdbcTemplate jdbcTemplate) {<br>        this.jdbcTemplate = jdbcTemplate;<br>    }<br><br>    public void runQuery(boolean param) {<br>        String sql = &quot;select * from records where param = ?&quot;;<br>        <strong>try {</strong><br>            List&lt;String&gt; ids = jdbcTemplate.query(sql,<br>                    new RowMapper&lt;String&gt;() {<br>                        @Override<br>                        @SneakyThrows<br>                        public String mapRow(ResultSet rs, <br>                          int rowNum) {<br>                            return rs.getString(&quot;id&quot;);<br>                        }<br>                    }, param);<br>            <em>log</em>.info(&quot;We retrieved &quot; + ids.size() + &quot; records.&quot;);<br>        <strong>} catch (DataAccessException e) {<br>            <em>log</em>.error(&quot;Failed running query: &quot; + sql);<br>            <em>log</em>.error(&quot;Parameters: param=&quot; + param);<br>            <em>log</em>.error(&quot;Exception: &quot;, e);<br>            throw e;<br>        }</strong><br>    }<br>}</pre><p>But wait, if you test with this code, the error is logged twice! Once in the <strong>DatabaseService</strong> and once in the <strong>Controller</strong>. That is confusing. We can fix that by creating an <strong>AlreadyLoggedException</strong> class to wrap around the exception.</p><pre><strong>public class AlreadyLoggedException extends RuntimeException {<br>    public AlreadyLoggedException(Exception e) {<br>        super(e);<br>    }<br>}</strong></pre><p>Make sure to wrap the <strong>DataAccessException</strong> into this <strong>AlreadyLoggedException</strong> when rethrowing:</p><pre>import lombok.SneakyThrows;<br>import lombok.extern.slf4j.Slf4j;<br>import org.springframework.dao.DataAccessException;<br>import org.springframework.jdbc.core.JdbcTemplate;<br>import org.springframework.jdbc.core.RowMapper;<br>import org.springframework.stereotype.Service;<br><br>import java.sql.ResultSet;<br>import java.util.List;<br><br>@Service<br>@Slf4j<br>public class DatabaseService {<br>    private JdbcTemplate jdbcTemplate;<br><br>    public DatabaseService(JdbcTemplate jdbcTemplate) {<br>        this.jdbcTemplate = jdbcTemplate;<br>    }<br><br>    public void runQuery(boolean param) {<br>        String sql = &quot;select * from records where param = ?&quot;;<br>        try {<br>            List&lt;String&gt; ids = jdbcTemplate.query(sql,<br>                    new RowMapper&lt;String&gt;() {<br>                        @Override<br>                        @SneakyThrows<br>                        public String mapRow(ResultSet rs, <br>                           int rowNum) {<br>                            return rs.getString(&quot;id&quot;);<br>                        }<br>                    }, param);<br>            <em>log</em>.info(&quot;We retrieved &quot; + ids.size() + &quot; records.&quot;);<br>        } catch (DataAccessException e) {<br>            <em>log</em>.error(&quot;Failed running query: &quot; + sql);<br>            <em>log</em>.error(&quot;Parameters: param=&quot; + param);<br>            <em>log</em>.error(&quot;Exception: &quot;, e);<br>            <strong>throw new AlreadyLoggedException(e);</strong><br>        }<br>    }<br>}</pre><p>And finally add an extra @ExceptionHandler method in the controller to handle all <strong>AlreadyLoggedException</strong>s:</p><pre>import lombok.extern.slf4j.Slf4j;<br>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.http.HttpStatus;<br>import org.springframework.web.bind.annotation.ExceptionHandler;<br>import org.springframework.web.bind.annotation.GetMapping;<br>import org.springframework.web.bind.annotation.ResponseStatus;<br>import org.springframework.web.bind.annotation.RestController;<br><br>import javax.servlet.http.HttpServletRequest;<br><br>@Slf4j<br>@RestController<br>public class GetSomethingController {<br>    private final BusinessLogic businessLogic;<br><br>    @Autowired<br>    public GetSomethingController(BusinessLogic businessLogic) {<br>        this.businessLogic = businessLogic;<br>    }<br><br>    @GetMapping(&quot;/&quot;)<br>    public String getSomething() {<br>        businessLogic.doBusinessLogic();<br>        return &quot;Hello&quot;;<br>    }<br><br><strong>    @ExceptionHandler(AlreadyLoggedException.class)<br>    @ResponseStatus(HttpStatus.<em>INTERNAL_SERVER_ERROR</em>)<br>    public String handleUnexpectedErrorsThatAreAlreadyLogged() {<br>        <em>// Do not log<br>        </em>return &quot;Error&quot;;<br>    }</strong><br><br>    @ExceptionHandler(Exception.class)<br>    @ResponseStatus(HttpStatus.<em>INTERNAL_SERVER_ERROR</em>)<br>    public String handleUnexpectedErrors(HttpServletRequest req, Exception e) {<br>        <em>log</em>.error(&quot;Unexpected error occurred on request: &quot; + req.getServletPath(), e);<br>        return &quot;Error&quot;;<br>    }<br>}</pre><p>Done, no more duplicate error logging.</p><h3>Tracing back client errors to the correct Exception and Stack trace in the logs</h3><p>In all companies I’ve worked for, it was normal to respond with a HTTP 500 status in case of exceptions. But how do you troubleshoot this when such things happen on production? Imagine the following situation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vNvGq2Z2Rw1F2e0mVfGFnA.png" /></figure><p>Let’s say you’re the backend guy. The support desk tells you users are complaining that the button on the frontend gives generic errors.</p><p>What typically happens is that you’re going to ask one user the moment he got the error. If this user bothers to reply, he might give an inaccurate timestamp. If your log is full different of errors, it might be hard to find out what’s happening.</p><h3>Generating a unique ID for every error</h3><p>At the last few companies I’ve worked I have advocated to generate an ID for every exception and add it to the logs (along with the stacktrace). In the HTTP response, return this ID. That way your frontend can display it to the end user. When the end user complains to your customer service, the customer service can ask for this ID and pass it to the developer. With that ID, the developer can do a simple search in the logs to find the exact log message that caused the user’s problem.</p><p>Here’s that in a diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Pc9qHsyeDVdX6ZvA6dUfw.png" /></figure><h3>Implementing logging id’s and putting them in the HTTP response</h3><p>We can generate a unique identifier (UUID) in Java to make sure we generate something that is hopefully unique within our logs:</p><p>Let’s add it to our exception handler method.</p><pre><strong>import java.util.UUID;</strong></pre><pre>// other code</pre><pre>@ExceptionHandler(Exception.class)<br>@ResponseStatus(HttpStatus.<em>INTERNAL_SERVER_ERROR</em>)<br>public String handleUnexpectedErrors(HttpServletRequest req, Exception e) {<br>    <strong>final String uuid = UUID.<em>randomUUID</em>().toString();</strong><br>    <em>log</em>.error(<strong>uuid + &quot; </strong>Unexpected error occurred on request: &quot; + req.getServletPath(), e);<br>    <strong>return &quot;Error: &quot; + uuid;</strong><br>}</pre><p>To make this work for the exceptions that are caught on other places in the code such as the <strong>DatabaseService</strong>, we need to alter our <strong>AlreadyLoggedException</strong> so it can bring the UUID to the controller.</p><pre><strong>import lombok.Getter;</strong><br><br>public class AlreadyLoggedException extends RuntimeException {<br><strong>    @Getter<br>    private final String uuid;</strong><br><br>    public AlreadyLoggedException(Exception e<strong>, final String uuid</strong>) {<br>        super(e);<br>        <strong>this.uuid = uuid;</strong><br>    }<br>}</pre><p>Now we can update our <strong>DatabaseService</strong> to pass the UUID to the <strong>AlreadyLoggedException</strong>.</p><pre>import lombok.SneakyThrows;<br>import lombok.extern.slf4j.Slf4j;<br>import org.springframework.dao.DataAccessException;<br>import org.springframework.jdbc.core.JdbcTemplate;<br>import org.springframework.jdbc.core.RowMapper;<br>import org.springframework.stereotype.Service;<br><br>import java.sql.ResultSet;<br>import java.util.List;<br><strong>import java.util.UUID;</strong><br><br>@Service<br>@Slf4j<br>public class DatabaseService {<br>    private JdbcTemplate jdbcTemplate;<br><br>    public DatabaseService(JdbcTemplate jdbcTemplate) {<br>        this.jdbcTemplate = jdbcTemplate;<br>    }<br><br>    public void runQuery(boolean param) {<br>        String sql = &quot;select * from records where param = ?&quot;;<br>        try {<br>            List&lt;String&gt; ids = jdbcTemplate.query(sql,<br>                    new RowMapper&lt;String&gt;() {<br>                        @Override<br>                        @SneakyThrows<br>                        public String mapRow(ResultSet rs, int rowNum) {<br>                            return rs.getString(&quot;id&quot;);<br>                        }<br>                    }, param);<br>            <em>log</em>.info(&quot;We retrieved &quot; + ids.size() + &quot; records.&quot;);<br>        } catch (DataAccessException e) {<br>            <strong>final String uuid = UUID.<em>randomUUID</em>().toString();</strong><br>            <em>log</em>.error(<strong>uuid + </strong>&quot;Failed running query: &quot; + sql);<br>            <em>log</em>.error(<strong>uuid + </strong>&quot;Parameters: param=&quot; + param);<br>            <em>log</em>.error(<strong>uuid + </strong>&quot;Exception: &quot;, e);<br>            throw new AlreadyLoggedException(e<strong>, uuid</strong>);<br>        }<br>    }<br>}</pre><p>If you have to do this on many places, you might want to create a utility method to log the error and generate a UUID in there.</p><p>Finally, we can update the AlreadyLoggedException handler in the controller and make it pass the UUID to the HTTP response.</p><pre>@ExceptionHandler(AlreadyLoggedException.class)<br>@ResponseStatus(HttpStatus.<em>INTERNAL_SERVER_ERROR</em>)<br>public String handleUnexpectedErrorsThatAreAlreadyLogged(<br>        <strong>AlreadyLoggedException e</strong>) {<br>    <em>// Do not log<br>    </em><strong>return &quot;Error: &quot; + e.getUuid();</strong><br>}</pre><p>Here is what is looks like when you run the code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sgx8mOmOT_WgOGOIfwpjHQ.png" /></figure><p>In my example, I’ve not defined a whole API spec to keep it simple. If you have an API based on an OpenAPI spec, you want to define the JSON of the error responses and include this error UUID.</p><h3>Using Spring cloud sleuth</h3><p>When I wanted to implement passing a UUID back to the frontend at work I noticed that our project already used Sleuth for tracing. Sleuth provides autoconfiguration for distributed tracing. Distributed tracing is way beyond the scope of this article, but it does automatically add an id to every log message in your code. This id is unique for every incoming request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JtsqKkH6fBQu1HqyLXAulw.png" /><figcaption>Somehow it is printed twice, I’m not sure why.</figcaption></figure><p>To get this behavior, all you need to do is add this dependency to your spring boot application:</p><pre>&lt;dependency&gt;<br>   &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;<br>   &lt;artifactId&gt;spring-cloud-starter-sleuth&lt;/artifactId&gt;<br>&lt;/dependency&gt;</pre><p>You might also need to add this to your dependencyManagement if you haven’t already:</p><pre>&lt;dependencyManagement&gt;<br>   &lt;dependencies&gt;<br>      &lt;dependency&gt;<br>         &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;<br>         &lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;<br>         &lt;version&gt;2021.0.3&lt;/version&gt;<br>         &lt;type&gt;pom&lt;/type&gt;<br>         &lt;scope&gt;import&lt;/scope&gt;<br>      &lt;/dependency&gt;<br>   &lt;/dependencies&gt;<br>&lt;/dependencyManagement&gt;</pre><p>You can easily auto wire the Sleuth “tracer” in any Spring component to get the uuid and put it in your HTTP response:</p><pre>import lombok.extern.slf4j.Slf4j;<br>import org.springframework.beans.factory.annotation.Autowired;<br><strong>import org.springframework.cloud.sleuth.Tracer;</strong><br>import org.springframework.http.HttpStatus;<br>import org.springframework.web.bind.annotation.*;<br><br>import javax.servlet.http.HttpServletRequest;<br><br>@Slf4j<br>@RestController<br>public class GetSomethingController {<br><br>    private final BusinessLogic businessLogic;<br>    <strong>private final Tracer tracer;</strong><br><br>    @Autowired<br>    public GetSomethingController(<strong>Tracer tracer</strong>, BusinessLogic businessLogic) {<br>        <strong>this.tracer = tracer;</strong><br>        this.businessLogic = businessLogic;<br>    }<br><br>    @GetMapping(&quot;/&quot;)<br>    public String getSomething(String name) {<br>        businessLogic.doBusinessLogic();<br>        return &quot;Hello: &quot; + name;<br>    }<br><br>    @ExceptionHandler(AlreadyLoggedException.class)<br>    @ResponseStatus(HttpStatus.<em>INTERNAL_SERVER_ERROR</em>)<br>    public String handleUnexpectedErrorsThatAreAlreadyLogged() {<br>        <em>// Do not log<br>        </em>return &quot;Error: &quot;<strong> + tracer.currentSpan().context().traceId()</strong>;<br>    }<br><br>    @ExceptionHandler(Exception.class)<br>    @ResponseStatus(HttpStatus.<em>INTERNAL_SERVER_ERROR</em>)<br>    public String handleUnexpectedErrors(HttpServletRequest req, Exception e) {<br>        <em>log</em>.error(&quot;Unexpected error occurred on request: &quot; + req.getServletPath(), e);<br>        return &quot;Error: &quot; + <strong>tracer.currentSpan().context().traceId()</strong>;<br>    }<br><br>    <em>// Other handlers<br></em>}</pre><h3>Putting the trace id in a header</h3><p>You could also add a filter to add a header with the trace ID in any response. That way you can track any request, not just the ones that give an error:</p><pre>import javax.servlet.FilterChain;<br>import javax.servlet.ServletException;<br>import javax.servlet.ServletRequest;<br>import javax.servlet.ServletResponse;<br>import javax.servlet.http.HttpServletResponse;<br><br>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.cloud.sleuth.Tracer;<br>import org.springframework.stereotype.Component;<br>import org.springframework.web.filter.GenericFilterBean;<br><br>import java.io.IOException;<br><br>@Component<br>public class AddTraceIdToResponseFilter extends GenericFilterBean {<br>    private Tracer tracer;<br><br>    @Autowired<br>    public AddTraceIdToResponseFilter(Tracer tracer) {<br>        this.tracer = tracer;<br>    }<br><br>    @Override<br>    public void doFilter(<br>            ServletRequest request,<br>            ServletResponse response,<br>            FilterChain chain<br>    ) throws IOException, ServletException {<br>        if (response instanceof HttpServletResponse) {<br>            String traceId = tracer.currentSpan().context().traceId();<br>            HttpServletResponse res = (HttpServletResponse) response;<br>            res.addHeader(&quot;LEONS-TRACE-ID&quot;, traceId);<br>        }<br>        chain.doFilter(request, response);<br>    }<br>}</pre><p>When you run the application again with mvn spring-boot:run you should see the response header being added.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gRsXRcS2V5pJ7u3qRhtWtA.png" /></figure><h3>Validation errors</h3><p>In the GET request of my examples there is no validation. If you add POST or PUT requests you might need to add it. When we add a POST call to our RestController, we can put the @Valid annotation before the SomePost parameter to make Spring validate it:</p><pre><strong>import javax.validation.Valid;</strong></pre><pre>@Slf4j<br>@RestController<br>public class GetSomethingController {</pre><pre>// Other code</pre><pre><strong>  @PostMapping(&quot;/post&quot;)<br>  public String postSomething(@Valid @RequestBody SomePost somePost)<br>  {<br>    <em>log</em>.info(somePost.getEmail());<br>    return &quot;Posted&quot;;<br>  }</strong></pre><pre>// Exception handlers</pre><pre>}</pre><p>We need to define the SomePost class with some validation annotations to tell Spring how to validate it:</p><pre>import lombok.AllArgsConstructor;<br>import lombok.Data;<br><br><strong>import javax.validation.constraints.*;</strong><br><br>@AllArgsConstructor<br>@Data<br>public class SomePost {<br>    <strong>@NotNull(message = &quot;&#39;id&#39; field not found&quot;)<br>    @Min(value = 0L, message = &quot;The value must be a positive integer in the &#39;id&#39; field.&quot;)</strong><br>    private int id;<br><br>    <strong>@NotNull(message = &quot;&#39;message&#39; field not found&quot;)<br>    @Size(min = 5, max = 200, message = &quot;You must enter a message of min 5 characters and max 200 in the &#39;message&#39; field.&quot;)</strong><br>    private String message;<br><br>    <strong>@NotNull(message = &quot;&#39;email&#39; field not found&quot;)<br>    @Email(message = &quot;Enter a valid e-mail address in the &#39;email&#39; field.&quot;)</strong><br>    private String email;<br>}</pre><p>If we now run our application with mvn spring-boot:run and execute a curl command with an invalid e-mail in the request body:</p><pre>curl -X POST <a href="http://localhost:8080/post">http://localhost:8080/post</a> -H &#39;Content-Type: application/json&#39; -d &#39;{&quot;id&quot;:&quot;9001&quot;,&quot;message&quot;:&quot;My message&quot;, &quot;email&quot;:&quot;invalidemail&quot;}&#39;</pre><p>When Spring attempts to validate this request it will fail the validation and throw a MethodArgumentNotValidException. Our exception handler logs the stacktrace below and throws a 500:</p><pre>org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public java.lang.String net.leejjon.demo.GetSomethingController.postSomething(net.leejjon.demo.SomePost): [Field error in object &#39;somePost&#39; on field &#39;email&#39;: rejected value [invalidemail]; codes [Email.somePost.email,Email.email,Email.java.lang.String,Email]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [somePost.email,email]; arguments []; default message [email],[Ljavax.validation.constraints.Pattern$Flag;<a href="http://twitter.com/16ee46a2">@16ee46a2</a>,.*]; default message [must be a well-formed email address]] <br>        at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:141) ~[spring-webmvc-5.3.14.jar:5.3.14]</pre><p>Apart from this stacktrace not being useful as our application is not doing anything wrong, the service should respond with 400 (BAD_REQUEST).</p><p>So let’s write a separate exception handler for validation errors:</p><pre>@ExceptionHandler(MethodArgumentNotValidException.class)<br>@ResponseStatus(HttpStatus.<em>BAD_REQUEST</em>)<br>public String handleValidationErrors(HttpServletRequest req, MethodArgumentNotValidException e) {<br>    StringBuilder validationErrorMessage = new StringBuilder(&quot;Validation error: \n&quot;);<br>    for (ObjectError vallidationError : e.getAllErrors()) {<br>        validationErrorMessage.append(vallidationError.getObjectName());<br>        validationErrorMessage.append(&quot; &quot;);<br>        validationErrorMessage.append(vallidationError.getDefaultMessage());<br>        validationErrorMessage.append(&quot;\n&quot;);<br>    }<br><br>    return validationErrorMessage.toString();<br>}</pre><p>Now the curl command will get a 400 response with the following body:</p><pre>Validation error: Enter a valid e-mail address.</pre><h3>Too long didn’t read</h3><p>Things to remember:</p><ul><li>Catch all exceptions at the system boundaries. In a REST service this means the controller.</li><li>Log your exceptions with a randomly generated UUID.</li><li>Return the generated UUID to the consumer of your service. If they complain that something doesn’t work, ask for this UUID so you can find to the related exception in your logs.</li><li>Don’t log validation failures. Provide good API documentation so the consumer of your API can figure out what to do if they get 400’s back from your service.</li></ul><p>The full code <a href="https://github.com/Leejjon/ErrorHandlingSpring">can be found on GitHub</a>. There might be a 100 ways to improve this. If you think I might be missing something essential, let me know!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afda273c9d2f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>