<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>hobby_is_coding.log</title>
        <link>https://velog.io/</link>
        <description>퇴사했지만, 코딩은 하고싶어</description>
        <lastBuildDate>Fri, 14 Feb 2025 10:21:57 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <image>
            <title>hobby_is_coding.log</title>
            <url>https://velog.velcdn.com/images/hobby_is_coding/profile/283d45cc-7308-4b52-828a-e97ee8b413dd/social_profile.jpeg</url>
            <link>https://velog.io/</link>
        </image>
        <copyright>Copyright (C) 2019. hobby_is_coding.log. All rights reserved.</copyright>
        <atom:link href="https://v2.velog.io/rss/hobby_is_coding" rel="self" type="application/rss+xml"/>
        <item>
            <title><![CDATA[Understanding Middleware in Next.js (1)]]></title>
            <link>https://velog.io/@hobby_is_coding/Understanding-Middleware-in-Next.js-1</link>
            <guid>https://velog.io/@hobby_is_coding/Understanding-Middleware-in-Next.js-1</guid>
            <pubDate>Fri, 14 Feb 2025 10:21:57 GMT</pubDate>
            <description><![CDATA[<p><img src="https://velog.velcdn.com/images/hobby_is_coding/post/eefe13ea-fb4c-4065-abd3-95774f2184f0/image.png" alt=""></p>
<p>For session management in a Next.js project using <strong>Auth.js</strong> or <strong>Clerk</strong>, middleware is almost essential. However, until now, I have simply copied and pasted the code provided in the documentation into my <code>middleware.ts</code> file without giving much thought to what middleware actually is, why it is used, or what its benefits are.</p>
<p>So today, I am taking the time to <strong>deep dive into middleware</strong> and explore its purpose and advantages!</p>
<hr>
<h2 id="1-what-is-middleware-in-nextjs"><strong>1. What is Middleware in Next.js?</strong></h2>
<p>Let&#39;s start with the official Next.js documentation:</p>
<blockquote>
<p>Middleware allows you to run code before a request is completed. Then, based on the incoming request, you can modify the response by rewriting, redirecting, modifying the request or response headers, or responding directly.</p>
</blockquote>
<p>In simple terms, <strong>middleware intercepts every request</strong>, allowing us to add various logic or directly manipulate the request/response.</p>
<p><img src="https://velog.velcdn.com/images/hobby_is_coding/post/30b9e694-54fc-4670-b6c2-ccfa3f103652/image.gif" alt=""></p>
<h3 id="but-what-does-request-mean-in-nextjs"><strong>But what does &quot;request&quot; mean in Next.js?</strong></h3>
<hr>
<h2 id="2-what-is-considered-a-request-in-nextjs"><strong>2. What is Considered a Request in Next.js?</strong></h2>
<h3 id="1-page-requests"><strong>(1) Page Requests</strong></h3>
<p>A <strong>page request</strong> occurs when:</p>
<ul>
<li>A user directly visits <code>/dashboard</code></li>
<li>A user navigates to <code>/dashboard</code> using <code>&lt;Link&gt;</code></li>
<li>A user refreshes the page while on <code>/dashboard</code></li>
</ul>
<h3 id="2-fetching-data"><strong>(2) Fetching Data</strong></h3>
<p>Requests also occur when fetching data, such as:</p>
<ul>
<li>Calling an <strong>API route</strong> (<code>app/api/*</code> or <code>pages/api/*</code>)</li>
<li>Using <strong>Server Actions</strong> to fetch data</li>
</ul>
<h3 id="3-does-middleware-run-on-re-renders"><strong>(3) Does Middleware Run on Re-renders?</strong></h3>
<p>Some people mistakenly believe that middleware runs when a component re-renders. However, <strong>middleware is NOT triggered by component re-renders</strong>.</p>
<p>According to the official Next.js documentation:</p>
<blockquote>
<p>Middleware execution is NOT directly triggered by a component re-render. Middleware runs only on incoming HTTP requests from the browser or API calls.</p>
</blockquote>
<p>In short, middleware does <strong>NOT</strong> run when a component re-renders on the client side.</p>
<hr>
<h2 id="3-when-should-you-use-middleware"><strong>3. When Should You Use Middleware?</strong></h2>
<p>Integrating Middleware into your Next.js application can significantly improve <strong>performance, security, and user experience</strong>. Below are some common use cases:</p>
<h3 id="1-authentication-and-authorization"><strong>(1) Authentication and Authorization</strong></h3>
<ul>
<li>Middleware can check session cookies in request headers to verify whether the user is authenticated before allowing access to a protected route.</li>
</ul>
<h3 id="2-redirects-and-path-rewriting"><strong>(2) Redirects and Path Rewriting</strong></h3>
<ul>
<li>If a user is not logged in, middleware can <strong>redirect</strong> them to the login page before they reach a protected route.</li>
</ul>
<h3 id="3-logging-and-analytics"><strong>(3) Logging and Analytics</strong></h3>
<ul>
<li>Middleware can capture and analyze request data before processing it in an API or page.</li>
</ul>
<hr>
<h2 id="4-applying-middleware-in-a-nextjs-project"><strong>4. Applying Middleware in a Next.js Project</strong></h2>
<p>To apply middleware correctly, you must follow <strong>two key conventions</strong>:</p>
<ol>
<li><strong>The <code>middleware.ts</code> file must be in the root directory</strong> of your project.</li>
<li><strong>The filename must be exactly <code>middleware.ts</code> or <code>middleware.js</code></strong>.</li>
</ol>
<p><img src="https://velog.velcdn.com/images/hobby_is_coding/post/d618dcc1-5f08-4380-9a41-9b213b42bd9b/image.PNG" alt=""></p>
<p>If your middleware is not working, check these two points first!</p>
<h3 id="example-middleware-implementation"><strong>Example Middleware Implementation</strong></h3>
<pre><code class="language-ts">import { NextResponse } from &#39;next/server&#39;;
import type { NextRequest } from &#39;next/server&#39;;

// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
  return NextResponse.redirect(new URL(&#39;/home&#39;, request.url));
}</code></pre>
<p>In the example above:</p>
<ul>
<li>Middleware <strong>intercepts all requests</strong> before they reach the actual page or API route.</li>
<li>The function takes a <code>request</code> parameter of type <strong><code>NextRequest</code></strong>.</li>
</ul>
<hr>
<h2 id="5-understanding-nextrequest"><strong>5. Understanding <code>NextRequest</code></strong></h2>
<p>The <code>NextRequest</code> object <strong>extends the Web API Request</strong> and provides additional convenience methods.</p>
<h3 id="analysis-of-nextrequest"><strong>Analysis of <code>NextRequest</code></strong></h3>
<pre><code class="language-ts">// request.ts
export declare class NextRequest extends Request {
    [INTERNALS]: {
        cookies: RequestCookies;
        url: string;
        nextUrl: NextURL;
    };
    constructor(input: URL | RequestInfo, init?: RequestInit);
    get cookies(): RequestCookies;
    get nextUrl(): NextURL;
    get url(): string;
}</code></pre>
<p><strong>NextRequest</strong> is a subclass of <strong>Request</strong>. It inherits all the standard request properties (headers, body).</p>
<pre><code class="language-ts">export declare class NextRequest extends Request {
  // extends means to inherit all the properties.</code></pre>
<p> But it stores internal metadata related to the Next.js-specific features, such as</p>
<ul>
<li><strong>cookies</strong> (to store request cookies)<ul>
<li><strong>nextUrl</strong> (for easier URL handling)</li>
</ul>
</li>
</ul>
<pre><code class="language-ts">[INTERNALS]: { //Internal is unique key for object properties
        cookies: RequestCookies;
        url: string;
        nextUrl: NextURL;
};</code></pre>
<p>In the next post, we will explore case studies on how middleware is used in each scenario.</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Web Architecture: CSR & SSR]]></title>
            <link>https://velog.io/@hobby_is_coding/Web-Architecture-CSR-SSR</link>
            <guid>https://velog.io/@hobby_is_coding/Web-Architecture-CSR-SSR</guid>
            <pubDate>Sun, 18 Feb 2024 14:06:55 GMT</pubDate>
            <description><![CDATA[<h3 id="1-web-app-architecture">1. web app architecture</h3>
<p><strong>a structure that determines the way your product and business will operate, perform and scale.</strong></p>
<h3 id="2-web-app-architecture-types">2. web app architecture types</h3>
<ol>
<li><strong>MPA</strong> : consist of multiple HTML pages, sends requests to server for different HTML in every change.</li>
<li><strong>SPA</strong> : consists of single page, sends requests to server for data that needs to be changed.</li>
</ol>
<h3 id="3-csr-vs-ssr">3. CSR vs SSR</h3>
<p>The difference between CSR and SSR is that wich side(client or server) is responsible for <strong>rendering</strong> the web page.</p>
<p><strong>what is rendering?</strong></p>
<blockquote>
<p>the process of converting application code into interactive web pages</p>
</blockquote>
<p>📍 <strong>CSR</strong> : it’s the browser that generates the entire app, including the UI, data, and functionality.</p>
<p>The client recieves empty HTML(Shell), CSS and Javascript bundle from front server. And sends request to back server for data while the web page is loading.</p>
<p>👍 <strong>pros</strong> : </p>
<ul>
<li>better user interactivity</li>
</ul>
<p>→ no need to reload pages</p>
<ul>
<li>caching</li>
</ul>
<p>👎 <strong>cons</strong> :</p>
<ul>
<li>initial loading may take too long</li>
</ul>
<p>→ The browser needs to download and run the whole JS includes frameworks and library</p>
<ul>
<li>Low SEO</li>
</ul>
<p>→ Bots will only see empty HTML</p>
<p>💡 <strong>solutions</strong>:</p>
<ul>
<li>code splitting</li>
<li>tree-shake</li>
<li>SSR</li>
</ul>
<p><img src="https://velog.velcdn.com/images/hobby_is_coding/post/dd53ad3f-88ac-47f8-a03a-fc708bcbb5f5/image.jpg" alt=""></p>
<p>📍 <strong>SSR</strong> :  it’s the server that generates static HTML. The client receives fully rendered HTML page. </p>
<p>👍 <strong>pros</strong> : </p>
<ul>
<li>better SEO</li>
<li>Faster initial page loads</li>
</ul>
<p>👎 <strong>cons</strong> :</p>
<ul>
<li>blinking issue, non-rich site interaction</li>
<li>server side overhead</li>
<li>need to wait before interaction</li>
</ul>
<p>→ TTV &lt; TTI </p>
<p>🪜 <strong>SSR Steps</strong></p>
<ol>
<li><strong>Client’s HTTP request</strong> – When the user enters the URL into the browser’s address bar, it establishes an HTTP connection with the server, then sends the server a request for the HTML document. </li>
<li><strong>Data fetching</strong> – The server fetches any required data from the database or third-party APIs.</li>
<li><strong>Server-side pre-rendering</strong> – The server compiles the JavaScript components into static HTML.</li>
<li><strong>Server’s HTTP response</strong> – The server sends this HTML document to the client.</li>
<li><strong>Page load and rendering</strong>– The client downloads the HTML file and displays the static components on the page.</li>
<li><strong>Hydration</strong> – The client downloads the JavaScript file embedded into the HTML, processes the code, and attaches event listeners to the components. This process is called hydration.</li>
</ol>
<p><img src="https://velog.velcdn.com/images/hobby_is_coding/post/5addf749-c1c7-4c3c-be6e-ee13bd9dd04c/image.png" alt=""></p>
<hr>
<h3 id="4-csr-vs-ssr-in-point-of-ttv--tti">4. CSR vs SSR in point of TTV &amp; TTI</h3>
<p><strong>TTV(Time To View)</strong> refers to how much time it takes for users to see the rendered web page. </p>
<p><strong>TTI(Time To Interact)</strong> refers to how much time it takes the JS file to be loaded so users can interact with web page.</p>
<p>In CSR, users can’t see or interact with web page until all JS files are loaded. Therefore, TTI and TTV is same. Otherwise, in SSR, users are able to see the web page but it takes some time for users to interact with the page. Thus TTV is longer than TTI.</p>
<h3 id="5-is-csr-and-spa-same">5. Is CSR and SPA same?</h3>
<p>The answer is <strong>NO</strong>. The difference between MPA / SPA is how many pages the web app uses. Otherwise, the difference between CSR / SSR is where the web page is rendered. Therefore, the relationship is not an equal sign.</p>
<p>SSR supports MPA for showing multiple pages to users, and SPA is based on CSR for intracting with users on a sigle page.</p>
<h3 id="6-what-to-choose-then">6. What to choose then?</h3>
<p>CRS is suited for the web page which requires high interactivity with users and the contents changes frequently. </p>
<p>SSR can be implemented in web app that is static and contains heavy contents. Moreover, SSR is better choice when SEO is important element for web app</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[props in React]]></title>
            <link>https://velog.io/@hobby_is_coding/props-in-React</link>
            <guid>https://velog.io/@hobby_is_coding/props-in-React</guid>
            <pubDate>Sun, 26 Nov 2023 08:00:41 GMT</pubDate>
            <description><![CDATA[<h1 id="what-is-props">What is props?</h1>
<p>React components accept arbitrary <code>inputs</code> (called “<strong>props</strong>”) and return React elements describing what should appear on the screen.</p>
<pre><code class="language-javascript">function Welcome(props) {
  return &lt;h1&gt;Hello, {props.name}&lt;/h1&gt;;
}</code></pre>
<p>So props able <code>parent</code> components to deliver data to <code>child</code> components.</p>
<pre><code class="language-javascript">function Parent() {
  return (
      &lt;Child data=&quot;this is props for child!&quot;/&gt;
  )
}

function Child(props) {
  return &lt;h1&gt;{props.data}&lt;/h1&gt;
}</code></pre>
<hr>
<p>Props have 2 major characteristics. </p>
<h2 id="1-immutable">1. Immutable</h2>
<p>props must be <strong>read-only</strong> they should not be modified when passed to other components.</p>
<pre><code class="language-javascript">// 1st example
function Child(props) { // props is 1
  const modified = props.number + 1 ❌

  return &lt;h1&gt;{modified}&lt;/h1&gt;
}</code></pre>
<p>It is more easy to understand why props must be immutable according to the fact that props are more likely a <strong>reference</strong> from parent component. Therefore it is the parent components who <strong>owns</strong> the data.</p>
<pre><code class="language-javascript">// 2nd example
function Child(props) {
  props.data = &quot;I want to change this&quot; ❌

  return &lt;h1&gt;{props.data}&lt;/h1&gt;
}</code></pre>
<hr>
<h2 id="2-unidirectional">2. Unidirectional</h2>
<p>Props must be <code>one-way</code> data flow, which means props need to be passed only from parent components to child components.</p>
<p><code>Props-drilling</code> occurs when a parent component passes data down to its children and then those children pass the same data down to their own children.</p>
<p>If the depth gets too deep, props-drilling could make React app difficult to manage and maintain.</p>
<h1 id="what-are-the-benefits">What are the benefits?</h1>
<p>Immutability and unidirection have benefits.</p>
<blockquote>
<ol>
<li>comprehend the logic more quickly</li>
<li>simplify the data flow</li>
</ol>
</blockquote>
<p>Props is one of the most important concept in React. Those two characteristics keep the React app <strong>consistent</strong> and <strong>reliable</strong> in controlling the flow of data between components.</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Let's compare "var" vs "let" vs "const". And why we should not use var anymore?]]></title>
            <link>https://velog.io/@hobby_is_coding/Lets-compare-var-vs-let-vs-const.-And-why-we-should-not-use-var-anymore</link>
            <guid>https://velog.io/@hobby_is_coding/Lets-compare-var-vs-let-vs-const.-And-why-we-should-not-use-var-anymore</guid>
            <pubDate>Sat, 14 Oct 2023 12:29:49 GMT</pubDate>
            <description><![CDATA[<p><img src="https://velog.velcdn.com/images/hobby_is_coding/post/878f4cd7-6c2c-44bc-8de9-ccab4db95756/image.png" alt=""></p>
<h1 id="good-bye-var">Good bye &quot;var&quot;</h1>
<p>Today, I assume most of programmers don&#39;t use the &quot;var&quot; variable type anymore. Replacing it by &quot;let&quot; and &quot;const&quot;. But why? Just because of &quot;var&quot; is just from old version?
This post is to make everybody clear what is difference between them and why &quot;var&quot; is depreciated.</p>
<h2 id="before-we-start">before we start</h2>
<p>For those who is new to javascript. There are 3 terminologies we should know when creating a variable.</p>
<ul>
<li><p><strong>Declaration</strong> : define variables using const, let, and var.</p>
<pre><code class="language-javascript">var name;
let age;
const address;</code></pre>
</li>
<li><p><strong>Initialization</strong> : value is added through the assign operator (=) after the variable is declared.</p>
<pre><code class="language-javascript">var name;
name = &#39;minjae&#39;
</code></pre>
</li>
</ul>
<p>let age;
age = 30</p>
<pre><code>- **Assigning** : means declaring a variable and assigning a value at the same time.
```javascript
var name = &#39;minjae&#39;;
let age = 30;
const married = false;</code></pre><hr>
<h2 id="1-redeclaring-variables">1. Redeclaring variables</h2>
<p><code>var</code> can be declared again with same variable name.
<code>let</code> and <code>const</code> can not be declared again with same name.</p>
<pre><code class="language-javascript">//example
var name = &#39;Santa&#39;;
var name = &#39;Pikachu&#39;; // OK

let age = 20;
let age = 30; //SyntaxError: Identifier &#39;age&#39; has already been declared

const character = &#39;Mario&#39;;
const character = &#39;Bumble Bee&#39;; //SyntaxError: Identifier &#39;character&#39; has already been declared

</code></pre>
<h2 id="2-unassigning-variables">2. Unassigning variables</h2>
<p>&quot;<code>var</code>&quot; and &quot;<code>let</code>&quot; can be unassigned during declaration.
&quot;<code>const</code>&quot; must be assigned during declaration.</p>
<pre><code class="language-javascript">//example
var cat //OK
let squirrel //OK

const spider //SyntaxError: Missing initializer in const declaration</code></pre>
<h2 id="3-reassigning">3. Reassigning</h2>
<p>&quot;<code>var</code>&quot; and &quot;<code>let</code>&quot; can be reassigned.
&quot;<code>const</code>&quot; can not be reassigned.</p>
<pre><code class="language-javascript">//example
var name = &quot;Elgol&quot;
let country = &quot;Germany&quot;
const age = 5

//Reassign
name = &quot;minjae&quot; //OK
country = &quot;Korea&quot; //OK

age = 30 //TypeError: Assignment to constant variable.</code></pre>
<h2 id="4-accessing-variables-before-declaring">4. Accessing variables before declaring</h2>
<p>what is so bizzare about javascript is that you can access variables before declaring it.</p>
<p>&quot;<code>var</code>&quot; can be accessed before declaring variables.
&quot;<code>let</code>&quot; and &quot;<code>const</code>&quot; can not be accessed before declaring variables.</p>
<pre><code class="language-javascript">//example
console.log(cat)
console.log(dog)
//console.log(bird)

var cat //OK
let dog </code></pre>
<pre><code>//result
undefined
console.log(dog)
            ^
ReferenceError: Cannot access &#39;dog&#39; before initialization</code></pre><hr>
<h2 id="hoisting">Hoisting</h2>
<p>The phenomenon above is called <code>Hoisting</code>. It works as if the variables have been lifted to the top.</p>
<blockquote>
<p>Hoisting is JavaScript&#39;s default behavior of moving all declarations to the top of the current scope.</p>
</blockquote>
<p>Variables declared by <code>var</code> are subject to the <code>declaration</code> and <code>initialization</code> stages at once. That is, the variable is declared in the very first line of code (Actually, space for the variable is secured in the memory) and then initialized to <code>undefined</code>.</p>
<pre><code class="language-javascript">// var hoisting example
console.log(movie); //undefined 
var movie = &#39;Forest Gump&#39;;</code></pre>
<p>The <code>let</code> and <code>const</code> variables are also hosted, but they are not initialized to <code>undefined</code>. Therefore it emits error when accessed before them.</p>
<pre><code class="language-javascript">// let const hoisting example
console.log(music); 
console.log(music2); 

let music = &#39;Wonderwall&#39;;
const music2 = &#39;Summer&#39;;

//ERROR!!
//ReferenceError: Cannot access &#39;music&#39; before initialization</code></pre>
<h1 id="conclusion">Conclusion</h1>
<p>As we saw on previous example how each variable type is working, <code>var</code> has high chance of causing errors.</p>
<p>It is recommened using <code>let</code> and <code>const</code> to prevent <a href="#hoisting">hoisting</a> and <a href="##1.-Redeclaring-variables">redeclaring</a> problems that you can run application more reliable and safe.</p>
]]></description>
        </item>
    </channel>
</rss>