<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Paul on Medium]]></title>
        <description><![CDATA[Stories by Paul on Medium]]></description>
        <link>https://medium.com/@akellbl4?source=rss-357af7270bd3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*hpoZy9tkgHM_nvCULMGqug.png</url>
            <title>Stories by Paul on Medium</title>
            <link>https://medium.com/@akellbl4?source=rss-357af7270bd3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 14 Nov 2026 18:49:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@akellbl4/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[An Overview on the Current State of Next.js Router]]></title>
            <link>https://javascript.plainenglish.io/an-overview-on-current-state-of-next-js-router-df42f02c6466?source=rss-357af7270bd3------2</link>
            <guid isPermaLink="false">https://medium.com/p/df42f02c6466</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Paul]]></dc:creator>
            <pubDate>Tue, 08 Jun 2021 17:46:34 GMT</pubDate>
            <atom:updated>2021-06-09T06:24:31.238Z</atom:updated>
            <content:encoded><![CDATA[<h4>These tips and tricks for you on the Next.js router help make your DX better and code cleaner</h4><blockquote>Originally posted in <a href="https://pavel.mineev.me/blog/nextjs-router-tips-and-tricks">my blog at pavel.mineev.me</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mh7tBNWy5zeZa4D6QTBXgw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@dnevozhai">Denys Nevozhai</a> on Unsplash</figcaption></figure><h4>Disclaimer</h4><p>Next.js is a dynamically developed framework and a lot of old articles about it could be outdated because Next.js made a huge leap forward. I wrote this article in May 2021 and now the current version is 10.2.3. I suggest checking the <a href="https://nextjs.org/docs/getting-started">docs</a> while you look at examples because some of the tips could be outdated due to improved API.</p><h3>Features Were Released You Probably Don’t Know</h3><p>If you use Next.js for a long time, your projects should have a huge amount of code, and you just keep writing it in the style that it already has. Sometimes because you’re used to it and sometimes because you use your codebase as a catalog of snippets and examples. That’s why you can skip the new cool stuff.</p><h4>Server redirects and not found status</h4><p>These features, as well as previous ones, were released in version 10 and I still see a lot of questions about how to make redirect on the server. First of all, I suggest checking if you use modern fetching methods because only they get updates and new features. I explained <a href="https://pavel.mineev.me/blog/why-i-got-rid-of-getinitialprops-in-my-nextjs-project">here</a> other reasons as to why we should use modern fetching methods.</p><p>Here is a simple way to make redirect or show page 404:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08af270a925f1d308bb5b221118d5fb5/href">https://medium.com/media/08af270a925f1d308bb5b221118d5fb5/href</a></iframe><h4>Automatic resolving of href param</h4><p>Try to find next/link in your project that has href and as prop as well. If you’ve found it, it&#39;s time to update that links. Because starting with the 10.x version, you don&#39;t need to pass both of the params. You can drop href param and rename as to href, and it will work just as well.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84c7e123ca7ee712938214cbe1548743/href">https://medium.com/media/84c7e123ca7ee712938214cbe1548743/href</a></iframe><h3>Client Router Functions</h3><p>I noticed that people often use router.push from useRouter. If you use it in functions that were wrapped in useCallback or just inside useEffect you have to mention it in dependencies like in the example down below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/872ad65b352b2bd018e7b88829f482b6/href">https://medium.com/media/872ad65b352b2bd018e7b88829f482b6/href</a></iframe><p>It’s completely unnecessary because these functions don’t update over re-renders but ESlint rules will tell you that you should do it. Solutions for this are fairly easy. Just use routing functions from the Router. It makes your code cleaner and nicer. 😉</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dbf532da639a04ad1c5652dc1c20c320/href">https://medium.com/media/dbf532da639a04ad1c5652dc1c20c320/href</a></iframe><p>Sometimes when I work with the router I need to get the current pathname. But react/router stores the dynamic representation of the router in that field, and it&#39;s the same as the router field. We can create these params by ourselves with asPath. I created a wrapper over useRouter and always have access to the props If I need them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed4b5dc784b9ebe10f1ad6ed99a2dfd5/href">https://medium.com/media/ed4b5dc784b9ebe10f1ad6ed99a2dfd5/href</a></iframe><p>The wrapper gives us handy props that we can use in a situation when we need to update a current route.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5ffe831dbf9085c54a69a03f555b582/href">https://medium.com/media/f5ffe831dbf9085c54a69a03f555b582/href</a></iframe><p>Do you wonder why the second param in Router.replace is null? Since 10.x, with automatic resolving, we can skip that param.</p><p>Since we have such a feature as automatic resolving we can use it in next/router, but it is a small inconvenience in its API. Because if you want to pass options you should pass dynamic route to the first argument and full path to the second in earlier versions and Next.js kept those 3 arguments for backward compatibility. I have small handy functions that help me to use router functions easier.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e8417776f55d13b8be2b85df25f0b11/href">https://medium.com/media/9e8417776f55d13b8be2b85df25f0b11/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f1d9beeeded41307d3143977637439e/href">https://medium.com/media/1f1d9beeeded41307d3143977637439e/href</a></iframe><h3>Conclusion</h3><p>Thank you for reading the article till the end. You can find the complete code on <a href="https://github.com/akellbl4/nextjs-router-tips">GitHub</a>. There is always some way to make good tools better. Small improvements could help you and your project a lot.</p><p><em>More content at </em><a href="http://plainenglish.io/"><em>plainenglish.io</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df42f02c6466" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/an-overview-on-current-state-of-next-js-router-df42f02c6466">An Overview on the Current State of Next.js Router</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why I Got Rid of getInitialProps in My Next.js Project]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/better-programming/why-i-got-rid-of-getinitialprops-in-my-next-js-project-fc926e98ed61?source=rss-357af7270bd3------2"><img src="https://cdn-images-1.medium.com/max/2600/1*AvxmP7KcNm9XTpcrZyoEcg.jpeg" width="5283"></a></p><p class="medium-feed-snippet">And how you can do the same in your project with a modern data-fetching API</p><p class="medium-feed-link"><a href="https://medium.com/better-programming/why-i-got-rid-of-getinitialprops-in-my-next-js-project-fc926e98ed61?source=rss-357af7270bd3------2">Continue reading on Better Programming »</a></p></div>]]></description>
            <link>https://medium.com/better-programming/why-i-got-rid-of-getinitialprops-in-my-next-js-project-fc926e98ed61?source=rss-357af7270bd3------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc926e98ed61</guid>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Paul]]></dc:creator>
            <pubDate>Tue, 09 Mar 2021 15:32:30 GMT</pubDate>
            <atom:updated>2021-03-09T15:32:30.407Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Fetch Shared Data in Next.js With Single Request]]></title>
            <link>https://itnext.io/fetch-shared-data-in-next-js-with-single-request-833433fa8ed1?source=rss-357af7270bd3------2</link>
            <guid isPermaLink="false">https://medium.com/p/833433fa8ed1</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[static-site-generator]]></category>
            <category><![CDATA[cache]]></category>
            <dc:creator><![CDATA[Paul]]></dc:creator>
            <pubDate>Fri, 05 Mar 2021 21:57:00 GMT</pubDate>
            <atom:updated>2021-03-07T15:44:59.577Z</atom:updated>
            <content:encoded><![CDATA[<h4>Take a look at caching strategies with NodeJS layer in front of Next.js</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*MiKzv9CkoZJBQhrrnUVIaQ@2x.png" /><figcaption>Photo by <a href="https://unsplash.com/@gordonplant">Gordon Plant</a> on <a href="https://unsplash.com/">Unsplash</a></figcaption></figure><p>While reviewing my current project’s build process, I noticed that generating pages with getStaticProps yields many requests to one endpoint. It’s not a big problem in my case since our backend is quite fast but googling this issue showed that some people are struggling with it. For example, the issue might be serious in case of a slow database or when the backend can’t cope with 100 requests per second. So I decided to try solving this problem and improving the build time of my project.</p><h3>Why So Many Requests?</h3><p>Before diving deep into Next.js’s build process let’s look how another static site generator Gatsby — does a similar thing. Gatsby has a very different mechanism as it does SSG only when Next.js is started as an SSR framework.</p><p>Gatsby has several building process steps, but the main two are: pulling data and populating components with data, and then rendering. Gatsby plugins usually use GraphQL data layer for storing data from data sources (surely you can build without GraphQL data layer and use unstructured data). Gatsby runs data plugins, then they fetch data and put it on the data layer. Then it gets GraphQL queries from pages and prepares data for templates. After that populates components with data. This way of building pages seems simple and straightforward. But Next.js works the other way.</p><p>As I have said before, Next.js originally was SSR-only framework with SSG feature added later. It has API where you get data and pass it to every page, but this method breaks SSG optimization. I’m talking about getInitialData when it is used inside _app.js</p><p>To have SSG pages we use only getStaticProps or just make pages without data fetching. When Next.js builds pages it runs data fetching function if it is inside of the page file and then renders a page with this data. And the process is repeated page after page. So if data’s shared, it will be requested every time for every page. If the number of pages in the application is increasing then the number of requests is also growing.</p><p>My app requires showing the list of cities on every page and these cities should be defined on server. I knew how Gatsby worked and wanted to add a similar mechanism but simpler and without GraphQL 🙂</p><h3>My First Approach</h3><p>The first idea was really simple. When the first page calls fetchCities for example, I save the promise returned by fetch to module scoped variable. When the next page calls fetchCities I check if our variable points to the promise and return it if so.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2b528cbe55c19c69e79e15f188a6cc4/href">https://medium.com/media/e2b528cbe55c19c69e79e15f188a6cc4/href</a></iframe><p>The concept is simple. We just save request promise in module scope and then return it to all the next calls. Problem is — this approach doesn’t work. The reason is workers. Next.js runs each page build in its own worker. It means that I’m not able to share data between pages and I don’t have access to a place where I can share data between workers without huge effort. Also, I check if our request works in CI because fetchCities could be called on revalidation and I want to get fresh data every time in this case.</p><h3>Maybe Google Could Help?</h3><p>I had a couple more ideas but I decided to google it. Maybe I’m not the first one who is facing this problem. I found other people who thought about the same question and they had different reasons to do it. Somebody just started using Next.js and its backend wasn’t able to serve a decent amount of requests. Other people tried to make transition from Gatsby to Next and realized their backend wasn’t too fast for 300 RPS. I knew how Gatsby worked because I faced it on the previous job. Gatsby does this differently. First of all, prepares building by collecting data to local cache, and in this case all resources are fetched only once. So that every page gets data from this cache. My idea was similar but I didn’t need to add cache on top of Next.js.</p><h3>Cache Data in Files</h3><p>One of the potential ways to solve the problem is caching in files. We need to add one more step to our build process and write something like this.</p><p>We run the script that fetches data and saves it to files. The disadvantage of this method is that we need to collect all endpoints in it for data fetching.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28651fe6ff92c8d7935b42c84dd3c74c/href">https://medium.com/media/28651fe6ff92c8d7935b42c84dd3c74c/href</a></iframe><p>Once data is collected, we can run server which will serve cached data for building our application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12172c689ead9504fadbc7dfbb5b40da/href">https://medium.com/media/12172c689ead9504fadbc7dfbb5b40da/href</a></iframe><p>These options are good but have their own cons. For example, we should collect all endpoints for caching and update them. Or you should wait till all resources are cached. On the other hand, all resources are cached on disk and don’t stay in memory. I wanted to share this piece of code just to show you all the options.</p><h3>Cache It Right in Memory on Build</h3><p>Yes, it’s the easiest and the fastest way to cache your data and not wait for anything. Just pass different API_PATH on building stage and run cache server before you run next build.</p><p>Code of our server could look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51c737785463fefc8c5d82f4d0d39d50/href">https://medium.com/media/51c737785463fefc8c5d82f4d0d39d50/href</a></iframe><p>The idea is the same as the first one – we just store data on a different level. All requests will be calling our cache proxy. When the request is received it is stored in a map and all subsequent requests to the same resource will get the same response. It should be really fast but if you request a lot of data you should be careful about memory. Otherwise, you can make a hybrid of these two approaches by caching data just in time and store it on disk instead of memory.</p><h3>Conclusion</h3><p>Actually, Next.js team planned to add the ability for using getStaticProps in _app.js. After that, the whole problem could be solved without any additional effort. I keep an eye on this <a href="https://github.com/vercel/next.js/discussions/10949#discussioncomment-44898">discussion</a> but it is still on hold.</p><p>Maybe you have a similar issue with your data source and I’d be glad if this article is of help to you. The goal was not just to solve the problem but to try using different approaches. I hope I’ve shown something new and maybe it will inspire you to solve some problems and do some “mind” exercises. These examples could be helpful for other problems when you need to cache data. You can use them and modify them for your needs.<br><a href="https://github.com/akellbl4/cache-proxy">Full examples</a> stored on GitHub.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=833433fa8ed1" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/fetch-shared-data-in-next-js-with-single-request-833433fa8ed1">Fetch Shared Data in Next.js With Single Request</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>