<?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: Michael Bashurov</title>
    <description>The latest articles on DEV Community by Michael Bashurov (@saitonakamura).</description>
    <link>https://dev.to/saitonakamura</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%2F41046%2Fe2e1fbc6-8759-4060-856a-2fb366e3baf6.jpg</url>
      <title>DEV Community: Michael Bashurov</title>
      <link>https://dev.to/saitonakamura</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saitonakamura"/>
    <language>en</language>
    <item>
      <title>Next.js app on Github Pages</title>
      <dc:creator>Michael Bashurov</dc:creator>
      <pubDate>Tue, 20 Feb 2018 18:37:52 +0000</pubDate>
      <link>https://dev.to/saitonakamura/nextjs-app-on-github-pages--541f</link>
      <guid>https://dev.to/saitonakamura/nextjs-app-on-github-pages--541f</guid>
      <description>&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ANZ7KxwBB1NSuVsJP4FeMnQ.jpeg" 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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ANZ7KxwBB1NSuVsJP4FeMnQ.jpeg" alt="Next.js + Github Pages"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is a cross-post from &lt;a href="https://medium.com/@saitonakamura/next-js-app-on-github-pages-768020f2b65e" rel="noopener noreferrer"&gt;my Medium&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Recently, I’ve decided to finally create my homepage. For this task I decided to go with Next.js and it’s amazing &lt;a href="https://github.com/zeit/next.js/#static-html-export" rel="noopener noreferrer"&gt;static export functionality&lt;/a&gt;. Without thinking twice I also decided to use github pages, since the fact that I’ll github. And I was like: “It should be plain and simple, just export the app into the &lt;code&gt;docs&lt;/code&gt; folder (with the &lt;code&gt;next build&lt;/code&gt; and &lt;code&gt;next export -o docs&lt;/code&gt;) and you’re done”. Those, who are already giggling, are actually right, because it turned out to be not a piece of a cake.&lt;/p&gt;

&lt;p&gt;First of all we have &lt;a href="https://github.com/zeit/next.js/wiki/Deploying-a-Next.js-app-into-GitHub-Pages" rel="noopener noreferrer"&gt;this guide&lt;/a&gt; (which actually missed some crucial information, which I’ve added, but it’s better to proceed reading the post). The first important part is &lt;code&gt;.nojekyll&lt;/code&gt; file, because Jekyll ignores folders, that starts with underscores and we have &lt;code&gt;_next&lt;/code&gt;. So you should add empty file called &lt;code&gt;.nojekyll&lt;/code&gt; in your docs folder. Don’t worry, it won’t be deleted upon &lt;code&gt;next export&lt;/code&gt;. You also have an option to use &lt;code&gt;gh-pages&lt;/code&gt; branch (there is a cool git feature called subtree for it, &lt;a href="https://gist.github.com/cobyism/4730490" rel="noopener noreferrer"&gt;see here&lt;/a&gt;). So it should be ok, right?…&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2Aw0IMUgxXUqA-Nutq6VLU2g.jpeg" 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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2Aw0IMUgxXUqA-Nutq6VLU2g.jpeg" alt="Cuphead Cagney Carnation boss"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  First problem
&lt;/h1&gt;

&lt;p&gt;If it isn’t your personal or organization page, your github page will be published under &lt;code&gt;http://{username}.github.io/{reponame}&lt;/code&gt;. That means that by default links to the scripts will be broken. But no worries, &lt;code&gt;next.js&lt;/code&gt; has an option to fix it, it’s called &lt;code&gt;assetPrefix&lt;/code&gt;. The catch is that you don’t wanna clear the &lt;code&gt;assetPrefix&lt;/code&gt; for local development, so it’s better to rely on &lt;code&gt;process.env.NODE_ENV&lt;/code&gt;, or whatever you use to distinguish dev from prod.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AYAeOvAWO34gc2u6HNXad-A.jpeg" 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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AYAeOvAWO34gc2u6HNXad-A.jpeg" alt="Cuphead Cala Maria boss"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  And we proceed to the next one
&lt;/h1&gt;

&lt;p&gt;You’ve updated to paths to static files, but what about links? We should also add prefix to them. The easiest way to pass the prefix to the components and pages would be using &lt;code&gt;webpack.DefinePlugin&lt;/code&gt; and webpack config function (Next.js version ≥ 5).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Next.js also have an option to use &lt;code&gt;env-config.js&lt;/code&gt;, but I don’t see the point of splitting the one concern into two files.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And then we use it like this. Important part is that we don’t simply prefixing it to the &lt;code&gt;href&lt;/code&gt;, but using &lt;code&gt;as&lt;/code&gt; prop. Otherwise this won’t be a client-side navigation, but full page reload. I wonder what‘s the reasons of this (see &lt;a href="https://github.com/zeit/next.js/issues/3335#issuecomment-348802894" rel="noopener noreferrer"&gt;this thread&lt;/a&gt; for more information).&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You could also create a simple wrapper for it.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ARwxavy5zVS8Qm30hkbSoGw.jpeg" 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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ARwxavy5zVS8Qm30hkbSoGw.jpeg" alt="Cuphead Devil boss"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Bonus problem
&lt;/h1&gt;

&lt;p&gt;If you’re using Next.js v5 universal webpack configuration features, like &lt;code&gt;withCss&lt;/code&gt; decorator that allows you to use old-style css approach (plain css, css-modules, sass, less, post-css) that creates separate file, you may want to use &lt;code&gt;_document.js&lt;/code&gt; file to fine-tune the template that Next.js will be using to render your page. But in case of &lt;code&gt;assetPrefix&lt;/code&gt; you may also want to alter the url for this script. Fortunately you can get the &lt;code&gt;assetPrefix&lt;/code&gt; from props (&lt;code&gt;this.props.__NEXT_DATA__.assetPrefix&lt;/code&gt;), like this.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AaMjZLxnhwjYnn1v-DYJk7A.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AaMjZLxnhwjYnn1v-DYJk7A.png" alt="Cuphead "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for the attention, hope you won’t be facing another bunch of whatever problems soon!&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>javascript</category>
      <category>githubpages</category>
    </item>
  </channel>
</rss>
