<?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>Forem: moay</title>
    <description>The latest articles on Forem by moay (@moay).</description>
    <link>https://forem.com/moay</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%2F404167%2Fa31f22da-4e20-4164-b9f3-34d7ea53ce6e.png</url>
      <title>Forem: moay</title>
      <link>https://forem.com/moay</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/moay"/>
    <language>en</language>
    <item>
      <title>Laravel Inertia.js - Running SSR on a different port than 13714</title>
      <dc:creator>moay</dc:creator>
      <pubDate>Thu, 09 Feb 2023 21:14:12 +0000</pubDate>
      <link>https://forem.com/moay/laravel-inertiajs-running-ssr-on-a-different-port-than-13714-hnj</link>
      <guid>https://forem.com/moay/laravel-inertiajs-running-ssr-on-a-different-port-than-13714-hnj</guid>
      <description>&lt;p&gt;I am getting into &lt;a href="https://inertiajs.com/"&gt;Inertia.js&lt;/a&gt; and so far love it for its simplicity and the huge advantages it brings for building a modern website on a modern stack driven by both frontend awesomeness with a Javascript framework of your choice and the power of Laravel and PHP in the backend.&lt;/p&gt;

&lt;p&gt;One of my main concerns was whether Inertia would be a good solution in terms of SEO. Turns out: it is! 🎉 With its inbuilt SSR capabilities, it is a breeze to setup a completely server-side rendered, SEO friendly frontend that still profits from all the Inertia magic.&lt;/p&gt;

&lt;p&gt;Setting SSR up is &lt;a href="https://inertiajs.com/server-side-rendering"&gt;well documented&lt;/a&gt; and easy to do. When everything is setup, running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan inertia:start-ssr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will start the SSR server and Laravel will give you plain HTML with all the client side features still working. But: it will always run the SSR server on port 13714 and I couldn't find any existing resource showing how to change that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Changing the default SSR port
&lt;/h2&gt;

&lt;p&gt;There might me multiple reasons to change the port. Some examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The port is already taken. This is not very likely when running only one website on a server (or locally), but as soon as you have two projects running, the runners would conflict each other.&lt;/li&gt;
&lt;li&gt;The port is forbidden. Some hosters will not allow you to assign any port outside of a given range.&lt;/li&gt;
&lt;li&gt;You don't like the number. Also unlikely, but maybe you are just not a fan of numbers starting with a 13.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Changing the port to something else takes two steps. You'll need to &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;reconfigure the SSR build to make the SSR server run on the desired port and&lt;/li&gt;
&lt;li&gt;change the Laravel runtime config so that the Laravel runtime talks to the SSR server on the correct port.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is (as far as I know) not documented and I had to dive into the sourcecode for both the &lt;a href="https://github.com/inertiajs/inertia/blob/master/packages/core/src/server.ts"&gt;inertia server runner&lt;/a&gt; and the &lt;a href="https://github.com/inertiajs/inertia-laravel/blob/master/src/Commands/StartSsr.php"&gt;Laravel command to start the server&lt;/a&gt; to understand what is going on. Here we go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Changing the port for the SSR build
&lt;/h2&gt;

&lt;p&gt;Step 1: Given that you have followed the SSR setup guide, when running&lt;br&gt;
&lt;/p&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;the configuration is baked into the bundle that will be executed when starting the SSR server (you can find this bundle at &lt;code&gt;bootstrap/ssr/ssr.js&lt;/code&gt;). By default, no port is passed in, so the default port is used.&lt;/p&gt;

&lt;p&gt;In order to change what port will be used later, this has to be changed in your &lt;code&gt;resources/js/ssr.js&lt;/code&gt; file. The port can be changed by passing it &lt;strong&gt;as a second parameter to the &lt;code&gt;createServer&lt;/code&gt; function&lt;/strong&gt;. So&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;page&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;createInertiaApp&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;render&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;renderToString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;resolve&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;=&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;pages&lt;/span&gt; &lt;span class="o"&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;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;glob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Pages/**/*.vue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;eager&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;`./Pages/&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="s2"&gt;.vue`&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nx"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;plugin&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="nx"&gt;createSSRApp&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;render&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="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="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;plugin&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="mi"&gt;8080&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The change in regards to the defaults here is the &lt;strong&gt;last line&lt;/strong&gt;. I'm passing in the port as a second parameter. With this setup, run &lt;code&gt;npm run build&lt;/code&gt; again and the next time you run &lt;code&gt;php artisan inertia:start-ssr&lt;/code&gt; you will see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Starting SSR server on port 8080...
Inertia SSR server started.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awesome. But: This change alone will not do. Laravel will still try to talk to the SSR server on port 13714. Let's change this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Changing the port for the Laravel backend
&lt;/h2&gt;

&lt;p&gt;The laravel inertia adapter has a configuration file that is not published by default. If you didn't do this yet, run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan vendor:publish &lt;span class="nt"&gt;--provider&lt;/span&gt; &lt;span class="s2"&gt;"Inertia&lt;/span&gt;&lt;span class="se"&gt;\S&lt;/span&gt;&lt;span class="s2"&gt;erviceProvider"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will publish a file named &lt;code&gt;inertia.php&lt;/code&gt; to your &lt;code&gt;config&lt;/code&gt; folder. Open this and voilà, there you can change the SSR server url that Laravel is talking to. Change the URL to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'url'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'http://127.0.0.1:8080'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and you are all set. 🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  Making the port easier to configure
&lt;/h2&gt;

&lt;p&gt;Changing the port in two places seems a bit tedious, also this port should be depending on your environment. So let's make it an environment variable.&lt;/p&gt;

&lt;p&gt;In your &lt;code&gt;config/inertia.php&lt;/code&gt;, change this line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'url'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'http://127.0.0.1:8080'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'url'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'http://127.0.0.1:'&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'VITE_INERTIA_SSR_PORT'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13714&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in your &lt;code&gt;ssr.js&lt;/code&gt;, instead of hardcoding the port also inject the env variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;page&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;createInertiaApp&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="c1"&gt;// config here...&lt;/span&gt;
  &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;VITE_INERTIA_SSR_PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="mi"&gt;13714&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With both changes in place, you can now add the &lt;code&gt;VITE_INERTIA_SSR_PORT&lt;/code&gt; variable to your &lt;code&gt;.env&lt;/code&gt; file and set it there.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;VITE_INERTIA_SSR_PORT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Notes
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Yes, the &lt;code&gt;VITE_&lt;/code&gt; prefix for the env variable name is obligatory in order to make it available to the &lt;code&gt;ssr.js&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Keep in mind that the port for the SSR server is still baked into the build bundle when running &lt;code&gt;npm run build&lt;/code&gt;. Just changing the env variable will not change the port the server is running on if you do not recreate the bundle.&lt;/li&gt;
&lt;li&gt;Yes, this should be documented somewhere. If you have the power to add it to the docs, do so.&lt;/li&gt;
&lt;/ul&gt;

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