<?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: 𝙱𝚛𝚎𝚝𝚝</title>
    <description>The latest articles on DEV Community by 𝙱𝚛𝚎𝚝𝚝 (@brettcnelson).</description>
    <link>https://dev.to/brettcnelson</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%2F163318%2F5f93e594-445a-41c7-afbc-5435020d0420.png</url>
      <title>DEV Community: 𝙱𝚛𝚎𝚝𝚝</title>
      <link>https://dev.to/brettcnelson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brettcnelson"/>
    <language>en</language>
    <item>
      <title>Getting Create React App to Work with Custom Domains on GitHub User Project Pages</title>
      <dc:creator>𝙱𝚛𝚎𝚝𝚝</dc:creator>
      <pubDate>Sun, 05 May 2019 06:32:19 +0000</pubDate>
      <link>https://dev.to/brettcnelson/getting-create-react-app-to-work-with-custom-domains-on-github-user-project-pages-2hp2</link>
      <guid>https://dev.to/brettcnelson/getting-create-react-app-to-work-with-custom-domains-on-github-user-project-pages-2hp2</guid>
      <description>&lt;p&gt;I recently deployed an app made with &lt;a href="https://github.com/facebook/create-react-app"&gt;Create React App&lt;/a&gt; to &lt;a href="https://pages.github.com/"&gt;GitHub Pages&lt;/a&gt;. I ran into a problem getting the build assets to be served correctly and thought this quick post might help someone who runs into the same issue.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;tldr: change the &lt;code&gt;homepage&lt;/code&gt; in &lt;code&gt;package.json&lt;/code&gt; from &lt;code&gt;[username].github.io/[project]&lt;/code&gt; to your custom domain so that the build assets are served at root rather than at &lt;code&gt;/[project]&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This post assumes you have a working User Project Page at &lt;code&gt;[username].github.io/[project]&lt;/code&gt;. If your app is at the default user page - &lt;code&gt;[username].github.io&lt;/code&gt; - you won't have this problem.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://help.github.com/en/articles/using-a-custom-domain-with-github-pages"&gt;GitHub Help docs&lt;/a&gt; are very thorough, and you should not have much of a problem configuring your DNS records or adding a custom domain to your repo settings. I added the custom subdomain &lt;code&gt;www.[site].[domain]&lt;/code&gt; to the settings in my project repo. I then set up an &lt;a href="https://help.github.com/en/articles/setting-up-an-apex-domain-and-www-subdomain"&gt;apex domain and &lt;code&gt;www&lt;/code&gt; subdomain&lt;/a&gt; by adding 4 &lt;a href="https://help.github.com/en/articles/setting-up-an-apex-domain"&gt;A records&lt;/a&gt; pointing to GitHub's IP addresses and a &lt;a href="https://help.github.com/en/articles/setting-up-a-www-subdomain"&gt;CNAME record for the &lt;code&gt;www&lt;/code&gt; subdomain&lt;/a&gt; pointing to &lt;code&gt;[username].github.io&lt;/code&gt;. You may configure your records differently, but as long as your (sub)domain is pointing to your project page, the rest of this post will still be relevant if you can't get your assets served.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: If you are using the &lt;a href="https://www.npmjs.com/package/gh-pages"&gt;gh-pages&lt;/a&gt; package to push your build folder to the gh-pages branch of your repo, it will overwrite the CNAME record added to that branch when you entered your custom domain on the settings page. I added &lt;code&gt;www.[site].[domain]&lt;/code&gt; to a &lt;code&gt;CNAME&lt;/code&gt; file in the &lt;code&gt;public&lt;/code&gt; folder of my master branch. That way Create React App adds it to the build.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I thought that is all I would have to do to get my domain working. But when I went to &lt;code&gt;www.[site].[domain]&lt;/code&gt;, the page didn't load. Checking the console showed that my &lt;code&gt;index.html&lt;/code&gt; was served, so my DNS records with GitHub and my domain provider were working. But the script tags pointed to &lt;code&gt;/[project]/static&lt;/code&gt; rather than just &lt;code&gt;/static&lt;/code&gt;. Similarly, the page was looking for my favicon at &lt;code&gt;/[project]&lt;/code&gt; rather than root. Why had the build been correctly served at &lt;code&gt;[username].github.io/[project]&lt;/code&gt; only to break after adding my custom domain?&lt;/p&gt;

&lt;p&gt;The problem is that Create React App creates an environment variable, &lt;code&gt;PUBLIC_URL&lt;/code&gt;, to set the path to assets. And, crucially, it uses the &lt;code&gt;homepage&lt;/code&gt; value in &lt;code&gt;package.json&lt;/code&gt; to determine if any subdirectories should be prepended to paths. When the site was at &lt;code&gt;[username].github.io/[project]&lt;/code&gt;, having &lt;code&gt;homepage&lt;/code&gt; set to that url didn't cause a problem because the build folder was located at &lt;code&gt;/[project]&lt;/code&gt;. The page would look for assets at &lt;code&gt;/[project]/static&lt;/code&gt;, for example, and they were there.&lt;/p&gt;

&lt;p&gt;But when the site moved to &lt;code&gt;www.[site].[domain]&lt;/code&gt;, the build folder became located at root. The page was still looking for assets at &lt;code&gt;/[project]/static&lt;/code&gt;, but there wasn't a &lt;code&gt;/[project]&lt;/code&gt; directory in the build folder, obviously.&lt;/p&gt;

&lt;p&gt;Changing the &lt;code&gt;homepage&lt;/code&gt; in &lt;code&gt;package.json&lt;/code&gt; from &lt;code&gt;[username].github.io/[project]&lt;/code&gt; to the custom domain now causes the page to look for assets in the right place, and the site loads.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In fact, I think you can set &lt;code&gt;homepage&lt;/code&gt; to any url that doesn't contain subdirectories (assuming your build is at root) and it will work - though that's obviously a bad idea for other reasons.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is another &lt;a href="https://medium.com/@shushugah/2-customs-domains-for-2-github-pages-a8a9e77d3e57"&gt;quirk&lt;/a&gt; of GitHub serving your page at a subdirectory.&lt;/p&gt;

</description>
      <category>github</category>
      <category>react</category>
    </item>
  </channel>
</rss>
