<?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: Rushi Patel</title>
    <description>The latest articles on DEV Community by Rushi Patel (@rushi-patel).</description>
    <link>https://dev.to/rushi-patel</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%2F973135%2F6c773693-9d39-45a0-9444-52723de0d37b.jpg</url>
      <title>DEV Community: Rushi Patel</title>
      <link>https://dev.to/rushi-patel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rushi-patel"/>
    <language>en</language>
    <item>
      <title>Interview Cheat Sheet: App Performance in React Native</title>
      <dc:creator>Rushi Patel</dc:creator>
      <pubDate>Sat, 08 Mar 2025 16:28:36 +0000</pubDate>
      <link>https://dev.to/rushi-patel/interview-cheat-sheet-app-performance-in-react-native-3p5k</link>
      <guid>https://dev.to/rushi-patel/interview-cheat-sheet-app-performance-in-react-native-3p5k</guid>
      <description>&lt;h2&gt;
  
  
  Special Article: Interview Cheat Sheet
&lt;/h2&gt;

&lt;p&gt;This cheat sheet is designed to help you quickly answer interview questions and prepare for presentations on optimizing app performance in React Native. It covers the need for performance optimization, detailed app-level and code-level strategies, what React Native offers out of the box, and common mistakes to avoid.&lt;/p&gt;



&lt;h2&gt;
  
  
  Why Focus on App Performance in React Native?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;User Experience&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Engagement &amp;amp; Retention&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Resource Efficiency&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Competitive Advantage&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  What this cheat sheet covers:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;App-Level Optimizations:&lt;/strong&gt; Strategies that affect the overall app architecture and resource management.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Code-Level Optimizations:&lt;/strong&gt; Best practices in coding to reduce unnecessary computations and improve rendering.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;React Native Features:&lt;/strong&gt; Built-in tools and libraries that enhance performance.&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  App-Level Optimizations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Efficient Asset Management:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Use appropriately sized images and leverage caching tools.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;SVGs &amp;amp; CDN:&lt;/strong&gt; Utilize SVGs for vector images and host images on a CDN to reduce bundle size.&lt;/li&gt;
&lt;li&gt;  Implement lazy loading to defer non-critical assets until needed.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Network Optimizations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Request Batching:&lt;/strong&gt; Combine multiple network requests into one.&lt;/li&gt;
&lt;li&gt;  Use caching strategies to reduce redundant calls.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Optimized Build Configuration:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Code Splitting:&lt;/strong&gt; Load only the necessary code chunks.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Tree Shaking:&lt;/strong&gt; Remove unused code with modern bundlers.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Minification &amp;amp; Obfuscation:&lt;/strong&gt; Use production builds to reduce size and improve load times.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Profiling &amp;amp; Monitoring:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Integrate tools like &lt;strong&gt;Flipper&lt;/strong&gt; to monitor performance.&lt;/li&gt;
&lt;li&gt;  Employ error tracking services (e.g., Sentry) to catch runtime issues early.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;h2&gt;
  
  
  Code-Level Optimizations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Optimizing Rendering:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Avoid Unnecessary Renders:&lt;/strong&gt; Use &lt;code&gt;React.memo&lt;/code&gt;, &lt;code&gt;PureComponent&lt;/code&gt;, and control re-renders.&lt;/li&gt;
&lt;li&gt;  Leverage &lt;strong&gt;useMemo&lt;/strong&gt; and &lt;strong&gt;useCallback&lt;/strong&gt; to memoize values and functions.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Avoid Inline Functions &amp;amp; Objects:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Define functions and objects outside of render or memoize them to prevent re-creation on each render.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Efficient State Management:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Keep state localized when possible.&lt;/li&gt;
&lt;li&gt;  Use immutable updates to make re-render decisions more efficient.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Optimizing Animations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Always set &lt;code&gt;useNativeDriver: true&lt;/code&gt; to offload animation work to native code.&lt;/li&gt;
&lt;li&gt;This is transfer the heavy animation operations from JS to Native side (Android/iOS)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Avoid Expensive Computations in Render:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Precompute heavy values outside the render cycle.&lt;/li&gt;
&lt;li&gt;Reuse the heavy computed values instead of calculating them every single time.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Virtualization:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Use &lt;code&gt;FlatList&lt;/code&gt;, &lt;code&gt;SectionList&lt;/code&gt;, or &lt;code&gt;VirtualizedList&lt;/code&gt; to render large datasets efficiently.&lt;/li&gt;
&lt;li&gt;Avoid using &lt;code&gt;ScrollView&lt;/code&gt; when rendering homogeneous large data and instead leverage the Virtualization.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Effective use of useEffect:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Manage side effects properly. For example, fetch data on component mount:&lt;/li&gt;
&lt;li&gt;Verify the dependency array is correct to avoid unnecessary trigger of useEffects.&lt;/li&gt;
&lt;li&gt;There is no thumb rule, but try to avoid using too many useEffects in a single component.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Error Boundaries:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Implement error boundaries to catch and handle runtime errors gracefully.&lt;/li&gt;
&lt;li&gt;This improves the User Experience (UX) as app doesn't crashes for runtime errors.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Leveraging Async Operations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Use asynchronous functions (&lt;code&gt;async/await&lt;/code&gt;) to offload heavy computations and avoid blocking the main thread.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Advanced Network Optimizations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Implement request batching, pagination, and caching responses at the code level to reduce data load and improve performance.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;h2&gt;
  
  
  What React Native Provides for a Performant App (Brief)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Hermes JavaScript Engine:&lt;/strong&gt; Improves start-up time and reduces memory usage.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Native Driver for Animations:&lt;/strong&gt; Offloads animations to native code.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Flipper Integration:&lt;/strong&gt; Provides insights into network calls, layout performance, and more.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Fast Refresh:&lt;/strong&gt; Enhances developer experience by retaining component state during updates.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Bridging Architecture:&lt;/strong&gt; Efficient communication between JavaScript and native modules. (part of Old Architecture)&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Optimized UI Components:&lt;/strong&gt; Components like &lt;code&gt;FlatList&lt;/code&gt;, &lt;code&gt;SectionList&lt;/code&gt;, and &lt;code&gt;VirtualizedList&lt;/code&gt; are designed for high performance.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Over-the-Air Updates:&lt;/strong&gt; Use tools like CodePush to deliver performance improvements without a full app update.&lt;/li&gt;
&lt;/ul&gt;




&lt;blockquote&gt;
&lt;p&gt;Pro-tip: &lt;br&gt;
Leveraging tools like &lt;strong&gt;SonarQube&lt;/strong&gt;, &lt;strong&gt;ESLint&lt;/strong&gt;, and &lt;strong&gt;Prettier&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Using these tools can significantly improve code quality while writing it. These tools help enforce best practices, catch potential bugs early, and ensure consistency in the codebase. &lt;/p&gt;

&lt;p&gt;Integrating them into your development workflow enables real-time feedback, reducing technical debt and improving maintainability.&lt;/p&gt;


&lt;/blockquote&gt;



&lt;br&gt;
&lt;br&gt;
&lt;h2&gt;
  
  
  Closing Comments
&lt;/h2&gt;

&lt;p&gt;This cheat sheet serves as a handy reference to quickly recall essential strategies and best practices when discussing or implementing performance optimizations in React Native. Use it to prepare for interviews, technical discussions, or presentations, and ensure you cover both high-level concepts and practical code-level techniques.&lt;/p&gt;




&lt;p&gt;For any questions or suggestions, please feel free to comment below. 💬&lt;/p&gt;

&lt;p&gt;If you find this article useful, share it with your friends and follow for regular update of such good articles. 🔗&lt;/p&gt;

&lt;p&gt;Happy interviewing and coding!&lt;/p&gt;

&lt;p&gt;Rushi Patel, Signing Off! 😊&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>interview</category>
      <category>cheatsheet</category>
      <category>performance</category>
    </item>
    <item>
      <title>Optimising App Performance in React Native: A Detailed Guide</title>
      <dc:creator>Rushi Patel</dc:creator>
      <pubDate>Wed, 05 Mar 2025 05:28:17 +0000</pubDate>
      <link>https://dev.to/rushi-patel/optimising-app-performance-in-react-native-a-detailed-guide-2g8e</link>
      <guid>https://dev.to/rushi-patel/optimising-app-performance-in-react-native-a-detailed-guide-2g8e</guid>
      <description>&lt;h2&gt;
  
  
  React Native App Performance
&lt;/h2&gt;

&lt;p&gt;In today's fast-paced digital world, app performance directly impacts user experience, engagement, and retention. React Native simplifies cross-platform development, but as apps grow in complexity, maintaining optimal performance becomes challenging. &lt;/p&gt;

&lt;p&gt;This guide explores &lt;strong&gt;key performance factors, best practices, and optimization strategies—both at the app and code level—along with common pitfalls and real-world examples&lt;/strong&gt; to help you build smooth, efficient applications.  &lt;/p&gt;



&lt;h2&gt;
  
  
  The Need for a Performant App
&lt;/h2&gt;

&lt;p&gt;A high-performance app is crucial for several reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Enhanced User Experience:&lt;/strong&gt; Slow load times, laggy interactions, or choppy animations can frustrate users and drive them away from your app.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Higher Engagement &amp;amp; Retention:&lt;/strong&gt; A smooth, responsive interface encourages users to spend more time on the app, which is key for user retention and engagement.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Better Reviews &amp;amp; Ratings:&lt;/strong&gt; Users are more likely to leave positive reviews when they have a seamless experience.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Efficient Resource Usage:&lt;/strong&gt; Optimized performance means your app uses less battery and data, which is essential for mobile devices.&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  Factors That Influence App Performance
&lt;/h2&gt;

&lt;p&gt;While some performance considerations are specific to React Native, many factors apply universally to mobile apps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;CPU and Memory Usage:&lt;/strong&gt; Excessive computations or memory leaks can slow down your app, especially on devices with limited resources.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Network Latency:&lt;/strong&gt; Unoptimized network calls and large payloads can lead to delays in data fetching.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Rendering Efficiency:&lt;/strong&gt; The way UI elements are rendered (e.g., overdraw, heavy animations) can significantly affect performance.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Concurrency and Multithreading:&lt;/strong&gt; Proper handling of asynchronous tasks ensures that heavy computations do not block the main UI thread.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Resource Loading:&lt;/strong&gt; Efficiently loading images, fonts, and other assets is critical to reduce initial load times.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Garbage Collection:&lt;/strong&gt; Inefficient memory management can lead to frequent garbage collection pauses, causing jank.&lt;/li&gt;
&lt;/ol&gt;



&lt;h2&gt;
  
  
  Improving Performance in React Native
&lt;/h2&gt;

&lt;p&gt;Optimizing performance in React Native involves strategies at both the &lt;strong&gt;app level&lt;/strong&gt; and the &lt;strong&gt;code level&lt;/strong&gt;. Let's dive into these two areas:&lt;/p&gt;
&lt;h3&gt;
  
  
  App-Level Optimizations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Efficient Asset Management:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Image Optimization:&lt;/strong&gt; Use appropriately sized images and leverage tools like &lt;code&gt;react-native-fast-image&lt;/code&gt; to handle caching and progressive loading.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;SVGs &amp;amp; CDN:&lt;/strong&gt; Use SVGs for vector images when possible and consider hosting your images and other static assets on a Content Delivery Network (CDN) instead of bundling them with the app. This approach reduces bundle size and improves load times.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Lazy Loading Assets:&lt;/strong&gt; Load non-critical assets on demand rather than at startup.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Network Optimizations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Batching Requests:&lt;/strong&gt; Reduce the number of network calls by batching data requests.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Caching Strategies:&lt;/strong&gt; Implement caching mechanisms to minimize repeated network calls.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Optimized Build Configuration:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Code Splitting:&lt;/strong&gt; Break your code into smaller chunks and load them as needed.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Tree Shaking:&lt;/strong&gt; Use modern bundlers that support tree shaking to remove dead code and reduce bundle size. (&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Tree_shaking" rel="noopener noreferrer"&gt;Learn More&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Minification and Obfuscation:&lt;/strong&gt; Use production builds with minification to reduce app size and load times. (&lt;a href="https://medium.com/@aqeel_ahmad/code-obfuscation-in-react-native-a-comprehensive-guide-46a9f8bedd95" rel="noopener noreferrer"&gt;Learn More&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Profiling and Monitoring:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Performance Monitoring Tools:&lt;/strong&gt; Integrate tools like &lt;a href="https://fbflipper.com/docs/getting-started/" rel="noopener noreferrer"&gt;Flipper&lt;/a&gt; and React Native &lt;a href="https://reactnative.dev/docs/performance" rel="noopener noreferrer"&gt;Performance Monitor&lt;/a&gt; to identify bottlenecks.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Error Tracking:&lt;/strong&gt; Use services such as &lt;em&gt;Sentry&lt;/em&gt; to monitor runtime errors that could impact performance.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;h3&gt;
  
  
  Code-Level Optimizations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Optimizing Rendering:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Avoid Unnecessary Renders:&lt;/strong&gt; Use &lt;code&gt;React.memo&lt;/code&gt;, &lt;code&gt;PureComponent&lt;/code&gt;, or &lt;code&gt;shouldComponentUpdate&lt;/code&gt; to prevent redundant renders.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;useMemo &amp;amp; useCallback:&lt;/strong&gt; Leverage these hooks to memoize values and callbacks, preventing the creation of new instances on every render.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight tsx"&gt;&lt;code&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useMemo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;TouchableOpacity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;StyleSheet&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handlePress&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// handle press action&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;containerStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#fff&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;}),&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;TouchableOpacity&lt;/span&gt; &lt;span class="na"&gt;onPress&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handlePress&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;containerStyle&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;TouchableOpacity&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Avoiding Inline Functions and Objects:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Define functions and objects outside of render methods or memoize them using hooks to avoid creating new instances on every render. Which will eventually improve the app performance.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Efficient State Management:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Local vs. Global State:&lt;/strong&gt; Keep state as localized as possible to avoid unnecessary re-renders. Use libraries like Redux or MobX judiciously.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Immutable Data Structures:&lt;/strong&gt; Prefer using immutable updates so React can quickly determine when a re-render is necessary.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Optimizing Animations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Use Native Driver:&lt;/strong&gt; For animations, always set &lt;code&gt;useNativeDriver: true&lt;/code&gt; when possible. This offloads animation calculations from JavaScript to the native side.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Animated&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;Animated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fadeAnim&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;toValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;useNativeDriver&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// set this to true where possible&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Avoiding Expensive Computations in Render:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Precompute Values:&lt;/strong&gt; Move heavy calculations outside the render cycle. Consider computing them during data fetching or within lifecycle methods/hooks.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Virtualization:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Virtualized Lists:&lt;/strong&gt; Utilize React Native's built-in components like &lt;code&gt;FlatList&lt;/code&gt;, &lt;code&gt;SectionList&lt;/code&gt;, or &lt;code&gt;VirtualizedList&lt;/code&gt; to efficiently render large datasets. Virtualization ensures that only the visible items are rendered, which reduces memory usage and improves performance.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Effective use of useEffect:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Managing Side Effects:&lt;/strong&gt; Use the &lt;code&gt;useEffect&lt;/code&gt; hook to handle side effects efficiently. For example, fetching data once when a component mounts.&lt;/li&gt;
&lt;li&gt;Make sure hooks have correct variables in dependency array in order to under or over calls of effects.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Error Boundaries:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Handling Runtime Errors:&lt;/strong&gt; Implement error boundaries in your application to catch errors in the component tree and prevent the entire app from crashing. This is a key factor in User Experience.&lt;/p&gt;

&lt;p&gt;Wrap critical components with &lt;code&gt;&amp;lt;ErrorBoundary&amp;gt;&lt;/code&gt; to ensure graceful error handling.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&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;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Text&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ErrorBoundary&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;hasError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nf"&gt;getDerivedStateFromError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;hasError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;componentDidCatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Log error details to an error reporting service&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Something went wrong.&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ErrorBoundary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Leveraging Async Operations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Offloading Heavy Tasks:&lt;/strong&gt; Use asynchronous operations (e.g., &lt;code&gt;async/await&lt;/code&gt;, Promises) to ensure heavy computations or data fetching do not block the main UI thread.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;processData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Process data asynchronously&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;heavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&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;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Advanced Network Optimizations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Request Batching, Pagination &amp;amp; Caching:&lt;/strong&gt; In addition to app-level network strategies, incorporate code-level optimizations such as:

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Request Batching:&lt;/strong&gt; Combine multiple API requests into a single call when possible.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Pagination:&lt;/strong&gt; Load data in chunks rather than all at once, which reduces initial load time.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Caching Responses:&lt;/strong&gt; Cache network responses locally to minimize repeated calls and improve perceived performance. Can use some API Clients like &lt;a href="https://tanstack.com/query/latest/docs/framework/react/react-native" rel="noopener noreferrer"&gt;React Query&lt;/a&gt; which provideds in-built functionality of caching.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;



&lt;h2&gt;
  
  
  What React Native Provides for building a Performant App
&lt;/h2&gt;

&lt;p&gt;React Native comes with several built-in features and tools designed to help you build high-performance applications. Here are some key offerings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Hermes JavaScript Engine:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hermes is an open-source JavaScript engine optimized for React Native. It reduces memory usage, improves start-up time, and offers better overall performance by compiling JavaScript ahead-of-time.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Native Driver for Animations:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;React Native's Animated API allows you to leverage the native driver (&lt;code&gt;useNativeDriver: true&lt;/code&gt;), which offloads animation calculations from the JavaScript thread to the native side, ensuring smoother and more efficient animations.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Flipper Integration:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Flipper is a debugging and performance tool that comes integrated with React Native. It provides insights into network requests, layout performance, and more, allowing you to identify and resolve performance bottlenecks quickly.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Fast Refresh:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While primarily a developer experience feature, Fast Refresh helps you quickly iterate on your app without losing its state. This efficiency can indirectly contribute to better performance tuning by making it easier to test changes in real time.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Bridging Architecture:&lt;/strong&gt; (Available in React Native Versions below 0.76)&lt;/p&gt;

&lt;p&gt;React Native's bridge allows for efficient communication between JavaScript and native modules. This separation ensures that heavy computations or platform-specific operations can be executed natively, reducing the burden on the JavaScript thread.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Optimized UI Components:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Built-in components like &lt;code&gt;FlatList&lt;/code&gt;, &lt;code&gt;SectionList&lt;/code&gt;, and &lt;code&gt;VirtualizedList&lt;/code&gt; are optimized for rendering large datasets, ensuring that your app remains responsive even when handling complex lists.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Code Push and Over-the-Air Updates:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Tools like Microsoft's CodePush allow you to update your app's JavaScript bundle without requiring a full app store update. This enables you to roll out performance improvements quickly and efficiently.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By leveraging these built-in features, you can significantly enhance the performance of your React Native applications while reducing development time and complexity.&lt;/p&gt;



&lt;h2&gt;
  
  
  Common Mistakes That Affect Performance
&lt;/h2&gt;

&lt;p&gt;Even experienced developers can inadvertently introduce performance issues. Here are some common pitfalls to avoid:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Overusing Anonymous Functions:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Defining functions inline in render methods can lead to unnecessary re-renders. Instead, define them outside or memoize them using &lt;code&gt;useCallback&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Neglecting List Optimization:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Rendering large lists without proper optimization (e.g., using &lt;code&gt;FlatList&lt;/code&gt; with &lt;code&gt;initialNumToRender&lt;/code&gt;, &lt;code&gt;getItemLayout&lt;/code&gt;, and keys) can lead to performance bottlenecks.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Improper Use of State:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Updating state frequently or in large batches can cause excessive re-rendering. Batch updates or use debouncing/throttling where needed.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Ignoring Performance Profiling:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Not taking advantage of profiling tools means missing out on identifying the true bottlenecks in your application.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Excessive Re-renders Due to Inline Styles and Objects:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Recreating style objects and other non-primitive values on every render forces React to re-render components unnecessarily.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Interview Cheat Sheet: React Native App Performance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I've created a special interview-focused guide on optimizing React Native app performance. It serves as a quick reference, covering key optimizations, best practices, and common pitfalls—all in a concise format. Save it now for easy access!&lt;/p&gt;

&lt;p&gt;&lt;a href=""&gt;Read the article&lt;/a&gt;&lt;/p&gt;


&lt;/blockquote&gt;



&lt;h2&gt;
  
  
  Closing Comments
&lt;/h2&gt;

&lt;p&gt;Optimizing React Native performance is an ongoing process that combines architectural decisions with code-level improvements. By avoiding unnecessary re-renders, optimizing lists, and leveraging performance monitoring tools, you can ensure a smooth user experience. &lt;/p&gt;




&lt;p&gt;For any questions or suggestions, please feel free to comment below. 💬&lt;/p&gt;

&lt;p&gt;If you find this article useful, share it with your friends and follow for regular update of such good articles. 🔗&lt;/p&gt;

&lt;p&gt;Rushi Patel, Signing Off! 😊&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>performance</category>
      <category>mobile</category>
      <category>react</category>
    </item>
    <item>
      <title>Mapped and Conditional Types in TypeScript: A Deep Dive</title>
      <dc:creator>Rushi Patel</dc:creator>
      <pubDate>Mon, 03 Mar 2025 05:18:24 +0000</pubDate>
      <link>https://dev.to/rushi-patel/mapped-and-conditional-types-in-typescript-a-deep-dive-437k</link>
      <guid>https://dev.to/rushi-patel/mapped-and-conditional-types-in-typescript-a-deep-dive-437k</guid>
      <description>&lt;p&gt;TypeScript is a top choice for large-scale JavaScript apps, thanks to its powerful type system. Two key features— &lt;strong&gt;Mapped Types&lt;/strong&gt; and &lt;strong&gt;Conditional Types&lt;/strong&gt; —let you transform and optimize types efficiently.&lt;/p&gt;

&lt;p&gt;In this post, we’ll explore their syntax, applications, and how to build custom utility types.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pro Tip:&lt;/strong&gt;&lt;br&gt;
If you are new to Utility Types of TypeScript, it would be great to refer this article. It'll help you getting maximum from this article. &lt;a href="https://dev.to/rushi-patel/utility-types-in-typescript-a-detailed-explanation-2m9p"&gt;Read Article here&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In TypeScript, you often need to transform types—making properties optional, required, or filtering keys. While Mapped &amp;amp; Conditional Types aren’t used daily, understanding them can be valuable when needed.&lt;/p&gt;

&lt;p&gt;This post covers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Mapped Types&lt;/strong&gt; – Syntax, functionality, and common use cases.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Conditional Types&lt;/strong&gt; – Filtering, transformations, and applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Custom Utility Types&lt;/strong&gt; – Practical snippets combining both concepts.&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;Let's take &lt;code&gt;User&lt;/code&gt; type for reference as below:&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;isAdmin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Mapped Types
&lt;/h2&gt;

&lt;p&gt;Mapped types allow you to create new types by iterating over the properties of an existing type. They are essential when you want to transform each property of a type uniformly.&lt;/p&gt;

&lt;h3&gt;
  
  
  General Syntax
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;MappedType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;UnionType&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;ValueType&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;Here, Key iterates over a union (commonly derived from keyof T), and you assign a new value type to each key.&lt;/p&gt;


&lt;h3&gt;
  
  
  Applications and Use Cases
&lt;/h3&gt;

&lt;p&gt;Mapped types are incredibly versatile. Let’s explore some common applications with examples:&lt;/p&gt;
&lt;h4&gt;
  
  
  1.  Making All Properties Optional
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; When you need to create a version of an interface where not every property is mandatory—for instance, when accepting partial updates in an API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PartialMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;]?:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Valid (isAdmin is no longer a required property)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userPayload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PartialMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&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;h4&gt;
  
  
  2. Making All Properties Required
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; When you want to enforce that every property is provided, even if the original type allowed some properties to be optional (useful when you’re certain that all data will be available).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;RequiredMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Valid (all property are specified)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;RequiredMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;isAdmin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// In Valid&lt;/span&gt;
&lt;span class="c1"&gt;// Error: Missing properties age, isAdmin from RequiredMapped&amp;lt;User&amp;gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;RequiredMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. Creating Non-Nullable Versions of a Type
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; Sometimes you want to ensure that none of the properties can be null or undefined. This is very useful when sanitizing or processing data that must be complete.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;NonNullableMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nb"&gt;Exclude&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserNonNullable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;NonNullableMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Now all properties of UserNonNullable will have null and undefined removed from their types.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  4. Creating Read-Only Versions of a Type
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; Enforcing immutability in parts of your application, such as configuration objects or API responses, to prevent accidental 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="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ReadOnlyMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Key&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;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ReadOnlyMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;isAdmin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;  &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Error: Cannot assign to 'name' because it is a read-only property.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These examples illustrate how mapped types can be applied to modify or enhance the shape of your types to better fit the needs of your application. Mapped types is not limited to only above mentioned usecases, you can leverage it for any suitable scenario as applicable.&lt;/p&gt;



&lt;h2&gt;
  
  
  Conditional Types
&lt;/h2&gt;

&lt;p&gt;Conditional types introduce logic into your type definitions. They let you create types that can vary based on conditions, similar to the ternary operator in JavaScript but operating at the type level.&lt;/p&gt;
&lt;h3&gt;
  
  
  General Syntax
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;U&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;X&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If type &lt;code&gt;T&lt;/code&gt; is assignable to type &lt;code&gt;U&lt;/code&gt;, the type resolves to &lt;code&gt;X&lt;/code&gt;; otherwise, it resolves to &lt;code&gt;Y&lt;/code&gt;.&lt;/p&gt;


&lt;h3&gt;
  
  
  Applications and Use Cases
&lt;/h3&gt;

&lt;p&gt;Conditional types are ideal for filtering and transforming union types, and making types more dynamic. Here are a few detailed examples:&lt;/p&gt;
&lt;h4&gt;
  
  
  1. Excluding Specific Types from a Union
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; You might have a union type where certain members are not applicable in a given context. For example, excluding customer role from a set of user roles.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ExcludeMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;never&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Example&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CUSTOMER&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUPER_ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;AdminRole&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ExcludeMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserRoles&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CUSTOMER&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AdminRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CUSTOMER&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Not Valid ❌&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AdminRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Valid ✅&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Extracting Specific Types from a Union
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; You may need to isolate a subset of types from a union, such as extracting roles that have admin responsibilities.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ExtractMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;never&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Example&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CUSTOMER&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUPER_ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;AdminRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ExtractMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserRoles&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUPER_ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// Only include 'ADMIN' &amp;amp; 'SUPER_ADMIN' from UserRoles&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AdminRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CUSTOMER&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Not Valid ❌&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AdminRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Valid ✅&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. Dynamic Type Transformation Based on Conditions
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; Conditional types can help adjust the type of a property based on some condition. For example, transforming a type based on whether it extends a particular interface or type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ConditionalExample&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Example&lt;/span&gt;
&lt;span class="c1"&gt;// If T is string, then type becomes string[], otherwise it remains T.&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Test1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ConditionalExample&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Test1 is string[]&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Test2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ConditionalExample&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Test2 is number&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These examples show how conditional types empower you to build flexible, context-aware type definitions that can adapt to various scenarios in your codebase.&lt;/p&gt;



&lt;h2&gt;
  
  
  Custom Utility Types with Mapped &amp;amp; Conditional Types
&lt;/h2&gt;

&lt;p&gt;One of the most powerful aspects of TypeScript is the ability to create your own utility types. By combining mapped and conditional types, you can design custom types that enhance type safety and code reuse. Below are shared snippets of such utility types.&lt;/p&gt;
&lt;h3&gt;
  
  
  1. &lt;strong&gt;&lt;code&gt;Partial&amp;lt;Type&amp;gt;&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Creates a version of a type where all properties are optional.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PartialMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;]?:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. &lt;strong&gt;&lt;code&gt;Required&amp;lt;Type&amp;gt;&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Creates a version of a type where all properties are required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;RequiredMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&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. &lt;strong&gt;&lt;code&gt;NonNullable&amp;lt;Type&amp;gt;&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Transforms a type to exclude null and undefined from each property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;NonNullableMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nb"&gt;Exclude&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. &lt;strong&gt;&lt;code&gt;Pick&amp;lt;Type&amp;gt;&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Extracts a subset of properties from a type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PickMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Key&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;
  
  
  5. &lt;strong&gt;&lt;code&gt;Omit&amp;lt;Type&amp;gt;&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Creates a type by omitting specific properties from another type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;OmitMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Key&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;never&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Key&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Key&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;
  
  
  6. &lt;strong&gt;&lt;code&gt;ReadOnly&amp;lt;Type&amp;gt;&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Marks all properties of a type as read-only.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ReadOnlyMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Key&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;
  
  
  7. Conditional Utility Types: &lt;code&gt;Exclude&lt;/code&gt; &amp;amp; &lt;code&gt;Extract&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;These conditional types allow filtering of union types.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exclude
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ExcludeMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;never&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Extract
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ExtractMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;never&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CUSTOMER&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUPER_ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

  &lt;span class="c1"&gt;// Both below resolves to the union 'ADMIN' | 'SUPER_ADMIN'&lt;/span&gt;
  &lt;span class="c1"&gt;// one using Extract and other using Exclude&lt;/span&gt;
  &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;AdminRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ExtractMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserRoles&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUPER_ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;AdminRole&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ExcludeMapped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserRoles&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CUSTOMER&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  Closing Comments
&lt;/h2&gt;

&lt;p&gt;Mapped and conditional types are key to TypeScript’s advanced type system, enabling cleaner, more maintainable code through reusable utilities.&lt;/p&gt;

&lt;p&gt;Mastering these concepts will help you tackle (or at least understand) complex type scenarios in TypeScript.&lt;/p&gt;




&lt;p&gt;For any questions or suggestions, please feel free to comment below. 💬&lt;/p&gt;

&lt;p&gt;If you find this article useful, share it with your friends and follow for regular update of such good articles. 🔗&lt;/p&gt;

&lt;p&gt;Rushi Patel, Signing Off! 😊&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Server-Side Rendering (SSR) and Static Site Generation (SSG) in React: A Deep Dive</title>
      <dc:creator>Rushi Patel</dc:creator>
      <pubDate>Fri, 21 Feb 2025 05:16:47 +0000</pubDate>
      <link>https://dev.to/rushi-patel/server-side-rendering-ssr-and-static-site-generation-ssg-a-deep-dive-16kf</link>
      <guid>https://dev.to/rushi-patel/server-side-rendering-ssr-and-static-site-generation-ssg-a-deep-dive-16kf</guid>
      <description>&lt;h2&gt;
  
  
  SSR and SSG in React: A Deep Dive
&lt;/h2&gt;

&lt;p&gt;In today's fast-evolving web landscape, performance and SEO are paramount. As React developers, we often face challenges in delivering fast, efficient, and SEO-friendly applications. Two powerful techniques to address these challenges are &lt;strong&gt;Server-Side Rendering (SSR)&lt;/strong&gt; and &lt;strong&gt;Static Site Generation (SSG)&lt;/strong&gt;. In this blog post, we'll explore these rendering strategies in depth---discussing what they are, how they differ from &lt;strong&gt;routine client-side rendering&lt;/strong&gt;, their benefits, how to implement them in React, and more.&lt;/p&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Myth:&lt;/strong&gt; SSR and SSG Are Exclusive to React  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reality:&lt;/strong&gt; SSR and SSG are &lt;strong&gt;generic rendering techniques&lt;/strong&gt;, not limited to React. They are used in various frameworks like Angular Universal, Nuxt.js (Vue), PHP, Django etc to optimize performance and SEO.&lt;/p&gt;
&lt;/blockquote&gt;



&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Modern web applications need to be not only interactive and dynamic but also quick to load and friendly to search engines. Traditional &lt;strong&gt;client-side rendering (CSR)&lt;/strong&gt; --- where the browser downloads a minimal HTML shell (skeleton) and then renders content using JavaScript --- can sometimes lead to poor performance, especially on slower devices or networks. &lt;/p&gt;

&lt;p&gt;This is where SSR and SSG come into play. They offer alternative ways to pre-render content, either on each request (SSR) or at build time (SSG), ultimately leading to improved user experience and SEO. Let's check how they differ from each other.&lt;/p&gt;



&lt;h2&gt;
  
  
  How Do SSR and SSG Differ from Routine Client-Side Rendering (CSR)?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Client-Side Rendering (CSR):&lt;/strong&gt;&lt;br&gt;
CSR shifts the heavy lifting (load) to the client's browser. The server sends a skeleton HTML file (with some &lt;code&gt;&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt;), and JavaScript takes over to render the actual content. While this allows for rich interactivity, it may delay the initial content display, adversely affecting both performance and SEO.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Server-Side Rendering (SSR):&lt;/strong&gt;&lt;br&gt;
With SSR, the server pre-renders the complete HTML for a page on each request. This means users get fully formed pages right away, which improves the initial load time and SEO. However, this dynamic rendering can increase server load, as every request requires processing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Static Site Generation (SSG):&lt;/strong&gt;&lt;br&gt;
SSG builds the HTML pages at compile time. These static files can then be served instantly via a Content Delivery Network (CDN). The approach is incredibly fast and secure, but it's best suited for content that doesn't change frequently.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  Why Use SSR and SSG? Benefits &amp;amp; Use Cases
&lt;/h2&gt;

&lt;p&gt;Both SSR and SSG address limitations of CSR by improving:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance:&lt;/strong&gt;&lt;br&gt;
Pre-rendered pages lead to faster Time-to-First-Byte (TTFB) and better perceived performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SEO:&lt;/strong&gt;&lt;br&gt;
Search engines can crawl pre-rendered HTML more effectively, leading to better indexing and ranking. Now a days AI tools also crawl the web pages to provide accurate &amp;amp; latest response.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;User Experience:&lt;/strong&gt;&lt;br&gt;
With faster load times, users are less likely to abandon a page, improving engagement and conversion rates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flexibility:&lt;/strong&gt;&lt;br&gt;
SSR is ideal for dynamic, frequently changing data (e.g., dashboards, e-commerce pages), while SSG is perfect for static content (e.g., blogs, marketing sites, landing pages).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



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

&lt;p&gt;You might have heard of &lt;code&gt;hydration&lt;/code&gt; in Rendering Methods. Let's quickly see what is means.&lt;/p&gt;

&lt;p&gt;Hydration is the process where &lt;strong&gt;React takes over a pre-rendered HTML page, attaches event listeners, and makes it interactive&lt;/strong&gt;. Instead of building the DOM from scratch (as in client-side rendering), React &lt;strong&gt;"adopts"&lt;/strong&gt; the existing HTML and efficiently updates it.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Hydration Works
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; The server &lt;strong&gt;renders and sends&lt;/strong&gt; static HTML to the browser.&lt;/li&gt;
&lt;li&gt; The browser &lt;strong&gt;displays&lt;/strong&gt; the static content immediately, improving perceived performance.&lt;/li&gt;
&lt;li&gt; React's JavaScript bundle &lt;strong&gt;loads and runs&lt;/strong&gt; in the background.&lt;/li&gt;
&lt;li&gt; React &lt;strong&gt;matches the virtual DOM with the actual DOM&lt;/strong&gt; and attaches event handlers, eventually making it interactive.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As Dan Abramov (one of the core member of React team) puts it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Hydration is like watering the 'dry' HTML with the 'water' of interactivity and event handlers."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;



&lt;h2&gt;
  
  
  Deep Dive into Server-Side Rendering (SSR)
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Definition &amp;amp; Usage
&lt;/h3&gt;

&lt;p&gt;SSR involves rendering React components on the server on every request, producing a complete HTML page before sending it to the client. Once the browser receives the HTML, React "hydrates" the page---attaching event listeners and making it interactive.&lt;/p&gt;

&lt;h3&gt;
  
  
  How It Works
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Request:&lt;/strong&gt; A client requests a page.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Render on Server:&lt;/strong&gt; The server renders the React component tree into HTML.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Response:&lt;/strong&gt; The server sends this pre-rendered HTML to the browser.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Hydration:&lt;/strong&gt; The browser loads JavaScript and attaches event handlers, converting the static HTML into a fully interactive application.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Example Code (Next.js Implementation)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// pages/index.js (Next.js example)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getServerSideProps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Fetch data that changes on every request&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchDataFromAPI&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;SSR with Next.js&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Data fetched from the server: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Advantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Improved SEO:&lt;/strong&gt; Pre-rendered HTML is easier for search engines to index.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Faster Initial Load:&lt;/strong&gt; Users see content quicker, improving perceived performance.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Dynamic Content:&lt;/strong&gt; Ideal for pages that require real-time data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Limitations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Increased Server Load:&lt;/strong&gt; Every request triggers a new rendering cycle.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Complexity:&lt;/strong&gt; Managing state and caching strategies on the server can be challenging.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Scalability:&lt;/strong&gt; May require additional infrastructure to handle high traffic.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Efforts:&lt;/strong&gt; Require additional efforts in development with SSR Rendering.&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  Deep Dive into Static Site Generation (SSG)
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Definition &amp;amp; Usage
&lt;/h3&gt;

&lt;p&gt;SSG pre-renders pages at build time, creating static HTML files that are deployed to a CDN. These pages load almost instantaneously since they are served directly without additional processing on each request.&lt;/p&gt;

&lt;h3&gt;
  
  
  How It Works
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Build Time:&lt;/strong&gt; During the build process, React components are rendered into static HTML.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Deployment:&lt;/strong&gt; The static files are deployed to a CDN.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Request:&lt;/strong&gt; When a user requests a page, the CDN serves the pre-built HTML immediately.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Hydration:&lt;/strong&gt; Similar to SSR, React hydrates the static HTML on the client side.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Example Code (Next.js Implementation)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// pages/blog/[slug].js (Next.js example for SSG)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getStaticPaths&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;paths&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;fetchBlogPaths&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// e.g., [{ params: { slug: 'my-first-post' } }]&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;paths&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;fallback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getStaticProps&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;params&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;post&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;fetchBlogPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;BlogPost&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;article&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;article&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Advantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Speed:&lt;/strong&gt; Static files are served quickly via a CDN.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Security:&lt;/strong&gt; No server-side processing reduces the attack surface.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Scalability:&lt;/strong&gt; Easily handles large volumes of traffic without additional server resources.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Limitations&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Inflexibility for Dynamic Data:&lt;/strong&gt; Not suitable for content that changes frequently.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Rebuild Overhead:&lt;/strong&gt; Every update to content requires a rebuild and redeployment.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Limited Interactivity Pre-Hydration:&lt;/strong&gt; Content is static until JavaScript is loaded.&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  Implementing SSR and SSG in React
&lt;/h2&gt;

&lt;p&gt;The React ecosystem has embraced both SSR and SSG, with frameworks like &lt;strong&gt;Next.js&lt;/strong&gt; and &lt;strong&gt;Gatsby&lt;/strong&gt; offering seamless integration.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Next.js:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;SSR:&lt;/strong&gt; Use &lt;code&gt;getServerSideProps&lt;/code&gt; to fetch data on every request.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;SSG:&lt;/strong&gt; Use &lt;code&gt;getStaticProps&lt;/code&gt; (and &lt;code&gt;getStaticPaths&lt;/code&gt; for dynamic routes) to generate static pages at build time.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  &lt;strong&gt;Gatsby:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;  Primarily an SSG framework, Gatsby builds static pages using GraphQL queries during the build process.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Both frameworks manage the heavy lifting with help of hydration, routing, and data fetching and making it straightforward to choose the right strategy for your needs.&lt;/p&gt;



&lt;h2&gt;
  
  
  SSR vs. SSG: A Comparison Table
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Aspect&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Server-Side Rendering (SSR)&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Static Site Generation (SSG)&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Rendering Time&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;On every request&lt;/td&gt;
&lt;td&gt;At build time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Content Freshness&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Always up-to-date&lt;/td&gt;
&lt;td&gt;Requires rebuild for updates&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Good, but can be slower under heavy load&lt;/td&gt;
&lt;td&gt;Extremely fast with CDN distribution&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;SEO&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Excellent&lt;/td&gt;
&lt;td&gt;Excellent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Server Load&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Higher (dynamic rendering per request)&lt;/td&gt;
&lt;td&gt;Minimal (static files served)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Use Cases&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Dynamic pages (e.g., dashboards, e-commerce)&lt;/td&gt;
&lt;td&gt;Static pages (e.g., blogs, landing pages)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Complexity&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;More complex setup and state management&lt;/td&gt;
&lt;td&gt;Simpler, but less flexible for dynamic data&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Closing Comments
&lt;/h2&gt;

&lt;p&gt;Understanding SSR and SSG is essential for building high-performance, SEO-friendly React applications. While SSR offers the flexibility needed for dynamic content, SSG provides unbeatable speed and scalability for static content. The choice between them ultimately depends on your project requirements. &lt;/p&gt;




&lt;p&gt;For any questions or suggestions, please feel free to comment below. 💬&lt;/p&gt;

&lt;p&gt;If you find this article useful, share it with your friends and follow for regular update of such good articles. 🔗&lt;/p&gt;

&lt;p&gt;Rushi Patel, Signing Off! 😊&lt;/p&gt;

</description>
      <category>ssr</category>
      <category>ssg</category>
      <category>nextjs</category>
      <category>react</category>
    </item>
    <item>
      <title>Utility Types in TypeScript: Partial, Required, Pick, Omit and 10 more...</title>
      <dc:creator>Rushi Patel</dc:creator>
      <pubDate>Tue, 18 Feb 2025 16:45:56 +0000</pubDate>
      <link>https://dev.to/rushi-patel/utility-types-in-typescript-a-detailed-explanation-2m9p</link>
      <guid>https://dev.to/rushi-patel/utility-types-in-typescript-a-detailed-explanation-2m9p</guid>
      <description>&lt;h3&gt;
  
  
  Utility Types in TypeScript
&lt;/h3&gt;

&lt;p&gt;TypeScript provides utility types to help transform or create new types from existing ones. &lt;/p&gt;

&lt;p&gt;These types are built into TypeScript and are extremely useful for making your code cleaner, more expressive, and easier to maintain. You might be aware about some and regularly using it while some from the list might be new to learn.&lt;/p&gt;

&lt;p&gt;Below is a breakdown of key utility types with &lt;strong&gt;Definitions and examples&lt;/strong&gt;. Added a summary table in the end, don't forget to read it.&lt;/p&gt;

&lt;p&gt;Let's take &lt;code&gt;User&lt;/code&gt; type for reference as below:&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;isAdmin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;br&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  1. &lt;code&gt;Partial&amp;lt;Type&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt; Makes all properties of Type optional.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; When you want to create a version of a type where some or all properties might not be defined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Partial&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// Valid ✅&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  2. &lt;code&gt;Required&amp;lt;Type&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt; Makes all properties of Type required.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; To enforce that every property must be provided.&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;// Not Valid ❌&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Required&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; 

&lt;span class="c1"&gt;// Valid ✅ (all properties provided) &lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Required&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;isAdmin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  3. &lt;code&gt;Readonly&amp;lt;Type&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt;: Makes all properties of Type readonly (cannot be reassigned).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; To create immutable objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Readonly&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;isAdmin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;  &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Error: Cannot assign to 'name' because it is a read-only property.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  4. &lt;code&gt;Pick&amp;lt;Type, Keys&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt;: Constructs a type by picking a subset of properties from a given Type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; To extract specific properties from a type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userPreview&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Pick&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  5. &lt;code&gt;Omit&amp;lt;Type, Keys&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt;: Constructs a type by omitting specific properties from a given Type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; To exclude certain properties from a type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userDetails&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Omit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;age&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;isAdmin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&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;You can see that we have achieved same underlying type using Pick and Omit. (Picking &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;name&lt;/code&gt; is same as Omitting &lt;code&gt;age&lt;/code&gt; and &lt;code&gt;isAdmin&lt;/code&gt; from type &lt;code&gt;User&lt;/code&gt;)&lt;/p&gt;



&lt;h2&gt;
  
  
  6. &lt;code&gt;Record&amp;lt;Keys, Type&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt;: Constructs a type with keys of Keys and values of Type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; To create a dictionary-like object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CUSTOMER&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUPER_ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// Valid ✅&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;roleMatrix&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserRoles&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CUSTOMER&lt;/span&gt;&lt;span class="dl"&gt;'&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUPER_ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Not Valid ❌&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;roleMatrix&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserRoles&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CUSTOMER&lt;/span&gt;&lt;span class="dl"&gt;'&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUPER_ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MANAGER&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="c1"&gt;// Error: As MANAGER is not present in UserRoles&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  7. &lt;code&gt;Exclude&amp;lt;Type, ExcludedUnion&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt;: Excludes from Type those types that are assignable to ExcludedUnion.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; To filter out specific types from a union.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CUSTOMER&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUPER_ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;AdminRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Exclude&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserRoles&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CUSTOMER&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// remove the CUSTOMER from UserRoles&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AdminRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CUSTOMER&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Not Valid ❌&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AdminRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Valid ✅&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  8. &lt;code&gt;Extract&amp;lt;Type, Union&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt;: Extracts from Type those types that are assignable to Union.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; To filter specific types from a union.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CUSTOMER&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUPER_ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;AdminRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Extract&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserRoles&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUPER_ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// Only include 'ADMIN' &amp;amp; 'SUPER_ADMIN' from UserRoles&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AdminRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CUSTOMER&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Not Valid ❌&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AdminRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADMIN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Valid ✅&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  9. &lt;code&gt;NonNullable&amp;lt;Type&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt;: Excludes null and undefined from Type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; To ensure a type is not nullable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Valid ✅&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;id1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;12&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;id2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;id3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;id4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;NonNullId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NonNullable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserId&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;// Not Valid ❌&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;id5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;NonNullId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Error: Type 'null' is not assignable to type 'NonNullId'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Pro Tip:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;You can create your own NonNullable type using Exclude. Here is how it can be done.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;NonNullId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Exclude&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;// Not Valid ❌&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;NonNullId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Error: Type 'null' is not assignable to type 'NonNullId'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  10. &lt;code&gt;ReturnType&amp;lt;Type&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt;: Constructs a type consisting of the return type of a function type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; To infer and reuse function return types.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;ReturnType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;getUser&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// { id: number; name: string }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  11. &lt;code&gt;Parameters&amp;lt;Type&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt;: Constructs a tuple (array) type of the parameters of a function type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; To infer and reuse function parameter types. Gives the type of function parammets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;updateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UpdateParams&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Parameters&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;updateUser&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// [number, string]&lt;/span&gt;

&lt;span class="c1"&gt;// Valid ✅&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;updateBook&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UpdateParams&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// params = (id: number, name: string)&lt;/span&gt;

&lt;span class="nf"&gt;updateBook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Atomic Habits&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;h2&gt;
  
  
  Summary Table of Key Utility Types
&lt;/h2&gt;

&lt;p&gt;You might be feeling there are so many utility types provided by TypeScript. Isn't it?&lt;br&gt;
But no worries, this table will summarize all of them so that you can refer it anytime.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Utility Type&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Partial&amp;lt;Type&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Makes all properties optional&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Required&amp;lt;Type&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Makes all properties required&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Readonly&amp;lt;Type&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Makes all properties readonly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Pick&amp;lt;Type, K&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Picks specific properties&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Omit&amp;lt;Type, K&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Omits specific properties&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Record&amp;lt;K, T&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Creates a dictionary-like type&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Exclude&amp;lt;T, U&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Excludes types from a union&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Extract&amp;lt;T, U&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Extracts types from a union&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;NonNullable&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Removes &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ReturnType&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Gets the return type of a function&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Parameters&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Gets the parameters of a function&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Closing Comments 👋
&lt;/h2&gt;

&lt;p&gt;In this article, we learned various Utility Types provided by TypeScript and understood well with help of code snippets.&lt;/p&gt;




&lt;p&gt;For any questions or suggestions, please feel free to comment below. 💬&lt;/p&gt;

&lt;p&gt;If you find this article useful, share it with your friends and follow for regular update of such good articles. 🔗&lt;/p&gt;

&lt;p&gt;Rushi Patel, Signing Off! 😊&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>softwaredevelopment</category>
      <category>react</category>
    </item>
    <item>
      <title>The New Architecture of React Native: All you need to know</title>
      <dc:creator>Rushi Patel</dc:creator>
      <pubDate>Tue, 18 Feb 2025 16:22:12 +0000</pubDate>
      <link>https://dev.to/rushi-patel/decoding-the-new-architecture-of-react-native-4hd5</link>
      <guid>https://dev.to/rushi-patel/decoding-the-new-architecture-of-react-native-4hd5</guid>
      <description>&lt;p&gt;React Native lets developers build native mobile apps with JavaScript, and it's been a game-changer.  But like all technology, it needs updates to get better.  The "New Architecture" is a big change under the hood, making React Native work differently. &lt;/p&gt;

&lt;p&gt;This post explains what it is, clears up misunderstandings, and looks at why it's a big deal for React Native development. &lt;strong&gt;We'll cover what's new, how it changed over time, the tools involved, and how it compares to the old way of doing things.&lt;/strong&gt;&lt;/p&gt;



&lt;h2&gt;
  
  
  1. &lt;strong&gt;What is the "New Architecture"? (And Busting Some Myths)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The "New Architecture" isn't a completely new framework. It's a fundamental rewrite of the core of React Native, focusing on improving performance, flexibility, and developer experience. It introduces new core components and paradigms that optimize the interaction between JavaScript and native code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Myths:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Myth 1: The New Architecture replaces JavaScript:&lt;/strong&gt; False. JavaScript remains the primary language for building React Native UIs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Myth 2: The New Architecture is only for new projects:&lt;/strong&gt; False. Existing projects can migrate to the New Architecture incrementally.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Myth 3: The New Architecture is unstable:&lt;/strong&gt; False. While it went through phases of experimentation, it's now stable and recommended for production apps.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  2. Why Was the New Architecture Needed?
&lt;/h2&gt;

&lt;p&gt;Over the years, developers encountered several limitations with the legacy architecture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Bridge:&lt;/strong&gt; Communication between JavaScript and native code relied on a "bridge," which could become a performance bottleneck, especially with complex interactions or large data transfers. This asynchronous bridge could lead to UI jank and slower performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;UI Blocking:&lt;/strong&gt; JavaScript execution could block the UI thread, leading to a poor user experience.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Single-Thread Constraints:&lt;/strong&gt; The old design ran many tasks on a single thread, which limited the ability to perform synchronous operations and hindered support for modern React features like Suspense and Transitions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The legacy bridge, while functional, became a bottleneck for complex apps, hindering performance and limiting React Native's potential.&lt;/p&gt;
&lt;/blockquote&gt;



&lt;h2&gt;
  
  
  3. The Evolution and Timeline
&lt;/h2&gt;

&lt;p&gt;The journey toward the New Architecture began back in &lt;strong&gt;2018&lt;/strong&gt; when the React Native team started rethinking the core internals. Here's a brief timeline of its evolution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;2018:&lt;/strong&gt; Conceptualization of a new core to overcome the limitations of the bridge-based approach.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;React Native 0.68:&lt;/strong&gt; Experimental opt-in release where early adopters could try the New Architecture.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Subsequent Releases (0.69 - 0.75):&lt;/strong&gt; Progressive improvements were made, with new tools (such as Codegen, Turbo Modules, and Fabric) integrated and bugs ironed out.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;React Native 0.76:&lt;/strong&gt; The New Architecture was enabled by default in production-level apps, signaling its maturity and readiness for widespread adoption.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each milestone brought incremental enhancements, steadily transforming React Native into a more robust, performance-oriented framework.&lt;/p&gt;



&lt;h2&gt;
  
  
  4. The Architectural Shift: New Additions &amp;amp; Best Practices
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Key Changes &amp;amp; Additions
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;JSI &amp;amp; Removal of the Bridge:&lt;/strong&gt; The asynchronous bridge is replaced by the JavaScript Interface (JSI), enabling direct, synchronous communication between JavaScript and native code, significantly improving performance. This paves the way for a future "bridgeless" mode.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Turbo Modules:&lt;/strong&gt; Native modules are now loaded lazily using the Turbo Module system. This reduces startup overhead and improves memory management, while also supporting type-safe interfaces.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fabric Renderer:&lt;/strong&gt; Fabric modernizes the rendering pipeline by unifying rendering logic in C++ and enabling concurrent rendering. This supports React 18 features like Suspense and Transitions and provides more direct access to native UI components for better performance and consistency.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Codegen:&lt;/strong&gt; Automated code generation from typed JavaScript (or TypeScript/Flow) definitions ensures consistent interfaces between JavaScript and native code, minimizing errors and improving developer experience.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Concurrent React Integration:&lt;/strong&gt; React 18's concurrent features integrate seamlessly with Fabric, enabling smoother UI updates and improved responsiveness.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;JSI, Turbo Modules, and Fabric are the core building blocks of the New Architecture, each playing a crucial role in enhancing performance and developer experience.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Ideal Practices
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Incremental Migration:&lt;/strong&gt; Don't try to rewrite your entire app at once. Migrate to the New Architecture incrementally, starting with less critical components.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use the New Architecture from the start for new projects:&lt;/strong&gt; This will save you migration effort later.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Understand the core concepts:&lt;/strong&gt; Familiarize yourself with Fabric, Turbo Modules, and JSI to optimize your code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stay Updated:&lt;/strong&gt; Since the architecture is continuously evolving, keep your project dependencies and libraries updated.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  5. The New "Framework" Approach in React Native
&lt;/h2&gt;

&lt;p&gt;The New Architecture is not just an incremental update---it's a paradigm shift that nudges React Native closer to a cohesive &lt;strong&gt;framework&lt;/strong&gt; rather than a collection of loosely coupled modules. This integrated approach means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Unified Core:&lt;/strong&gt; With a shared C++ core, both iOS and Android (and even other platforms) now rely on the same underlying systems.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Enhanced Cross-Platform Consistency:&lt;/strong&gt; Tools like Fabric and Turbo Modules ensure that your app behaves similarly across platforms, reducing platform-specific bugs.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Future-Proofing:&lt;/strong&gt; With built-in support for concurrent rendering and upcoming React features, the framework is poised to adopt innovations more swiftly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This "framework" mindset encourages developers to think in terms of a complete, cohesive solution rather than stitching together disparate parts.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The New Architecture moves React Native towards a more unified and cohesive framework, improving cross-platform consistency and future-proofing the platform.&lt;/p&gt;
&lt;/blockquote&gt;



&lt;h2&gt;
  
  
  6. Comparison: Old Architecture vs. New Architecture
&lt;/h2&gt;

&lt;p&gt;Below is a side-by-side comparison highlighting the key differences:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Aspect&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Old Architecture&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;New Architecture&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Communication&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Asynchronous bridge with JSON serialization&lt;/td&gt;
&lt;td&gt;Direct, synchronous communication via JSI&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Native Module Loading&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Eager initialization; all modules loaded at startup&lt;/td&gt;
&lt;td&gt;Lazy loading with Turbo Modules&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Rendering&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Legacy renderer with potential layout delays&lt;/td&gt;
&lt;td&gt;Fabric renderer supporting concurrent rendering&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Type Safety &amp;amp; Code Generation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Manual bridging; prone to human errors&lt;/td&gt;
&lt;td&gt;Automated Codegen ensuring type-safe interfaces&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Overhead due to serialization; single-thread limitations&lt;/td&gt;
&lt;td&gt;Reduced overhead; optimized for concurrent features&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Future Readiness&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Limited support for modern React features (e.g., Suspense)&lt;/td&gt;
&lt;td&gt;Built to leverage React 18 features like Suspense &amp;amp; Transitions&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This table makes it easier to appreciate how the New Architecture lays the groundwork for faster, smoother, and more scalable mobile apps.&lt;/p&gt;



&lt;h2&gt;
  
  
  Verdict: A Significant Leap Forward
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;The New Architecture represents a significant advancement in React Native.&lt;/p&gt;

&lt;p&gt;It addresses the limitations of the older architecture, leading to substantial improvements in performance, flexibility, and developer experience. &lt;/p&gt;

&lt;p&gt;While it requires some learning and adaptation, the benefits make it a worthwhile investment for any React Native developer.&lt;/p&gt;
&lt;/blockquote&gt;



&lt;h2&gt;
  
  
  Closing Comments
&lt;/h2&gt;

&lt;p&gt;React Native's New Architecture significantly improves performance and developer experience. Understanding its core concepts and best practices is key to building high-performance mobile apps.  Staying up-to-date with the latest developments is crucial for maximizing React Native's potential.&lt;/p&gt;




&lt;p&gt;For any questions or suggestions, please feel free to comment below. 💬&lt;/p&gt;

&lt;p&gt;If you find this article useful, share it with your friends and follow for regular update of such good articles. 🔗&lt;/p&gt;

&lt;p&gt;Rushi Patel, Signing Off! 😊&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>architecture</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Deploy Next JS App to Google Cloud Run with Github Actions CI/CD - A Complete Guide</title>
      <dc:creator>Rushi Patel</dc:creator>
      <pubDate>Sun, 10 Mar 2024 19:15:01 +0000</pubDate>
      <link>https://dev.to/rushi-patel/deploy-next-js-app-to-google-cloud-run-with-github-actions-cicd-a-complete-guide-l29</link>
      <guid>https://dev.to/rushi-patel/deploy-next-js-app-to-google-cloud-run-with-github-actions-cicd-a-complete-guide-l29</guid>
      <description>&lt;p&gt;This guide provides you a step-by-step process to &lt;strong&gt;deploy your Next app efficiently to Google Cloud Run&lt;/strong&gt;. Each section offers actionable steps, ensuring a smooth deployment journey.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  In this guide, we'll cover the following sections:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Pre-requisite&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Next Project Config&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;GCP Account &amp;amp; GCP Project Setup&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dockerize your App&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Github Actions CI/CD&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Testing&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Whether you're a seasoned developer seeking a detailed deployment workflow or an enthusiast eager to take your Next project live, this guide will equip you with the necessary knowledge and steps for a successful deployment journey. Let's start with pre-requisites.&lt;/p&gt;



&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Section 1: Pre-requisites
&lt;/h1&gt;

&lt;p&gt;Before diving into deploying a Next app to Google Cloud Run, it's essential to ensure that you have the necessary tools and knowledge in place. Mininum requirements are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Next &amp;amp; npm Installed:&lt;/strong&gt; Make sure Next.js and npm are installed on your machine. Next 18 is used throughout this guide.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Next JS Setup:&lt;/strong&gt; Familiarize yourself with creating a Next project and running it locally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Google Cloud Account:&lt;/strong&gt; If you don't have one yet, don't worry; we'll create it in a later step.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Section 2: Next JS Project Config
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Prepare and Run Next JS Project
&lt;/h3&gt;

&lt;p&gt;If you haven't already set up a Next JS project, create your own project or clone the demo project from &lt;a href="https://github.com/rushi-2001/next-js-cloud-run-setup" rel="noopener noreferrer"&gt;Github here&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open your terminal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the following command to clone Next JS project:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  git clone https://github.com/rushi-2001/next-js-cloud-run-setup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install the dependencies:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  yarn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Run the app:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  yarn dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will start a development server, allowing you to view your Next app in a browser at &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prepare a build of your Next app:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  yarn build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Next Scripts Configuration:
Ensure your &lt;code&gt;package.json&lt;/code&gt; includes the following scripts:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"next dev"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"next build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"next start"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure your scripts align with the provided commands for starting &amp;amp; building your Next app.&lt;/p&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Section 3: GCP Account &amp;amp; GCP Project Setup
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Step 1: Create a New Project
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Cloud Console&lt;/a&gt; and sign in.&lt;/li&gt;
&lt;li&gt;Click on "Select a project" at the top and then click on "New Project."&lt;/li&gt;
&lt;li&gt;Choose a unique project name and select the desired organization if applicable.&lt;/li&gt;
&lt;li&gt;Once the project is created, note down the Project ID for future reference.&lt;/li&gt;
&lt;li&gt;Link this project to appropriate Billing Account.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 2: Enable Required APIs
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the &lt;strong&gt;APIs &amp;amp; Services&lt;/strong&gt; section in the Google Cloud Console.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Library&lt;/strong&gt; in the left sidebar.&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;Cloud Storage&lt;/strong&gt; and enable the &lt;strong&gt;Cloud Storage API.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;Cloud Run API&lt;/strong&gt; and enable the &lt;strong&gt;Cloud Run API.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 3: Set Up Cloud Storage
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;strong&gt;Cloud Storage&lt;/strong&gt; section in the Google Cloud Console.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Create Bucket&lt;/strong&gt; to make a new bucket.&lt;/li&gt;
&lt;li&gt;Choose a unique name and select the &lt;code&gt;us-central1&lt;/code&gt; region.&lt;/li&gt;
&lt;li&gt;Leave other settings as default and create the bucket.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 4: Install GCP CLI
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the root directory of your Next app in the terminal.&lt;/li&gt;
&lt;li&gt;Install the Google Cloud SDK following the instructions at &lt;a href="https://cloud.google.com/sdk/docs/install" rel="noopener noreferrer"&gt;Google Cloud SDK Installation Guide&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Authenticate the Google Cloud SDK by running &lt;code&gt;gcloud auth login&lt;/code&gt; and following the on-screen instructions.&lt;/li&gt;
&lt;li&gt;Set the project ID by running &lt;code&gt;gcloud config set project PROJECT_ID&lt;/code&gt;, replacing PROJECT_ID with your actual Project ID.&lt;/li&gt;
&lt;/ol&gt;






&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Service Account Setup
&lt;/h1&gt;

&lt;h4&gt;
  
  
  Generate &amp;amp; Download a Service Account
&lt;/h4&gt;

&lt;p&gt;To set up a Service Account for Google Cloud Run deployment, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Cloud Console&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Navigate to &lt;strong&gt;IAM &amp;amp; Admin&lt;/strong&gt; → &lt;strong&gt;Service Accounts&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Create Service Account&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Provide an appropriate name and description for the service account. For instance, use &lt;code&gt;github-ci-cd&lt;/code&gt; as it will be utilized for Github CI/CD.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Assign the following roles:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Cloud Run Admin&lt;/li&gt;
&lt;li&gt;Cloud Build Service Account&lt;/li&gt;
&lt;li&gt;Cloud Run Service Agent&lt;/li&gt;
&lt;li&gt;Service Account User&lt;/li&gt;
&lt;li&gt;Storage Object Admin&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Click the three dots and select &lt;strong&gt;Manage keys&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Click on &lt;strong&gt;Add Key → Create New Key&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Choose the &lt;strong&gt;JSON key&lt;/strong&gt; type and securely download it. Remember, this key grants access to Google Cloud resources, so keep it safe.&lt;/p&gt;&lt;/li&gt;

&lt;/ol&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 4: Dockerize your Next App
&lt;/h1&gt;

&lt;h4&gt;
  
  
  What is Docker
&lt;/h4&gt;

&lt;p&gt;Docker is a platform that enables developers to automate the deployment of applications inside lightweight, portable containers. Containers encapsulate the application and its dependencies, ensuring consistency and portability across various environments.&lt;/p&gt;

&lt;h4&gt;
  
  
  Placement:
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;Dockerfile&lt;/code&gt;, which contains instructions to build a Docker image for your Next app, should be placed in the root directory of your project, alongside your source code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example Configuration:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# Use Node.js as the base image&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; node:18.17.0-alpine&lt;/span&gt;

&lt;span class="c"&gt;# Set the working directory in the container&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;

&lt;span class="c"&gt;# Copy package.json and yarn.lock to the container&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package.json yarn.lock ./&lt;/span&gt;

&lt;span class="c"&gt;# Install dependencies&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--frozen-lockfile&lt;/span&gt;

&lt;span class="c"&gt;# Copy the app's source code to the container&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;

&lt;span class="c"&gt;# Build the Next app&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn build

&lt;span class="c"&gt;# Serve the production build&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["yarn", "start"]&lt;/span&gt;


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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Explanation of Configuration:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Base Image:&lt;/strong&gt; Uses Next.js version 18.17.0 Alpine as the lightweight base image.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dependency Installation:&lt;/strong&gt; Copies &lt;code&gt;package.json&lt;/code&gt; and &lt;code&gt;yarn.lock&lt;/code&gt;, installs dependencies using Yarn, leveraging Docker layer caching for efficiency.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Source Code and Build:&lt;/strong&gt; Copies the app's source code, builds the Next app, and sets up the working directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Serve the Build:&lt;/strong&gt; Uses &lt;code&gt;yarn start&lt;/code&gt; to serve the built Next app from the &lt;code&gt;build&lt;/code&gt; directory when the container starts.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 5: CI/CD using GitHub Actions
&lt;/h1&gt;

&lt;h3&gt;
  
  
  CI/CD and GitHub Actions:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CI/CD:&lt;/strong&gt; CI (Continuous Integration) and CD (Continuous Deployment) automate the software delivery process. CI involves merging code changes into a shared repository frequently, automatically running tests, and validating changes. CD automates the deployment of validated code changes to production or staging environments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CI/CD in GitHub Actions:&lt;/strong&gt; GitHub Actions provide workflows to automate tasks in your repository, including CI/CD processes. These workflows are defined in YAML files and can be triggered by various events like code pushes, pull requests, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Storing Service Account Key and Project ID in GitHub Secrets:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Store your Service Account Key and Project ID as secrets in your GitHub repository to securely access them during the CI/CD process.&lt;/li&gt;
&lt;li&gt;You can create Github secrets by &lt;strong&gt;Github Repository → Settings → Secrets &amp;amp; Variables → Actions → Secrets tab → New Repository Secret&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Secrets:

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;GCP_SA_KEY&lt;/code&gt;: Your entire JSON of Service Account Key generated in previous step.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GCP_PROJECT_ID&lt;/code&gt; Your GCP Project ID.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DOCKER_IMAGE_NAME&lt;/code&gt; Your preferred name of Docker Image.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Workflow File Name &amp;amp; Placement:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The workflow file can be named &lt;code&gt;cloud-run-deploy.yml&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Place this file in the &lt;code&gt;.github/workflows&lt;/code&gt; directory in your project. Refer the below given repository incase of any confusion.&lt;/li&gt;
&lt;li&gt;Paste the below configuration in recently created yml file.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Configuration
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to Cloud Run&lt;/span&gt;

&lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;SERVICE_NAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;next-app-project&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;main&lt;/span&gt;
  &lt;span class="na"&gt;pull_request&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;main&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;dockerize-and-deploy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;

    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Checkout repository&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v2&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Google Cloud Auth&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;google-github-actions/auth@v2'&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;credentials_json&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;${{&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;secrets.GCP_SA_KEY&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;}}'&lt;/span&gt;
          &lt;span class="na"&gt;project_id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.GCP_PROJECT_ID }}&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Set up Cloud SDK&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;google-github-actions/setup-gcloud@v2'&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Configure Docker&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;gcloud auth configure-docker&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Build and Push Docker Image&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;docker build -t gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest .&lt;/span&gt;
          &lt;span class="s"&gt;docker push gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to Cloud Run&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;gcloud run deploy $SERVICE_NAME \&lt;/span&gt;
            &lt;span class="s"&gt;--image gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest \&lt;/span&gt;
            &lt;span class="s"&gt;--platform managed \&lt;/span&gt;
            &lt;span class="s"&gt;--region us-central1 \&lt;/span&gt;
            &lt;span class="s"&gt;--allow-unauthenticated&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;This workflow triggers on pushes or pull requests to the &lt;code&gt;main&lt;/code&gt; branch. It uses &lt;strong&gt;GitHub Actions&lt;/strong&gt; to perform various steps such as building the Next app, authenticating with Google Cloud, setting up the Cloud SDK, dockerize your app and deploying the app to Google Cloud Run.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When you are deploying for &lt;strong&gt;first time&lt;/strong&gt; to bucket, remove the service name from &lt;code&gt;gcloud run deploy&lt;/code&gt; command on line 42. As first deployment must be of default service. From second deploy you provide preferred service name.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 6: Testing the CI/CD
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Push Your Changes to GitHub (on Main Branch):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ensure your latest changes are pushed to the main branch of your GitHub repository.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Navigate to the 'Actions' Tab in GitHub Repository:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Visit your GitHub repository and go to the 'Actions' tab.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check Workflow Status:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Verify the status of your Next app deployment workflow using GitHub Actions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open the Deployed App URL:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Access the provided URL after a successful deployment in your browser to confirm your Next application runs smoothly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In GitHub Actions logs, the URL might contain masked text (*** text) representing your GCP project ID. Replace *** with your project ID and open the URL in your browser.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Access Google Cloud Engine Services:&lt;/strong&gt;&lt;br&gt;
Go to Google Cloud, locate and click on Cloud Run, then navigate to &lt;strong&gt;Services&lt;/strong&gt; tab. Find your recent deployments and click on the service to open your Next App in a new tab.&lt;/p&gt;



&lt;h1&gt;
  
  
  GitHub Repository Link
&lt;/h1&gt;

&lt;p&gt;Access the complete code reference for this guide by visiting the &lt;a href="https://github.com/rushi-2001/next-js-cloud-run-setup" rel="noopener noreferrer"&gt;GitHub Repository&lt;/a&gt;.&lt;br&gt;
Feel free to clone the repository and experiment with it on your own!&lt;/p&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;🚀 Congratulations on mastering the deployment of your Next app to Google Cloud Run and GitHub Actions! 🎉&lt;/p&gt;

&lt;p&gt;Embrace this automated workflow to streamline your development journey and propel your projects to new heights. You can refer the above given &lt;strong&gt;repository&lt;/strong&gt; link as and when required. If you have any queries or need guidance, feel free to chat or &lt;strong&gt;drop a comment.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Keep coding, exploring, and sharing the joy of efficient deployments! Don't forget to &lt;strong&gt;like and share this guide&lt;/strong&gt; to inspire others on their deployment adventures! 👍&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding! 💻✨&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>googlecloud</category>
      <category>nextjs</category>
      <category>cicd</category>
      <category>githubactions</category>
    </item>
    <item>
      <title>Deploy Node JS Project to Google Cloud Run with Github Actions CI/CD - A Complete Guide</title>
      <dc:creator>Rushi Patel</dc:creator>
      <pubDate>Sun, 10 Mar 2024 19:14:51 +0000</pubDate>
      <link>https://dev.to/rushi-patel/deploy-node-js-project-to-google-cloud-run-with-github-actions-cicd-a-complete-guide-2kk5</link>
      <guid>https://dev.to/rushi-patel/deploy-node-js-project-to-google-cloud-run-with-github-actions-cicd-a-complete-guide-2kk5</guid>
      <description>&lt;p&gt;This guide provides you a step-by-step process to &lt;strong&gt;deploy your Node app efficiently to Google Cloud Run&lt;/strong&gt;. Each section offers actionable steps, ensuring a smooth deployment journey.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  In this guide, we'll cover the following sections:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Pre-requisite&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Node Project Config&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;GCP Account &amp;amp; GCP Project Setup&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dockerize your App&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Github Actions CI/CD&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Testing&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Whether you're a seasoned developer seeking a detailed deployment workflow or an enthusiast eager to take your Node project live, this guide will equip you with the necessary knowledge and steps for a successful deployment journey. Let's start with pre-requisites.&lt;/p&gt;



&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Section 1: Pre-requisites
&lt;/h1&gt;

&lt;p&gt;Before diving into deploying a Node app to Google Cloud Run, it's essential to ensure that you have the necessary tools and knowledge in place. Mininum requirements are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Node &amp;amp; npm Installed:&lt;/strong&gt; Make sure Node.js and npm are installed on your machine. Node 18 is used throughout this guide.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Node JS Setup:&lt;/strong&gt; Familiarize yourself with creating a Node project and running it locally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Google Cloud Account:&lt;/strong&gt; If you don't have one yet, don't worry; we'll create it in a later step.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Section 2: Node JS Project Config
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Prepare and Run Node JS Project
&lt;/h3&gt;

&lt;p&gt;If you haven't already set up a Node JS project, create your own project or clone the demo project from &lt;a href="https://github.com/rushi-2001/node-express-cloud-run-setup" rel="noopener noreferrer"&gt;Github here&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open your terminal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the following command to clone Node JS project:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  git clone https://github.com/rushi-2001/node-express-cloud-run-setup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install the dependencies:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  yarn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Run the app:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  yarn start-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will start a development server, allowing you to view your Node server in a browser at &lt;a href="http://localhost:8080" rel="noopener noreferrer"&gt;http://localhost:8080&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prepare a build of your Node app:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  yarn build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command creates a build folder at the root of your project, containing the compiled version of your Node app.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exclude build folder from Git: 
Add &lt;code&gt;build/&lt;/code&gt; to your &lt;code&gt;.gitignore&lt;/code&gt; file to prevent it from being tracked and pushed to your Git repository.&lt;/li&gt;
&lt;li&gt;Node Scripts Configuration:
Ensure your &lt;code&gt;package.json&lt;/code&gt; includes the following scripts:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
   &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node build/index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
   &lt;/span&gt;&lt;span class="nl"&gt;"start-dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"nodemon src/index.ts"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
   &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"tsc"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure your scripts align with the provided commands for starting &amp;amp; building app.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; When deploying your application to Google App Engine, ensure that the &lt;code&gt;start&lt;/code&gt; command in your &lt;code&gt;package.json&lt;/code&gt; points to the correct file for launching your application. &lt;br&gt; For &lt;strong&gt;JavaScript&lt;/strong&gt; project you can use &lt;code&gt;"start": "node index.js"&lt;/code&gt; and for &lt;strong&gt;TypeScript&lt;/strong&gt; project it must be pointing the JS file inside build folder. &lt;code&gt;"start": "node build/index.js"&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Section 3: GCP Account &amp;amp; GCP Project Setup
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Step 1: Create a New Project
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Cloud Console&lt;/a&gt; and sign in.&lt;/li&gt;
&lt;li&gt;Click on "Select a project" at the top and then click on "New Project."&lt;/li&gt;
&lt;li&gt;Choose a unique project name and select the desired organization if applicable.&lt;/li&gt;
&lt;li&gt;Once the project is created, note down the Project ID for future reference.&lt;/li&gt;
&lt;li&gt;Link this project to appropriate Billing Account.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 2: Enable Required APIs
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the &lt;strong&gt;APIs &amp;amp; Services&lt;/strong&gt; section in the Google Cloud Console.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Library&lt;/strong&gt; in the left sidebar.&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;Cloud Storage&lt;/strong&gt; and enable the &lt;strong&gt;Cloud Storage API.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;Cloud Run API&lt;/strong&gt; and enable the &lt;strong&gt;Cloud Run API.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 3: Set Up Cloud Storage
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;strong&gt;Cloud Storage&lt;/strong&gt; section in the Google Cloud Console.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Create Bucket&lt;/strong&gt; to make a new bucket.&lt;/li&gt;
&lt;li&gt;Choose a unique name and select the &lt;code&gt;us-central1&lt;/code&gt; region.&lt;/li&gt;
&lt;li&gt;Leave other settings as default and create the bucket.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 4: Install GCP CLI
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the root directory of your Node app in the terminal.&lt;/li&gt;
&lt;li&gt;Install the Google Cloud SDK following the instructions at &lt;a href="https://cloud.google.com/sdk/docs/install" rel="noopener noreferrer"&gt;Google Cloud SDK Installation Guide&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Authenticate the Google Cloud SDK by running &lt;code&gt;gcloud auth login&lt;/code&gt; and following the on-screen instructions.&lt;/li&gt;
&lt;li&gt;Set the project ID by running &lt;code&gt;gcloud config set project PROJECT_ID&lt;/code&gt;, replacing PROJECT_ID with your actual Project ID.&lt;/li&gt;
&lt;/ol&gt;






&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Service Account Setup
&lt;/h1&gt;

&lt;h4&gt;
  
  
  Generate &amp;amp; Download a Service Account
&lt;/h4&gt;

&lt;p&gt;To set up a Service Account for Google Cloud Run deployment, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Cloud Console&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Navigate to &lt;strong&gt;IAM &amp;amp; Admin&lt;/strong&gt; → &lt;strong&gt;Service Accounts&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Create Service Account&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Provide an appropriate name and description for the service account. For instance, use &lt;code&gt;github-ci-cd&lt;/code&gt; as it will be utilized for Github CI/CD.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Assign the following roles:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Cloud Run Admin&lt;/li&gt;
&lt;li&gt;Cloud Build Service Account&lt;/li&gt;
&lt;li&gt;Cloud Run Service Agent&lt;/li&gt;
&lt;li&gt;Service Account User&lt;/li&gt;
&lt;li&gt;Storage Object Admin&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Click the three dots and select &lt;strong&gt;Manage keys&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Click on &lt;strong&gt;Add Key → Create New Key&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Choose the &lt;strong&gt;JSON key&lt;/strong&gt; type and securely download it. Remember, this key grants access to Google Cloud resources, so keep it safe.&lt;/p&gt;&lt;/li&gt;

&lt;/ol&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 4: Dockerize your Node App
&lt;/h1&gt;

&lt;h4&gt;
  
  
  What is Docker
&lt;/h4&gt;

&lt;p&gt;Docker is a platform that enables developers to automate the deployment of applications inside lightweight, portable containers. Containers encapsulate the application and its dependencies, ensuring consistency and portability across various environments.&lt;/p&gt;

&lt;h4&gt;
  
  
  Placement:
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;Dockerfile&lt;/code&gt;, which contains instructions to build a Docker image for your Node app, should be placed in the root directory of your project, alongside your source code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example Configuration:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# Use Node.js as the base image&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; node:18.15.0-alpine&lt;/span&gt;

&lt;span class="c"&gt;# Set the working directory in the container&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;

&lt;span class="c"&gt;# Copy package.json and yarn.lock to the container&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package.json yarn.lock ./&lt;/span&gt;

&lt;span class="c"&gt;# Install dependencies&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--frozen-lockfile&lt;/span&gt;

&lt;span class="c"&gt;# Copy the app's source code to the container&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;

&lt;span class="c"&gt;# Build the Next app&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn build

&lt;span class="c"&gt;# Serve the production build&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["yarn", "start"]&lt;/span&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Explanation of Configuration:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Base Image:&lt;/strong&gt; Uses Node.js version 18.15.0 Alpine as the lightweight base image.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dependency Installation:&lt;/strong&gt; Copies &lt;code&gt;package.json&lt;/code&gt; and &lt;code&gt;yarn.lock&lt;/code&gt;, installs dependencies using Yarn, leveraging Docker layer caching for efficiency.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Source Code and Build:&lt;/strong&gt; Copies the app's source code, builds the Node app, and sets up the working directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Serve the Build:&lt;/strong&gt; Uses &lt;code&gt;npx serve&lt;/code&gt; to serve the built Node app from the &lt;code&gt;build&lt;/code&gt; directory when the container starts.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 5: CI/CD using GitHub Actions
&lt;/h1&gt;

&lt;h3&gt;
  
  
  CI/CD and GitHub Actions:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CI/CD:&lt;/strong&gt; CI (Continuous Integration) and CD (Continuous Deployment) automate the software delivery process. CI involves merging code changes into a shared repository frequently, automatically running tests, and validating changes. CD automates the deployment of validated code changes to production or staging environments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CI/CD in GitHub Actions:&lt;/strong&gt; GitHub Actions provide workflows to automate tasks in your repository, including CI/CD processes. These workflows are defined in YAML files and can be triggered by various events like code pushes, pull requests, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Storing Service Account Key and Project ID in GitHub Secrets:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Store your Service Account Key and Project ID as secrets in your GitHub repository to securely access them during the CI/CD process.&lt;/li&gt;
&lt;li&gt;You can create Github secrets by &lt;strong&gt;Github Repository → Settings → Secrets &amp;amp; Variables → Actions → Secrets tab → New Repository Secret&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Secrets:

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;GCP_SA_KEY&lt;/code&gt;: Your entire JSON of Service Account Key generated in previous step.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GCP_PROJECT_ID&lt;/code&gt; Your GCP Project ID.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DOCKER_IMAGE_NAME&lt;/code&gt; Your preferred name of Docker Image.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Workflow File Name &amp;amp; Placement:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The workflow file can be named &lt;code&gt;cloud-run-deploy.yml&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Place this file in the &lt;code&gt;.github/workflows&lt;/code&gt; directory in your project. Refer the below given repository incase of any confusion.&lt;/li&gt;
&lt;li&gt;Paste the below configuration in recently created yml file.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Configuration
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to Cloud Run&lt;/span&gt;

&lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;SERVICE_NAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;node-express-api&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;main&lt;/span&gt;
  &lt;span class="na"&gt;pull_request&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;main&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;dockerize-and-deploy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;

    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Checkout repository&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v2&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Google Cloud Auth&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;google-github-actions/auth@v2'&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;credentials_json&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;${{&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;secrets.GCP_SA_KEY&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;}}'&lt;/span&gt;
          &lt;span class="na"&gt;project_id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.GCP_PROJECT_ID }}&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Set up Cloud SDK&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;google-github-actions/setup-gcloud@v2'&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Configure Docker&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;gcloud auth configure-docker&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Build and Push Docker Image&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;docker build -t gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest .&lt;/span&gt;
          &lt;span class="s"&gt;docker push gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to Cloud Run&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;gcloud run deploy $SERVICE_NAME \&lt;/span&gt;
            &lt;span class="s"&gt;--image gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest \&lt;/span&gt;
            &lt;span class="s"&gt;--platform managed \&lt;/span&gt;
            &lt;span class="s"&gt;--region us-central1 \&lt;/span&gt;
            &lt;span class="s"&gt;--allow-unauthenticated&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;This workflow triggers on pushes or pull requests to the &lt;code&gt;main&lt;/code&gt; branch. It uses &lt;strong&gt;GitHub Actions&lt;/strong&gt; to perform various steps such as building the Node app, authenticating with Google Cloud, setting up the Cloud SDK, dockerize your app and deploying the app to Google Cloud Run.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When you are deploying for &lt;strong&gt;first time&lt;/strong&gt; to bucket, remove the service name from &lt;code&gt;gcloud run deploy&lt;/code&gt; command on line 43. As first deployment must be of default service. From second deploy you provide preferred service name.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 6: Testing the CI/CD
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Push Your Changes to GitHub (on Main Branch):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ensure your latest changes are pushed to the main branch of your GitHub repository.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Navigate to the 'Actions' Tab in GitHub Repository:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Visit your GitHub repository and go to the 'Actions' tab.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check Workflow Status:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Verify the status of your Node app deployment workflow using GitHub Actions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open the Deployed App URL:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Access the provided URL after a successful deployment in your browser to confirm your Node application runs smoothly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In GitHub Actions logs, the URL might contain masked text (**** text) representing your GCP project ID. Replace **** with your project ID and open the URL in your browser.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Access Google Cloud Engine Services:&lt;/strong&gt;&lt;br&gt;
Go to Google Cloud, locate and click on Cloud Run, then navigate to &lt;strong&gt;Services&lt;/strong&gt; tab. Find your recent deployments and click on the service to open your Node App in a new tab.&lt;/p&gt;



&lt;h1&gt;
  
  
  GitHub Repository Link
&lt;/h1&gt;

&lt;p&gt;Access the complete code reference for this guide by visiting the &lt;a href="https://github.com/rushi-2001/node-express-cloud-run-setup" rel="noopener noreferrer"&gt;GitHub Repository&lt;/a&gt;.&lt;br&gt;
Feel free to clone the repository and experiment with it on your own!&lt;/p&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;🚀 Congratulations on mastering the deployment of your Node app to Google Cloud Run and GitHub Actions! 🎉&lt;/p&gt;

&lt;p&gt;Embrace this automated workflow to streamline your development journey and propel your projects to new heights. You can refer the above given &lt;strong&gt;repository&lt;/strong&gt; link as and when required. If you have any queries or need guidance, feel free to chat or &lt;strong&gt;drop a comment.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Keep coding, exploring, and sharing the joy of efficient deployments! Don't forget to &lt;strong&gt;like and share this guide&lt;/strong&gt; to inspire others on their deployment adventures! 👍&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding! 💻✨&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>googlecloud</category>
      <category>node</category>
      <category>cicd</category>
      <category>githubactions</category>
    </item>
    <item>
      <title>Deploy React App to Google Cloud Run with Github Actions CI/CD - A Complete Guide</title>
      <dc:creator>Rushi Patel</dc:creator>
      <pubDate>Sun, 10 Mar 2024 19:14:38 +0000</pubDate>
      <link>https://dev.to/rushi-patel/deploy-react-app-to-google-cloud-run-with-github-actions-cicd-a-complete-guide-52pf</link>
      <guid>https://dev.to/rushi-patel/deploy-react-app-to-google-cloud-run-with-github-actions-cicd-a-complete-guide-52pf</guid>
      <description>&lt;p&gt;This guide provides you a step-by-step process to &lt;strong&gt;deploy your React app efficiently to Google Cloud Run&lt;/strong&gt;. Each section offers actionable steps, ensuring a smooth deployment journey.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  In this guide, we'll cover the following sections:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Pre-requisite&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;React Project Config&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;GCP Account &amp;amp; GCP Project Setup&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dockerize your App&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Github Actions CI/CD&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Testing&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Whether you're a seasoned developer seeking a detailed deployment workflow or an enthusiast eager to take your React project live, this guide will equip you with the necessary knowledge and steps for a successful deployment journey. Let's start with pre-requisites.&lt;/p&gt;



&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Section 1: Pre-requisites
&lt;/h1&gt;

&lt;p&gt;Before diving into deploying a React app to Google Cloud Run, it's essential to ensure that you have the necessary tools and knowledge in place. Mininum requirements are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Node &amp;amp; npm Installed:&lt;/strong&gt; Make sure Node.js and npm are installed on your machine. Node 18 is used throughout this guide.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;React JS Setup:&lt;/strong&gt; Familiarize yourself with creating a React project and running it locally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Google Cloud Account:&lt;/strong&gt; If you don't have one yet, don't worry; we'll create it in a later step.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Section 2: React Project Config
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Initialize &amp;amp; Run a React App
&lt;/h3&gt;

&lt;p&gt;If you haven't already set up a React project, follow these steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open your terminal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the following command to create a new React app:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  npx create-react-app my-react-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace my-react-app with your own project name.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run the React app:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will start a development server, allowing you to view your React app in a browser at &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prepare a build of your React app:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command creates a build folder at the root of your project, containing the compiled version of your React app.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exclude build folder from Git: 
Add &lt;code&gt;build/&lt;/code&gt; to your &lt;code&gt;.gitignore&lt;/code&gt; file to prevent it from being tracked and pushed to your Git repository.&lt;/li&gt;
&lt;li&gt;React Scripts Configuration:
Ensure your &lt;code&gt;package.json&lt;/code&gt; includes the following scripts:
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;p&gt;Make sure your scripts align with the provided commands for starting, building, testing, and ejecting your React app.&lt;/p&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Section 3: GCP Account &amp;amp; GCP Project Setup
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Step 1: Create a New Project
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Cloud Console&lt;/a&gt; and sign in.&lt;/li&gt;
&lt;li&gt;Click on "Select a project" at the top and then click on "New Project."&lt;/li&gt;
&lt;li&gt;Choose a unique project name and select the desired organization if applicable.&lt;/li&gt;
&lt;li&gt;Once the project is created, note down the Project ID for future reference.&lt;/li&gt;
&lt;li&gt;Link this project to appropriate Billing Account.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 2: Enable Required APIs
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the &lt;strong&gt;APIs &amp;amp; Services&lt;/strong&gt; section in the Google Cloud Console.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Library&lt;/strong&gt; in the left sidebar.&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;Cloud Storage&lt;/strong&gt; and enable the &lt;strong&gt;Cloud Storage API.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;Cloud Run API&lt;/strong&gt; and enable the &lt;strong&gt;Cloud Run API.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 3: Set Up Cloud Storage
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;strong&gt;Cloud Storage&lt;/strong&gt; section in the Google Cloud Console.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Create Bucket&lt;/strong&gt; to make a new bucket.&lt;/li&gt;
&lt;li&gt;Choose a unique name and select the &lt;code&gt;us-central1&lt;/code&gt; region.&lt;/li&gt;
&lt;li&gt;Leave other settings as default and create the bucket.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 4: Install GCP CLI
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the root directory of your React app in the terminal.&lt;/li&gt;
&lt;li&gt;Install the Google Cloud SDK following the instructions at &lt;a href="https://cloud.google.com/sdk/docs/install" rel="noopener noreferrer"&gt;Google Cloud SDK Installation Guide&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Authenticate the Google Cloud SDK by running &lt;code&gt;gcloud auth login&lt;/code&gt; and following the on-screen instructions.&lt;/li&gt;
&lt;li&gt;Set the project ID by running &lt;code&gt;gcloud config set project PROJECT_ID&lt;/code&gt;, replacing PROJECT_ID with your actual Project ID.&lt;/li&gt;
&lt;/ol&gt;






&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Service Account Setup
&lt;/h1&gt;

&lt;h4&gt;
  
  
  Generate &amp;amp; Download a Service Account
&lt;/h4&gt;

&lt;p&gt;To set up a Service Account for Google Cloud Run deployment, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Cloud Console&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Navigate to &lt;strong&gt;IAM &amp;amp; Admin&lt;/strong&gt; → &lt;strong&gt;Service Accounts&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Create Service Account&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Provide an appropriate name and description for the service account. For instance, use &lt;code&gt;github-ci-cd&lt;/code&gt; as it will be utilized for Github CI/CD.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Assign the following roles:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Cloud Run Admin&lt;/li&gt;
&lt;li&gt;Cloud Build Service Account&lt;/li&gt;
&lt;li&gt;Cloud Run Service Agent&lt;/li&gt;
&lt;li&gt;Service Account User&lt;/li&gt;
&lt;li&gt;Storage Object Admin&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Click the three dots and select &lt;strong&gt;Manage keys&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Click on &lt;strong&gt;Add Key → Create New Key&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Choose the &lt;strong&gt;JSON key&lt;/strong&gt; type and securely download it. Remember, this key grants access to Google Cloud resources, so keep it safe.&lt;/p&gt;&lt;/li&gt;

&lt;/ol&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 4: Dockerize your React App
&lt;/h1&gt;

&lt;h4&gt;
  
  
  What is Docker
&lt;/h4&gt;

&lt;p&gt;Docker is a platform that enables developers to automate the deployment of applications inside lightweight, portable containers. Containers encapsulate the application and its dependencies, ensuring consistency and portability across various environments.&lt;/p&gt;

&lt;h4&gt;
  
  
  Placement:
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;Dockerfile&lt;/code&gt;, which contains instructions to build a Docker image for your React app, should be placed in the root directory of your project, alongside your source code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example Configuration:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# Use Node.js as the base image&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; node:18.15.0-alpine&lt;/span&gt;

&lt;span class="c"&gt;# Set the working directory in the container&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;

&lt;span class="c"&gt;# Copy package.json and yarn.lock to the container&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package.json yarn.lock ./&lt;/span&gt;

&lt;span class="c"&gt;# Install dependencies&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--frozen-lockfile&lt;/span&gt;

&lt;span class="c"&gt;# Install dependencies&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn &lt;span class="nb"&gt;install&lt;/span&gt;

&lt;span class="c"&gt;# Copy the app's source code to the container&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;

&lt;span class="c"&gt;# Build the React app&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn build

&lt;span class="c"&gt;# Serve the build&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["npx", "serve", "-s", "build"]&lt;/span&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Explanation of Configuration:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Base Image:&lt;/strong&gt; Uses Node.js version 18.15.0 Alpine as the lightweight base image.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dependency Installation:&lt;/strong&gt; Copies &lt;code&gt;package.json&lt;/code&gt; and &lt;code&gt;yarn.lock&lt;/code&gt;, installs dependencies using Yarn, leveraging Docker layer caching for efficiency.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Source Code and Build:&lt;/strong&gt; Copies the app's source code, builds the React app, and sets up the working directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Serve the Build:&lt;/strong&gt; Uses &lt;code&gt;npx serve&lt;/code&gt; to serve the built React app from the &lt;code&gt;build&lt;/code&gt; directory when the container starts.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 5: CI/CD using GitHub Actions
&lt;/h1&gt;

&lt;h3&gt;
  
  
  CI/CD and GitHub Actions:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CI/CD:&lt;/strong&gt; CI (Continuous Integration) and CD (Continuous Deployment) automate the software delivery process. CI involves merging code changes into a shared repository frequently, automatically running tests, and validating changes. CD automates the deployment of validated code changes to production or staging environments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CI/CD in GitHub Actions:&lt;/strong&gt; GitHub Actions provide workflows to automate tasks in your repository, including CI/CD processes. These workflows are defined in YAML files and can be triggered by various events like code pushes, pull requests, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Storing Service Account Key and Project ID in GitHub Secrets:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Store your Service Account Key and Project ID as secrets in your GitHub repository to securely access them during the CI/CD process.&lt;/li&gt;
&lt;li&gt;You can create Github secrets by &lt;strong&gt;Github Repository → Settings → Secrets &amp;amp; Variables → Actions → Secrets tab → New Repository Secret&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Secrets:

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;GCP_SA_KEY&lt;/code&gt;: Your entire JSON of Service Account Key generated in previous step.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GCP_PROJECT_ID&lt;/code&gt; Your GCP Project ID.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DOCKER_IMAGE_NAME&lt;/code&gt; Your preferred name of Docker Image.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Workflow File Name &amp;amp; Placement:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The workflow file can be named &lt;code&gt;cloud-run-deploy.yml&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Place this file in the &lt;code&gt;.github/workflows&lt;/code&gt; directory in your project. Refer the below given repository incase of any confusion.&lt;/li&gt;
&lt;li&gt;Paste the below configuration in recently created yml file.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Configuration
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to Cloud Run&lt;/span&gt;

&lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;SERVICE_NAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;react-app&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;main&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;dockerize-and-deploy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;

    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Checkout repository&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v2&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Set up Google Cloud SDK&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;google-github-actions/setup-gcloud@v0.2.0&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;service_account_key&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.GCP_SA_KEY }}&lt;/span&gt;
          &lt;span class="na"&gt;project_id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.GCP_PROJECT_ID }}&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Configure Docker&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;gcloud auth configure-docker&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Build and Push Docker Image&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;docker build -t gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest .&lt;/span&gt;
          &lt;span class="s"&gt;docker push gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to Cloud Run&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;gcloud run deploy $SERVICE_NAME \&lt;/span&gt;
            &lt;span class="s"&gt;--image gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest \&lt;/span&gt;
            &lt;span class="s"&gt;--platform managed \&lt;/span&gt;
            &lt;span class="s"&gt;--region us-central1 \&lt;/span&gt;
            &lt;span class="s"&gt;--allow-unauthenticated&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;This workflow triggers on pushes or pull requests to the &lt;code&gt;main&lt;/code&gt; branch. It uses &lt;strong&gt;GitHub Actions&lt;/strong&gt; to perform various steps such as building the React app, authenticating with Google Cloud, setting up the Cloud SDK, dockerize your app and deploying the app to Google Cloud Run.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When you are deploying for &lt;strong&gt;first time&lt;/strong&gt; to bucket, remove the service name from &lt;code&gt;gcloud run deploy&lt;/code&gt; command on line 36. As first deployment must be of default service. From second deploy you provide preferred service name.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 6: Testing the CI/CD
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Push Your Changes to GitHub (on Main Branch):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ensure your latest changes are pushed to the main branch of your GitHub repository.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Navigate to the 'Actions' Tab in GitHub Repository:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Visit your GitHub repository and go to the 'Actions' tab.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check Workflow Status:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Verify the status of your React app deployment workflow using GitHub Actions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open the Deployed App URL:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Access the provided URL after a successful deployment in your browser to confirm your React application runs smoothly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In GitHub Actions logs, the URL might contain masked text (**** text) representing your GCP project ID. Replace **** with your project ID and open the URL in your browser.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Access Google Cloud Engine Services:&lt;/strong&gt;&lt;br&gt;
Go to Google Cloud, locate and click on Cloud Run, then navigate to &lt;strong&gt;Services&lt;/strong&gt; tab. Find your recent deployments and click on the service to open your React App in a new tab.&lt;/p&gt;



&lt;h1&gt;
  
  
  GitHub Repository Link
&lt;/h1&gt;

&lt;p&gt;Access the complete code reference for this guide by visiting the &lt;a href="https://github.com/rushi-2001/react-cloud-run-setup" rel="noopener noreferrer"&gt;GitHub Repository&lt;/a&gt;.&lt;br&gt;
Feel free to clone the repository and experiment with it on your own!&lt;/p&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;🚀 Congratulations on mastering the deployment of your React app to Google Cloud Run and GitHub Actions! 🎉&lt;/p&gt;

&lt;p&gt;Embrace this automated workflow to streamline your development journey and propel your projects to new heights. You can refer the above given &lt;strong&gt;repository&lt;/strong&gt; link as and when required. If you have any queries or need guidance, feel free to chat or &lt;strong&gt;drop a comment.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Keep coding, exploring, and sharing the joy of efficient deployments! Don't forget to &lt;strong&gt;like and share this guide&lt;/strong&gt; to inspire others on their deployment adventures! 👍&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding! 💻✨&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>googlecloud</category>
      <category>react</category>
      <category>cicd</category>
      <category>githubactions</category>
    </item>
    <item>
      <title>Deploy Next JS App to Google App Engine with Github Actions CI/CD - A Complete Guide</title>
      <dc:creator>Rushi Patel</dc:creator>
      <pubDate>Tue, 16 Jan 2024 18:10:27 +0000</pubDate>
      <link>https://dev.to/rushi-patel/deploy-next-js-app-to-google-app-engine-with-github-actions-cicd-a-complete-guide-2db</link>
      <guid>https://dev.to/rushi-patel/deploy-next-js-app-to-google-app-engine-with-github-actions-cicd-a-complete-guide-2db</guid>
      <description>&lt;p&gt;This guide provides you a step-by-step process to &lt;strong&gt;deploy your Next JS App efficiently to Google App Engine&lt;/strong&gt;. Each section offers actionable steps, ensuring a smooth deployment journey.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  In this guide, we'll cover the following sections:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Pre-requisite&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Next JS Project Config&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;GCP Account &amp;amp; GCP Project Setup&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;App.yaml &amp;amp; Explanation&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Github Actions CI/CD&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Testing&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Whether you're a seasoned developer seeking a detailed deployment workflow or an enthusiast eager to take your Next JS project live, this guide will equip you with the necessary knowledge and steps for a successful deployment journey. Let's start with pre-requisites.&lt;/p&gt;



&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Section 1: Pre-requisites
&lt;/h1&gt;

&lt;p&gt;Before diving into deploying a Next JS App to Google App Engine (GAE), it's essential to ensure that you have the necessary tools and knowledge in place. Minimum requirements are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Node, YARN &amp;amp; npm Installed:&lt;/strong&gt; Make sure Node.js, YARN and npm are installed on your machine. Node 18 is used throughout this guide.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simple Next JS Project:&lt;/strong&gt; Familiarize yourself with creating a Next Project project and running it locally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Google Cloud Account:&lt;/strong&gt; If you don't have one yet, don't worry; we'll create it in a later step.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Section 2: Next JS Project Config
&lt;/h1&gt;
&lt;h3&gt;
  
  
  Prepare and Run Next JS Project
&lt;/h3&gt;

&lt;p&gt;If you haven't already set up a Next JS project, create your own project or clone the demo project from &lt;a href="https://github.com/rushi-2001/next-js-app-engine-setup" rel="noopener noreferrer"&gt;Github here&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open your terminal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the following command to clone Next JS project:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  git clone https://github.com/rushi-2001/next-js-app-engine-setup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Install the dependencies:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  yarn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Run the app:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  yarn dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This will start a development server, allowing you to view your Next app in a browser at &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prepare a build of your Next app:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  yarn build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Next Scripts Configuration:
Ensure your &lt;code&gt;package.json&lt;/code&gt; includes the following scripts:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"next dev"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"next build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"next start"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Make sure your scripts align with the provided commands for starting &amp;amp; building your Next app.&lt;/p&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Section 3: GCP Account &amp;amp; GCP Project Setup
&lt;/h1&gt;
&lt;h3&gt;
  
  
  Step 1: Create a New Project
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Cloud Console&lt;/a&gt; and sign in.&lt;/li&gt;
&lt;li&gt;Click on "Select a project" at the top and then click on "New Project."&lt;/li&gt;
&lt;li&gt;Choose a unique project name and select the desired organization if applicable.&lt;/li&gt;
&lt;li&gt;Once the project is created, note down the Project ID for future reference.&lt;/li&gt;
&lt;li&gt;Link this project to appropriate Billing Account.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Step 2: Enable Required APIs
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the &lt;strong&gt;APIs &amp;amp; Services&lt;/strong&gt; section in the Google Cloud Console.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Library&lt;/strong&gt; in the left sidebar.&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;Cloud Storage&lt;/strong&gt; and enable the &lt;strong&gt;Cloud Storage API.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;App Engine Admin&lt;/strong&gt; and enable the &lt;strong&gt;App Engine Admin API.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Step 3: Set Up Cloud Storage
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;strong&gt;Cloud Storage&lt;/strong&gt; section in the Google Cloud Console.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Create Bucket&lt;/strong&gt; to make a new bucket.&lt;/li&gt;
&lt;li&gt;Choose a unique name and select the desired region.&lt;/li&gt;
&lt;li&gt;Leave other settings as default and create the bucket.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Step 4: Install GCP CLI
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the root directory of your Next app in the terminal.&lt;/li&gt;
&lt;li&gt;Install the Google Cloud SDK following the instructions at &lt;a href="https://cloud.google.com/sdk/docs/install" rel="noopener noreferrer"&gt;Google Cloud SDK Installation Guide&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Authenticate the Google Cloud SDK by running &lt;code&gt;gcloud auth login&lt;/code&gt; and following the on-screen instructions.&lt;/li&gt;
&lt;li&gt;Set the project ID by running &lt;code&gt;gcloud config set project PROJECT_ID&lt;/code&gt;, replacing PROJECT_ID with your actual Project ID.&lt;/li&gt;
&lt;/ol&gt;






&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Section 4: Service Account Setup
&lt;/h1&gt;
&lt;h4&gt;
  
  
  Generate &amp;amp; Download a Service Account
&lt;/h4&gt;

&lt;p&gt;To set up a Service Account for Google App Engine deployment, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Cloud Console&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Navigate to &lt;strong&gt;IAM &amp;amp; Admin&lt;/strong&gt; → &lt;strong&gt;Service Accounts&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Create Service Account&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Provide an appropriate name and description for the service account. For instance, use &lt;code&gt;github-ci-cd&lt;/code&gt; as it will be utilized for Github CI/CD.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Assign the following roles:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;App Engine Admin&lt;/li&gt;
&lt;li&gt;Cloud Build Service Account&lt;/li&gt;
&lt;li&gt;Service Account User&lt;/li&gt;
&lt;li&gt;Storage Object Admin&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click the three dots and select &lt;strong&gt;Manage keys&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on &lt;strong&gt;Add Key → Create New Key&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose the &lt;strong&gt;JSON key&lt;/strong&gt; type and securely download it. Remember, this key grants access to Google Cloud resources, so keep it safe.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Step 5: Create app.yaml and Explanation
&lt;/h1&gt;
&lt;h4&gt;
  
  
  What is app.yaml?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;app.yaml&lt;/code&gt; file configures settings and routing for Google App Engine applications.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Placement:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Keep the &lt;code&gt;app.yaml&lt;/code&gt; in your project's root directory alongside your source code.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Example Configuration:
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# [START app_yaml]&lt;/span&gt;
&lt;span class="na"&gt;runtime&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nodejs18&lt;/span&gt;

&lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;my-next-app&lt;/span&gt;

&lt;span class="na"&gt;handlers&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/.*&lt;/span&gt;
    &lt;span class="na"&gt;secure&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always&lt;/span&gt;
    &lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;auto&lt;/span&gt;

&lt;span class="c1"&gt;# [END app_yaml]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Explanation of Configuration:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;runtime:&lt;/strong&gt; Specifies the runtime environment (e.g., &lt;code&gt;nodejs18&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;service:&lt;/strong&gt; Defines the service name, typically a project-specific prefix or subdomain.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This configuration example directs incoming requests to the appropriate static files within the &lt;code&gt;build&lt;/code&gt; directory, which further handles the different API routing.&lt;/p&gt;
&lt;h4&gt;
  
  
  Deploy your App Locally:
&lt;/h4&gt;

&lt;p&gt;Deploy your Node app by executing the command in root directory of your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcloud app deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will package and upload your compiled build to Google Cloud. The deployment process may take a few minutes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When you are deploying for &lt;strong&gt;first time&lt;/strong&gt; of the bucket, comment out line 4, where you specify service name. As first deployment must be of default service.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After deployment completion, you'll receive a URL where your Next app is hosted. Open the URL to check the deployed Next App.&lt;/p&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 6: CI/CD using GitHub Actions
&lt;/h1&gt;

&lt;h3&gt;
  
  
  CI/CD and GitHub Actions:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CI/CD:&lt;/strong&gt; CI (Continuous Integration) and CD (Continuous Deployment) automate the software delivery process. CI involves merging code changes into a shared repository frequently, automatically running tests, and validating changes. CD automates the deployment of validated code changes to production or staging environments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CI/CD in GitHub Actions:&lt;/strong&gt; GitHub Actions provide workflows to automate tasks in your repository, including CI/CD processes. These workflows are defined in YAML files and can be triggered by various events like code pushes, pull requests, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Storing Service Account Key and Project ID in GitHub Secrets:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Store your Service Account Key and Project ID as secrets in your GitHub repository to securely access them during the CI/CD process.&lt;/li&gt;
&lt;li&gt;You can create Github secrets by &lt;strong&gt;Github Repository → Settings → Secrets &amp;amp; Variables → Actions → Secrets tab → New Repository Secret&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Secrets:

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;GCP_SA_KEY&lt;/code&gt;: Your entire JSON of Service Account Key generated in previous step.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GCP_PROJECT_ID&lt;/code&gt; Your GCP Project ID.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Workflow File Name &amp;amp; Placement:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The workflow file should be named &lt;code&gt;gcp-deploy.yml&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Place this file in the &lt;code&gt;.github/workflows&lt;/code&gt; directory in your project. Refer the below given repository incase of any confusion.&lt;/li&gt;
&lt;li&gt;Paste the below configuration in recently created yml file.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Configuration
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to Goggle App Engine (GAE)&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;main&lt;/span&gt;
  &lt;span class="na"&gt;pull_request&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;main&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;build-and-deploy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;

    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Checkout repository&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v2&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Setup Node.js and yarn&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/setup-node@v2&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;node-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;18"&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Install dependencies&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;yarn install --frozen-lockfile&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Build Next Project&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;yarn build&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Google Cloud Auth&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;google-github-actions/auth@v2"&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;credentials_json&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;${{&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;secrets.GCP_SA_KEY&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;}}"&lt;/span&gt;
          &lt;span class="na"&gt;project_id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.GCP_PROJECT_ID }}&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Set up Cloud SDK&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;google-github-actions/setup-gcloud@v2"&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to Google App Engine&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;gcloud app deploy app.yaml --quiet&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;This workflow triggers on pushes or pull requests to the &lt;code&gt;main&lt;/code&gt; branch. It uses &lt;strong&gt;GitHub Actions&lt;/strong&gt; to perform various steps such as building the Next app, authenticating with Google Cloud, setting up the Cloud SDK, and deploying the app to Google Cloud Platform.&lt;/p&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 7: Testing the CI/CD
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Push Your Changes to GitHub (on Main Branch):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ensure your latest changes are pushed to the main branch of your GitHub repository.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Navigate to the 'Actions' Tab in GitHub Repository:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Visit your GitHub repository and go to the 'Actions' tab.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check Workflow Status:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Verify the status of your Next app deployment workflow using GitHub Actions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open the Deployed App URL:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Access the provided URL after a successful deployment in your browser to confirm your Next application runs smoothly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In GitHub Actions logs, the URL might contain masked text (**** text) representing your GCP project ID. Replace **** with your project ID and open the URL in your browser.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Access Google Cloud Engine Services:&lt;/strong&gt;&lt;br&gt;
Go to Google Cloud, locate and click on App Engine, then navigate to &lt;strong&gt;Services&lt;/strong&gt; in the sidebar. Find your recent deployments and click on the service to open your Next App in a new tab.&lt;/p&gt;



&lt;h1&gt;
  
  
  GitHub Repository Link
&lt;/h1&gt;

&lt;p&gt;Access the complete code reference for this guide by visiting the &lt;a href="https://github.com/rushi-2001/next-js-app-engine-setup" rel="noopener noreferrer"&gt;GitHub Repository&lt;/a&gt;.&lt;br&gt;
Feel free to clone the repository and experiment with it on your own!&lt;/p&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;🚀 Congratulations on mastering the deployment of your Next JS Project to Google App Engine and GitHub Actions! 🎉&lt;/p&gt;

&lt;p&gt;Embrace this automated workflow to streamline your development journey and propel your projects to new heights. You can refer the above given &lt;strong&gt;repository&lt;/strong&gt; link as and when required. If you have any queries or need guidance, feel free to chat or &lt;strong&gt;drop a comment.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Keep coding, exploring, and sharing the joy of efficient deployments! Don't forget to &lt;strong&gt;like and share this guide&lt;/strong&gt; to inspire others on their deployment adventures! 👍&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding! 💻✨&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>googlecloud</category>
      <category>cicd</category>
      <category>githubactions</category>
    </item>
    <item>
      <title>Deploy Node JS Project to Google App Engine with Github Actions CI/CD - A Complete Guide</title>
      <dc:creator>Rushi Patel</dc:creator>
      <pubDate>Tue, 16 Jan 2024 18:10:03 +0000</pubDate>
      <link>https://dev.to/rushi-patel/deploy-node-js-project-to-google-app-engine-with-github-actions-cicd-a-complete-guide-3od9</link>
      <guid>https://dev.to/rushi-patel/deploy-node-js-project-to-google-app-engine-with-github-actions-cicd-a-complete-guide-3od9</guid>
      <description>&lt;p&gt;This guide provides you a step-by-step process to &lt;strong&gt;deploy your Node JS Project efficiently to Google App Engine&lt;/strong&gt;. Each section offers actionable steps, ensuring a smooth deployment journey.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  In this guide, we'll cover the following sections:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Pre-requisite&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Node Project Config&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GCP Account &amp;amp; GCP Project Setup&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;App.yaml &amp;amp; Explanation&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Github Actions CI/CD&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Testing&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Whether you're a seasoned developer seeking a detailed deployment workflow or an enthusiast eager to take your Node project live, this guide will equip you with the necessary knowledge and steps for a successful deployment journey. Let's start with pre-requisites.&lt;/p&gt;



&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Section 1: Pre-requisites
&lt;/h1&gt;

&lt;p&gt;Before diving into deploying a Node JS Project to Google App Engine (GAE), it's essential to ensure that you have the necessary tools and knowledge in place. Mininum requirements are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Node, YARN &amp;amp; npm Installed:&lt;/strong&gt; Make sure Node.js, YARN and npm are installed on your machine. Node 18 is used throughout this guide.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simple Node JS Project:&lt;/strong&gt; Familiarize yourself with creating a Node Project project and running it locally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Google Cloud Account:&lt;/strong&gt; If you don't have one yet, don't worry; we'll create it in a later step.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Section 2: Node JS Project Config
&lt;/h1&gt;
&lt;h3&gt;
  
  
  Prepare and Run Node JS Project
&lt;/h3&gt;

&lt;p&gt;If you haven't already set up a Node JS project, create your own project or clone the demo project from &lt;a href="https://github.com/rushi-2001/node-express-app-engine-setup" rel="noopener noreferrer"&gt;Github here&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open your terminal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the following command to clone Node JS project:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git clone https://github.com/rushi-2001/node-express-app-engine-setup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Install the dependencies:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   yarn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Run the app:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   yarn start-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This will start a development server, allowing you to view your Node server in a browser at &lt;a href="http://localhost:8080" rel="noopener noreferrer"&gt;http://localhost:8080&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prepare a build of your Node app:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   yarn build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This command creates a build folder at the root of your project, containing the compiled version of your Node app.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exclude build folder from Git: 
Add &lt;code&gt;build/&lt;/code&gt; to your &lt;code&gt;.gitignore&lt;/code&gt; file to prevent it from being tracked and pushed to your Git repository.&lt;/li&gt;
&lt;li&gt;Node Scripts Configuration:
Ensure your &lt;code&gt;package.json&lt;/code&gt; includes the following scripts:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node build/index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start-dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"nodemon src/index.ts"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"tsc"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Make sure your scripts align with the provided commands for starting &amp;amp; building app.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; When deploying your application to Google App Engine, ensure that the &lt;code&gt;start&lt;/code&gt; command in your &lt;code&gt;package.json&lt;/code&gt; points to the correct file for launching your application. &lt;br&gt; For &lt;strong&gt;JavaScript&lt;/strong&gt; project you can use &lt;code&gt;"start": "node index.js"&lt;/code&gt; and for &lt;strong&gt;TypeScript&lt;/strong&gt; project it must be pointing the JS file inside build folder. &lt;code&gt;"start": "node build/index.js"&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Section 3: GCP Account &amp;amp; GCP Project Setup
&lt;/h1&gt;
&lt;h3&gt;
  
  
  Step 1: Create a New Project
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Cloud Console&lt;/a&gt; and sign in.&lt;/li&gt;
&lt;li&gt;Click on "Select a project" at the top and then click on "New Project."&lt;/li&gt;
&lt;li&gt;Choose a unique project name and select the desired organization if applicable.&lt;/li&gt;
&lt;li&gt;Once the project is created, note down the Project ID for future reference.&lt;/li&gt;
&lt;li&gt;Link this project to appropriate Billing Account.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Step 2: Enable Required APIs
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the &lt;strong&gt;APIs &amp;amp; Services&lt;/strong&gt; section in the Google Cloud Console.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Library&lt;/strong&gt; in the left sidebar.&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;Cloud Storage&lt;/strong&gt; and enable the &lt;strong&gt;Cloud Storage API.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;App Engine Admin&lt;/strong&gt; and enable the &lt;strong&gt;App Engine Admin API.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Step 3: Set Up Cloud Storage
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;strong&gt;Cloud Storage&lt;/strong&gt; section in the Google Cloud Console.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Create Bucket&lt;/strong&gt; to make a new bucket.&lt;/li&gt;
&lt;li&gt;Choose a unique name and select the desired region.&lt;/li&gt;
&lt;li&gt;Leave other settings as default and create the bucket.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Step 4: Install GCP CLI
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the root directory of your Node app in the terminal.&lt;/li&gt;
&lt;li&gt;Install the Google Cloud SDK following the instructions at &lt;a href="https://cloud.google.com/sdk/docs/install" rel="noopener noreferrer"&gt;Google Cloud SDK Installation Guide&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Authenticate the Google Cloud SDK by running &lt;code&gt;gcloud auth login&lt;/code&gt; and following the on-screen instructions.&lt;/li&gt;
&lt;li&gt;Set the project ID by running &lt;code&gt;gcloud config set project PROJECT_ID&lt;/code&gt;, replacing PROJECT_ID with your actual Project ID.&lt;/li&gt;
&lt;/ol&gt;






&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Section 4: Service Account Setup
&lt;/h1&gt;
&lt;h4&gt;
  
  
  Generate &amp;amp; Download a Service Account
&lt;/h4&gt;

&lt;p&gt;To set up a Service Account for Google App Engine deployment, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Cloud Console&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Navigate to &lt;strong&gt;IAM &amp;amp; Admin&lt;/strong&gt; → &lt;strong&gt;Service Accounts&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Create Service Account&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Provide an appropriate name and description for the service account. For instance, use &lt;code&gt;github-ci-cd&lt;/code&gt; as it will be utilized for Github CI/CD.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Assign the following roles:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;App Engine Admin&lt;/li&gt;
&lt;li&gt;Cloud Build Service Account&lt;/li&gt;
&lt;li&gt;Service Account User&lt;/li&gt;
&lt;li&gt;Storage Object Admin&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click the three dots and select &lt;strong&gt;Manage keys&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on &lt;strong&gt;Add Key → Create New Key&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose the &lt;strong&gt;JSON key&lt;/strong&gt; type and securely download it. Remember, this key grants access to Google Cloud resources, so keep it safe.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Step 5: Create app.yaml and Explanation
&lt;/h1&gt;
&lt;h4&gt;
  
  
  What is app.yaml?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;app.yaml&lt;/code&gt; file configures settings and routing for Google App Engine applications.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Placement:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Keep the &lt;code&gt;app.yaml&lt;/code&gt; in your project's root directory alongside your source code.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Example Configuration:
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# [START app_yaml]&lt;/span&gt;

&lt;span class="na"&gt;runtime&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nodejs18&lt;/span&gt;

&lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;node-express-api&lt;/span&gt;

&lt;span class="c1"&gt;# [END app_yaml]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Explanation of Configuration:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;runtime:&lt;/strong&gt; Specifies the runtime environment (e.g., &lt;code&gt;nodejs18&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;service:&lt;/strong&gt; Defines the service name, typically a project-specific prefix or subdomain.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This configuration example directs incoming requests to the appropriate static files within the &lt;code&gt;build&lt;/code&gt; directory, which further handles the different API routing.&lt;/p&gt;
&lt;h4&gt;
  
  
  Deploy your App Locally:
&lt;/h4&gt;

&lt;p&gt;Deploy your Node app by executing the command in root directory of your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   gcloud app deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will package and upload your compiled build to Google Cloud. The deployment process may take a few minutes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When you are deploying for &lt;strong&gt;first time&lt;/strong&gt; of the bucket, comment out line 4, where you specify service name. As first deployment must be of default service. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After deployment completion, you'll receive a URL where your Node app is hosted. Open the URL to check the deployed Node App.&lt;/p&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 6: CI/CD using GitHub Actions
&lt;/h1&gt;

&lt;h3&gt;
  
  
  CI/CD and GitHub Actions:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CI/CD:&lt;/strong&gt; CI (Continuous Integration) and CD (Continuous Deployment) automate the software delivery process. CI involves merging code changes into a shared repository frequently, automatically running tests, and validating changes. CD automates the deployment of validated code changes to production or staging environments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CI/CD in GitHub Actions:&lt;/strong&gt; GitHub Actions provide workflows to automate tasks in your repository, including CI/CD processes. These workflows are defined in YAML files and can be triggered by various events like code pushes, pull requests, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Storing Service Account Key and Project ID in GitHub Secrets:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Store your Service Account Key and Project ID as secrets in your GitHub repository to securely access them during the CI/CD process.&lt;/li&gt;
&lt;li&gt;You can create Github secrets by &lt;strong&gt;Github Repository → Settings → Secrets &amp;amp; Variables → Actions → Secrets tab → New Repository Secret&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Secrets:

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;GCP_SA_KEY&lt;/code&gt;: Your entire JSON of Service Account Key generated in previous step.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GCP_PROJECT_ID&lt;/code&gt; Your GCP Project ID.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Workflow File Name &amp;amp; Placement:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The workflow file should be named &lt;code&gt;gcp-deploy.yml&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Place this file in the &lt;code&gt;.github/workflows&lt;/code&gt; directory in your project. Refer the below given repository incase of any confusion.&lt;/li&gt;
&lt;li&gt;Paste the below configuration in recently created yml file.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Configuration
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to Goggle App Engine (GAE)&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;main&lt;/span&gt;
  &lt;span class="na"&gt;pull_request&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;main&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;build-and-deploy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;

    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Checkout repository&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v2&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Setup Node.js and yarn&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/setup-node@v2&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;node-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;18'&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Install dependencies&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;yarn install --frozen-lockfile&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Build Node Project&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;yarn build&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Google Cloud Auth&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;google-github-actions/auth@v2'&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;credentials_json&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;${{&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;secrets.GCP_SA_KEY&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;}}'&lt;/span&gt;
          &lt;span class="na"&gt;project_id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.GCP_PROJECT_ID }}&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Set up Cloud SDK&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;google-github-actions/setup-gcloud@v2'&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to Google App Engine&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;gcloud app deploy app.yaml --quiet&lt;/span&gt;

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

&lt;/div&gt;





&lt;p&gt;This workflow triggers on pushes or pull requests to the &lt;code&gt;main&lt;/code&gt; branch. It uses &lt;strong&gt;GitHub Actions&lt;/strong&gt; to perform various steps such as building the Node app, authenticating with Google Cloud, setting up the Cloud SDK, and deploying the app to Google Cloud Platform.&lt;/p&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 7: Testing the CI/CD
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Push Your Changes to GitHub (on Main Branch):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ensure your latest changes are pushed to the main branch of your GitHub repository.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Navigate to the 'Actions' Tab in GitHub Repository:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Visit your GitHub repository and go to the 'Actions' tab.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check Workflow Status:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Verify the status of your Node app deployment workflow using GitHub Actions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open the Deployed App URL:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Access the provided URL after a successful deployment in your browser to confirm your Node application runs smoothly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In GitHub Actions logs, the URL might contain masked text (**** text) representing your GCP project ID. Replace **** with your project ID and open the URL in your browser.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Access Google Cloud Engine Services:&lt;/strong&gt;&lt;br&gt;
Go to Google Cloud, locate and click on App Engine, then navigate to &lt;strong&gt;Services&lt;/strong&gt; in the sidebar. Find your recent deployments and click on the service to open your Node App in a new tab.&lt;/p&gt;



&lt;h1&gt;
  
  
  GitHub Repository Link
&lt;/h1&gt;

&lt;p&gt;Access the complete code reference for this guide by visiting the &lt;a href="https://github.com/rushi-2001/node-express-app-engine-setup" rel="noopener noreferrer"&gt;GitHub Repository&lt;/a&gt;.&lt;br&gt;
Feel free to clone the repository and experiment with it on your own!&lt;/p&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;🚀 Congratulations on mastering the deployment of your Node JS Project to Google App Engine and GitHub Actions! 🎉&lt;/p&gt;

&lt;p&gt;Embrace this automated workflow to streamline your development journey and propel your projects to new heights. You can refer the above given &lt;strong&gt;repository&lt;/strong&gt; link as and when required. If you have any queries or need guidance, feel free to chat or &lt;strong&gt;drop a comment.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Keep coding, exploring, and sharing the joy of efficient deployments! Don't forget to &lt;strong&gt;like and share this guide&lt;/strong&gt; to inspire others on their deployment adventures! 👍&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding! 💻✨&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>googlecloud</category>
      <category>cicd</category>
      <category>githubactions</category>
    </item>
    <item>
      <title>Deploy React App to Google App Engine with Github Actions CI/CD - A Complete Guide</title>
      <dc:creator>Rushi Patel</dc:creator>
      <pubDate>Tue, 09 Jan 2024 12:02:11 +0000</pubDate>
      <link>https://dev.to/rushi-patel/deploy-react-app-to-google-app-engine-with-github-actions-cicd-a-complete-guide-67h</link>
      <guid>https://dev.to/rushi-patel/deploy-react-app-to-google-app-engine-with-github-actions-cicd-a-complete-guide-67h</guid>
      <description>&lt;p&gt;This guide provides you a step-by-step process to &lt;strong&gt;deploy your React app efficiently to Google App Engine&lt;/strong&gt;. Each section offers actionable steps, ensuring a smooth deployment journey.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  In this guide, we'll cover the following sections:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Pre-requisite&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;React Project Config&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;GCP Account &amp;amp; GCP Project Setup&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;App.yaml and Explanation&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Github Actions CI/CD&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Testing&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Whether you're a seasoned developer seeking a detailed deployment workflow or an enthusiast eager to take your React project live, this guide will equip you with the necessary knowledge and steps for a successful deployment journey. Let's start with pre-requisites.&lt;/p&gt;



&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Section 1: Pre-requisites
&lt;/h1&gt;

&lt;p&gt;Before diving into deploying a React app to Google App Engine, it's essential to ensure that you have the necessary tools and knowledge in place. Mininum requirements are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Node &amp;amp; npm Installed:&lt;/strong&gt; Make sure Node.js and npm are installed on your machine. Node 18 is used throughout this guide.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;React JS Setup:&lt;/strong&gt; Familiarize yourself with creating a React project and running it locally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Google Cloud Account:&lt;/strong&gt; If you don't have one yet, don't worry; we'll create it in a later step.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Section 2: React Project Config
&lt;/h1&gt;
&lt;h3&gt;
  
  
  Initialize &amp;amp; Run a React App
&lt;/h3&gt;

&lt;p&gt;If you haven't already set up a React project, follow these steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open your terminal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the following command to create a new React app:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  npx create-react-app my-react-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Replace my-react-app with your own project name.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run the React app:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This will start a development server, allowing you to view your React app in a browser at &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prepare a build of your React app:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This command creates a build folder at the root of your project, containing the compiled version of your React app.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exclude build folder from Git: 
Add &lt;code&gt;build/&lt;/code&gt; to your &lt;code&gt;.gitignore&lt;/code&gt; file to prevent it from being tracked and pushed to your Git repository.&lt;/li&gt;
&lt;li&gt;React Scripts Configuration:
Ensure your &lt;code&gt;package.json&lt;/code&gt; includes the following scripts:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react-scripts start"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react-scripts build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react-scripts test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"eject"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react-scripts eject"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Make sure your scripts align with the provided commands for starting, building, testing, and ejecting your React app.&lt;/p&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Section 3: GCP Account &amp;amp; GCP Project Setup
&lt;/h1&gt;
&lt;h3&gt;
  
  
  Step 1: Create a New Project
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Cloud Console&lt;/a&gt; and sign in.&lt;/li&gt;
&lt;li&gt;Click on "Select a project" at the top and then click on "New Project."&lt;/li&gt;
&lt;li&gt;Choose a unique project name and select the desired organization if applicable.&lt;/li&gt;
&lt;li&gt;Once the project is created, note down the Project ID for future reference.&lt;/li&gt;
&lt;li&gt;Link this project to appropriate Billing Account.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Step 2: Enable Required APIs
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the &lt;strong&gt;APIs &amp;amp; Services&lt;/strong&gt; section in the Google Cloud Console.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Library&lt;/strong&gt; in the left sidebar.&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;Cloud Storage&lt;/strong&gt; and enable the &lt;strong&gt;Cloud Storage API.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;App Engine Admin&lt;/strong&gt; and enable the &lt;strong&gt;App Engine Admin API.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Step 3: Set Up Cloud Storage
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;strong&gt;Cloud Storage&lt;/strong&gt; section in the Google Cloud Console.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Create Bucket&lt;/strong&gt; to make a new bucket.&lt;/li&gt;
&lt;li&gt;Choose a unique name and select the desired region.&lt;/li&gt;
&lt;li&gt;Leave other settings as default and create the bucket.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Step 4: Install GCP CLI
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the root directory of your React app in the terminal.&lt;/li&gt;
&lt;li&gt;Install the Google Cloud SDK following the instructions at &lt;a href="https://cloud.google.com/sdk/docs/install" rel="noopener noreferrer"&gt;Google Cloud SDK Installation Guide&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Authenticate the Google Cloud SDK by running &lt;code&gt;gcloud auth login&lt;/code&gt; and following the on-screen instructions.&lt;/li&gt;
&lt;li&gt;Set the project ID by running &lt;code&gt;gcloud config set project PROJECT_ID&lt;/code&gt;, replacing PROJECT_ID with your actual Project ID.&lt;/li&gt;
&lt;/ol&gt;






&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Service Account Setup
&lt;/h1&gt;
&lt;h4&gt;
  
  
  Generate &amp;amp; Download a Service Account
&lt;/h4&gt;

&lt;p&gt;To set up a Service Account for Google App Engine deployment, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Cloud Console&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Navigate to &lt;strong&gt;IAM &amp;amp; Admin&lt;/strong&gt; → &lt;strong&gt;Service Accounts&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Create Service Account&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Provide an appropriate name and description for the service account. For instance, use &lt;code&gt;github-ci-cd&lt;/code&gt; as it will be utilized for Github CI/CD.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Assign the following roles:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;App Engine Admin&lt;/li&gt;
&lt;li&gt;Cloud Build Service Account&lt;/li&gt;
&lt;li&gt;Service Account User&lt;/li&gt;
&lt;li&gt;Storage Object Admin&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click the three dots and select &lt;strong&gt;Manage keys&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on &lt;strong&gt;Add Key → Create New Key&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose the &lt;strong&gt;JSON key&lt;/strong&gt; type and securely download it. Remember, this key grants access to Google Cloud resources, so keep it safe.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Step 4: Create app.yaml and Explanation
&lt;/h1&gt;
&lt;h4&gt;
  
  
  What is app.yaml?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;app.yaml&lt;/code&gt; file configures settings and routing for Google App Engine applications.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Placement:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Keep the &lt;code&gt;app.yaml&lt;/code&gt; in your project's root directory alongside your source code.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Example Configuration:
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# [START app_yaml]&lt;/span&gt;

&lt;span class="na"&gt;runtime&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nodejs18&lt;/span&gt;
&lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;my-react-app&lt;/span&gt; &lt;span class="c1"&gt;# prefix/subdomain of the bucket specific to project and environment&lt;/span&gt;

&lt;span class="na"&gt;handlers&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/static/js/(.*)&lt;/span&gt;
    &lt;span class="na"&gt;static_files&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build/static/js/\1&lt;/span&gt;
    &lt;span class="na"&gt;upload&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build/static/js/(.*)&lt;/span&gt;
    &lt;span class="na"&gt;secure&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always&lt;/span&gt;

  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/static/css/(.*)&lt;/span&gt;
    &lt;span class="na"&gt;static_files&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build/static/css/\1&lt;/span&gt;
    &lt;span class="na"&gt;upload&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build/static/css/(.*)&lt;/span&gt;
    &lt;span class="na"&gt;secure&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always&lt;/span&gt;

  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/static/media/(.*)&lt;/span&gt;
    &lt;span class="na"&gt;static_files&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build/static/media/\1&lt;/span&gt;
    &lt;span class="na"&gt;upload&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build/static/media/(.*)&lt;/span&gt;
    &lt;span class="na"&gt;secure&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always&lt;/span&gt;

  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/(.*\.(json|ico|png|jpg|svg))$&lt;/span&gt;
    &lt;span class="na"&gt;static_files&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build/\1&lt;/span&gt;
    &lt;span class="na"&gt;upload&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build/.*\.(json|ico|png|jpg|svg)$&lt;/span&gt;
    &lt;span class="na"&gt;secure&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always&lt;/span&gt;

  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/&lt;/span&gt;
    &lt;span class="na"&gt;static_files&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build/index.html&lt;/span&gt;
    &lt;span class="na"&gt;upload&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build/index.html&lt;/span&gt;
    &lt;span class="na"&gt;secure&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always&lt;/span&gt;

  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/.*&lt;/span&gt;
    &lt;span class="na"&gt;static_files&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build/index.html&lt;/span&gt;
    &lt;span class="na"&gt;upload&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build/index.html&lt;/span&gt;
    &lt;span class="na"&gt;secure&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always&lt;/span&gt;
&lt;span class="c1"&gt;# [END app_yaml]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Explanation of Configuration:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;runtime:&lt;/strong&gt; Specifies the runtime environment (e.g., &lt;code&gt;nodejs18&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;service:&lt;/strong&gt; Defines the service name, typically a project-specific prefix or subdomain.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;handlers&lt;/code&gt; section defines how incoming requests are handled:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each &lt;code&gt;url&lt;/code&gt; pattern specifies a path or file type.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;static_files&lt;/code&gt; points to the location of the static file in your project.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;upload&lt;/code&gt; indicates where the file should be uploaded within the Google App Engine environment.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;secure&lt;/code&gt; ensures that requests are served over HTTPS.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This configuration example directs incoming requests to the appropriate static files within the &lt;code&gt;build&lt;/code&gt; directory, ensuring proper handling and security for various file types and paths.&lt;/p&gt;
&lt;h4&gt;
  
  
  Deploy your App Locally:
&lt;/h4&gt;

&lt;p&gt;Deploy your React app by executing the command in root directory of your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcloud app deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will package and upload your compiled build to Google Cloud. The deployment process may take a few minutes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When you are deploying for &lt;strong&gt;first time&lt;/strong&gt;, comment out line 4, where you specify service name. As first deployment must be of default service.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After deployment completion, you'll receive a URL where your React app is hosted. Open the URL to check the deployed React App.&lt;/p&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 5: CI/CD using GitHub Actions
&lt;/h1&gt;

&lt;h3&gt;
  
  
  CI/CD and GitHub Actions:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CI/CD:&lt;/strong&gt; CI (Continuous Integration) and CD (Continuous Deployment) automate the software delivery process. CI involves merging code changes into a shared repository frequently, automatically running tests, and validating changes. CD automates the deployment of validated code changes to production or staging environments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CI/CD in GitHub Actions:&lt;/strong&gt; GitHub Actions provide workflows to automate tasks in your repository, including CI/CD processes. These workflows are defined in YAML files and can be triggered by various events like code pushes, pull requests, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Storing Service Account Key and Project ID in GitHub Secrets:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Store your Service Account Key and Project ID as secrets in your GitHub repository to securely access them during the CI/CD process.&lt;/li&gt;
&lt;li&gt;You can create Github secrets by &lt;strong&gt;Github Repository → Settings → Secrets &amp;amp; Variables → Actions → Secrets tab → New Repository Secret&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Secrets:

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;GCP_SA_KEY&lt;/code&gt;: Your entire JSON of Service Account Key generated in previous step.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GCP_PROJECT_ID&lt;/code&gt; Your GCP Project ID.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Workflow File Name &amp;amp; Placement:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The workflow file should be named &lt;code&gt;gcp-deploy.yml&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Place this file in the &lt;code&gt;.github/workflows&lt;/code&gt; directory in your project. Refer the below given repository incase of any confusion.&lt;/li&gt;
&lt;li&gt;Paste the below configuration in recently created yml file.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Configuration
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to GCP&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;main&lt;/span&gt;
  &lt;span class="na"&gt;pull_request&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;main&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;build-and-deploy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;

    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Checkout repository&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v2&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Setup Node.js and yarn&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/setup-node@v2&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;node-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;18'&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Install dependencies&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;yarn install&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Build React app&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;yarn run build&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Create temp folder&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mkdir temp_folder&lt;/span&gt;

      &lt;span class="c1"&gt;# This will create a temporary folder&lt;/span&gt;
      &lt;span class="c1"&gt;# It will have build folder and app.yaml&lt;/span&gt;
      &lt;span class="c1"&gt;# app.yaml will use the relative build folder to deploy to GCPs&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Copy build to temp folder&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cp -r build/ temp_folder/build/&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Copy app.yaml to temp folder&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cp app.yaml temp_folder/app.yaml&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;List contents of temp folder for verification&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ls -l temp_folder/&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Google Cloud Auth&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;google-github-actions/auth@v2'&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;credentials_json&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;${{&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;secrets.GCP_SA_KEY&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;}}'&lt;/span&gt;
          &lt;span class="na"&gt;project_id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.GCP_PROJECT_ID }}&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Set up Cloud SDK&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;google-github-actions/setup-gcloud@v2'&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to Google Cloud Storage&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;cd temp_folder&lt;/span&gt;
          &lt;span class="s"&gt;gcloud app deploy app.yaml --quiet&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;This workflow triggers on pushes or pull requests to the &lt;code&gt;main&lt;/code&gt; branch. It uses &lt;strong&gt;GitHub Actions&lt;/strong&gt; to perform various steps such as building the React app, authenticating with Google Cloud, setting up the Cloud SDK, and deploying the app to Google Cloud Platform.&lt;/p&gt;




&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 6: Testing the CI/CD
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Push Your Changes to GitHub (on Main Branch):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ensure your latest changes are pushed to the main branch of your GitHub repository.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Navigate to the 'Actions' Tab in GitHub Repository:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Visit your GitHub repository and go to the 'Actions' tab.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check Workflow Status:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Verify the status of your React app deployment workflow using GitHub Actions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open the Deployed App URL:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Access the provided URL after a successful deployment in your browser to confirm your React application runs smoothly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In GitHub Actions logs, the URL might contain masked text (*&lt;strong&gt;* text) representing your GCP project ID. Replace **&lt;/strong&gt; with your project ID and open the URL in your browser.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Access Google Cloud Engine Services:&lt;/strong&gt;&lt;br&gt;
Go to Google Cloud, locate and click on App Engine, then navigate to &lt;strong&gt;Services&lt;/strong&gt; in the sidebar. Find your recent deployments and click on the service to open your React App in a new tab.&lt;/p&gt;



&lt;h1&gt;
  
  
  GitHub Repository Link
&lt;/h1&gt;

&lt;p&gt;Access the complete code reference for this guide by visiting the &lt;a href="https://github.com/rushi-2001/react-app-engine-setup" rel="noopener noreferrer"&gt;GitHub Repository&lt;/a&gt;.&lt;br&gt;
Feel free to clone the repository and experiment with it on your own!&lt;/p&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;🚀 Congratulations on mastering the deployment of your React app to Google App Engine and GitHub Actions! 🎉&lt;/p&gt;

&lt;p&gt;Embrace this automated workflow to streamline your development journey and propel your projects to new heights. You can refer the above given &lt;strong&gt;repository&lt;/strong&gt; link as and when required. If you have any queries or need guidance, feel free to chat or &lt;strong&gt;drop a comment.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Keep coding, exploring, and sharing the joy of efficient deployments! Don't forget to &lt;strong&gt;like and share this guide&lt;/strong&gt; to inspire others on their deployment adventures! 👍&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding! 💻✨&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>googlecloud</category>
      <category>react</category>
      <category>cicd</category>
      <category>githubactions</category>
    </item>
  </channel>
</rss>
