<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: César Zoleko</title>
    <description>The latest articles on DEV Community by César Zoleko (@csar_zoleko_e6c3bb497f0d).</description>
    <link>https://dev.to/csar_zoleko_e6c3bb497f0d</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2586469%2F68f4a2c7-fb38-4422-84c9-ff1de8090fae.jpg</url>
      <title>DEV Community: César Zoleko</title>
      <link>https://dev.to/csar_zoleko_e6c3bb497f0d</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/csar_zoleko_e6c3bb497f0d"/>
    <language>en</language>
    <item>
      <title># Serving React as Static Files from NestJS — Te Underrated Production Pattern</title>
      <dc:creator>César Zoleko</dc:creator>
      <pubDate>Sat, 18 Apr 2026 16:33:20 +0000</pubDate>
      <link>https://dev.to/csar_zoleko_e6c3bb497f0d/-serving-react-as-static-files-from-nestjs-te-underrated-production-pattern-2o3h</link>
      <guid>https://dev.to/csar_zoleko_e6c3bb497f0d/-serving-react-as-static-files-from-nestjs-te-underrated-production-pattern-2o3h</guid>
      <description>&lt;p&gt;Most tutorials about NestJS and React (or Vue, Angular, Svelte — pick your SPA) assume you'll deploy them separately: a Node server for the API, a CDN or Nginx for the frontend, maybe a reverse proxy gluing them together. That setup works fine, but it comes with real operational overhead — two services to monitor, two deploy pipelines to maintain, and a whole category of bugs that only exist because your frontend and backend live on different origins.&lt;/p&gt;

&lt;p&gt;There's a simpler path. NestJS can serve your compiled SPA as static files, making the whole thing a single deployable unit. It's not widely discussed, and I think it deserves more attention.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Quick note on scope:&lt;/strong&gt; this pattern works with any Single Page Application — React, Vue 3, Angular, Svelte, SolidJS, you name it. The moment your framework's build step produces a static &lt;code&gt;dist/&lt;/code&gt; folder (which they all do), NestJS can serve it. I'll use &lt;strong&gt;React + Vite&lt;/strong&gt; throughout this article as a concrete example, but every concept applies directly to any other SPA.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  What "serving static files" actually means here
&lt;/h2&gt;

&lt;p&gt;When you run &lt;code&gt;npm run build&lt;/code&gt; on any modern SPA (React, Vue, Angular, Svelte…), you get a &lt;code&gt;dist/&lt;/code&gt; folder containing &lt;code&gt;index.html&lt;/code&gt;, bundled JS, CSS, and assets. That's it — no server required. Any HTTP server capable of serving files can host it.&lt;/p&gt;

&lt;p&gt;NestJS ships with &lt;code&gt;@nestjs/serve-static&lt;/code&gt;, a module that turns your NestJS app into exactly that kind of server. You point it at your build output, and NestJS handles the rest: serving &lt;code&gt;index.html&lt;/code&gt; for unknown routes (client-side routing support), caching headers, MIME types, everything.&lt;/p&gt;

&lt;p&gt;The result: &lt;strong&gt;one process, one port, one deployment&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Browser
  │
  ▼
NestJS (port 4200)
  ├── /api/*    → NestJS controllers (your REST API)
  └── /*        → React static files (index.html + assets)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Setting it up
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Install the module
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @nestjs/serve-static
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Configure the React build output
&lt;/h3&gt;

&lt;p&gt;With Vite, point the build output directly into your NestJS project so the compiled app is always in the right place:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// front-app/vite.config.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;defineConfig&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vite&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;react&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@vitejs/plugin-react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;react&lt;/span&gt;&lt;span class="p"&gt;()],&lt;/span&gt;
  &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;outDir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../back-app/client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;emptyOutDir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Register ServeStaticModule in NestJS
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.module.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ServeStaticModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/serve-static&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;join&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;ServeStaticModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forRoot&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;rootPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;..&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="na"&gt;renderPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="c1"&gt;// serve index.html for any unmatched route&lt;/span&gt;
      &lt;span class="na"&gt;exclude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/(.*)&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="c1"&gt;// keep /api/* routed to controllers&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="c1"&gt;// ... your other modules&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Prefix all your API routes
&lt;/h3&gt;

&lt;p&gt;This is the critical step. Every NestJS controller must live under a common prefix so it never conflicts with static file serving:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// main.ts&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppModule&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setGlobalPrefix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your React app then calls &lt;code&gt;/api/orders&lt;/code&gt;, &lt;code&gt;/api/products&lt;/code&gt; — same origin, no CORS needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Development workflow — keep Vite's HMR
&lt;/h3&gt;

&lt;p&gt;You still want two separate processes during development. Vite's HMR is too valuable to give up. Run NestJS on port 4200 and Vite on port 3000, with Vite proxying &lt;code&gt;/api&lt;/code&gt; requests to the backend:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// vite.config.ts (dev only)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;proxy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:4200&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;changeOrigin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In development, Vite serves the frontend. In production, NestJS serves the compiled output. The code itself — every &lt;code&gt;fetch('/api/...')&lt;/code&gt; call — stays identical.&lt;/p&gt;




&lt;h2&gt;
  
  
  The advantages that actually matter in production
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Same-origin requests — cookies and CSRF just work
&lt;/h3&gt;

&lt;p&gt;This is the biggest win and the one most people overlook.&lt;/p&gt;

&lt;p&gt;When your frontend and backend share the same origin (&lt;code&gt;https://yourdomain.com&lt;/code&gt;), cookies are sent automatically with every request. Session-based authentication, CSRF double-submit patterns, &lt;code&gt;SameSite: strict&lt;/code&gt; cookies — all of it works without any extra configuration.&lt;/p&gt;

&lt;p&gt;With a split deployment you need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configure CORS (&lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt;, &lt;code&gt;credentials: true&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Align cookie domains and &lt;code&gt;SameSite&lt;/code&gt; policies across origins&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;credentials: 'include'&lt;/code&gt; on every fetch call&lt;/li&gt;
&lt;li&gt;Accept that &lt;code&gt;SameSite: strict&lt;/code&gt; is off the table&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each one of these is a potential footgun. A concrete example: in a session + CSRF setup (like &lt;code&gt;csrf-csrf&lt;/code&gt;), the token validation ties the CSRF token to the session ID. With same-origin, the session cookie arrives with every request automatically. With cross-origin, you're one misconfigured &lt;code&gt;allowedOrigins&lt;/code&gt; away from a 403 in production that only manifests after a page refresh.&lt;/p&gt;

&lt;h3&gt;
  
  
  One service to deploy and monitor
&lt;/h3&gt;

&lt;p&gt;A typical split setup means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Two Docker images, two build pipelines&lt;/li&gt;
&lt;li&gt;Two services in &lt;code&gt;docker-compose.yml&lt;/code&gt; or Kubernetes manifests&lt;/li&gt;
&lt;li&gt;A reverse proxy (Nginx, Traefik) sitting in front of both&lt;/li&gt;
&lt;li&gt;Logs and metrics spread across multiple services&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the unified approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# docker-compose.yml — that's the whole file&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;4200:4200"&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;NODE_ENV=production&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;DATABASE_URL=${DATABASE_URL}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One image. One service. One set of logs. One health check endpoint.&lt;/p&gt;

&lt;h3&gt;
  
  
  A clean multi-stage Dockerfile
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:20-alpine&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;frontend-build&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app/front-app&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; front-app/package*.json ./&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm ci
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; front-app/ .&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm run build
&lt;span class="c"&gt;# Vite outputs to ../back-app/client (configured in vite.config.ts)&lt;/span&gt;

&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:20-alpine&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;backend-build&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app/back-app&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; back-app/package*.json ./&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm ci
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; back-app/ .&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=frontend-build /app/back-app/client ./client&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm run build

&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; node:20-alpine&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=backend-build /app/back-app/dist ./dist&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=backend-build /app/back-app/client ./client&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=backend-build /app/back-app/node_modules ./node_modules&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; back-app/package.json .&lt;/span&gt;

&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 4200&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["node", "dist/main"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No Nginx config. No reverse proxy. The Node process handles everything.&lt;/p&gt;

&lt;h3&gt;
  
  
  No environment-specific frontend builds
&lt;/h3&gt;

&lt;p&gt;In a split deployment, your React app needs to know the API URL at build time — a &lt;code&gt;VITE_API_URL&lt;/code&gt; env variable baked into the bundle. That URL must match wherever you actually deployed the backend. Get it wrong and you're shipping a build that points at the wrong server, or worse, at localhost.&lt;/p&gt;

&lt;p&gt;With unified serving, the API is always at the same origin. No &lt;code&gt;VITE_API_URL&lt;/code&gt; to manage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// same line, every environment — staging, preprod, production&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/orders&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same build artifact works everywhere. No environment-specific rebuilds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Static asset caching with Vite's content hashes
&lt;/h3&gt;

&lt;p&gt;Vite hashes all asset filenames by default (&lt;code&gt;main.a3f2b1.js&lt;/code&gt;). This means you can apply aggressive caching headers safely — browsers will only re-fetch when the content actually changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;ServeStaticModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forRoot&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;rootPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;..&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;serveStaticOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;setHeaders&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;filePath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\.(&lt;/span&gt;&lt;span class="sr"&gt;js|css|png|jpg|svg|woff2&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filePath&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cache-Control&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;public, max-age=31536000, immutable&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;index.html&lt;/code&gt; gets no cache header (or a short one), so deployments propagate immediately while assets stay cached on the client indefinitely.&lt;/p&gt;




&lt;h2&gt;
  
  
  When to use it — and when not to
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;Recommendation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Single NestJS app + single React frontend&lt;/td&gt;
&lt;td&gt;✅ Unified serving&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Session-based auth or strict CSRF&lt;/td&gt;
&lt;td&gt;✅ Unified serving (same-origin is a hard win)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Small to medium team, monolith deployment&lt;/td&gt;
&lt;td&gt;✅ Unified serving&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multiple frontend apps sharing the same backend&lt;/td&gt;
&lt;td&gt;❌ Split deployment&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Frontend needs global CDN edge caching&lt;/td&gt;
&lt;td&gt;❌ Split deployment&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Microservices backend&lt;/td&gt;
&lt;td&gt;❌ Split deployment&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Independent scaling of frontend and backend&lt;/td&gt;
&lt;td&gt;❌ Split deployment&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Why you don't see this pattern often
&lt;/h2&gt;

&lt;p&gt;The industry default of "separate everything" made sense when teams were large, traffic was high, and independent scaling was a priority from day one. That mindset spread, became the tutorial default, and stuck.&lt;/p&gt;

&lt;p&gt;There's also a perception issue: serving static files from an API server feels wrong to people who think of Node as "not a real web server." That mental model is outdated. Node handles static file serving perfectly well for the vast majority of projects, and &lt;code&gt;@nestjs/serve-static&lt;/code&gt; is built exactly for this use case.&lt;/p&gt;

&lt;p&gt;The result is that a genuinely useful, operationally simple pattern gets overlooked in favour of more complex setups that most projects don't need.&lt;/p&gt;




&lt;h2&gt;
  
  
  Closing thoughts
&lt;/h2&gt;

&lt;p&gt;Serving React from NestJS removes an entire class of cross-origin problems, collapses your deployment to a single container, and makes local-to-production parity easy to maintain. The &lt;code&gt;@nestjs/serve-static&lt;/code&gt; module handles the hard parts. The rest is pointing it at a &lt;code&gt;dist/&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;It's not the flashiest architecture. In production, that's usually the point.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Have you tried this pattern in a real project? Curious about the tradeoffs you ran into — drop a comment below.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Tags:&lt;/strong&gt; &lt;code&gt;#nestjs&lt;/code&gt; &lt;code&gt;#react&lt;/code&gt; &lt;code&gt;#vue&lt;/code&gt; &lt;code&gt;#angular&lt;/code&gt; &lt;code&gt;#spa&lt;/code&gt; &lt;code&gt;#typescript&lt;/code&gt; &lt;code&gt;#webdev&lt;/code&gt; &lt;code&gt;#nodejs&lt;/code&gt; &lt;code&gt;#devops&lt;/code&gt; &lt;code&gt;#javascript&lt;/code&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>node</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Dynamic forms with discriminatedUnion and React Hook Form</title>
      <dc:creator>César Zoleko</dc:creator>
      <pubDate>Thu, 19 Dec 2024 11:41:44 +0000</pubDate>
      <link>https://dev.to/csar_zoleko_e6c3bb497f0d/dynamic-forms-with-discriminatedunion-and-react-hook-form-276a</link>
      <guid>https://dev.to/csar_zoleko_e6c3bb497f0d/dynamic-forms-with-discriminatedunion-and-react-hook-form-276a</guid>
      <description>&lt;p&gt;Form validation is a crucial aspect of modern web applications. With libraries such as React Hook Form (RHF) and Zod, you can efficiently validate dynamic forms, including those with complex structures such as payment methods. This article explains how to use both Zod and RHF to validate forms dynamically using the powerful discriminatedUnion.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is a discriminatedUnion?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A discriminatedUnion is an advanced typing technique used to model objects with different structures but sharing a common discriminating field. This discriminant field is used to identify which subtype is being used and to perform validation or manipulation accordingly.&lt;br&gt;
For example, consider a payment form that may contain three types of payment method:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Credit card (requires a card number and CVV)&lt;/li&gt;
&lt;li&gt;PayPal (requires an email address)&lt;/li&gt;
&lt;li&gt;Bank transfer (requires an account number and bank code).
Each of these types can be modelled using a discriminatedUnion
&lt;strong&gt;Example of Data Modelling with Zod&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as zod from "zod"

const IsNotEmptyString = (message: string) =&amp;gt; zod.string({message}).min(1, {message})


export enum PaymentMethodEnum {
   CREDIT_CARD ="creditCard", 
   PAYPAL = "paypal", 
   BANKTRANSFER ="bankTransfer"
}

  const creditCardSchema = zod.object({
   type: zod.literal(PaymentMethodEnum.CREDIT_CARD),
   cardNumber: zod.string().regex(/^\d{16}$/, "The card number must contain 16 digits"),
   cvv: zod.string().regex(/^\d{3}$/, "The Card Validation Code must contain 3 digits"),
 })

 const paypalSchema =  zod.object({
   type: zod.literal(PaymentMethodEnum.PAYPAL),
   email: zod.string().email("PayPal email is invalid"),
 })

 const bankTransferSchema =  zod.object({
   type: zod.literal(PaymentMethodEnum.BANKTRANSFER),
   accountNumber: IsNotEmptyString("The account number must contain at least 10 characters"),
   bankCode: IsNotEmptyString("The bank code must contain at least 4 characters")
 })

export const paymentMethodSchema =  () =&amp;gt; zod.discriminatedUnion("type",[
   creditCardSchema, paypalSchema,bankTransferSchema
]); 

export type PaymentMethodSchemaType = zod.infer &amp;lt; ReturnType &amp;lt;typeof paymentMethodSchema&amp;gt;&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;**&lt;/p&gt;
&lt;h2&gt;
  
  
  How does validation work?
&lt;/h2&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;discriminatedUnion inspects the type field:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If type is ‘creditCard’, Zod validates according to 
the constraints defined in the first object.&lt;/li&gt;
&lt;li&gt;If type is ‘paypal’, it checks only the rules linked 
to the PayPal object.&lt;/li&gt;
&lt;li&gt;If type is ‘bankTransfer’, it validates according to 
the bank transfer criteria.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Strict validation :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each type is independent. If a user 
provides an invalid type or omits a required field, 
Zod triggers a specific error.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Simplicity for React Hook Form:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The schema allows RHF to dynamically adapt validation 
according to the type field, simplifying the logic in 
the form.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;**&lt;/p&gt;
&lt;h2&gt;
  
  
  Why use a discriminatedUnion here?
&lt;/h2&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Clear separation of responsibilities 

&lt;ul&gt;
&lt;li&gt;Each payment method specific rules that must not 
interfere with each other interfere.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Flexibility

&lt;ul&gt;
&lt;li&gt;Allows new types (e.g. a new payment 
method) to be added easily. types (for example, a new payment method).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Security 

&lt;ul&gt;
&lt;li&gt;Ensures that any invalid values are detected at 
validation time.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;**&lt;/p&gt;
&lt;h2&gt;
  
  
  Integration with React Hook Form
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
React Hook Form makes it easy to manage forms while remaining high-performance and flexible. Here's how to integrate Zod and RHF to validate a form based on discriminatedUnion.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useForm, SubmitHandler, FieldErrors } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import './payment.css';
import {
  PaymentMethodEnum,
  paymentMethodSchema,
  PaymentMethodSchemaType,
} from '../validators/validate-payment-schema';

const Payment = () =&amp;gt; {
  const form = useForm&amp;lt;PaymentMethodSchemaType&amp;gt;({
    resolver: zodResolver(paymentMethodSchema()),
    defaultValues: {
      type: PaymentMethodEnum.CREDIT_CARD,
    },
  });

  const { register, formState, handleSubmit } = form;

  const { errors } = formState;

  const paymentType = form.watch().type;

  const handleChangePaymentType = (type: PaymentMethodEnum) =&amp;gt; {
    form.setValue('type', type);
  };

  const handleResetForm = () =&amp;gt; {
    form.reset(getDefaultFormState(paymentType));
  };

  const onSubmit: SubmitHandler&amp;lt;PaymentMethodSchemaType&amp;gt; = (data) =&amp;gt; {
    console.log('data', data);
  };

  const PaymentTypeFormNode: React.ReactNode = (() =&amp;gt; {
    switch (paymentType) {
      case PaymentMethodEnum.BANKTRANSFER:
        const bankTransferErrors = getErrorsByPaymentType(errors, paymentType);

        return (
          &amp;lt;div&amp;gt;
            &amp;lt;div className="form"&amp;gt;
              &amp;lt;label&amp;gt;Account number&amp;lt;/label&amp;gt;
              &amp;lt;input
                {...register('accountNumber')}
                placeholder="Enter your account number"
              /&amp;gt;
              {bankTransferErrors?.accountNumber?.message &amp;amp;&amp;amp; (
                &amp;lt;span className="errormessage"&amp;gt;
                  {bankTransferErrors.accountNumber.message}
                &amp;lt;/span&amp;gt;
              )}
            &amp;lt;/div&amp;gt;

            &amp;lt;div className="form"&amp;gt;
              &amp;lt;label&amp;gt;Bank code&amp;lt;/label&amp;gt;
              &amp;lt;input
                {...register('bankCode')}
                placeholder="Enter your bank code"
              /&amp;gt;
              {bankTransferErrors?.bankCode?.message &amp;amp;&amp;amp; (
                &amp;lt;span className="errormessage"&amp;gt;
                  {bankTransferErrors.bankCode.message}
                &amp;lt;/span&amp;gt;
              )}
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;
        );

      case PaymentMethodEnum.CREDIT_CARD:
        const creditCardErrors = getErrorsByPaymentType(errors, paymentType);
        return (
          &amp;lt;div&amp;gt;
            &amp;lt;div className="form"&amp;gt;
              &amp;lt;label&amp;gt;Card Number&amp;lt;/label&amp;gt;
              &amp;lt;input
                {...register('cardNumber')}
                placeholder="Enter your card number"
              /&amp;gt;
              {creditCardErrors?.cardNumber &amp;amp;&amp;amp; (
                &amp;lt;span className="errormessage"&amp;gt;
                  {creditCardErrors.cardNumber.message}
                &amp;lt;/span&amp;gt;
              )}
            &amp;lt;/div&amp;gt;

            &amp;lt;div className="form"&amp;gt;
              &amp;lt;label&amp;gt;CVV&amp;lt;/label&amp;gt;
              &amp;lt;input
                {...register('cvv')}
                placeholder="Enter your card validation code"
              /&amp;gt;
              {creditCardErrors?.cvv &amp;amp;&amp;amp; (
                &amp;lt;span className="errormessage"&amp;gt;
                  {creditCardErrors.cvv.message}
                &amp;lt;/span&amp;gt;
              )}
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;
        );

      case PaymentMethodEnum.PAYPAL:
        const paypalErrors = getErrorsByPaymentType(errors, paymentType);
        return (
          &amp;lt;div className="form"&amp;gt;
            &amp;lt;label&amp;gt;Email&amp;lt;/label&amp;gt;
            &amp;lt;input
              type="email"
              {...register('email')}
              placeholder="Enter your email"
            /&amp;gt;
            {paypalErrors?.email?.message &amp;amp;&amp;amp; (
              &amp;lt;span className="errormessage"&amp;gt;{paypalErrors.email.message}&amp;lt;/span&amp;gt;
            )}
          &amp;lt;/div&amp;gt;
        );

      default:
        throw new Error(
          'Exhaustive Guard Error : received value' + paymentType
        );
    }
  })();

  return (
    &amp;lt;form className="form-wrapper" onSubmit={handleSubmit(onSubmit)}&amp;gt;
      &amp;lt;div className="form" style={{ marginBottom: '1rem' }}&amp;gt;
        &amp;lt;label&amp;gt;Type de paiement&amp;lt;/label&amp;gt;
        &amp;lt;select
          value={paymentType}
          onChange={(e) =&amp;gt;
            handleChangePaymentType(e.target.value as PaymentMethodEnum)
          }
          style={{ display: 'block', marginTop: '0.5rem' }}
        &amp;gt;
          &amp;lt;option value="creditCard"&amp;gt;Credit Card&amp;lt;/option&amp;gt;
          &amp;lt;option value="paypal"&amp;gt;PayPal&amp;lt;/option&amp;gt;
          &amp;lt;option value="bankTransfer"&amp;gt;Bank Transfer&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;

        &amp;lt;span className="icon"&amp;gt;
          &amp;lt;svg
            xmlns="http://www.w3.org/2000/svg"
            width="10"
            height="6"
            viewBox="0 0 10 6"
          &amp;gt;
            &amp;lt;path
              id="Tracé_532"
              data-name="Tracé 532"
              d="M295.717,353.232a.725.725,0,0,1,1.063,0,.8.8,0,0,1,.163.256.834.834,0,0,1,0,.606.8.8,0,0,1-.163.256l-4.248,4.418a.726.726,0,0,1-1.064,0l-4.248-4.418a.8.8,0,0,1-.163-.256.834.834,0,0,1,0-.606.8.8,0,0,1,.163-.256.725.725,0,0,1,1.063,0L292,356.856Z"
              transform="translate(-287 -353)"
              fill="#333"
            /&amp;gt;
          &amp;lt;/svg&amp;gt;
        &amp;lt;/span&amp;gt;
      &amp;lt;/div&amp;gt;

      {PaymentTypeFormNode}

      &amp;lt;div style={{ display: 'flex', gap: 20 }}&amp;gt;
        &amp;lt;button type="button" onClick={handleResetForm}&amp;gt;
          Cancel
        &amp;lt;/button&amp;gt;
        &amp;lt;button type="submit" className="submit-btn"&amp;gt;
          Submit
        &amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
  );
};

export default Payment;

function getErrorsByPaymentType&amp;lt;T extends PaymentMethodSchemaType['type']&amp;gt;(
  errors: FieldErrors&amp;lt;PaymentMethodSchemaType&amp;gt;,
  _type: T
): FieldErrors&amp;lt;Extract&amp;lt;PaymentMethodSchemaType, { type: T }&amp;gt;&amp;gt; | null {
  return errors as FieldErrors&amp;lt;Extract&amp;lt;PaymentMethodSchemaType, { type: T }&amp;gt;&amp;gt;;
}

const bankTransferDefault: Extract&amp;lt;
  PaymentMethodSchemaType,
  { type: PaymentMethodEnum.BANKTRANSFER }
&amp;gt; = {
  accountNumber: '',
  bankCode: '',
  type: PaymentMethodEnum.BANKTRANSFER,
};

const paypalDefault: Extract&amp;lt;
  PaymentMethodSchemaType,
  { type: PaymentMethodEnum.PAYPAL }
&amp;gt; = {
  email: '',
  type: PaymentMethodEnum.PAYPAL,
};

const creditCardDefault: Extract&amp;lt;
  PaymentMethodSchemaType,
  { type: PaymentMethodEnum.CREDIT_CARD }
&amp;gt; = {
  cardNumber: '',
  cvv: '',
  type: PaymentMethodEnum.CREDIT_CARD,
};

const defaultValue = [creditCardDefault, paypalDefault, bankTransferDefault];

const getDefaultFormState= (type: PaymentMethodEnum) =&amp;gt; {
  return defaultValue.find(
    (item) =&amp;gt; item.type === type
  ) as PaymentMethodSchemaType;
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
So that's it folks. I hope you found this article helpful. discriminatedUnion is a utility type that can be used in many ways. If you think there are more ways to use discriminatedUnion, please let me know in the comments. Thanks for reading this article. See you all in my next article🐸.&lt;/p&gt;

&lt;p&gt;test application link: &lt;br&gt;
&lt;a href="https://stackblitz.com/edit/vitejs-vite-ppgw9zrb?file=src%2Fpages%2Fpayments.tsx" rel="noopener noreferrer"&gt;https://stackblitz.com/edit/vitejs-vite-ppgw9zrb?file=src%2Fpages%2Fpayments.tsx&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>typescript</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
