<?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: Neel Bhalodiya</title>
    <description>The latest articles on DEV Community by Neel Bhalodiya (@neel-002b).</description>
    <link>https://dev.to/neel-002b</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%2F2211511%2Fdadeb4a9-11a7-4d99-a5e8-14176c359809.png</url>
      <title>DEV Community: Neel Bhalodiya</title>
      <link>https://dev.to/neel-002b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/neel-002b"/>
    <language>en</language>
    <item>
      <title>Inertiajs Server-side Rendering (SSR) For React (Vite Setup)</title>
      <dc:creator>Neel Bhalodiya</dc:creator>
      <pubDate>Mon, 14 Oct 2024 18:04:22 +0000</pubDate>
      <link>https://dev.to/neel-002b/inertiajs-server-side-rendering-ssr-for-react-vite-setup-46he</link>
      <guid>https://dev.to/neel-002b/inertiajs-server-side-rendering-ssr-for-react-vite-setup-46he</guid>
      <description>&lt;p&gt;Before starting, ensure you're using the latest version of Inertia.js. If not, you can upgrade by following the &lt;a href="https://inertiajs.com/upgrade-guide" rel="noopener noreferrer"&gt;Upgrade Guide&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Install Required Packages
&lt;/h2&gt;

&lt;h3&gt;
  
  
  For React:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You don't need to install any specific package for SSR in React.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  For Laravel:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You need to install the SSR package for Laravel:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;composer require inertiajs/inertia-laravel&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Create an SSR File
&lt;/h2&gt;

&lt;p&gt;Create an ssr.jsx or ssr.js file inside the resources/js/ directory. The full path should look like this: resources/js/ssr.js. You can use the following command to create the file: &lt;br&gt;
&lt;code&gt;touch resources/js/ssr.js&lt;/code&gt;&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;After creating the file, you need to define the resolve and setup functions for your ssr.jsx or ssr.js.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For the exmple this shown you can check here:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  Step 3: Update vite.config.js
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Now, update your vite.config.js file by adding the SSR property. In the laravel function (after the input function), add the following line 
ssr: &lt;code&gt;'resources/js/ssr.js'&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  Step 4: Update package.json
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Next, modify your package.json file to include both a normal build and an SSR build. Replace the existing build script with:
&lt;code&gt;"build": "vite build &amp;amp;&amp;amp; vite build - ssr"&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Now, run the following build command in your terminal:&lt;br&gt;
&lt;code&gt;npm run build&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After the build completes, start the SSR service with the following Artisan command:&lt;br&gt;
&lt;code&gt;php artisan inertia:start-ssr&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 5: Update app.jsx
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;In your app.jsx, replace createRoot with hydrateRoot to enable SSR.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  Note: Potential Issues with SSR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;After completing all the steps, you may encounter errors with some React components. This is because certain npm libraries do not support SSR. For example, I faced issues with the lightgallery npm package as it didn't fully support SSR. In such cases, you can conditionally import libraries that don't work with SSR.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;With these steps, you should have a fully functional SSR setup for Inertia.js with React using Vite as your bundler.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>laravel</category>
      <category>inertiajs</category>
      <category>react</category>
      <category>ssr</category>
    </item>
  </channel>
</rss>
