<?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: Christian</title>
    <description>The latest articles on DEV Community by Christian (@ckastbjerg).</description>
    <link>https://dev.to/ckastbjerg</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%2F604264%2F15a9c652-c555-43ca-b9c2-9e1b9e10a467.png</url>
      <title>DEV Community: Christian</title>
      <link>https://dev.to/ckastbjerg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ckastbjerg"/>
    <language>en</language>
    <item>
      <title>How to eliminate dead links in your Next.js app</title>
      <dc:creator>Christian</dc:creator>
      <pubDate>Fri, 26 Mar 2021 22:48:27 +0000</pubDate>
      <link>https://dev.to/ckastbjerg/why-we-don-t-have-dead-links-at-proper-anymore-24n3</link>
      <guid>https://dev.to/ckastbjerg/why-we-don-t-have-dead-links-at-proper-anymore-24n3</guid>
      <description>&lt;p&gt;At my company, &lt;a href="https://helloproper.com/" rel="noopener noreferrer"&gt;Proper&lt;/a&gt;, we like pages. Like...a lot! Our platform is a fairly large Next.js application consisting of ~70 pages. And we link between pages ~200 places in the application.&lt;/p&gt;

&lt;p&gt;We find that having pages make features easily discoverable by end-users and developers alike. And having pages (urls) for each of our features help us maintain a sane information architecture throughout our platform.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://nextjs.org/docs/routing/introduction" rel="noopener noreferrer"&gt;Next.js file-system based router&lt;/a&gt; help us stay consistent and organised around our pages. But we've had some incidents where our application was released with dead links. &lt;/p&gt;

&lt;p&gt;At one point, a file in the &lt;code&gt;/pages&lt;/code&gt; folder was renamed and we simply overlooked (forgot to change) some of the links to that page. Another time, a bit of "clever" string concatenation caused an issue. In this case, we had moved a page, and failed to update all links to the page correctly due to the concatenated links.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;code&gt;next-type-safe-routes&lt;/code&gt; to the rescue
&lt;/h1&gt;

&lt;p&gt;To mitigate this issue, I created &lt;a href="https://github.com/ckastbjerg/next-type-safe-routes" rel="noopener noreferrer"&gt;&lt;code&gt;next-type-safe-routes&lt;/code&gt;&lt;/a&gt;. A small Next.js plugin accompanied by a couple of utility methods.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;next-type-safe-routes&lt;/code&gt; parses the &lt;code&gt;/pages&lt;/code&gt; folder in your Next.js app and generates types for all the &lt;strong&gt;pages&lt;/strong&gt; and &lt;strong&gt;API routes&lt;/strong&gt; in the application. These types can then be used to ensure that you only link to pages (and only use API routes) that &lt;em&gt;actually&lt;/em&gt; exists in your application.&lt;/p&gt;

&lt;p&gt;With the types generated, we can use the &lt;code&gt;getRoute&lt;/code&gt; utility to retrieve a link that is &lt;strong&gt;guaranteed to exist&lt;/strong&gt; in the application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getRoute&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;next-type-safe-routes&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;getRoute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/users&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// for simple routes&lt;/span&gt;
&lt;span class="nf"&gt;getRoute&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;route&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/users/[userId]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// for dynamic routes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And with a tiny abstraction on top of the &lt;code&gt;next/router&lt;/code&gt; and &lt;code&gt;next/link&lt;/code&gt;, our routing developer experience now looks something like this:&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%2Fs4.gifyu.com%2Fimages%2Fexampleb977337946e5e898.gif" 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%2Fs4.gifyu.com%2Fimages%2Fexampleb977337946e5e898.gif" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As an added benefit, we also get type-safe API routes!&lt;/p&gt;

&lt;p&gt;Having type-safety for all our links gives us confidence when refactoring as well as a top notch developer experience with auto-completed routes. I'm hoping this library might help others who face the same challenges we did 🙂&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/ckastbjerg/next-type-safe-routes" rel="noopener noreferrer"&gt;https://github.com/ckastbjerg/next-type-safe-routes&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>typescript</category>
      <category>tooling</category>
    </item>
  </channel>
</rss>
