<?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: zzDanDanzz</title>
    <description>The latest articles on DEV Community by zzDanDanzz (@zzdandanzz).</description>
    <link>https://dev.to/zzdandanzz</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%2F902182%2F6b78afc8-0bef-4a34-acfa-314f957fd3ea.jpg</url>
      <title>DEV Community: zzDanDanzz</title>
      <link>https://dev.to/zzdandanzz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zzdandanzz"/>
    <language>en</language>
    <item>
      <title>Deploying Next.js to Github Pages</title>
      <dc:creator>zzDanDanzz</dc:creator>
      <pubDate>Tue, 02 Aug 2022 16:53:00 +0000</pubDate>
      <link>https://dev.to/zzdandanzz/deploying-nextjs-to-github-pages-39mj</link>
      <guid>https://dev.to/zzdandanzz/deploying-nextjs-to-github-pages-39mj</guid>
      <description>&lt;p&gt;important: this will use &lt;strong&gt;next export&lt;/strong&gt; so the Next.js server won't be running and you won't have things like API routes or catch-all routes in your regular pages (for more details &lt;a href="https://nextjs.org/docs/advanced-features/static-html-export#unsupported-features"&gt;see the docs&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;(by the way, I learned all of this by looking at the &lt;a href="https://github.com/vercel/next.js/tree/canary/examples/github-pages"&gt;official Next.js example&lt;/a&gt; for usage with github pages)&lt;/p&gt;

&lt;p&gt;Ready? Let's go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Update &lt;code&gt;next.config.js&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nextConfig = {

  // ... you might have other stuff above here!
  // just add this basePath property
  basePath: 'REPOSITORY_NAME'
}

module.exports = nextConfig
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;By default when running &lt;code&gt;next export&lt;/code&gt;, Next.js expects the root of your site to be &lt;code&gt;&amp;lt;some_domain_name&amp;gt;.com/&lt;/code&gt; but that's not (always) what happens with Github Pages. If you create a Project Site (which means you just have a random project on a random repo and you wanna enable github pages for it) then your URL will be: &lt;code&gt;https://&amp;lt;your_github_username&amp;gt;.github.io/&amp;lt;name_of_repository&amp;gt;/&lt;/code&gt; and we need to take into account that extra &lt;code&gt;&amp;lt;name_of_repository&amp;gt;/&lt;/code&gt; at the end by modifying basePath in the Next config. &lt;/p&gt;

&lt;p&gt;HOWEVER, now when you run a dev server at &lt;code&gt;http://localhost:3000/&lt;/code&gt; you need to append that basePath there too! so if your repo is called 'awesome-app' and you change your basePath to be '/awesome-app' now you can access your app at &lt;code&gt;http://localhost:3000/awesome-app&lt;/code&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  Though you have to skip this step in some cases:
&lt;/h4&gt;

&lt;p&gt;for user and organization sites it will be &lt;code&gt;https://&amp;lt;your_github_username&amp;gt;.github.io.&lt;/code&gt; and from what I understand (haven’t tried it) for both the project site and user/organization site you can set up custom domains but in any case you can skip this part because the root of your site is in accordance with the default basePath.&lt;/p&gt;

&lt;h2&gt;
  
  
  build and export
&lt;/h2&gt;

&lt;p&gt;Run the following commands in order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;npx next build&lt;/code&gt; - build your app&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npx next export&lt;/code&gt; - create static export of your app&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;if all goes well, you should see an ‘out’ folder inside of which are the files github is going to serve&lt;/p&gt;

&lt;h2&gt;
  
  
  add .nojekyll file to the out/ directory
&lt;/h2&gt;

&lt;p&gt;to opt out of using jekyll (since you’re using next.js) with github pages you have to add a .nojekyll file to out/&lt;/p&gt;

&lt;p&gt;command: &lt;code&gt;touch out/.nojekyll&lt;/code&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  commit changes
&lt;/h2&gt;

&lt;p&gt;run &lt;code&gt;git add out/ -f&lt;/code&gt; then &lt;code&gt;git commit -m "Deploy"&lt;/code&gt;&lt;br&gt;
you need to add the &lt;code&gt;-f&lt;/code&gt; option because git will complain about the out/ dir being in .gitignore&lt;/p&gt;
&lt;h2&gt;
  
  
  and one last (awesome) command
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;git subtree push --prefix out origin gh-pages&lt;/code&gt;&lt;br&gt;
this will make a new branch called &lt;code&gt;gh-pages&lt;/code&gt; on the remote repository &lt;code&gt;origin&lt;/code&gt; (which is on github) and in this new branch it will have all the content inside of the out/ directory but they will all be in the root so that the files can easily be served by github from that branch.&lt;/p&gt;
&lt;h2&gt;
  
  
  make the workflow easy
&lt;/h2&gt;

&lt;p&gt;you can add this "deploy" script to your package.json&lt;br&gt;
&lt;/p&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="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;you&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;might&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;have&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;other&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;scripts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;above&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;and&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;around!&lt;/span&gt;&lt;span class="w"&gt;

    &lt;/span&gt;&lt;span class="nl"&gt;"deploy"&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 &amp;amp;&amp;amp; next export &amp;amp;&amp;amp; touch out/.nojekyll &amp;amp;&amp;amp; git add out/ -f &amp;amp;&amp;amp; git commit -m &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Deploy&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; git subtree push --prefix out origin gh-pages"&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;and run it with the command &lt;code&gt;npm run deploy&lt;/code&gt; or &lt;code&gt;yarn deploy&lt;/code&gt;&lt;/p&gt;

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