[{"data":1,"prerenderedAt":21727},["ShallowReactive",2],{"blog-published-articles":3,"privacy-policy":21465},[4,1422,3684,5920,8321,8793,10146,10816,11455,13642,15369,16925,17654,17767,18255,19030,19537,19706,19931,20121,20605,20890],{"id":5,"title":6,"body":7,"date":1402,"description":1403,"draft":1404,"extension":1405,"featured":1404,"image":1406,"meta":1409,"navigation":1410,"path":1411,"seo":1412,"stem":1413,"tags":1414,"updatedAt":1420,"__hash__":1421},"blog\u002Fblog\u002Fvues-reactivity-is-better-than-i-realized.md","Vue’s Reactivity Is Better Than I Realized",{"type":8,"value":9,"toc":1386},"minimark",[10,14,17,33,36,39,42,48,51,54,57,74,79,82,85,175,178,181,258,261,264,314,317,320,323,326,404,407,410,414,417,502,505,574,577,597,600,603,606,609,612,616,619,622,625,627,635,638,651,654,657,660,664,667,670,673,676,679,682,688,691,694,698,701,704,710,713,716,719,765,768,775,778,781,784,787,790,794,797,800,803,806,809,812,885,888,894,897,900,903,917,920,923,927,930,936,939,942,1002,1005,1017,1020,1023,1027,1030,1033,1036,1039,1042,1045,1048,1051,1054,1058,1061,1064,1067,1070,1073,1076,1079,1082,1085,1088,1094,1097,1100,1103,1107,1110,1113,1116,1119,1122,1125,1128,1131,1134,1137,1140,1144,1147,1150,1153,1156,1162,1165,1168,1171,1174,1177,1180,1183,1186,1189,1192,1196,1199,1202,1205,1223,1226,1228,1237,1239,1241,1250,1252,1255,1258,1261,1264,1267,1270,1273,1277,1280,1283,1286,1289,1292,1295,1298,1301,1304,1307,1310,1313,1317,1320,1323,1346,1349,1352,1355,1358,1361,1364,1367,1370,1373,1376,1379,1382],[11,12,13],"p",{},"Sometimes you understand a technology best when you stop using it and start asking why it works.",[11,15,16],{},"I’ve been working with Vue for quite a while now, and like most developers, I’ve spent a lot of time using its reactive APIs without thinking too deeply about what happens underneath.",[11,18,19,23,24,23,27,23,30],{},[20,21,22],"code",{},"ref"," ",[20,25,26],{},"reactive",[20,28,29],{},"computed",[20,31,32],{},"watch",[11,34,35],{},"They become muscle memory.",[11,37,38],{},"You write the code, the UI updates, and you move on.",[11,40,41],{},"Recently, while working on one of my applications, I found myself questioning something very simple:",[43,44,45],"blockquote",{},[11,46,47],{},"If one object inside an array changes, what exactly does Vue update?",[11,49,50],{},"It sounds like a small question.",[11,52,53],{},"It sent me down a much deeper rabbit hole.",[11,55,56],{},"And the more I read about how Vue’s reactivity works, the more I appreciated the design decisions behind it.",[11,58,59,60,67,68,73],{},"Originally published on ",[61,62,66],"a",{"href":63,"rel":64},"https:\u002F\u002Flevelup.gitconnected.com\u002Fvues-reactivity-is-better-than-i-realized-af71a4c90293",[65],"nofollow","Medium"," in ",[61,69,72],{"href":70,"rel":71},"https:\u002F\u002Flevelup.gitconnected.com\u002F",[65],"Level Up Coding",".",[75,76,78],"h2",{"id":77},"the-problem-i-was-actually-solving","The problem I was actually solving",[11,80,81],{},"One of the applications I’ve been building is heavily configuration-driven.",[11,83,84],{},"Instead of manually creating every form field, I have metadata describing them:",[86,87,92],"pre",{"className":88,"code":89,"language":90,"meta":91,"style":91},"language-ts shiki shiki-themes github-light github-dark","{\n  id: 'country',\n  name: 'country',\n  label: 'Country',\n  type: 'select',\n  placeholder: 'Select country'\n}\n","ts","",[20,93,94,103,120,132,145,158,169],{"__ignoreMap":91},[95,96,99],"span",{"class":97,"line":98},"line",1,[95,100,102],{"class":101},"sVt8B","{\n",[95,104,106,110,113,117],{"class":97,"line":105},2,[95,107,109],{"class":108},"sScJk","  id",[95,111,112],{"class":101},": ",[95,114,116],{"class":115},"sZZnC","'country'",[95,118,119],{"class":101},",\n",[95,121,123,126,128,130],{"class":97,"line":122},3,[95,124,125],{"class":108},"  name",[95,127,112],{"class":101},[95,129,116],{"class":115},[95,131,119],{"class":101},[95,133,135,138,140,143],{"class":97,"line":134},4,[95,136,137],{"class":108},"  label",[95,139,112],{"class":101},[95,141,142],{"class":115},"'Country'",[95,144,119],{"class":101},[95,146,148,151,153,156],{"class":97,"line":147},5,[95,149,150],{"class":108},"  type",[95,152,112],{"class":101},[95,154,155],{"class":115},"'select'",[95,157,119],{"class":101},[95,159,161,164,166],{"class":97,"line":160},6,[95,162,163],{"class":108},"  placeholder",[95,165,112],{"class":101},[95,167,168],{"class":115},"'Select country'\n",[95,170,172],{"class":97,"line":171},7,[95,173,174],{"class":101},"}\n",[11,176,177],{},"The UI consumes this metadata and renders the appropriate component.",[11,179,180],{},"Something roughly like:",[86,182,186],{"className":183,"code":184,"language":185,"meta":91,"style":91},"language-vue shiki shiki-themes github-light github-dark","\u003CInputField\n  v-for=\"field in fields\"\n  :key=\"field.id\"\n  :field=\"field\"\n\u002F>\n","vue",[20,187,188,197,221,238,253],{"__ignoreMap":91},[95,189,190,193],{"class":97,"line":98},[95,191,192],{"class":101},"\u003C",[95,194,196],{"class":195},"s9eBZ","InputField\n",[95,198,199,203,206,209,212,215,218],{"class":97,"line":105},[95,200,202],{"class":201},"szBVR","  v-for",[95,204,205],{"class":101},"=",[95,207,208],{"class":115},"\"",[95,210,211],{"class":101},"field ",[95,213,214],{"class":201},"in",[95,216,217],{"class":101}," fields",[95,219,220],{"class":115},"\"\n",[95,222,223,226,229,231,233,236],{"class":97,"line":122},[95,224,225],{"class":101},"  :",[95,227,228],{"class":108},"key",[95,230,205],{"class":101},[95,232,208],{"class":115},[95,234,235],{"class":101},"field.id",[95,237,220],{"class":115},[95,239,240,242,245,247,249,251],{"class":97,"line":134},[95,241,225],{"class":101},[95,243,244],{"class":108},"field",[95,246,205],{"class":101},[95,248,208],{"class":115},[95,250,244],{"class":101},[95,252,220],{"class":115},[95,254,255],{"class":97,"line":147},[95,256,257],{"class":101},"\u002F>\n",[11,259,260],{},"This becomes particularly interesting with select inputs.",[11,262,263],{},"Some options are static:",[86,265,267],{"className":88,"code":266,"language":90,"meta":91,"style":91},"options: [\n  { label: 'Active', value: 'ACTIVE' },\n  { label: 'Inactive', value: 'INACTIVE' }\n]\n",[20,268,269,277,294,309],{"__ignoreMap":91},[95,270,271,274],{"class":97,"line":98},[95,272,273],{"class":108},"options",[95,275,276],{"class":101},": [\n",[95,278,279,282,285,288,291],{"class":97,"line":105},[95,280,281],{"class":101},"  { label: ",[95,283,284],{"class":115},"'Active'",[95,286,287],{"class":101},", value: ",[95,289,290],{"class":115},"'ACTIVE'",[95,292,293],{"class":101}," },\n",[95,295,296,298,301,303,306],{"class":97,"line":122},[95,297,281],{"class":101},[95,299,300],{"class":115},"'Inactive'",[95,302,287],{"class":101},[95,304,305],{"class":115},"'INACTIVE'",[95,307,308],{"class":101}," }\n",[95,310,311],{"class":97,"line":134},[95,312,313],{"class":101},"]\n",[11,315,316],{},"Others come from reference data returned by APIs.",[11,318,319],{},"That reference data lives in a Pinia store.",[11,321,322],{},"So instead of modifying my original configuration, I derive the data needed by the UI through a computed property.",[11,324,325],{},"Conceptually:",[86,327,329],{"className":88,"code":328,"language":90,"meta":91,"style":91},"const fields = computed(() => {\n  return metadata.map((field) => {\n    \u002F\u002F enrich field with dynamic reference data\n    \u002F\u002F resolve dependent options\n    \u002F\u002F return UI-ready metadata\n  })\n})\n",[20,330,331,354,378,384,389,394,399],{"__ignoreMap":91},[95,332,333,336,339,342,345,348,351],{"class":97,"line":98},[95,334,335],{"class":201},"const",[95,337,217],{"class":338},"sj4cs",[95,340,341],{"class":201}," =",[95,343,344],{"class":108}," computed",[95,346,347],{"class":101},"(() ",[95,349,350],{"class":201},"=>",[95,352,353],{"class":101}," {\n",[95,355,356,359,362,365,368,371,374,376],{"class":97,"line":105},[95,357,358],{"class":201},"  return",[95,360,361],{"class":101}," metadata.",[95,363,364],{"class":108},"map",[95,366,367],{"class":101},"((",[95,369,244],{"class":370},"s4XuR",[95,372,373],{"class":101},") ",[95,375,350],{"class":201},[95,377,353],{"class":101},[95,379,380],{"class":97,"line":122},[95,381,383],{"class":382},"sJ8bj","    \u002F\u002F enrich field with dynamic reference data\n",[95,385,386],{"class":97,"line":134},[95,387,388],{"class":382},"    \u002F\u002F resolve dependent options\n",[95,390,391],{"class":97,"line":147},[95,392,393],{"class":382},"    \u002F\u002F return UI-ready metadata\n",[95,395,396],{"class":97,"line":160},[95,397,398],{"class":101},"  })\n",[95,400,401],{"class":97,"line":171},[95,402,403],{"class":101},"})\n",[11,405,406],{},"This works beautifully.",[11,408,409],{},"But then I started thinking about what happens when something changes.",[75,411,413],{"id":412},"what-happens-when-one-object-changes","What happens when one object changes?",[11,415,416],{},"Imagine the following:",[86,418,420],{"className":88,"code":419,"language":90,"meta":91,"style":91},"const users = ref([\n  { id: 1, name: 'John', age: 25 },\n  { id: 2, name: 'Jane', age: 30 },\n  { id: 3, name: 'Bob', age: 40 },\n])\n",[20,421,422,437,459,478,497],{"__ignoreMap":91},[95,423,424,426,429,431,434],{"class":97,"line":98},[95,425,335],{"class":201},[95,427,428],{"class":338}," users",[95,430,341],{"class":201},[95,432,433],{"class":108}," ref",[95,435,436],{"class":101},"([\n",[95,438,439,442,445,448,451,454,457],{"class":97,"line":105},[95,440,441],{"class":101},"  { id: ",[95,443,444],{"class":338},"1",[95,446,447],{"class":101},", name: ",[95,449,450],{"class":115},"'John'",[95,452,453],{"class":101},", age: ",[95,455,456],{"class":338},"25",[95,458,293],{"class":101},[95,460,461,463,466,468,471,473,476],{"class":97,"line":122},[95,462,441],{"class":101},[95,464,465],{"class":338},"2",[95,467,447],{"class":101},[95,469,470],{"class":115},"'Jane'",[95,472,453],{"class":101},[95,474,475],{"class":338},"30",[95,477,293],{"class":101},[95,479,480,482,485,487,490,492,495],{"class":97,"line":134},[95,481,441],{"class":101},[95,483,484],{"class":338},"3",[95,486,447],{"class":101},[95,488,489],{"class":115},"'Bob'",[95,491,453],{"class":101},[95,493,494],{"class":338},"40",[95,496,293],{"class":101},[95,498,499],{"class":97,"line":147},[95,500,501],{"class":101},"])\n",[11,503,504],{},"And:",[86,506,508],{"className":183,"code":507,"language":185,"meta":91,"style":91},"\u003Cdiv\n  v-for=\"user in users\"\n  :key=\"user.id\"\n>\n  \u003Ch3>{{ user.name }}\u003C\u002Fh3>\n  \u003Cp>{{ user.age }}\u003C\u002Fp>\n\u003C\u002Fdiv>\n",[20,509,510,517,534,549,554,559,564],{"__ignoreMap":91},[95,511,512,514],{"class":97,"line":98},[95,513,192],{"class":101},[95,515,516],{"class":195},"div\n",[95,518,519,521,523,525,528,530,532],{"class":97,"line":105},[95,520,202],{"class":201},[95,522,205],{"class":101},[95,524,208],{"class":115},[95,526,527],{"class":101},"user ",[95,529,214],{"class":201},[95,531,428],{"class":101},[95,533,220],{"class":115},[95,535,536,538,540,542,544,547],{"class":97,"line":122},[95,537,225],{"class":101},[95,539,228],{"class":108},[95,541,205],{"class":101},[95,543,208],{"class":115},[95,545,546],{"class":101},"user.id",[95,548,220],{"class":115},[95,550,551],{"class":97,"line":134},[95,552,553],{"class":101},">\n",[95,555,556],{"class":97,"line":147},[95,557,558],{"class":101},"  \u003Ch3>{{ user.name }}\u003C\u002Fh3>\n",[95,560,561],{"class":97,"line":160},[95,562,563],{"class":101},"  \u003Cp>{{ user.age }}\u003C\u002Fp>\n",[95,565,566,569,572],{"class":97,"line":171},[95,567,568],{"class":101},"\u003C\u002F",[95,570,571],{"class":195},"div",[95,573,553],{"class":101},[11,575,576],{},"Now I change:",[86,578,580],{"className":88,"code":579,"language":90,"meta":91,"style":91},"users.value[1].age = 31\n",[20,581,582],{"__ignoreMap":91},[95,583,584,587,589,592,594],{"class":97,"line":98},[95,585,586],{"class":101},"users.value[",[95,588,444],{"class":338},[95,590,591],{"class":101},"].age ",[95,593,205],{"class":201},[95,595,596],{"class":338}," 31\n",[11,598,599],{},"Does Vue rebuild the entire list?",[11,601,602],{},"Does it recreate all three DOM elements?",[11,604,605],{},"Does it somehow know that only Jane’s age changed?",[11,607,608],{},"The answer is a little more nuanced than “Vue only re-renders one element.”",[11,610,611],{},"And understanding that distinction is important.",[75,613,615],{"id":614},"rendering-and-updating-arent-the-same-thing","Rendering and updating aren’t the same thing",[11,617,618],{},"When reactive state changes, Vue’s rendering effect can run again and produce a new virtual DOM representation.",[11,620,621],{},"But that does not mean Vue throws away the existing DOM and recreates everything.",[11,623,624],{},"Vue compares the new virtual DOM with the previous one.",[11,626,325],{},[86,628,633],{"className":629,"code":631,"language":632,"meta":91},[630],"language-text","Reactive state changes\n        ↓\nRender function runs\n        ↓\nNew virtual DOM\n        ↓\nDiff against previous virtual DOM\n        ↓\nPatch only what changed\n        ↓\nReal DOM\n","text",[20,634,631],{"__ignoreMap":91},[11,636,637],{},"So in our example, Vue can determine that:",[639,640,641,645,648],"ul",{},[642,643,644],"li",{},"John \u002F 25 didn’t change.",[642,646,647],{},"Jane \u002F 30 → 31 changed.",[642,649,650],{},"Bob \u002F 40 didn’t change.",[11,652,653],{},"The existing DOM nodes are reused.",[11,655,656],{},"Only the affected part needs to be patched.",[11,658,659],{},"That distinction changed how I thought about Vue’s performance.",[75,661,663],{"id":662},"but-the-really-interesting-part-is-reactivity","But the really interesting part is reactivity",[11,665,666],{},"The virtual DOM isn’t the most interesting part to me anymore.",[11,668,669],{},"The interesting part is how Vue knows what depends on what.",[11,671,672],{},"Vue 3’s reactivity system is built around JavaScript Proxies and dependency tracking.",[11,674,675],{},"When reactive state is accessed, Vue can track that relationship.",[11,677,678],{},"When reactive state changes, Vue can trigger the effects that depend on it.",[11,680,681],{},"Very roughly:",[86,683,686],{"className":684,"code":685,"language":632,"meta":91},[630],"state.country\n     ↓\ncomputed field metadata\n     ↓\nState select\n     ↓\nDOM\n",[20,687,685],{"__ignoreMap":91},[11,689,690],{},"Change country, and the dependent computation becomes invalid.",[11,692,693],{},"That’s incredibly useful for the kind of UI I build.",[75,695,697],{"id":696},"this-is-where-computed-properties-become-really-powerful","This is where computed properties become really powerful",[11,699,700],{},"Consider a cascading dropdown.",[11,702,703],{},"You might have:",[86,705,708],{"className":706,"code":707,"language":632,"meta":91},[630],"Country\n   ↓\nState\n   ↓\nCity\n",[20,709,707],{"__ignoreMap":91},[11,711,712],{},"The state options depend on the selected country.",[11,714,715],{},"The city options depend on the selected state.",[11,717,718],{},"My metadata can describe that dependency:",[86,720,722],{"className":88,"code":721,"language":90,"meta":91,"style":91},"{\n  id: 'state',\n  type: 'select',\n  dependsOn: ['country']\n}\n",[20,723,724,728,739,749,761],{"__ignoreMap":91},[95,725,726],{"class":97,"line":98},[95,727,102],{"class":101},[95,729,730,732,734,737],{"class":97,"line":105},[95,731,109],{"class":108},[95,733,112],{"class":101},[95,735,736],{"class":115},"'state'",[95,738,119],{"class":101},[95,740,741,743,745,747],{"class":97,"line":122},[95,742,150],{"class":108},[95,744,112],{"class":101},[95,746,155],{"class":115},[95,748,119],{"class":101},[95,750,751,754,757,759],{"class":97,"line":134},[95,752,753],{"class":108},"  dependsOn",[95,755,756],{"class":101},": [",[95,758,116],{"class":115},[95,760,313],{"class":101},[95,762,763],{"class":97,"line":147},[95,764,174],{"class":101},[11,766,767],{},"And my computed metadata resolves the actual options based on the current form state.",[11,769,770,771,774],{},"So when ",[20,772,773],{},"form.country"," changes, the computed value is invalidated and the dependent options are recalculated.",[11,776,777],{},"I don’t have to manually tell Vue:",[11,779,780],{},"“Hey, the state dropdown needs to update now.”",[11,782,783],{},"The dependency is already present in the computation.",[11,785,786],{},"That is one of those things that feels almost boring when you’re using Vue.",[11,788,789],{},"Once you understand what’s happening underneath, it’s actually quite elegant.",[75,791,793],{"id":792},"then-i-started-thinking-about-react","Then I started thinking about React",[11,795,796],{},"This is where the comparison gets interesting.",[11,798,799],{},"I’m not saying React is bad.",[11,801,802],{},"Far from it.",[11,804,805],{},"React is an incredibly capable library, and its ecosystem is enormous.",[11,807,808],{},"But Vue and React approach reactivity from fundamentally different directions.",[11,810,811],{},"Consider a simplified React component:",[86,813,817],{"className":814,"code":815,"language":816,"meta":91,"style":91},"language-tsx shiki shiki-themes github-light github-dark","function User() {\n  return (\n    \u003C>\n      \u003Ch1>{user.name}\u003C\u002Fh1>\n      \u003Cp>{user.age}\u003C\u002Fp>\n    \u003C\u002F>\n  )\n}\n","tsx",[20,818,819,830,837,842,857,870,875,880],{"__ignoreMap":91},[95,820,821,824,827],{"class":97,"line":98},[95,822,823],{"class":201},"function",[95,825,826],{"class":108}," User",[95,828,829],{"class":101},"() {\n",[95,831,832,834],{"class":97,"line":105},[95,833,358],{"class":201},[95,835,836],{"class":101}," (\n",[95,838,839],{"class":97,"line":122},[95,840,841],{"class":101},"    \u003C>\n",[95,843,844,847,850,853,855],{"class":97,"line":134},[95,845,846],{"class":101},"      \u003C",[95,848,849],{"class":195},"h1",[95,851,852],{"class":101},">{user.name}\u003C\u002F",[95,854,849],{"class":195},[95,856,553],{"class":101},[95,858,859,861,863,866,868],{"class":97,"line":147},[95,860,846],{"class":101},[95,862,11],{"class":195},[95,864,865],{"class":101},">{user.age}\u003C\u002F",[95,867,11],{"class":195},[95,869,553],{"class":101},[95,871,872],{"class":97,"line":160},[95,873,874],{"class":101},"    \u003C\u002F>\n",[95,876,877],{"class":97,"line":171},[95,878,879],{"class":101},"  )\n",[95,881,883],{"class":97,"line":882},8,[95,884,174],{"class":101},[11,886,887],{},"When state changes, React’s basic model is roughly:",[86,889,892],{"className":890,"code":891,"language":632,"meta":91},[630],"State changes\n     ↓\nComponent function runs again\n     ↓\nNew element tree\n     ↓\nReconciliation\n     ↓\nDOM updates\n",[20,893,891],{"__ignoreMap":91},[11,895,896],{},"The important distinction is that running the component function and updating the DOM are two different things.",[11,898,899],{},"React can execute a component again without actually changing the corresponding DOM.",[11,901,902],{},"And React has become very good at optimizing this process.",[11,904,905,906,909,910,909,913,916],{},"Techniques such as ",[20,907,908],{},"memo",", ",[20,911,912],{},"useMemo",[20,914,915],{},"useCallback",", selector-based state management, and newer compiler-based optimizations can reduce unnecessary work.",[11,918,919],{},"So React is absolutely capable of building extremely fast applications.",[11,921,922],{},"But the mental model is different.",[75,924,926],{"id":925},"vue-feels-more-like-a-dependency-graph","Vue feels more like a dependency graph",[11,928,929],{},"With Vue, I tend to think about the application like this:",[86,931,934],{"className":932,"code":933,"language":632,"meta":91},[630],"Reactive state\n      │\n      ├──────→ Component A\n      │\n      ├──────→ Computed B\n      │\n      └──────→ Component C\n",[20,935,933],{"__ignoreMap":91},[11,937,938],{},"The reactive system tracks relationships between state and consumers.",[11,940,941],{},"That makes the following kind of code feel very natural:",[86,943,945],{"className":88,"code":944,"language":90,"meta":91,"style":91},"const filteredItems = computed(() => {\n  return items.value.filter(\n    item => item.category === selectedCategory.value,\n  )\n})\n",[20,946,947,964,977,994,998],{"__ignoreMap":91},[95,948,949,951,954,956,958,960,962],{"class":97,"line":98},[95,950,335],{"class":201},[95,952,953],{"class":338}," filteredItems",[95,955,341],{"class":201},[95,957,344],{"class":108},[95,959,347],{"class":101},[95,961,350],{"class":201},[95,963,353],{"class":101},[95,965,966,968,971,974],{"class":97,"line":105},[95,967,358],{"class":201},[95,969,970],{"class":101}," items.value.",[95,972,973],{"class":108},"filter",[95,975,976],{"class":101},"(\n",[95,978,979,982,985,988,991],{"class":97,"line":122},[95,980,981],{"class":370},"    item",[95,983,984],{"class":201}," =>",[95,986,987],{"class":101}," item.category ",[95,989,990],{"class":201},"===",[95,992,993],{"class":101}," selectedCategory.value,\n",[95,995,996],{"class":97,"line":134},[95,997,879],{"class":101},[95,999,1000],{"class":97,"line":147},[95,1001,403],{"class":101},[11,1003,1004],{},"The computed value depends on:",[639,1006,1007,1012],{},[642,1008,1009],{},[20,1010,1011],{},"items",[642,1013,1014],{},[20,1015,1016],{},"selectedCategory",[11,1018,1019],{},"Change either one, and Vue knows the computed value needs to be invalidated.",[11,1021,1022],{},"It’s declarative dependency management.",[75,1024,1026],{"id":1025},"react-feels-more-like-re-executing-a-description-of-the-ui","React feels more like re-executing a description of the UI",[11,1028,1029],{},"The React mental model is different.",[11,1031,1032],{},"You describe what the UI should look like for the current state.",[11,1034,1035],{},"When state changes, React can run the relevant component logic again and reconcile the result.",[11,1037,1038],{},"This model has enormous advantages.",[11,1040,1041],{},"It’s simple.",[11,1043,1044],{},"It’s predictable.",[11,1046,1047],{},"And it has allowed React to become one of the most successful UI libraries ever created.",[11,1049,1050],{},"But personally, I find Vue’s reactive model particularly pleasant for applications with lots of derived state.",[11,1052,1053],{},"Especially enterprise applications.",[75,1055,1057],{"id":1056},"and-this-matters-for-configuration-driven-applications","And this matters for configuration-driven applications",[11,1059,1060],{},"This is where my original question became relevant again.",[11,1062,1063],{},"My forms aren’t enormous.",[11,1065,1066],{},"They’re split into smaller subforms.",[11,1068,1069],{},"Each subform has its own metadata and computed state.",[11,1071,1072],{},"Reference data is loaded into Pinia when the application initializes.",[11,1074,1075],{},"Dynamic options are resolved through dictionary-style lookups rather than repeatedly scanning large collections.",[11,1077,1078],{},"Dependent fields declare their dependencies.",[11,1080,1081],{},"The UI simply consumes the resulting metadata.",[11,1083,1084],{},"So when reference data changes or a user changes a parent selection, Vue’s reactivity system naturally propagates the relevant changes.",[11,1086,1087],{},"The architecture becomes something like:",[86,1089,1092],{"className":1090,"code":1091,"language":632,"meta":91},[630],"API\n ↓\nPinia reference store\n ↓\nReactive form state\n ↓\nComputed metadata\n ↓\nGeneric input components\n ↓\nDOM\n",[20,1093,1091],{"__ignoreMap":91},[11,1095,1096],{},"And I don’t need every layer to manually notify the next layer.",[11,1098,1099],{},"The dependencies are expressed through the data itself.",[11,1101,1102],{},"That’s the part I really like.",[75,1104,1106],{"id":1105},"i-dont-think-vue-is-faster-than-react","I don’t think Vue is “faster than React”",[11,1108,1109],{},"After going down this rabbit hole, I actually became more careful about saying things like:",[11,1111,1112],{},"“Vue is faster than React.”",[11,1114,1115],{},"That’s an oversimplification.",[11,1117,1118],{},"There are benchmarks where one wins and benchmarks where the other wins.",[11,1120,1121],{},"There are applications where the difference is irrelevant.",[11,1123,1124],{},"There are also architectural decisions that matter far more than the framework.",[11,1126,1127],{},"A poorly designed Vue application can absolutely be slower than a well-designed React application.",[11,1129,1130],{},"And React has continued improving its rendering and compilation story significantly.",[11,1132,1133],{},"The more accurate statement is:",[11,1135,1136],{},"Vue and React have different approaches to managing UI updates, and Vue’s fine-grained reactivity can make certain kinds of state-heavy applications particularly pleasant to build.",[11,1138,1139],{},"That’s a much more useful distinction.",[75,1141,1143],{"id":1142},"what-surprised-me-most","What surprised me most",[11,1145,1146],{},"The funny thing is that I wasn’t trying to optimize my application when I started looking into this.",[11,1148,1149],{},"I was simply curious.",[11,1151,1152],{},"I had a computed property.",[11,1154,1155],{},"It returned an array of metadata.",[11,1157,1158,1159,73],{},"That array was rendered with ",[20,1160,1161],{},"v-for",[11,1163,1164],{},"Some properties depended on Pinia state.",[11,1166,1167],{},"Some select options depended on other form values.",[11,1169,1170],{},"And I suddenly wondered:",[11,1172,1173],{},"“Am I accidentally making Vue recompute and rerender everything whenever one field changes?”",[11,1175,1176],{},"The answer was nuanced.",[11,1178,1179],{},"Yes, the rendering process can run again.",[11,1181,1182],{},"No, Vue doesn’t blindly recreate the entire DOM.",[11,1184,1185],{},"And the reactive dependency tracking allows Vue to efficiently propagate changes through the parts of the application that actually depend on them.",[11,1187,1188],{},"Once I understood that, I stopped worrying about the architecture I had built.",[11,1190,1191],{},"More importantly, I understood why it worked.",[75,1193,1195],{"id":1194},"good-abstractions-become-even-better-when-you-understand-the-machinery-underneath","Good abstractions become even better when you understand the machinery underneath",[11,1197,1198],{},"This is probably my biggest takeaway.",[11,1200,1201],{},"As developers, we often use abstractions without understanding how they work.",[11,1203,1204],{},"We write:",[86,1206,1208],{"className":88,"code":1207,"language":90,"meta":91,"style":91},"computed(...)\n",[20,1209,1210],{"__ignoreMap":91},[95,1211,1212,1214,1217,1220],{"class":97,"line":98},[95,1213,29],{"class":108},[95,1215,1216],{"class":101},"(",[95,1218,1219],{"class":201},"...",[95,1221,1222],{"class":101},")\n",[11,1224,1225],{},"and move on.",[11,1227,1204],{},[86,1229,1231],{"className":183,"code":1230,"language":185,"meta":91,"style":91},"v-for\n",[20,1232,1233],{"__ignoreMap":91},[95,1234,1235],{"class":97,"line":98},[95,1236,1230],{"class":101},[11,1238,1225],{},[11,1240,1204],{},[86,1242,1244],{"className":88,"code":1243,"language":90,"meta":91,"style":91},"store.someReferenceData\n",[20,1245,1246],{"__ignoreMap":91},[95,1247,1248],{"class":97,"line":98},[95,1249,1243],{"class":101},[11,1251,1225],{},[11,1253,1254],{},"That’s perfectly fine most of the time.",[11,1256,1257],{},"But occasionally, it’s worth stopping and asking:",[11,1259,1260],{},"“What is actually happening here?”",[11,1262,1263],{},"Understanding the machinery underneath makes you better at designing with the abstraction.",[11,1265,1266],{},"I didn’t change my architecture after learning more about Vue’s reactivity.",[11,1268,1269],{},"Instead, I became more confident in it.",[11,1271,1272],{},"And that’s arguably more valuable.",[75,1274,1276],{"id":1275},"vues-reactivity-is-one-of-those-features-you-appreciate-more-the-deeper-you-go","Vue’s reactivity is one of those features you appreciate more the deeper you go",[11,1278,1279],{},"When I first started using Vue, reactivity was simply one of the things that made Vue convenient.",[11,1281,1282],{},"Now I see it differently.",[11,1284,1285],{},"It’s not just:",[11,1287,1288],{},"“Change a variable and the UI updates.”",[11,1290,1291],{},"It’s a dependency-tracking system that allows you to build layers of derived state without manually coordinating every update.",[11,1293,1294],{},"For configuration-driven enterprise applications, that’s incredibly useful.",[11,1296,1297],{},"And perhaps that’s why Vue continues to feel so natural to me.",[11,1299,1300],{},"I don’t spend much time thinking:",[11,1302,1303],{},"“How do I make this component re-render?”",[11,1305,1306],{},"I spend more time thinking:",[11,1308,1309],{},"“What does this piece of UI depend on?”",[11,1311,1312],{},"Once that relationship is expressed correctly, Vue takes care of the propagation.",[75,1314,1316],{"id":1315},"the-framework-matters-less-than-understanding-the-model","The framework matters less than understanding the model",[11,1318,1319],{},"I still wouldn’t choose Vue over React simply because of reactivity.",[11,1321,1322],{},"There are many factors involved:",[639,1324,1325,1328,1331,1334,1337,1340,1343],{},[642,1326,1327],{},"ecosystem",[642,1329,1330],{},"team expertise",[642,1332,1333],{},"hiring",[642,1335,1336],{},"platform requirements",[642,1338,1339],{},"tooling",[642,1341,1342],{},"architecture",[642,1344,1345],{},"project constraints",[11,1347,1348],{},"But understanding the difference between their models has made me appreciate Vue much more.",[11,1350,1351],{},"And it reminded me of something I’ve learned repeatedly as an engineer:",[11,1353,1354],{},"The best abstractions are the ones you can use without thinking about their complexity—but understand well enough to reason about when something goes wrong.",[11,1356,1357],{},"Vue’s reactivity is one of those abstractions for me.",[11,1359,1360],{},"I used it for years.",[11,1362,1363],{},"Then one day I stopped and asked:",[11,1365,1366],{},"“How does this actually work?”",[11,1368,1369],{},"I’m glad I did.",[11,1371,1372],{},"Because now, when I look at a seemingly simple computed property driving a configuration-heavy UI, I don’t just see some framework magic.",[11,1374,1375],{},"I see a carefully designed reactive system doing exactly what it was built to do.",[11,1377,1378],{},"Thanks for reading.",[11,1380,1381],{},"Happy coding!",[1383,1384,1385],"style",{},"html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s9eBZ, html code.shiki .s9eBZ{--shiki-default:#22863A;--shiki-dark:#85E89D}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":91,"searchDepth":105,"depth":105,"links":1387},[1388,1389,1390,1391,1392,1393,1394,1395,1396,1397,1398,1399,1400,1401],{"id":77,"depth":105,"text":78},{"id":412,"depth":105,"text":413},{"id":614,"depth":105,"text":615},{"id":662,"depth":105,"text":663},{"id":696,"depth":105,"text":697},{"id":792,"depth":105,"text":793},{"id":925,"depth":105,"text":926},{"id":1025,"depth":105,"text":1026},{"id":1056,"depth":105,"text":1057},{"id":1105,"depth":105,"text":1106},{"id":1142,"depth":105,"text":1143},{"id":1194,"depth":105,"text":1195},{"id":1275,"depth":105,"text":1276},{"id":1315,"depth":105,"text":1316},"2026-08-26","Sometimes you understand a technology best when you stop using it and start asking why it works—Vue Proxies, virtual DOM, computed dependencies, and React comparisons.",false,"md",{"src":1407,"alt":1408},"\u002Fimages\u002Fblog\u002Fvues-reactivity-is-better-than-i-realized-cover.jpg","Cover graphic exploring Vue reactivity, dependency tracking, and virtual DOM updates",{},true,"\u002Fblog\u002Fvues-reactivity-is-better-than-i-realized",{"title":6,"description":1403},"blog\u002Fvues-reactivity-is-better-than-i-realized",[1415,1416,1417,1418,1419],"Vuejs","React","JavaScript","Software Development","Front End Development",null,"U9P0B1kX_ZVj45QMlLvEDKA9ja6_PWf8fBd5lFoGJ88",{"id":1423,"title":1424,"body":1425,"date":1402,"description":3672,"draft":1404,"extension":1405,"featured":1404,"image":3673,"meta":3676,"navigation":1410,"path":3677,"seo":3678,"stem":3679,"tags":3680,"updatedAt":1420,"__hash__":3683},"blog\u002Fblog\u002Fyour-enterprise-web-app-isnt-broken-your-browser-put-it-to-sleep.md","Your Enterprise Web App Isn’t Broken — Your Browser Put It to Sleep",{"type":8,"value":1426,"toc":3647},[1427,1430,1433,1436,1439,1442,1445,1448,1451,1454,1457,1460,1463,1466,1475,1479,1482,1485,1488,1491,1514,1517,1520,1523,1526,1529,1532,1535,1538,1542,1545,1548,1551,1554,1574,1577,1580,1583,1586,1590,1593,1596,1599,1602,1605,1611,1614,1617,1620,1623,1626,1629,1669,1676,1679,1681,1897,1903,1906,1909,1912,1918,1921,1924,1927,1930,1933,1936,1944,1947,1950,1954,1957,1960,1963,1966,1969,1972,1975,1978,1981,2016,2019,2022,2025,2028,2032,2035,2038,2041,2044,2067,2070,2073,2076,2079,2087,2090,2134,2137,2140,2143,2146,2149,2195,2198,2201,2204,2207,2210,2213,2216,2219,2222,2225,2228,2231,2234,2241,2249,2255,2258,2267,2270,2283,2285,2364,2367,2370,2373,2376,2380,2383,2465,2468,2562,2565,2568,2570,2576,2579,2582,2588,2591,2594,2598,2601,2603,2628,2631,2791,2794,2797,2800,2803,2806,2809,2812,2816,2819,2822,2837,2840,2843,2860,2863,2866,2869,2880,2883,2886,2890,2893,2896,2899,2902,2905,2908,2911,2914,2917,2920,2923,2925,2958,2961,2966,2969,2973,2976,2978,3353,3356,3358,3382,3385,3388,3391,3394,3397,3420,3424,3427,3430,3433,3439,3442,3448,3451,3457,3460,3463,3467,3470,3475,3478,3481,3484,3488,3491,3494,3497,3501,3504,3507,3510,3514,3517,3520,3523,3526,3529,3532,3535,3538,3541,3545,3548,3551,3554,3557,3560,3563,3566,3569,3572,3575,3578,3582,3585,3588,3591,3594,3622,3625,3628,3631,3634,3637,3640,3642,3644],[11,1428,1429],{},"How to handle long-lived browser tabs with the Page Visibility API",[11,1431,1432],{},"You open an enterprise portal at 9:00 AM. You work on it for a while, then switch to another application. The portal stays open in a browser tab. Hours later, you come back. The page is still there. It looks completely normal.",[11,1434,1435],{},"You click a button.",[11,1437,1438],{},"Nothing happens.",[11,1440,1441],{},"You click again.",[11,1443,1444],{},"Still nothing.",[11,1446,1447],{},"Maybe a dropdown doesn’t open. Maybe a request never completes. Maybe a component feels sluggish. Maybe something that worked perfectly in the morning suddenly behaves strangely.",[11,1449,1450],{},"So you refresh the page.",[11,1452,1453],{},"Everything works again.",[11,1455,1456],{},"If you’ve worked with large enterprise web applications, you’ve probably encountered some version of this.",[11,1458,1459],{},"The application wasn’t necessarily “broken.”",[11,1461,1462],{},"The browser had simply been optimizing a page that had been sitting unused for hours.",[11,1464,1465],{},"And this creates an interesting problem for enterprise applications: how do you gracefully handle a web application that users leave open for long periods of time?",[11,1467,59,1468,67,1472,73],{},[61,1469,66],{"href":1470,"rel":1471},"https:\u002F\u002Flevelup.gitconnected.com\u002Fyour-enterprise-web-app-isnt-broken-your-browser-put-it-to-sleep-92293ff0392b",[65],[61,1473,72],{"href":70,"rel":1474},[65],[75,1476,1478],{"id":1477},"the-enterprise-tab-problem","The Enterprise Tab Problem",[11,1480,1481],{},"Enterprise applications are different from many consumer websites.",[11,1483,1484],{},"A typical consumer website might be opened, used for a few minutes, and closed.",[11,1486,1487],{},"Enterprise applications can have a very different lifecycle.",[11,1489,1490],{},"A user might:",[639,1492,1493,1496,1499,1502,1505,1508,1511],{},[642,1494,1495],{},"Open the application at the beginning of their workday.",[642,1497,1498],{},"Keep the tab open for eight hours.",[642,1500,1501],{},"Switch between several applications.",[642,1503,1504],{},"Return to the portal occasionally.",[642,1506,1507],{},"Leave it in the background overnight.",[642,1509,1510],{},"Open multiple instances of the same portal.",[642,1512,1513],{},"Expect everything to work exactly as it did when they left.",[11,1515,1516],{},"This creates a situation where the lifetime of the browser tab can be much longer than the lifetime of the application’s internal state.",[11,1518,1519],{},"Your JavaScript application may have been initialized hours ago.",[11,1521,1522],{},"Your authentication token may have expired.",[11,1524,1525],{},"Your API connection may no longer be usable.",[11,1527,1528],{},"Your browser may have throttled background execution.",[11,1530,1531],{},"The browser may even have discarded parts of the page under memory pressure.",[11,1533,1534],{},"Yet from the user’s perspective, there is still just a tab sitting there.",[11,1536,1537],{},"And when they return, they naturally expect it to work.",[75,1539,1541],{"id":1540},"what-happens-to-a-background-tab","What Happens to a Background Tab?",[11,1543,1544],{},"Modern browsers are very good at managing resources.",[11,1546,1547],{},"Keeping every tab running at full speed all the time would waste CPU, memory, battery, and network resources.",[11,1549,1550],{},"So browsers can aggressively optimize pages that aren’t currently visible.",[11,1552,1553],{},"Depending on the browser, operating system, and available resources, a background page can experience things such as:",[639,1555,1556,1559,1562,1565,1568,1571],{},[642,1557,1558],{},"JavaScript timer throttling",[642,1560,1561],{},"Reduced CPU activity",[642,1563,1564],{},"Suspended or frozen page execution",[642,1566,1567],{},"Network activity being affected",[642,1569,1570],{},"Memory pressure causing a page to be discarded",[642,1572,1573],{},"Resources needing to be restored when the page becomes active again",[11,1575,1576],{},"This is normally a good thing.",[11,1578,1579],{},"If I have 30 tabs open and I’m only using one of them, I don’t want all 30 applications consuming CPU continuously.",[11,1581,1582],{},"The problem is that enterprise applications are often designed with the assumption that their application state remains continuously available.",[11,1584,1585],{},"That’s not always a safe assumption.",[75,1587,1589],{"id":1588},"the-authentication-token-problem","The Authentication Token Problem",[11,1591,1592],{},"There is another problem that often appears with long-lived tabs: authentication tokens expire.",[11,1594,1595],{},"Imagine that your application obtains an access token when the user logs in.",[11,1597,1598],{},"The user works normally for a while.",[11,1600,1601],{},"Then they leave the application in the background for several hours.",[11,1603,1604],{},"When they eventually return, the access token may have expired.",[11,1606,1607,1608,73],{},"If we simply send the next API request with that expired token, the backend will reject it with a ",[20,1609,1610],{},"401 Unauthorized",[11,1612,1613],{},"One way to handle this is to let the request fail and refresh the token after receiving the 401.",[11,1615,1616],{},"But I prefer a more proactive approach:",[11,1618,1619],{},"Check whether the token is about to expire before sending the request.",[11,1621,1622],{},"If you’re using Axios, this can be handled elegantly with a request interceptor.",[11,1624,1625],{},"When we obtain the token, we already know its expiration time. We can store that information alongside the token.",[11,1627,1628],{},"For example:",[86,1630,1632],{"className":88,"code":1631,"language":90,"meta":91,"style":91},"interface AuthToken {\n  accessToken: string\n  expiresAt: number\n}\n",[20,1633,1634,1644,1655,1665],{"__ignoreMap":91},[95,1635,1636,1639,1642],{"class":97,"line":98},[95,1637,1638],{"class":201},"interface",[95,1640,1641],{"class":108}," AuthToken",[95,1643,353],{"class":101},[95,1645,1646,1649,1652],{"class":97,"line":105},[95,1647,1648],{"class":370},"  accessToken",[95,1650,1651],{"class":201},":",[95,1653,1654],{"class":338}," string\n",[95,1656,1657,1660,1662],{"class":97,"line":122},[95,1658,1659],{"class":370},"  expiresAt",[95,1661,1651],{"class":201},[95,1663,1664],{"class":338}," number\n",[95,1666,1667],{"class":97,"line":134},[95,1668,174],{"class":101},[11,1670,1671,1672,1675],{},"The ",[20,1673,1674],{},"expiresAt"," value can represent the expiration time as a Unix timestamp.",[11,1677,1678],{},"Then, before every HTTP request, the Axios interceptor can check whether the existing token is still valid.",[11,1680,325],{},[86,1682,1684],{"className":88,"code":1683,"language":90,"meta":91,"style":91},"axios.interceptors.request.use(async (config) => {\n  const token = getStoredToken()\n\n  if (!token) {\n    return config\n  }\n\n  const isExpired = Date.now() >= token.expiresAt\n\n  if (isExpired) {\n    const refreshedToken = await refreshTokenSilently()\n    storeToken(refreshedToken)\n    config.headers.Authorization = `Bearer ${refreshedToken.accessToken}`\n    return config\n  }\n\n  config.headers.Authorization = `Bearer ${token.accessToken}`\n  return config\n})\n",[20,1685,1686,1711,1727,1732,1745,1753,1758,1762,1786,1791,1799,1818,1827,1849,1856,1861,1866,1885,1892],{"__ignoreMap":91},[95,1687,1688,1691,1694,1696,1699,1702,1705,1707,1709],{"class":97,"line":98},[95,1689,1690],{"class":101},"axios.interceptors.request.",[95,1692,1693],{"class":108},"use",[95,1695,1216],{"class":101},[95,1697,1698],{"class":201},"async",[95,1700,1701],{"class":101}," (",[95,1703,1704],{"class":370},"config",[95,1706,373],{"class":101},[95,1708,350],{"class":201},[95,1710,353],{"class":101},[95,1712,1713,1716,1719,1721,1724],{"class":97,"line":105},[95,1714,1715],{"class":201},"  const",[95,1717,1718],{"class":338}," token",[95,1720,341],{"class":201},[95,1722,1723],{"class":108}," getStoredToken",[95,1725,1726],{"class":101},"()\n",[95,1728,1729],{"class":97,"line":122},[95,1730,1731],{"emptyLinePlaceholder":1410},"\n",[95,1733,1734,1737,1739,1742],{"class":97,"line":134},[95,1735,1736],{"class":201},"  if",[95,1738,1701],{"class":101},[95,1740,1741],{"class":201},"!",[95,1743,1744],{"class":101},"token) {\n",[95,1746,1747,1750],{"class":97,"line":147},[95,1748,1749],{"class":201},"    return",[95,1751,1752],{"class":101}," config\n",[95,1754,1755],{"class":97,"line":160},[95,1756,1757],{"class":101},"  }\n",[95,1759,1760],{"class":97,"line":171},[95,1761,1731],{"emptyLinePlaceholder":1410},[95,1763,1764,1766,1769,1771,1774,1777,1780,1783],{"class":97,"line":882},[95,1765,1715],{"class":201},[95,1767,1768],{"class":338}," isExpired",[95,1770,341],{"class":201},[95,1772,1773],{"class":101}," Date.",[95,1775,1776],{"class":108},"now",[95,1778,1779],{"class":101},"() ",[95,1781,1782],{"class":201},">=",[95,1784,1785],{"class":101}," token.expiresAt\n",[95,1787,1789],{"class":97,"line":1788},9,[95,1790,1731],{"emptyLinePlaceholder":1410},[95,1792,1794,1796],{"class":97,"line":1793},10,[95,1795,1736],{"class":201},[95,1797,1798],{"class":101}," (isExpired) {\n",[95,1800,1802,1805,1808,1810,1813,1816],{"class":97,"line":1801},11,[95,1803,1804],{"class":201},"    const",[95,1806,1807],{"class":338}," refreshedToken",[95,1809,341],{"class":201},[95,1811,1812],{"class":201}," await",[95,1814,1815],{"class":108}," refreshTokenSilently",[95,1817,1726],{"class":101},[95,1819,1821,1824],{"class":97,"line":1820},12,[95,1822,1823],{"class":108},"    storeToken",[95,1825,1826],{"class":101},"(refreshedToken)\n",[95,1828,1830,1833,1835,1838,1841,1843,1846],{"class":97,"line":1829},13,[95,1831,1832],{"class":101},"    config.headers.Authorization ",[95,1834,205],{"class":201},[95,1836,1837],{"class":115}," `Bearer ${",[95,1839,1840],{"class":101},"refreshedToken",[95,1842,73],{"class":115},[95,1844,1845],{"class":101},"accessToken",[95,1847,1848],{"class":115},"}`\n",[95,1850,1852,1854],{"class":97,"line":1851},14,[95,1853,1749],{"class":201},[95,1855,1752],{"class":101},[95,1857,1859],{"class":97,"line":1858},15,[95,1860,1757],{"class":101},[95,1862,1864],{"class":97,"line":1863},16,[95,1865,1731],{"emptyLinePlaceholder":1410},[95,1867,1869,1872,1874,1876,1879,1881,1883],{"class":97,"line":1868},17,[95,1870,1871],{"class":101},"  config.headers.Authorization ",[95,1873,205],{"class":201},[95,1875,1837],{"class":115},[95,1877,1878],{"class":101},"token",[95,1880,73],{"class":115},[95,1882,1845],{"class":101},[95,1884,1848],{"class":115},[95,1886,1888,1890],{"class":97,"line":1887},18,[95,1889,358],{"class":201},[95,1891,1752],{"class":101},[95,1893,1895],{"class":97,"line":1894},19,[95,1896,403],{"class":101},[11,1898,1899,1900,73],{},"The important part here is the ",[20,1901,1902],{},"await",[11,1904,1905],{},"The interceptor can pause the outgoing request while the application silently obtains a fresh token.",[11,1907,1908],{},"Only after the new token has been obtained do we allow the original request to continue.",[11,1910,1911],{},"Conceptually, the lifecycle becomes:",[86,1913,1916],{"className":1914,"code":1915,"language":632,"meta":91},[630],"User returns to the application\n            ↓\nApplication makes an API request\n            ↓\nAxios request interceptor runs\n            ↓\nIs the token expired?\n       ↙              ↘\n     No                Yes\n      ↓                 ↓\nAttach token       Refresh silently\n      ↓                 ↓\n      └──────────┬──────┘\n                 ↓\n       Send original request\n",[20,1917,1915],{"__ignoreMap":91},[11,1919,1920],{},"This has an important advantage over waiting for a 401.",[11,1922,1923],{},"The application doesn’t need to intentionally make a request that we already know is going to fail.",[11,1925,1926],{},"Instead, authentication freshness is handled before the request reaches the backend.",[11,1928,1929],{},"Of course, the exact implementation depends on your authentication provider and token lifecycle. You may also want to refresh slightly before the actual expiration time rather than waiting until the exact expiry timestamp, to account for network latency and clock differences.",[11,1931,1932],{},"For example, you could consider a token stale when it has less than a few minutes remaining.",[11,1934,1935],{},"This gives us a clean separation of responsibilities:",[639,1937,1938,1941],{},[642,1939,1940],{},"Authentication freshness is handled by the HTTP\u002Fauthentication layer.",[642,1942,1943],{},"Application freshness is a separate problem.",[11,1945,1946],{},"Even if the token is successfully refreshed, the frontend application itself may still have been sitting in the background for several hours.",[11,1948,1949],{},"And that’s where the stale-tab problem begins.",[75,1951,1953],{"id":1952},"a-stale-application-is-a-different-problem","A Stale Application Is a Different Problem",[11,1955,1956],{},"Consider this scenario.",[11,1958,1959],{},"The user hasn’t touched the application for six hours.",[11,1961,1962],{},"They return.",[11,1964,1965],{},"Their authentication token is refreshed successfully.",[11,1967,1968],{},"The next API request works.",[11,1970,1971],{},"But the UI still behaves strangely.",[11,1973,1974],{},"Why?",[11,1976,1977],{},"Because authentication is only one part of the application.",[11,1979,1980],{},"A large frontend application can have many pieces of state:",[639,1982,1983,1986,1989,1992,1995,1998,2001,2004,2007,2010,2013],{},[642,1984,1985],{},"Component state",[642,1987,1988],{},"Pinia\u002Fstore state",[642,1990,1991],{},"Cached API responses",[642,1993,1994],{},"WebSocket connections",[642,1996,1997],{},"Event listeners",[642,1999,2000],{},"Timers",[642,2002,2003],{},"Background polling",[642,2005,2006],{},"Third-party libraries",[642,2008,2009],{},"Browser resources",[642,2011,2012],{},"Loaded application chunks",[642,2014,2015],{},"Client-side routing state",[11,2017,2018],{},"Some of those things may no longer represent a healthy application state after a very long period of inactivity.",[11,2020,2021],{},"This is what I mean by a stale application.",[11,2023,2024],{},"The application hasn’t necessarily crashed.",[11,2026,2027],{},"It has simply been alive for much longer than the lifecycle you originally expected.",[75,2029,2031],{"id":2030},"why-refreshing-can-sometimes-be-the-right-solution","Why Refreshing Can Sometimes Be the Right Solution",[11,2033,2034],{},"As frontend developers, we often try to make applications recover from everything.",[11,2036,2037],{},"And that’s usually a good instinct.",[11,2039,2040],{},"But there is a point where recovery logic becomes more complicated than simply starting the application again.",[11,2042,2043],{},"Think about what a full page refresh gives you:",[639,2045,2046,2049,2052,2055,2058,2061,2064],{},[642,2047,2048],{},"JavaScript is loaded again.",[642,2050,2051],{},"Application state is initialized again.",[642,2053,2054],{},"Routes are reconstructed.",[642,2056,2057],{},"API clients are recreated.",[642,2059,2060],{},"Resources are requested again.",[642,2062,2063],{},"Authentication state is re-established.",[642,2065,2066],{},"The application starts from a known state.",[11,2068,2069],{},"In other words:",[11,2071,2072],{},"A refresh gives the application a clean lifecycle.",[11,2074,2075],{},"For a complex enterprise portal that has been sitting untouched for hours, that can be a very reasonable recovery strategy.",[11,2077,2078],{},"The key is to do it deliberately and gracefully, rather than surprising the user.",[75,2080,2082,2083,2086],{"id":2081},"why-settimeout-isnt-enough","Why ",[20,2084,2085],{},"setTimeout()"," Isn’t Enough",[11,2088,2089],{},"The first solution that might come to mind is simple:",[86,2091,2093],{"className":88,"code":2092,"language":90,"meta":91,"style":91},"setTimeout(() => {\n  refreshApplication()\n}, 60 * 60 * 1000)\n",[20,2094,2095,2106,2113],{"__ignoreMap":91},[95,2096,2097,2100,2102,2104],{"class":97,"line":98},[95,2098,2099],{"class":108},"setTimeout",[95,2101,347],{"class":101},[95,2103,350],{"class":201},[95,2105,353],{"class":101},[95,2107,2108,2111],{"class":97,"line":105},[95,2109,2110],{"class":108},"  refreshApplication",[95,2112,1726],{"class":101},[95,2114,2115,2118,2121,2124,2127,2129,2132],{"class":97,"line":122},[95,2116,2117],{"class":101},"}, ",[95,2119,2120],{"class":338},"60",[95,2122,2123],{"class":201}," *",[95,2125,2126],{"class":338}," 60",[95,2128,2123],{"class":201},[95,2130,2131],{"class":338}," 1000",[95,2133,1222],{"class":101},[11,2135,2136],{},"Wait one hour, then refresh.",[11,2138,2139],{},"Unfortunately, this isn’t a reliable way to detect one hour of user inactivity.",[11,2141,2142],{},"The problem is that the browser controls how background pages are executed.",[11,2144,2145],{},"When your page becomes hidden, JavaScript timers may be throttled or otherwise deprioritized.",[11,2147,2148],{},"So if you write:",[86,2150,2152],{"className":88,"code":2151,"language":90,"meta":91,"style":91},"setTimeout(() => {\n  console.log('One hour passed')\n}, 60 * 60 * 1000)\n",[20,2153,2154,2164,2179],{"__ignoreMap":91},[95,2155,2156,2158,2160,2162],{"class":97,"line":98},[95,2157,2099],{"class":108},[95,2159,347],{"class":101},[95,2161,350],{"class":201},[95,2163,353],{"class":101},[95,2165,2166,2169,2172,2174,2177],{"class":97,"line":105},[95,2167,2168],{"class":101},"  console.",[95,2170,2171],{"class":108},"log",[95,2173,1216],{"class":101},[95,2175,2176],{"class":115},"'One hour passed'",[95,2178,1222],{"class":101},[95,2180,2181,2183,2185,2187,2189,2191,2193],{"class":97,"line":122},[95,2182,2117],{"class":101},[95,2184,2120],{"class":338},[95,2186,2123],{"class":201},[95,2188,2126],{"class":338},[95,2190,2123],{"class":201},[95,2192,2131],{"class":338},[95,2194,1222],{"class":101},[11,2196,2197],{},"you are essentially assuming:",[11,2199,2200],{},"“The browser will allow my JavaScript to execute exactly one hour from now.”",[11,2202,2203],{},"That’s not something you should rely on for background tabs.",[11,2205,2206],{},"The timer might execute later than expected.",[11,2208,2209],{},"The page might be frozen.",[11,2211,2212],{},"The page might be discarded.",[11,2214,2215],{},"The user might come back before the timer executes.",[11,2217,2218],{},"And even if the timer does eventually execute, it doesn’t really answer the question we care about.",[11,2220,2221],{},"The question isn’t:",[11,2223,2224],{},"“Did my JavaScript timer run for an hour?”",[11,2226,2227],{},"The question is:",[11,2229,2230],{},"“How long was the user away from this application?”",[11,2232,2233],{},"That’s a different problem.",[75,2235,2237,2238],{"id":2236},"the-better-approach-visibilitychange","The Better Approach: ",[20,2239,2240],{},"visibilitychange",[11,2242,2243,2244,73],{},"Fortunately, browsers provide an API specifically designed for this kind of scenario: the ",[61,2245,2248],{"href":2246,"rel":2247},"https:\u002F\u002Fdeveloper.mozilla.org\u002Fen-US\u002Fdocs\u002FWeb\u002FAPI\u002FPage_Visibility_API",[65],"Page Visibility API",[11,2250,2251,2252,2254],{},"The most useful part for our case is the ",[20,2253,2240],{}," event.",[11,2256,2257],{},"We can listen for changes to:",[86,2259,2261],{"className":88,"code":2260,"language":90,"meta":91,"style":91},"document.visibilityState\n",[20,2262,2263],{"__ignoreMap":91},[95,2264,2265],{"class":97,"line":98},[95,2266,2260],{"class":101},[11,2268,2269],{},"which can be:",[639,2271,2272,2277],{},[642,2273,2274],{},[20,2275,2276],{},"'visible'",[642,2278,2279,2280],{},"or ",[20,2281,2282],{},"'hidden'",[11,2284,1628],{},[86,2286,2288],{"className":88,"code":2287,"language":90,"meta":91,"style":91},"document.addEventListener('visibilitychange', () => {\n  if (document.visibilityState === 'hidden') {\n    \u002F\u002F The page is no longer visible\n  }\n\n  if (document.visibilityState === 'visible') {\n    \u002F\u002F The user has returned\n  }\n})\n",[20,2289,2290,2310,2325,2330,2334,2338,2351,2356,2360],{"__ignoreMap":91},[95,2291,2292,2295,2298,2300,2303,2306,2308],{"class":97,"line":98},[95,2293,2294],{"class":101},"document.",[95,2296,2297],{"class":108},"addEventListener",[95,2299,1216],{"class":101},[95,2301,2302],{"class":115},"'visibilitychange'",[95,2304,2305],{"class":101},", () ",[95,2307,350],{"class":201},[95,2309,353],{"class":101},[95,2311,2312,2314,2317,2319,2322],{"class":97,"line":105},[95,2313,1736],{"class":201},[95,2315,2316],{"class":101}," (document.visibilityState ",[95,2318,990],{"class":201},[95,2320,2321],{"class":115}," 'hidden'",[95,2323,2324],{"class":101},") {\n",[95,2326,2327],{"class":97,"line":122},[95,2328,2329],{"class":382},"    \u002F\u002F The page is no longer visible\n",[95,2331,2332],{"class":97,"line":134},[95,2333,1757],{"class":101},[95,2335,2336],{"class":97,"line":147},[95,2337,1731],{"emptyLinePlaceholder":1410},[95,2339,2340,2342,2344,2346,2349],{"class":97,"line":160},[95,2341,1736],{"class":201},[95,2343,2316],{"class":101},[95,2345,990],{"class":201},[95,2347,2348],{"class":115}," 'visible'",[95,2350,2324],{"class":101},[95,2352,2353],{"class":97,"line":171},[95,2354,2355],{"class":382},"    \u002F\u002F The user has returned\n",[95,2357,2358],{"class":97,"line":882},[95,2359,1757],{"class":101},[95,2361,2362],{"class":97,"line":1788},[95,2363,403],{"class":101},[11,2365,2366],{},"This gives us something much more useful than a timer.",[11,2368,2369],{},"We know when the page became hidden.",[11,2371,2372],{},"And we know when it became visible again.",[11,2374,2375],{},"Now we can measure the actual elapsed time.",[75,2377,2379],{"id":2378},"store-a-timestamp-not-a-timer","Store a Timestamp, Not a Timer",[11,2381,2382],{},"When the page becomes hidden, record the current time.",[86,2384,2386],{"className":88,"code":2385,"language":90,"meta":91,"style":91},"let hiddenAt: number | null = null\n\ndocument.addEventListener('visibilitychange', () => {\n  if (document.visibilityState === 'hidden') {\n    hiddenAt = Date.now()\n  }\n})\n",[20,2387,2388,2412,2416,2432,2444,2457,2461],{"__ignoreMap":91},[95,2389,2390,2393,2396,2398,2401,2404,2407,2409],{"class":97,"line":98},[95,2391,2392],{"class":201},"let",[95,2394,2395],{"class":101}," hiddenAt",[95,2397,1651],{"class":201},[95,2399,2400],{"class":338}," number",[95,2402,2403],{"class":201}," |",[95,2405,2406],{"class":338}," null",[95,2408,341],{"class":201},[95,2410,2411],{"class":338}," null\n",[95,2413,2414],{"class":97,"line":105},[95,2415,1731],{"emptyLinePlaceholder":1410},[95,2417,2418,2420,2422,2424,2426,2428,2430],{"class":97,"line":122},[95,2419,2294],{"class":101},[95,2421,2297],{"class":108},[95,2423,1216],{"class":101},[95,2425,2302],{"class":115},[95,2427,2305],{"class":101},[95,2429,350],{"class":201},[95,2431,353],{"class":101},[95,2433,2434,2436,2438,2440,2442],{"class":97,"line":134},[95,2435,1736],{"class":201},[95,2437,2316],{"class":101},[95,2439,990],{"class":201},[95,2441,2321],{"class":115},[95,2443,2324],{"class":101},[95,2445,2446,2449,2451,2453,2455],{"class":97,"line":147},[95,2447,2448],{"class":101},"    hiddenAt ",[95,2450,205],{"class":201},[95,2452,1773],{"class":101},[95,2454,1776],{"class":108},[95,2456,1726],{"class":101},[95,2458,2459],{"class":97,"line":160},[95,2460,1757],{"class":101},[95,2462,2463],{"class":97,"line":171},[95,2464,403],{"class":101},[11,2466,2467],{},"Then when the user returns:",[86,2469,2471],{"className":88,"code":2470,"language":90,"meta":91,"style":91},"document.addEventListener('visibilitychange', () => {\n  if (document.visibilityState === 'visible' && hiddenAt) {\n    const inactiveFor = Date.now() - hiddenAt\n    console.log(`Inactive for ${inactiveFor}ms`)\n    hiddenAt = null\n  }\n})\n",[20,2472,2473,2489,2505,2526,2546,2554,2558],{"__ignoreMap":91},[95,2474,2475,2477,2479,2481,2483,2485,2487],{"class":97,"line":98},[95,2476,2294],{"class":101},[95,2478,2297],{"class":108},[95,2480,1216],{"class":101},[95,2482,2302],{"class":115},[95,2484,2305],{"class":101},[95,2486,350],{"class":201},[95,2488,353],{"class":101},[95,2490,2491,2493,2495,2497,2499,2502],{"class":97,"line":105},[95,2492,1736],{"class":201},[95,2494,2316],{"class":101},[95,2496,990],{"class":201},[95,2498,2348],{"class":115},[95,2500,2501],{"class":201}," &&",[95,2503,2504],{"class":101}," hiddenAt) {\n",[95,2506,2507,2509,2512,2514,2516,2518,2520,2523],{"class":97,"line":122},[95,2508,1804],{"class":201},[95,2510,2511],{"class":338}," inactiveFor",[95,2513,341],{"class":201},[95,2515,1773],{"class":101},[95,2517,1776],{"class":108},[95,2519,1779],{"class":101},[95,2521,2522],{"class":201},"-",[95,2524,2525],{"class":101}," hiddenAt\n",[95,2527,2528,2531,2533,2535,2538,2541,2544],{"class":97,"line":134},[95,2529,2530],{"class":101},"    console.",[95,2532,2171],{"class":108},[95,2534,1216],{"class":101},[95,2536,2537],{"class":115},"`Inactive for ${",[95,2539,2540],{"class":101},"inactiveFor",[95,2542,2543],{"class":115},"}ms`",[95,2545,1222],{"class":101},[95,2547,2548,2550,2552],{"class":97,"line":147},[95,2549,2448],{"class":101},[95,2551,205],{"class":201},[95,2553,2411],{"class":338},[95,2555,2556],{"class":97,"line":160},[95,2557,1757],{"class":101},[95,2559,2560],{"class":97,"line":171},[95,2561,403],{"class":101},[11,2563,2564],{},"Now we’re no longer asking the browser to execute something after an hour.",[11,2566,2567],{},"We’re simply comparing two timestamps.",[11,2569,1628],{},[86,2571,2574],{"className":2572,"code":2573,"language":632,"meta":91},[630],"Page hidden:\n09:15:32\n\nPage visible:\n14:42:18\n",[20,2575,2573],{"__ignoreMap":91},[11,2577,2578],{},"The browser doesn’t need to continuously execute JavaScript during those five hours.",[11,2580,2581],{},"When the user returns, we can calculate:",[86,2583,2586],{"className":2584,"code":2585,"language":632,"meta":91},[630],"14:42:18 - 09:15:32\n",[20,2587,2585],{"__ignoreMap":91},[11,2589,2590],{},"and determine how long the application was actually hidden.",[11,2592,2593],{},"This is a much more robust approach.",[75,2595,2597],{"id":2596},"adding-an-inactivity-threshold","Adding an Inactivity Threshold",[11,2599,2600],{},"Now we can define a threshold.",[11,2602,1628],{},[86,2604,2606],{"className":88,"code":2605,"language":90,"meta":91,"style":91},"const ONE_HOUR = 60 * 60 * 1000\n",[20,2607,2608],{"__ignoreMap":91},[95,2609,2610,2612,2615,2617,2619,2621,2623,2625],{"class":97,"line":98},[95,2611,335],{"class":201},[95,2613,2614],{"class":338}," ONE_HOUR",[95,2616,341],{"class":201},[95,2618,2126],{"class":338},[95,2620,2123],{"class":201},[95,2622,2126],{"class":338},[95,2624,2123],{"class":201},[95,2626,2627],{"class":338}," 1000\n",[11,2629,2630],{},"Then:",[86,2632,2634],{"className":88,"code":2633,"language":90,"meta":91,"style":91},"let hiddenAt: number | null = null\n\ndocument.addEventListener('visibilitychange', () => {\n  if (document.visibilityState === 'hidden') {\n    hiddenAt = Date.now()\n    return\n  }\n\n  if (document.visibilityState === 'visible' && hiddenAt) {\n    const inactiveFor = Date.now() - hiddenAt\n\n    if (inactiveFor >= ONE_HOUR) {\n      \u002F\u002F Application has been inactive for too long\n    }\n\n    hiddenAt = null\n  }\n})\n",[20,2635,2636,2654,2658,2674,2686,2698,2703,2707,2711,2725,2743,2747,2761,2766,2771,2775,2783,2787],{"__ignoreMap":91},[95,2637,2638,2640,2642,2644,2646,2648,2650,2652],{"class":97,"line":98},[95,2639,2392],{"class":201},[95,2641,2395],{"class":101},[95,2643,1651],{"class":201},[95,2645,2400],{"class":338},[95,2647,2403],{"class":201},[95,2649,2406],{"class":338},[95,2651,341],{"class":201},[95,2653,2411],{"class":338},[95,2655,2656],{"class":97,"line":105},[95,2657,1731],{"emptyLinePlaceholder":1410},[95,2659,2660,2662,2664,2666,2668,2670,2672],{"class":97,"line":122},[95,2661,2294],{"class":101},[95,2663,2297],{"class":108},[95,2665,1216],{"class":101},[95,2667,2302],{"class":115},[95,2669,2305],{"class":101},[95,2671,350],{"class":201},[95,2673,353],{"class":101},[95,2675,2676,2678,2680,2682,2684],{"class":97,"line":134},[95,2677,1736],{"class":201},[95,2679,2316],{"class":101},[95,2681,990],{"class":201},[95,2683,2321],{"class":115},[95,2685,2324],{"class":101},[95,2687,2688,2690,2692,2694,2696],{"class":97,"line":147},[95,2689,2448],{"class":101},[95,2691,205],{"class":201},[95,2693,1773],{"class":101},[95,2695,1776],{"class":108},[95,2697,1726],{"class":101},[95,2699,2700],{"class":97,"line":160},[95,2701,2702],{"class":201},"    return\n",[95,2704,2705],{"class":97,"line":171},[95,2706,1757],{"class":101},[95,2708,2709],{"class":97,"line":882},[95,2710,1731],{"emptyLinePlaceholder":1410},[95,2712,2713,2715,2717,2719,2721,2723],{"class":97,"line":1788},[95,2714,1736],{"class":201},[95,2716,2316],{"class":101},[95,2718,990],{"class":201},[95,2720,2348],{"class":115},[95,2722,2501],{"class":201},[95,2724,2504],{"class":101},[95,2726,2727,2729,2731,2733,2735,2737,2739,2741],{"class":97,"line":1793},[95,2728,1804],{"class":201},[95,2730,2511],{"class":338},[95,2732,341],{"class":201},[95,2734,1773],{"class":101},[95,2736,1776],{"class":108},[95,2738,1779],{"class":101},[95,2740,2522],{"class":201},[95,2742,2525],{"class":101},[95,2744,2745],{"class":97,"line":1801},[95,2746,1731],{"emptyLinePlaceholder":1410},[95,2748,2749,2752,2755,2757,2759],{"class":97,"line":1820},[95,2750,2751],{"class":201},"    if",[95,2753,2754],{"class":101}," (inactiveFor ",[95,2756,1782],{"class":201},[95,2758,2614],{"class":338},[95,2760,2324],{"class":101},[95,2762,2763],{"class":97,"line":1829},[95,2764,2765],{"class":382},"      \u002F\u002F Application has been inactive for too long\n",[95,2767,2768],{"class":97,"line":1851},[95,2769,2770],{"class":101},"    }\n",[95,2772,2773],{"class":97,"line":1858},[95,2774,1731],{"emptyLinePlaceholder":1410},[95,2776,2777,2779,2781],{"class":97,"line":1863},[95,2778,2448],{"class":101},[95,2780,205],{"class":201},[95,2782,2411],{"class":338},[95,2784,2785],{"class":97,"line":1868},[95,2786,1757],{"class":101},[95,2788,2789],{"class":97,"line":1887},[95,2790,403],{"class":101},[11,2792,2793],{},"This gives us a simple rule:",[11,2795,2796],{},"If the application has been hidden for more than one hour, consider it stale.",[11,2798,2799],{},"The actual threshold doesn’t have to be one hour.",[11,2801,2802],{},"It depends on the application.",[11,2804,2805],{},"For some applications, 15 minutes might be appropriate.",[11,2807,2808],{},"For others, 2 hours might make more sense.",[11,2810,2811],{},"The important part is that the threshold should be based on the application’s expected lifecycle.",[75,2813,2815],{"id":2814},"dont-automatically-refresh-without-warning","Don’t Automatically Refresh Without Warning",[11,2817,2818],{},"There’s one important UX problem with the refresh strategy.",[11,2820,2821],{},"Imagine a user comes back after two hours and your application immediately executes:",[86,2823,2825],{"className":88,"code":2824,"language":90,"meta":91,"style":91},"window.location.reload()\n",[20,2826,2827],{"__ignoreMap":91},[95,2828,2829,2832,2835],{"class":97,"line":98},[95,2830,2831],{"class":101},"window.location.",[95,2833,2834],{"class":108},"reload",[95,2836,1726],{"class":101},[11,2838,2839],{},"That could be a terrible experience.",[11,2841,2842],{},"The user might have:",[639,2844,2845,2848,2851,2854,2857],{},[642,2846,2847],{},"Unsaved form data",[642,2849,2850],{},"A partially completed workflow",[642,2852,2853],{},"A draft",[642,2855,2856],{},"A selected configuration",[642,2858,2859],{},"Filters they spent time setting up",[11,2861,2862],{},"A silent refresh can destroy that state.",[11,2864,2865],{},"Instead, I prefer prompting the user.",[11,2867,2868],{},"Something like:",[43,2870,2871,2874,2877],{},[11,2872,2873],{},"You’ve been away for a while",[11,2875,2876],{},"This application has been inactive for more than an hour. Refresh the application to make sure everything is up to date.",[11,2878,2879],{},"Refresh application",[11,2881,2882],{},"This gives the user control.",[11,2884,2885],{},"And it communicates what is happening instead of making the application appear to randomly reload.",[75,2887,2889],{"id":2888},"what-about-unsaved-work","What About Unsaved Work?",[11,2891,2892],{},"This is probably the most important consideration before implementing this pattern.",[11,2894,2895],{},"A stale-tab strategy should never blindly assume that refreshing is safe.",[11,2897,2898],{},"For a read-only dashboard, refreshing after an hour might be completely harmless.",[11,2900,2901],{},"For a data-entry portal, it could be dangerous.",[11,2903,2904],{},"Consider a user who spends 45 minutes filling out a complex form.",[11,2906,2907],{},"They switch tabs for an hour to gather some information.",[11,2909,2910],{},"They come back.",[11,2912,2913],{},"Your application detects the inactivity and refreshes.",[11,2915,2916],{},"Their work is gone.",[11,2918,2919],{},"That’s unacceptable.",[11,2921,2922],{},"So the implementation should consider whether the user has unsaved changes.",[11,2924,1628],{},[86,2926,2928],{"className":88,"code":2927,"language":90,"meta":91,"style":91},"if (isApplicationStale && !hasUnsavedChanges) {\n  showRefreshPrompt()\n}\n",[20,2929,2930,2947,2954],{"__ignoreMap":91},[95,2931,2932,2935,2938,2941,2944],{"class":97,"line":98},[95,2933,2934],{"class":201},"if",[95,2936,2937],{"class":101}," (isApplicationStale ",[95,2939,2940],{"class":201},"&&",[95,2942,2943],{"class":201}," !",[95,2945,2946],{"class":101},"hasUnsavedChanges) {\n",[95,2948,2949,2952],{"class":97,"line":105},[95,2950,2951],{"class":108},"  showRefreshPrompt",[95,2953,1726],{"class":101},[95,2955,2956],{"class":97,"line":122},[95,2957,174],{"class":101},[11,2959,2960],{},"Or, if there are unsaved changes, you could show a stronger warning:",[43,2962,2963],{},[11,2964,2965],{},"This application has been inactive for a long time. You have unsaved changes. Please save your work before refreshing.",[11,2967,2968],{},"The exact behaviour depends heavily on the application.",[75,2970,2972],{"id":2971},"turning-it-into-a-reusable-vue-composable","Turning It Into a Reusable Vue Composable",[11,2974,2975],{},"If you’re using Vue, this logic can be extracted into a reusable composable.",[11,2977,2868],{},[86,2979,2981],{"className":88,"code":2980,"language":90,"meta":91,"style":91},"import { onMounted, onUnmounted, ref } from 'vue'\n\nexport function useTabInactivity(\n  threshold = 60 * 60 * 1000,\n) {\n  const isStale = ref(false)\n  const inactiveFor = ref(0)\n\n  let hiddenAt: number | null = null\n\n  const handleVisibilityChange = () => {\n    if (document.visibilityState === 'hidden') {\n      hiddenAt = Date.now()\n      return\n    }\n\n    if (document.visibilityState === 'visible' && hiddenAt) {\n      inactiveFor.value = Date.now() - hiddenAt\n      isStale.value = inactiveFor.value >= threshold\n      hiddenAt = null\n    }\n  }\n\n  onMounted(() => {\n    document.addEventListener(\n      'visibilitychange',\n      handleVisibilityChange,\n    )\n  })\n\n  onUnmounted(() => {\n    document.removeEventListener(\n      'visibilitychange',\n      handleVisibilityChange,\n    )\n  })\n\n  return {\n    isStale,\n    inactiveFor,\n  }\n}\n",[20,2982,2983,2997,3001,3014,3033,3037,3055,3072,3076,3095,3099,3115,3127,3140,3145,3149,3153,3167,3184,3199,3208,3213,3218,3223,3235,3245,3253,3259,3265,3270,3275,3287,3297,3304,3309,3314,3319,3324,3331,3337,3343,3348],{"__ignoreMap":91},[95,2984,2985,2988,2991,2994],{"class":97,"line":98},[95,2986,2987],{"class":201},"import",[95,2989,2990],{"class":101}," { onMounted, onUnmounted, ref } ",[95,2992,2993],{"class":201},"from",[95,2995,2996],{"class":115}," 'vue'\n",[95,2998,2999],{"class":97,"line":105},[95,3000,1731],{"emptyLinePlaceholder":1410},[95,3002,3003,3006,3009,3012],{"class":97,"line":122},[95,3004,3005],{"class":201},"export",[95,3007,3008],{"class":201}," function",[95,3010,3011],{"class":108}," useTabInactivity",[95,3013,976],{"class":101},[95,3015,3016,3019,3021,3023,3025,3027,3029,3031],{"class":97,"line":134},[95,3017,3018],{"class":370},"  threshold",[95,3020,341],{"class":201},[95,3022,2126],{"class":338},[95,3024,2123],{"class":201},[95,3026,2126],{"class":338},[95,3028,2123],{"class":201},[95,3030,2131],{"class":338},[95,3032,119],{"class":101},[95,3034,3035],{"class":97,"line":147},[95,3036,2324],{"class":101},[95,3038,3039,3041,3044,3046,3048,3050,3053],{"class":97,"line":160},[95,3040,1715],{"class":201},[95,3042,3043],{"class":338}," isStale",[95,3045,341],{"class":201},[95,3047,433],{"class":108},[95,3049,1216],{"class":101},[95,3051,3052],{"class":338},"false",[95,3054,1222],{"class":101},[95,3056,3057,3059,3061,3063,3065,3067,3070],{"class":97,"line":171},[95,3058,1715],{"class":201},[95,3060,2511],{"class":338},[95,3062,341],{"class":201},[95,3064,433],{"class":108},[95,3066,1216],{"class":101},[95,3068,3069],{"class":338},"0",[95,3071,1222],{"class":101},[95,3073,3074],{"class":97,"line":882},[95,3075,1731],{"emptyLinePlaceholder":1410},[95,3077,3078,3081,3083,3085,3087,3089,3091,3093],{"class":97,"line":1788},[95,3079,3080],{"class":201},"  let",[95,3082,2395],{"class":101},[95,3084,1651],{"class":201},[95,3086,2400],{"class":338},[95,3088,2403],{"class":201},[95,3090,2406],{"class":338},[95,3092,341],{"class":201},[95,3094,2411],{"class":338},[95,3096,3097],{"class":97,"line":1793},[95,3098,1731],{"emptyLinePlaceholder":1410},[95,3100,3101,3103,3106,3108,3111,3113],{"class":97,"line":1801},[95,3102,1715],{"class":201},[95,3104,3105],{"class":108}," handleVisibilityChange",[95,3107,341],{"class":201},[95,3109,3110],{"class":101}," () ",[95,3112,350],{"class":201},[95,3114,353],{"class":101},[95,3116,3117,3119,3121,3123,3125],{"class":97,"line":1820},[95,3118,2751],{"class":201},[95,3120,2316],{"class":101},[95,3122,990],{"class":201},[95,3124,2321],{"class":115},[95,3126,2324],{"class":101},[95,3128,3129,3132,3134,3136,3138],{"class":97,"line":1829},[95,3130,3131],{"class":101},"      hiddenAt ",[95,3133,205],{"class":201},[95,3135,1773],{"class":101},[95,3137,1776],{"class":108},[95,3139,1726],{"class":101},[95,3141,3142],{"class":97,"line":1851},[95,3143,3144],{"class":201},"      return\n",[95,3146,3147],{"class":97,"line":1858},[95,3148,2770],{"class":101},[95,3150,3151],{"class":97,"line":1863},[95,3152,1731],{"emptyLinePlaceholder":1410},[95,3154,3155,3157,3159,3161,3163,3165],{"class":97,"line":1868},[95,3156,2751],{"class":201},[95,3158,2316],{"class":101},[95,3160,990],{"class":201},[95,3162,2348],{"class":115},[95,3164,2501],{"class":201},[95,3166,2504],{"class":101},[95,3168,3169,3172,3174,3176,3178,3180,3182],{"class":97,"line":1887},[95,3170,3171],{"class":101},"      inactiveFor.value ",[95,3173,205],{"class":201},[95,3175,1773],{"class":101},[95,3177,1776],{"class":108},[95,3179,1779],{"class":101},[95,3181,2522],{"class":201},[95,3183,2525],{"class":101},[95,3185,3186,3189,3191,3194,3196],{"class":97,"line":1894},[95,3187,3188],{"class":101},"      isStale.value ",[95,3190,205],{"class":201},[95,3192,3193],{"class":101}," inactiveFor.value ",[95,3195,1782],{"class":201},[95,3197,3198],{"class":101}," threshold\n",[95,3200,3202,3204,3206],{"class":97,"line":3201},20,[95,3203,3131],{"class":101},[95,3205,205],{"class":201},[95,3207,2411],{"class":338},[95,3209,3211],{"class":97,"line":3210},21,[95,3212,2770],{"class":101},[95,3214,3216],{"class":97,"line":3215},22,[95,3217,1757],{"class":101},[95,3219,3221],{"class":97,"line":3220},23,[95,3222,1731],{"emptyLinePlaceholder":1410},[95,3224,3226,3229,3231,3233],{"class":97,"line":3225},24,[95,3227,3228],{"class":108},"  onMounted",[95,3230,347],{"class":101},[95,3232,350],{"class":201},[95,3234,353],{"class":101},[95,3236,3238,3241,3243],{"class":97,"line":3237},25,[95,3239,3240],{"class":101},"    document.",[95,3242,2297],{"class":108},[95,3244,976],{"class":101},[95,3246,3248,3251],{"class":97,"line":3247},26,[95,3249,3250],{"class":115},"      'visibilitychange'",[95,3252,119],{"class":101},[95,3254,3256],{"class":97,"line":3255},27,[95,3257,3258],{"class":101},"      handleVisibilityChange,\n",[95,3260,3262],{"class":97,"line":3261},28,[95,3263,3264],{"class":101},"    )\n",[95,3266,3268],{"class":97,"line":3267},29,[95,3269,398],{"class":101},[95,3271,3273],{"class":97,"line":3272},30,[95,3274,1731],{"emptyLinePlaceholder":1410},[95,3276,3278,3281,3283,3285],{"class":97,"line":3277},31,[95,3279,3280],{"class":108},"  onUnmounted",[95,3282,347],{"class":101},[95,3284,350],{"class":201},[95,3286,353],{"class":101},[95,3288,3290,3292,3295],{"class":97,"line":3289},32,[95,3291,3240],{"class":101},[95,3293,3294],{"class":108},"removeEventListener",[95,3296,976],{"class":101},[95,3298,3300,3302],{"class":97,"line":3299},33,[95,3301,3250],{"class":115},[95,3303,119],{"class":101},[95,3305,3307],{"class":97,"line":3306},34,[95,3308,3258],{"class":101},[95,3310,3312],{"class":97,"line":3311},35,[95,3313,3264],{"class":101},[95,3315,3317],{"class":97,"line":3316},36,[95,3318,398],{"class":101},[95,3320,3322],{"class":97,"line":3321},37,[95,3323,1731],{"emptyLinePlaceholder":1410},[95,3325,3327,3329],{"class":97,"line":3326},38,[95,3328,358],{"class":201},[95,3330,353],{"class":101},[95,3332,3334],{"class":97,"line":3333},39,[95,3335,3336],{"class":101},"    isStale,\n",[95,3338,3340],{"class":97,"line":3339},40,[95,3341,3342],{"class":101},"    inactiveFor,\n",[95,3344,3346],{"class":97,"line":3345},41,[95,3347,1757],{"class":101},[95,3349,3351],{"class":97,"line":3350},42,[95,3352,174],{"class":101},[11,3354,3355],{},"Then your application can decide what to do with that information.",[11,3357,1628],{},[86,3359,3361],{"className":88,"code":3360,"language":90,"meta":91,"style":91},"const { isStale } = useTabInactivity()\n",[20,3362,3363],{"__ignoreMap":91},[95,3364,3365,3367,3370,3373,3376,3378,3380],{"class":97,"line":98},[95,3366,335],{"class":201},[95,3368,3369],{"class":101}," { ",[95,3371,3372],{"class":338},"isStale",[95,3374,3375],{"class":101}," } ",[95,3377,205],{"class":201},[95,3379,3011],{"class":108},[95,3381,1726],{"class":101},[11,3383,3384],{},"And your UI can react accordingly.",[11,3386,3387],{},"This separation is useful because the composable doesn’t need to know how your application should recover.",[11,3389,3390],{},"It simply answers:",[11,3392,3393],{},"“Has this tab been inactive for too long?”",[11,3395,3396],{},"The application can then decide whether to:",[639,3398,3399,3402,3405,3408,3411,3414,3417],{},[642,3400,3401],{},"Refresh",[642,3403,3404],{},"Show a modal",[642,3406,3407],{},"Reinitialize some state",[642,3409,3410],{},"Reconnect a WebSocket",[642,3412,3413],{},"Refresh data",[642,3415,3416],{},"Ask the user to log in again",[642,3418,3419],{},"Do nothing",[75,3421,3423],{"id":3422},"refresh-everything-or-recover-selectively","Refresh Everything or Recover Selectively?",[11,3425,3426],{},"A full refresh isn’t always the best solution.",[11,3428,3429],{},"It’s simply one possible solution.",[11,3431,3432],{},"For a relatively simple application, you might only need to refresh your data:",[86,3434,3437],{"className":3435,"code":3436,"language":632,"meta":91},[630],"Tab becomes visible\n        ↓\nWas hidden > 1 hour?\n        ↓\n       Yes\n        ↓\nRefresh API data\n",[20,3438,3436],{"__ignoreMap":91},[11,3440,3441],{},"For a more complex application, you might need:",[86,3443,3446],{"className":3444,"code":3445,"language":632,"meta":91},[630],"Tab becomes visible\n        ↓\nWas hidden > 1 hour?\n        ↓\n       Yes\n        ↓\nCheck authentication\n        ↓\nReconnect services\n        ↓\nRefresh application state\n",[20,3447,3445],{"__ignoreMap":91},[11,3449,3450],{},"And for an application where you can’t guarantee that all state can be recovered correctly:",[86,3452,3455],{"className":3453,"code":3454,"language":632,"meta":91},[630],"Tab becomes visible\n        ↓\nWas hidden > 1 hour?\n        ↓\n       Yes\n        ↓\nAsk user to refresh\n",[20,3456,3454],{"__ignoreMap":91},[11,3458,3459],{},"There’s no universally correct answer.",[11,3461,3462],{},"The right strategy depends on how much state your application maintains and how safely that state can be reconstructed.",[75,3464,3466],{"id":3465},"a-useful-mental-model","A Useful Mental Model",[11,3468,3469],{},"I think about long-lived browser tabs in terms of three separate problems.",[3471,3472,3474],"h3",{"id":3473},"_1-authentication-freshness","1. Authentication freshness",[11,3476,3477],{},"“Is my user’s session\u002Ftoken still valid?”",[11,3479,3480],{},"Handle this at the authentication\u002FAPI layer.",[11,3482,3483],{},"For example, Axios interceptors can detect authentication failures and refresh tokens where appropriate.",[3471,3485,3487],{"id":3486},"_2-application-freshness","2. Application freshness",[11,3489,3490],{},"“Is the application itself still in a healthy state?”",[11,3492,3493],{},"This is where stale tabs become interesting.",[11,3495,3496],{},"A successful API request doesn’t necessarily mean the entire frontend is healthy.",[3471,3498,3500],{"id":3499},"_3-data-freshness","3. Data freshness",[11,3502,3503],{},"“Is the information displayed to the user still current?”",[11,3505,3506],{},"This might require refetching data when the user returns.",[11,3508,3509],{},"These are different problems and shouldn’t necessarily be solved with the same mechanism.",[75,3511,3513],{"id":3512},"dont-blame-the-browser","Don’t Blame the Browser",[11,3515,3516],{},"It’s tempting to think:",[11,3518,3519],{},"“The browser broke my application.”",[11,3521,3522],{},"But that’s not really what’s happening.",[11,3524,3525],{},"The browser is doing exactly what it’s designed to do.",[11,3527,3528],{},"If a tab hasn’t been visible for several hours, aggressively optimizing it is reasonable.",[11,3530,3531],{},"The application is the thing making an assumption:",[11,3533,3534],{},"“Once I initialize, I can remain alive indefinitely.”",[11,3536,3537],{},"Modern browsers don’t guarantee that.",[11,3539,3540],{},"So instead of fighting the browser, we should design our applications around the browser’s lifecycle.",[75,3542,3544],{"id":3543},"the-bigger-lesson","The Bigger Lesson",[11,3546,3547],{},"The web isn’t a place where your JavaScript gets to run continuously forever.",[11,3549,3550],{},"A browser tab has a lifecycle.",[11,3552,3553],{},"It can become visible.",[11,3555,3556],{},"It can become hidden.",[11,3558,3559],{},"It can be throttled.",[11,3561,3562],{},"It can be frozen.",[11,3564,3565],{},"It can potentially be discarded.",[11,3567,3568],{},"And eventually, it can be closed.",[11,3570,3571],{},"For short-lived applications, these details might never matter.",[11,3573,3574],{},"For enterprise applications that users keep open for eight, ten, or even more hours, they become much more important.",[11,3576,3577],{},"That’s why handling long-lived tabs should be considered part of application design rather than an unusual edge case.",[75,3579,3581],{"id":3580},"final-thoughts","Final Thoughts",[11,3583,3584],{},"The solution I ended up preferring is deliberately simple.",[11,3586,3587],{},"I don’t try to predict exactly what the browser did while the application was in the background.",[11,3589,3590],{},"I don’t rely on a timer running continuously for an hour.",[11,3592,3593],{},"Instead:",[639,3595,3596,3601,3604,3607,3610,3613,3616,3619],{},[642,3597,3598,3599,73],{},"Listen for ",[20,3600,2240],{},[642,3602,3603],{},"Record when the page becomes hidden.",[642,3605,3606],{},"When it becomes visible again, calculate the elapsed time.",[642,3608,3609],{},"If the inactive period exceeds a reasonable threshold, consider the application stale.",[642,3611,3612],{},"Let authentication interceptors handle expired tokens independently.",[642,3614,3615],{},"Decide whether the application can safely recover itself.",[642,3617,3618],{},"If not, ask the user to refresh.",[642,3620,3621],{},"Never blindly destroy unsaved user work.",[11,3623,3624],{},"The important idea isn’t really “refresh the page after one hour.”",[11,3626,3627],{},"It’s this:",[11,3629,3630],{},"Design your enterprise web application with the assumption that the browser may put it to sleep while the user is away.",[11,3632,3633],{},"Once you start thinking about the browser tab as having its own lifecycle, a lot of these seemingly random “the portal was working five hours ago, but now it’s acting weird” problems become much easier to reason about.",[11,3635,3636],{},"And sometimes, the best recovery strategy isn’t a complicated recovery system.",[11,3638,3639],{},"Sometimes, it’s simply giving the application a chance to start fresh.",[11,3641,1378],{},[11,3643,1381],{},[1383,3645,3646],{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":91,"searchDepth":105,"depth":105,"links":3648},[3649,3650,3651,3652,3653,3654,3656,3658,3659,3660,3661,3662,3663,3664,3669,3670,3671],{"id":1477,"depth":105,"text":1478},{"id":1540,"depth":105,"text":1541},{"id":1588,"depth":105,"text":1589},{"id":1952,"depth":105,"text":1953},{"id":2030,"depth":105,"text":2031},{"id":2081,"depth":105,"text":3655},"Why setTimeout() Isn’t Enough",{"id":2236,"depth":105,"text":3657},"The Better Approach: visibilitychange",{"id":2378,"depth":105,"text":2379},{"id":2596,"depth":105,"text":2597},{"id":2814,"depth":105,"text":2815},{"id":2888,"depth":105,"text":2889},{"id":2971,"depth":105,"text":2972},{"id":3422,"depth":105,"text":3423},{"id":3465,"depth":105,"text":3466,"children":3665},[3666,3667,3668],{"id":3473,"depth":122,"text":3474},{"id":3486,"depth":122,"text":3487},{"id":3499,"depth":122,"text":3500},{"id":3512,"depth":105,"text":3513},{"id":3543,"depth":105,"text":3544},{"id":3580,"depth":105,"text":3581},"How to handle long-lived browser tabs with the Page Visibility API—token refresh, stale-tab detection, and graceful recovery for enterprise portals.",{"src":3674,"alt":3675},"\u002Fimages\u002Fblog\u002Fyour-enterprise-web-app-isnt-broken-your-browser-put-it-to-sleep-cover.jpg","Cover graphic about enterprise web apps becoming stale when browsers put background tabs to sleep",{},"\u002Fblog\u002Fyour-enterprise-web-app-isnt-broken-your-browser-put-it-to-sleep",{"title":1424,"description":3672},"blog\u002Fyour-enterprise-web-app-isnt-broken-your-browser-put-it-to-sleep",[3681,3682,1417,1418,1415],"Web Development","Enterprise Architecture","vf7VRfqbyQOIPyW2BSze2DMGN008a4wj24SUQWbaUZU",{"id":3685,"title":3686,"body":3687,"date":5907,"description":3691,"draft":1404,"extension":1405,"featured":1410,"image":5908,"meta":5911,"navigation":1410,"path":5912,"seo":5913,"stem":5914,"tags":5915,"updatedAt":5918,"__hash__":5919},"blog\u002Fblog\u002F10-typescript-practices-that-make-your-codebase-better.md","10 TypeScript Practices That Make Your Codebase Better",{"type":8,"value":3688,"toc":5889},[3689,3692,3695,3703,3709,3712,3715,3718,3721,3747,3750,3753,3756,3759,3762,3766,3769,3772,3775,3778,3834,3837,3864,3867,3891,3894,3921,3924,3948,3951,3954,3996,3999,4002,4005,4008,4012,4015,4018,4021,4098,4101,4104,4107,4110,4113,4144,4147,4150,4153,4156,4160,4163,4185,4188,4191,4233,4236,4334,4336,4366,4369,4372,4375,4378,4381,4384,4392,4395,4433,4436,4526,4529,4532,4570,4576,4579,4585,4733,4736,4739,4742,4750,4753,4756,4758,4797,4800,4815,4818,4821,4856,4859,4862,4879,4882,4885,4893,4896,4936,4939,4942,4945,4948,4984,4987,5050,5053,5056,5059,5076,5079,5082,5086,5088,5132,5135,5138,5141,5196,5199,5284,5287,5290,5293,5296,5299,5303,5310,5313,5316,5319,5322,5327,5330,5333,5335,5341,5344,5347,5350,5353,5357,5360,5363,5366,5369,5372,5377,5380,5383,5386,5389,5531,5534,5537,5540,5543,5546,5549,5552,5555,5558,5562,5565,5568,5627,5630,5633,5635,5638,5641,5655,5658,5661,5664,5668,5671,5674,5677,5680,5751,5754,5757,5760,5763,5766,5769,5772,5775,5778,5782,5785,5836,5839,5843,5846,5849,5852,5855,5858,5861,5864,5867,5870,5873,5876,5879,5881,5884,5886],[11,3690,3691],{},"Practical patterns for writing TypeScript that remains readable, maintainable, and useful as your application grows.",[11,3693,3694],{},"This is Part 2 of a two-part series on writing better TypeScript.",[11,3696,3697,3698,3702],{},"In ",[61,3699,3701],{"href":3700},"\u002Fblog\u002Fyou-know-typescript-but-do-you-know-how-to-model-your-code-with-it","Part 1",", we looked at how to model domains and make invalid states harder to represent. This article focuses on the practical patterns that keep TypeScript code maintainable as the codebase grows.",[11,3704,59,3705,73],{},[61,3706,66],{"href":3707,"rel":3708},"https:\u002F\u002Fakshattriv.medium.com\u002F10-typescript-practices-that-make-your-codebase-better-1e458fb3275e",[65],[11,3710,3711],{},"Knowing TypeScript syntax is one thing.",[11,3713,3714],{},"Using TypeScript well in a large codebase is another.",[11,3716,3717],{},"Once an application grows beyond a few thousand lines, the biggest TypeScript problems usually aren’t caused by not knowing what a generic is.",[11,3719,3720],{},"They’re caused by things like:",[639,3722,3723,3726,3729,3732,3735,3738,3741,3744],{},[642,3724,3725],{},"abstractions that are too clever",[642,3727,3728],{},"types that are too broad",[642,3730,3731],{},"assertions everywhere",[642,3733,3734],{},"duplicated contracts",[642,3736,3737],{},"unreadable generics",[642,3739,3740],{},"functions with ambiguous APIs",[642,3742,3743],{},"types scattered across the project",[642,3745,3746],{},"compiler settings that aren’t taken seriously",[11,3748,3749],{},"In a small project, these problems might be harmless.",[11,3751,3752],{},"In a large application with dozens of developers, they compound.",[11,3754,3755],{},"A type that is slightly confusing today becomes technical debt six months later.",[11,3757,3758],{},"So this article isn’t about obscure TypeScript tricks.",[11,3760,3761],{},"It’s about practical decisions that make TypeScript code easier to understand and maintain.",[75,3763,3765],{"id":3764},"_1-use-generics-to-express-relationships","1. Use Generics to Express Relationships",[11,3767,3768],{},"Generics are often introduced as a way to make functions reusable.",[11,3770,3771],{},"That’s true.",[11,3773,3774],{},"But their more important purpose is expressing relationships between types.",[11,3776,3777],{},"Consider:",[86,3779,3781],{"className":88,"code":3780,"language":90,"meta":91,"style":91},"function getFirst\u003CT>(items: T[]): T | undefined {\n  return items[0]\n}\n",[20,3782,3783,3819,3830],{"__ignoreMap":91},[95,3784,3785,3787,3790,3792,3795,3798,3800,3802,3805,3808,3810,3812,3814,3817],{"class":97,"line":98},[95,3786,823],{"class":201},[95,3788,3789],{"class":108}," getFirst",[95,3791,192],{"class":101},[95,3793,3794],{"class":108},"T",[95,3796,3797],{"class":101},">(",[95,3799,1011],{"class":370},[95,3801,1651],{"class":201},[95,3803,3804],{"class":108}," T",[95,3806,3807],{"class":101},"[])",[95,3809,1651],{"class":201},[95,3811,3804],{"class":108},[95,3813,2403],{"class":201},[95,3815,3816],{"class":338}," undefined",[95,3818,353],{"class":101},[95,3820,3821,3823,3826,3828],{"class":97,"line":105},[95,3822,358],{"class":201},[95,3824,3825],{"class":101}," items[",[95,3827,3069],{"class":338},[95,3829,313],{"class":101},[95,3831,3832],{"class":97,"line":122},[95,3833,174],{"class":101},[11,3835,3836],{},"If you pass:",[86,3838,3840],{"className":88,"code":3839,"language":90,"meta":91,"style":91},"const users: User[] = [...]\n",[20,3841,3842],{"__ignoreMap":91},[95,3843,3844,3846,3848,3850,3852,3855,3857,3860,3862],{"class":97,"line":98},[95,3845,335],{"class":201},[95,3847,428],{"class":338},[95,3849,1651],{"class":201},[95,3851,826],{"class":108},[95,3853,3854],{"class":101},"[] ",[95,3856,205],{"class":201},[95,3858,3859],{"class":101}," [",[95,3861,1219],{"class":201},[95,3863,313],{"class":101},[11,3865,3866],{},"TypeScript knows:",[86,3868,3870],{"className":88,"code":3869,"language":90,"meta":91,"style":91},"const user = getFirst(users)\n\u002F\u002F User | undefined\n",[20,3871,3872,3886],{"__ignoreMap":91},[95,3873,3874,3876,3879,3881,3883],{"class":97,"line":98},[95,3875,335],{"class":201},[95,3877,3878],{"class":338}," user",[95,3880,341],{"class":201},[95,3882,3789],{"class":108},[95,3884,3885],{"class":101},"(users)\n",[95,3887,3888],{"class":97,"line":105},[95,3889,3890],{"class":382},"\u002F\u002F User | undefined\n",[11,3892,3893],{},"And if you pass:",[86,3895,3897],{"className":88,"code":3896,"language":90,"meta":91,"style":91},"const products: Product[] = [...]\n",[20,3898,3899],{"__ignoreMap":91},[95,3900,3901,3903,3906,3908,3911,3913,3915,3917,3919],{"class":97,"line":98},[95,3902,335],{"class":201},[95,3904,3905],{"class":338}," products",[95,3907,1651],{"class":201},[95,3909,3910],{"class":108}," Product",[95,3912,3854],{"class":101},[95,3914,205],{"class":201},[95,3916,3859],{"class":101},[95,3918,1219],{"class":201},[95,3920,313],{"class":101},[11,3922,3923],{},"you get:",[86,3925,3927],{"className":88,"code":3926,"language":90,"meta":91,"style":91},"const product = getFirst(products)\n\u002F\u002F Product | undefined\n",[20,3928,3929,3943],{"__ignoreMap":91},[95,3930,3931,3933,3936,3938,3940],{"class":97,"line":98},[95,3932,335],{"class":201},[95,3934,3935],{"class":338}," product",[95,3937,341],{"class":201},[95,3939,3789],{"class":108},[95,3941,3942],{"class":101},"(products)\n",[95,3944,3945],{"class":97,"line":105},[95,3946,3947],{"class":382},"\u002F\u002F Product | undefined\n",[11,3949,3950],{},"The generic preserves the relationship between input and output.",[11,3952,3953],{},"Compare that with:",[86,3955,3957],{"className":88,"code":3956,"language":90,"meta":91,"style":91},"function getFirst(items: unknown[]): unknown {\n  return items[0]\n}\n",[20,3958,3959,3982,3992],{"__ignoreMap":91},[95,3960,3961,3963,3965,3967,3969,3971,3974,3976,3978,3980],{"class":97,"line":98},[95,3962,823],{"class":201},[95,3964,3789],{"class":108},[95,3966,1216],{"class":101},[95,3968,1011],{"class":370},[95,3970,1651],{"class":201},[95,3972,3973],{"class":338}," unknown",[95,3975,3807],{"class":101},[95,3977,1651],{"class":201},[95,3979,3973],{"class":338},[95,3981,353],{"class":101},[95,3983,3984,3986,3988,3990],{"class":97,"line":105},[95,3985,358],{"class":201},[95,3987,3825],{"class":101},[95,3989,3069],{"class":338},[95,3991,313],{"class":101},[95,3993,3994],{"class":97,"line":122},[95,3995,174],{"class":101},[11,3997,3998],{},"Now you’ve lost that relationship.",[11,4000,4001],{},"A useful question when writing a generic is:",[11,4003,4004],{},"What relationship between these types am I trying to preserve?",[11,4006,4007],{},"If you can’t answer that, you might not need a generic.",[75,4009,4011],{"id":4010},"_2-dont-turn-everything-into-a-generic","2. Don’t Turn Everything Into a Generic",[11,4013,4014],{},"Generics are powerful.",[11,4016,4017],{},"That’s exactly why they are easy to overuse.",[11,4019,4020],{},"You can write something like:",[86,4022,4024],{"className":88,"code":4023,"language":90,"meta":91,"style":91},"function processData\u003C\n  T extends Record\u003Cstring, unknown>,\n  K extends keyof T,\n  R extends ...,\n>(...) {\n  \u002F\u002F ...\n}\n",[20,4025,4026,4036,4060,4074,4084,4089,4094],{"__ignoreMap":91},[95,4027,4028,4030,4033],{"class":97,"line":98},[95,4029,823],{"class":201},[95,4031,4032],{"class":108}," processData",[95,4034,4035],{"class":101},"\u003C\n",[95,4037,4038,4041,4044,4047,4049,4052,4054,4057],{"class":97,"line":105},[95,4039,4040],{"class":108},"  T",[95,4042,4043],{"class":201}," extends",[95,4045,4046],{"class":108}," Record",[95,4048,192],{"class":101},[95,4050,4051],{"class":338},"string",[95,4053,909],{"class":101},[95,4055,4056],{"class":338},"unknown",[95,4058,4059],{"class":101},">,\n",[95,4061,4062,4065,4067,4070,4072],{"class":97,"line":122},[95,4063,4064],{"class":108},"  K",[95,4066,4043],{"class":201},[95,4068,4069],{"class":201}," keyof",[95,4071,3804],{"class":108},[95,4073,119],{"class":101},[95,4075,4076,4079,4081],{"class":97,"line":134},[95,4077,4078],{"class":108},"  R",[95,4080,4043],{"class":201},[95,4082,4083],{"class":101}," ...,\n",[95,4085,4086],{"class":97,"line":147},[95,4087,4088],{"class":101},">(...) {\n",[95,4090,4091],{"class":97,"line":160},[95,4092,4093],{"class":382},"  \u002F\u002F ...\n",[95,4095,4096],{"class":97,"line":171},[95,4097,174],{"class":101},[11,4099,4100],{},"And feel pretty good about yourself.",[11,4102,4103],{},"Then someone else opens the file six months later.",[11,4105,4106],{},"They feel something else.",[11,4108,4109],{},"Complex generics introduce cognitive overhead.",[11,4111,4112],{},"If a simple function communicates the intent:",[86,4114,4116],{"className":88,"code":4115,"language":90,"meta":91,"style":91},"function processUser(user: User) {\n  \u002F\u002F ...\n}\n",[20,4117,4118,4136,4140],{"__ignoreMap":91},[95,4119,4120,4122,4125,4127,4130,4132,4134],{"class":97,"line":98},[95,4121,823],{"class":201},[95,4123,4124],{"class":108}," processUser",[95,4126,1216],{"class":101},[95,4128,4129],{"class":370},"user",[95,4131,1651],{"class":201},[95,4133,826],{"class":108},[95,4135,2324],{"class":101},[95,4137,4138],{"class":97,"line":105},[95,4139,4093],{"class":382},[95,4141,4142],{"class":97,"line":122},[95,4143,174],{"class":101},[11,4145,4146],{},"then use the simple function.",[11,4148,4149],{},"The goal isn’t to demonstrate how much TypeScript you know.",[11,4151,4152],{},"The goal is to make the code easier to understand.",[11,4154,4155],{},"Use generics when they express a meaningful relationship—not simply because they can.",[75,4157,4159],{"id":4158},"_3-prefer-type-narrowing-over-casting","3. Prefer Type Narrowing Over Casting",[11,4161,4162],{},"This is tempting:",[86,4164,4166],{"className":88,"code":4165,"language":90,"meta":91,"style":91},"const value = something as string\n",[20,4167,4168],{"__ignoreMap":91},[95,4169,4170,4172,4175,4177,4180,4183],{"class":97,"line":98},[95,4171,335],{"class":201},[95,4173,4174],{"class":338}," value",[95,4176,341],{"class":201},[95,4178,4179],{"class":101}," something ",[95,4181,4182],{"class":201},"as",[95,4184,1654],{"class":338},[11,4186,4187],{},"But you’re bypassing the compiler’s uncertainty.",[11,4189,4190],{},"Instead, narrow the value:",[86,4192,4194],{"className":88,"code":4193,"language":90,"meta":91,"style":91},"if (typeof something === 'string') {\n  console.log(something.toUpperCase())\n}\n",[20,4195,4196,4214,4229],{"__ignoreMap":91},[95,4197,4198,4200,4202,4205,4207,4209,4212],{"class":97,"line":98},[95,4199,2934],{"class":201},[95,4201,1701],{"class":101},[95,4203,4204],{"class":201},"typeof",[95,4206,4179],{"class":101},[95,4208,990],{"class":201},[95,4210,4211],{"class":115}," 'string'",[95,4213,2324],{"class":101},[95,4215,4216,4218,4220,4223,4226],{"class":97,"line":105},[95,4217,2168],{"class":101},[95,4219,2171],{"class":108},[95,4221,4222],{"class":101},"(something.",[95,4224,4225],{"class":108},"toUpperCase",[95,4227,4228],{"class":101},"())\n",[95,4230,4231],{"class":97,"line":122},[95,4232,174],{"class":101},[11,4234,4235],{},"For objects, create a type guard when appropriate:",[86,4237,4239],{"className":88,"code":4238,"language":90,"meta":91,"style":91},"function isUser(value: unknown): value is User {\n  return (\n    typeof value === 'object'\n    && value !== null\n    && 'id' in value\n    && 'name' in value\n  )\n}\n",[20,4240,4241,4271,4277,4290,4302,4315,4326,4330],{"__ignoreMap":91},[95,4242,4243,4245,4248,4250,4253,4255,4257,4260,4262,4264,4267,4269],{"class":97,"line":98},[95,4244,823],{"class":201},[95,4246,4247],{"class":108}," isUser",[95,4249,1216],{"class":101},[95,4251,4252],{"class":370},"value",[95,4254,1651],{"class":201},[95,4256,3973],{"class":338},[95,4258,4259],{"class":101},")",[95,4261,1651],{"class":201},[95,4263,4174],{"class":370},[95,4265,4266],{"class":201}," is",[95,4268,826],{"class":108},[95,4270,353],{"class":101},[95,4272,4273,4275],{"class":97,"line":105},[95,4274,358],{"class":201},[95,4276,836],{"class":101},[95,4278,4279,4282,4285,4287],{"class":97,"line":122},[95,4280,4281],{"class":201},"    typeof",[95,4283,4284],{"class":101}," value ",[95,4286,990],{"class":201},[95,4288,4289],{"class":115}," 'object'\n",[95,4291,4292,4295,4297,4300],{"class":97,"line":134},[95,4293,4294],{"class":201},"    &&",[95,4296,4284],{"class":101},[95,4298,4299],{"class":201},"!==",[95,4301,2411],{"class":338},[95,4303,4304,4306,4309,4312],{"class":97,"line":147},[95,4305,4294],{"class":201},[95,4307,4308],{"class":115}," 'id'",[95,4310,4311],{"class":201}," in",[95,4313,4314],{"class":370}," value\n",[95,4316,4317,4319,4322,4324],{"class":97,"line":160},[95,4318,4294],{"class":201},[95,4320,4321],{"class":115}," 'name'",[95,4323,4311],{"class":201},[95,4325,4314],{"class":370},[95,4327,4328],{"class":97,"line":171},[95,4329,879],{"class":101},[95,4331,4332],{"class":97,"line":882},[95,4333,174],{"class":101},[11,4335,2630],{},[86,4337,4339],{"className":88,"code":4338,"language":90,"meta":91,"style":91},"if (isUser(value)) {\n  console.log(value.name)\n}\n",[20,4340,4341,4353,4362],{"__ignoreMap":91},[95,4342,4343,4345,4347,4350],{"class":97,"line":98},[95,4344,2934],{"class":201},[95,4346,1701],{"class":101},[95,4348,4349],{"class":108},"isUser",[95,4351,4352],{"class":101},"(value)) {\n",[95,4354,4355,4357,4359],{"class":97,"line":105},[95,4356,2168],{"class":101},[95,4358,2171],{"class":108},[95,4360,4361],{"class":101},"(value.name)\n",[95,4363,4364],{"class":97,"line":122},[95,4365,174],{"class":101},[11,4367,4368],{},"The distinction is important:",[11,4370,4371],{},"Casting says:",[11,4373,4374],{},"“Trust me.”",[11,4376,4377],{},"Narrowing says:",[11,4379,4380],{},"“Here’s why this value is safe to treat this way.”",[11,4382,4383],{},"The second approach gives the type system something to work with.",[75,4385,4387,4388,4391],{"id":4386},"_4-use-never-to-catch-incomplete-logic","4. Use ",[20,4389,4390],{},"never"," to Catch Incomplete Logic",[11,4393,4394],{},"Imagine:",[86,4396,4398],{"className":88,"code":4397,"language":90,"meta":91,"style":91},"type Status =\n  | 'pending'\n  | 'approved'\n  | 'rejected'\n",[20,4399,4400,4411,4419,4426],{"__ignoreMap":91},[95,4401,4402,4405,4408],{"class":97,"line":98},[95,4403,4404],{"class":201},"type",[95,4406,4407],{"class":108}," Status",[95,4409,4410],{"class":201}," =\n",[95,4412,4413,4416],{"class":97,"line":105},[95,4414,4415],{"class":201},"  |",[95,4417,4418],{"class":115}," 'pending'\n",[95,4420,4421,4423],{"class":97,"line":122},[95,4422,4415],{"class":201},[95,4424,4425],{"class":115}," 'approved'\n",[95,4427,4428,4430],{"class":97,"line":134},[95,4429,4415],{"class":201},[95,4431,4432],{"class":115}," 'rejected'\n",[11,4434,4435],{},"You might write:",[86,4437,4439],{"className":88,"code":4438,"language":90,"meta":91,"style":91},"function getLabel(status: Status) {\n  switch (status) {\n    case 'pending':\n      return 'Pending'\n    case 'approved':\n      return 'Approved'\n    case 'rejected':\n      return 'Rejected'\n  }\n}\n",[20,4440,4441,4459,4467,4478,4486,4495,4502,4511,4518,4522],{"__ignoreMap":91},[95,4442,4443,4445,4448,4450,4453,4455,4457],{"class":97,"line":98},[95,4444,823],{"class":201},[95,4446,4447],{"class":108}," getLabel",[95,4449,1216],{"class":101},[95,4451,4452],{"class":370},"status",[95,4454,1651],{"class":201},[95,4456,4407],{"class":108},[95,4458,2324],{"class":101},[95,4460,4461,4464],{"class":97,"line":105},[95,4462,4463],{"class":201},"  switch",[95,4465,4466],{"class":101}," (status) {\n",[95,4468,4469,4472,4475],{"class":97,"line":122},[95,4470,4471],{"class":201},"    case",[95,4473,4474],{"class":115}," 'pending'",[95,4476,4477],{"class":101},":\n",[95,4479,4480,4483],{"class":97,"line":134},[95,4481,4482],{"class":201},"      return",[95,4484,4485],{"class":115}," 'Pending'\n",[95,4487,4488,4490,4493],{"class":97,"line":147},[95,4489,4471],{"class":201},[95,4491,4492],{"class":115}," 'approved'",[95,4494,4477],{"class":101},[95,4496,4497,4499],{"class":97,"line":160},[95,4498,4482],{"class":201},[95,4500,4501],{"class":115}," 'Approved'\n",[95,4503,4504,4506,4509],{"class":97,"line":171},[95,4505,4471],{"class":201},[95,4507,4508],{"class":115}," 'rejected'",[95,4510,4477],{"class":101},[95,4512,4513,4515],{"class":97,"line":882},[95,4514,4482],{"class":201},[95,4516,4517],{"class":115}," 'Rejected'\n",[95,4519,4520],{"class":97,"line":1788},[95,4521,1757],{"class":101},[95,4523,4524],{"class":97,"line":1793},[95,4525,174],{"class":101},[11,4527,4528],{},"That’s fine.",[11,4530,4531],{},"But what happens six months later when someone adds:",[86,4533,4535],{"className":88,"code":4534,"language":90,"meta":91,"style":91},"type Status =\n  | 'pending'\n  | 'approved'\n  | 'rejected'\n  | 'cancelled'\n",[20,4536,4537,4545,4551,4557,4563],{"__ignoreMap":91},[95,4538,4539,4541,4543],{"class":97,"line":98},[95,4540,4404],{"class":201},[95,4542,4407],{"class":108},[95,4544,4410],{"class":201},[95,4546,4547,4549],{"class":97,"line":105},[95,4548,4415],{"class":201},[95,4550,4418],{"class":115},[95,4552,4553,4555],{"class":97,"line":122},[95,4554,4415],{"class":201},[95,4556,4425],{"class":115},[95,4558,4559,4561],{"class":97,"line":134},[95,4560,4415],{"class":201},[95,4562,4432],{"class":115},[95,4564,4565,4567],{"class":97,"line":147},[95,4566,4415],{"class":201},[95,4568,4569],{"class":115}," 'cancelled'\n",[11,4571,4572,4573,73],{},"There might be dozens of places that depend on ",[20,4574,4575],{},"Status",[11,4577,4578],{},"You want the compiler to tell you where your assumptions are incomplete.",[11,4580,4581,4582,4584],{},"That’s where ",[20,4583,4390],{}," can help:",[86,4586,4588],{"className":88,"code":4587,"language":90,"meta":91,"style":91},"function assertNever(value: never): never {\n  throw new Error(`Unhandled value: ${value}`)\n}\n\nfunction getLabel(status: Status) {\n  switch (status) {\n    case 'pending':\n      return 'Pending'\n    case 'approved':\n      return 'Approved'\n    case 'rejected':\n      return 'Rejected'\n    default:\n      return assertNever(status)\n  }\n}\n",[20,4589,4590,4614,4637,4641,4645,4661,4667,4675,4681,4689,4695,4703,4709,4716,4725,4729],{"__ignoreMap":91},[95,4591,4592,4594,4597,4599,4601,4603,4606,4608,4610,4612],{"class":97,"line":98},[95,4593,823],{"class":201},[95,4595,4596],{"class":108}," assertNever",[95,4598,1216],{"class":101},[95,4600,4252],{"class":370},[95,4602,1651],{"class":201},[95,4604,4605],{"class":338}," never",[95,4607,4259],{"class":101},[95,4609,1651],{"class":201},[95,4611,4605],{"class":338},[95,4613,353],{"class":101},[95,4615,4616,4619,4622,4625,4627,4630,4632,4635],{"class":97,"line":105},[95,4617,4618],{"class":201},"  throw",[95,4620,4621],{"class":201}," new",[95,4623,4624],{"class":108}," Error",[95,4626,1216],{"class":101},[95,4628,4629],{"class":115},"`Unhandled value: ${",[95,4631,4252],{"class":101},[95,4633,4634],{"class":115},"}`",[95,4636,1222],{"class":101},[95,4638,4639],{"class":97,"line":122},[95,4640,174],{"class":101},[95,4642,4643],{"class":97,"line":134},[95,4644,1731],{"emptyLinePlaceholder":1410},[95,4646,4647,4649,4651,4653,4655,4657,4659],{"class":97,"line":147},[95,4648,823],{"class":201},[95,4650,4447],{"class":108},[95,4652,1216],{"class":101},[95,4654,4452],{"class":370},[95,4656,1651],{"class":201},[95,4658,4407],{"class":108},[95,4660,2324],{"class":101},[95,4662,4663,4665],{"class":97,"line":160},[95,4664,4463],{"class":201},[95,4666,4466],{"class":101},[95,4668,4669,4671,4673],{"class":97,"line":171},[95,4670,4471],{"class":201},[95,4672,4474],{"class":115},[95,4674,4477],{"class":101},[95,4676,4677,4679],{"class":97,"line":882},[95,4678,4482],{"class":201},[95,4680,4485],{"class":115},[95,4682,4683,4685,4687],{"class":97,"line":1788},[95,4684,4471],{"class":201},[95,4686,4492],{"class":115},[95,4688,4477],{"class":101},[95,4690,4691,4693],{"class":97,"line":1793},[95,4692,4482],{"class":201},[95,4694,4501],{"class":115},[95,4696,4697,4699,4701],{"class":97,"line":1801},[95,4698,4471],{"class":201},[95,4700,4508],{"class":115},[95,4702,4477],{"class":101},[95,4704,4705,4707],{"class":97,"line":1820},[95,4706,4482],{"class":201},[95,4708,4517],{"class":115},[95,4710,4711,4714],{"class":97,"line":1829},[95,4712,4713],{"class":201},"    default",[95,4715,4477],{"class":101},[95,4717,4718,4720,4722],{"class":97,"line":1851},[95,4719,4482],{"class":201},[95,4721,4596],{"class":108},[95,4723,4724],{"class":101},"(status)\n",[95,4726,4727],{"class":97,"line":1858},[95,4728,1757],{"class":101},[95,4730,4731],{"class":97,"line":1863},[95,4732,174],{"class":101},[11,4734,4735],{},"Now adding a new status produces a compiler error.",[11,4737,4738],{},"This is especially valuable in large codebases.",[11,4740,4741],{},"The type definition becomes the source of truth, and the compiler identifies all the places that need to change.",[75,4743,4745,4746,4749],{"id":4744},"_5-use-readonly-to-communicate-intent","5. Use ",[20,4747,4748],{},"readonly"," to Communicate Intent",[11,4751,4752],{},"Types aren’t only about what values are allowed.",[11,4754,4755],{},"They’re also about how those values are supposed to be used.",[11,4757,3777],{},[86,4759,4761],{"className":88,"code":4760,"language":90,"meta":91,"style":91},"type User = {\n  readonly id: string\n  name: string\n}\n",[20,4762,4763,4773,4785,4793],{"__ignoreMap":91},[95,4764,4765,4767,4769,4771],{"class":97,"line":98},[95,4766,4404],{"class":201},[95,4768,826],{"class":108},[95,4770,341],{"class":201},[95,4772,353],{"class":101},[95,4774,4775,4778,4781,4783],{"class":97,"line":105},[95,4776,4777],{"class":201},"  readonly",[95,4779,4780],{"class":370}," id",[95,4782,1651],{"class":201},[95,4784,1654],{"class":338},[95,4786,4787,4789,4791],{"class":97,"line":122},[95,4788,125],{"class":370},[95,4790,1651],{"class":201},[95,4792,1654],{"class":338},[95,4794,4795],{"class":97,"line":134},[95,4796,174],{"class":101},[11,4798,4799],{},"Now:",[86,4801,4803],{"className":88,"code":4802,"language":90,"meta":91,"style":91},"user.id = '123'\n",[20,4804,4805],{"__ignoreMap":91},[95,4806,4807,4810,4812],{"class":97,"line":98},[95,4808,4809],{"class":101},"user.id ",[95,4811,205],{"class":201},[95,4813,4814],{"class":115}," '123'\n",[11,4816,4817],{},"is rejected.",[11,4819,4820],{},"You can also make collections readonly:",[86,4822,4824],{"className":88,"code":4823,"language":90,"meta":91,"style":91},"function processUsers(users: readonly User[]) {\n  \u002F\u002F ...\n}\n",[20,4825,4826,4848,4852],{"__ignoreMap":91},[95,4827,4828,4830,4833,4835,4838,4840,4843,4845],{"class":97,"line":98},[95,4829,823],{"class":201},[95,4831,4832],{"class":108}," processUsers",[95,4834,1216],{"class":101},[95,4836,4837],{"class":370},"users",[95,4839,1651],{"class":201},[95,4841,4842],{"class":201}," readonly",[95,4844,826],{"class":108},[95,4846,4847],{"class":101},"[]) {\n",[95,4849,4850],{"class":97,"line":105},[95,4851,4093],{"class":382},[95,4853,4854],{"class":97,"line":122},[95,4855,174],{"class":101},[11,4857,4858],{},"Now the function can read the collection without implying that it owns or mutates it.",[11,4860,4861],{},"This is particularly useful for:",[639,4863,4864,4867,4870,4873,4876],{},[642,4865,4866],{},"configuration",[642,4868,4869],{},"shared data",[642,4871,4872],{},"constants",[642,4874,4875],{},"function parameters",[642,4877,4878],{},"immutable state",[11,4880,4881],{},"The benefit isn’t just preventing a mutation.",[11,4883,4884],{},"It’s communicating intent to every developer who reads the type.",[75,4886,4888,4889,4892],{"id":4887},"_6-dont-use-recordstring-unknown-as-a-replacement-for-real-types","6. Don’t Use ",[20,4890,4891],{},"Record\u003Cstring, unknown>"," as a Replacement for Real Types",[11,4894,4895],{},"You’ve probably seen code like:",[86,4897,4899],{"className":88,"code":4898,"language":90,"meta":91,"style":91},"function process(data: Record\u003Cstring, unknown>) {\n  \u002F\u002F ...\n}\n",[20,4900,4901,4928,4932],{"__ignoreMap":91},[95,4902,4903,4905,4908,4910,4913,4915,4917,4919,4921,4923,4925],{"class":97,"line":98},[95,4904,823],{"class":201},[95,4906,4907],{"class":108}," process",[95,4909,1216],{"class":101},[95,4911,4912],{"class":370},"data",[95,4914,1651],{"class":201},[95,4916,4046],{"class":108},[95,4918,192],{"class":101},[95,4920,4051],{"class":338},[95,4922,909],{"class":101},[95,4924,4056],{"class":338},[95,4926,4927],{"class":101},">) {\n",[95,4929,4930],{"class":97,"line":105},[95,4931,4093],{"class":382},[95,4933,4934],{"class":97,"line":122},[95,4935,174],{"class":101},[11,4937,4938],{},"Sometimes that’s exactly what you need.",[11,4940,4941],{},"If the function accepts arbitrary key-value data, great.",[11,4943,4944],{},"But if you actually know what the object represents, model it.",[11,4946,4947],{},"Instead of:",[86,4949,4950],{"className":88,"code":4898,"language":90,"meta":91,"style":91},[20,4951,4952,4976,4980],{"__ignoreMap":91},[95,4953,4954,4956,4958,4960,4962,4964,4966,4968,4970,4972,4974],{"class":97,"line":98},[95,4955,823],{"class":201},[95,4957,4907],{"class":108},[95,4959,1216],{"class":101},[95,4961,4912],{"class":370},[95,4963,1651],{"class":201},[95,4965,4046],{"class":108},[95,4967,192],{"class":101},[95,4969,4051],{"class":338},[95,4971,909],{"class":101},[95,4973,4056],{"class":338},[95,4975,4927],{"class":101},[95,4977,4978],{"class":97,"line":105},[95,4979,4093],{"class":382},[95,4981,4982],{"class":97,"line":122},[95,4983,174],{"class":101},[11,4985,4986],{},"use:",[86,4988,4990],{"className":88,"code":4989,"language":90,"meta":91,"style":91},"type User = {\n  id: string\n  name: string\n}\n\nfunction process(user: User) {\n  \u002F\u002F ...\n}\n",[20,4991,4992,5002,5010,5018,5022,5026,5042,5046],{"__ignoreMap":91},[95,4993,4994,4996,4998,5000],{"class":97,"line":98},[95,4995,4404],{"class":201},[95,4997,826],{"class":108},[95,4999,341],{"class":201},[95,5001,353],{"class":101},[95,5003,5004,5006,5008],{"class":97,"line":105},[95,5005,109],{"class":370},[95,5007,1651],{"class":201},[95,5009,1654],{"class":338},[95,5011,5012,5014,5016],{"class":97,"line":122},[95,5013,125],{"class":370},[95,5015,1651],{"class":201},[95,5017,1654],{"class":338},[95,5019,5020],{"class":97,"line":134},[95,5021,174],{"class":101},[95,5023,5024],{"class":97,"line":147},[95,5025,1731],{"emptyLinePlaceholder":1410},[95,5027,5028,5030,5032,5034,5036,5038,5040],{"class":97,"line":160},[95,5029,823],{"class":201},[95,5031,4907],{"class":108},[95,5033,1216],{"class":101},[95,5035,4129],{"class":370},[95,5037,1651],{"class":201},[95,5039,826],{"class":108},[95,5041,2324],{"class":101},[95,5043,5044],{"class":97,"line":171},[95,5045,4093],{"class":382},[95,5047,5048],{"class":97,"line":882},[95,5049,174],{"class":101},[11,5051,5052],{},"The difference might look small.",[11,5054,5055],{},"The impact isn’t.",[11,5057,5058],{},"The second version gives you:",[639,5060,5061,5064,5067,5070,5073],{},[642,5062,5063],{},"autocomplete",[642,5065,5066],{},"documentation",[642,5068,5069],{},"refactoring support",[642,5071,5072],{},"compile-time guarantees",[642,5074,5075],{},"clearer intent",[11,5077,5078],{},"Broad types are useful at genuinely dynamic boundaries.",[11,5080,5081],{},"They shouldn’t become the default representation for everything.",[75,5083,5085],{"id":5084},"_7-make-function-apis-self-documenting","7. Make Function APIs Self-Documenting",[11,5087,3777],{},[86,5089,5091],{"className":88,"code":5090,"language":90,"meta":91,"style":91},"createUser(\n  'Akshat',\n  'akshat@example.com',\n  true,\n  false,\n)\n",[20,5092,5093,5100,5107,5114,5121,5128],{"__ignoreMap":91},[95,5094,5095,5098],{"class":97,"line":98},[95,5096,5097],{"class":108},"createUser",[95,5099,976],{"class":101},[95,5101,5102,5105],{"class":97,"line":105},[95,5103,5104],{"class":115},"  'Akshat'",[95,5106,119],{"class":101},[95,5108,5109,5112],{"class":97,"line":122},[95,5110,5111],{"class":115},"  'akshat@example.com'",[95,5113,119],{"class":101},[95,5115,5116,5119],{"class":97,"line":134},[95,5117,5118],{"class":338},"  true",[95,5120,119],{"class":101},[95,5122,5123,5126],{"class":97,"line":147},[95,5124,5125],{"class":338},"  false",[95,5127,119],{"class":101},[95,5129,5130],{"class":97,"line":160},[95,5131,1222],{"class":101},[11,5133,5134],{},"What does each boolean mean?",[11,5136,5137],{},"You have to remember the function signature.",[11,5139,5140],{},"Now compare:",[86,5142,5144],{"className":88,"code":5143,"language":90,"meta":91,"style":91},"createUser({\n  name: 'Akshat',\n  email: 'akshat@example.com',\n  sendWelcomeEmail: true,\n  isAdmin: false,\n})\n",[20,5145,5146,5153,5163,5173,5183,5192],{"__ignoreMap":91},[95,5147,5148,5150],{"class":97,"line":98},[95,5149,5097],{"class":108},[95,5151,5152],{"class":101},"({\n",[95,5154,5155,5158,5161],{"class":97,"line":105},[95,5156,5157],{"class":101},"  name: ",[95,5159,5160],{"class":115},"'Akshat'",[95,5162,119],{"class":101},[95,5164,5165,5168,5171],{"class":97,"line":122},[95,5166,5167],{"class":101},"  email: ",[95,5169,5170],{"class":115},"'akshat@example.com'",[95,5172,119],{"class":101},[95,5174,5175,5178,5181],{"class":97,"line":134},[95,5176,5177],{"class":101},"  sendWelcomeEmail: ",[95,5179,5180],{"class":338},"true",[95,5182,119],{"class":101},[95,5184,5185,5188,5190],{"class":97,"line":147},[95,5186,5187],{"class":101},"  isAdmin: ",[95,5189,3052],{"class":338},[95,5191,119],{"class":101},[95,5193,5194],{"class":97,"line":160},[95,5195,403],{"class":101},[11,5197,5198],{},"The API explains itself.",[86,5200,5202],{"className":88,"code":5201,"language":90,"meta":91,"style":91},"type CreateUserOptions = {\n  name: string\n  email: string\n  sendWelcomeEmail: boolean\n  isAdmin: boolean\n}\n\nfunction createUser(options: CreateUserOptions) {\n  \u002F\u002F ...\n}\n",[20,5203,5204,5215,5223,5232,5242,5251,5255,5259,5276,5280],{"__ignoreMap":91},[95,5205,5206,5208,5211,5213],{"class":97,"line":98},[95,5207,4404],{"class":201},[95,5209,5210],{"class":108}," CreateUserOptions",[95,5212,341],{"class":201},[95,5214,353],{"class":101},[95,5216,5217,5219,5221],{"class":97,"line":105},[95,5218,125],{"class":370},[95,5220,1651],{"class":201},[95,5222,1654],{"class":338},[95,5224,5225,5228,5230],{"class":97,"line":122},[95,5226,5227],{"class":370},"  email",[95,5229,1651],{"class":201},[95,5231,1654],{"class":338},[95,5233,5234,5237,5239],{"class":97,"line":134},[95,5235,5236],{"class":370},"  sendWelcomeEmail",[95,5238,1651],{"class":201},[95,5240,5241],{"class":338}," boolean\n",[95,5243,5244,5247,5249],{"class":97,"line":147},[95,5245,5246],{"class":370},"  isAdmin",[95,5248,1651],{"class":201},[95,5250,5241],{"class":338},[95,5252,5253],{"class":97,"line":160},[95,5254,174],{"class":101},[95,5256,5257],{"class":97,"line":171},[95,5258,1731],{"emptyLinePlaceholder":1410},[95,5260,5261,5263,5266,5268,5270,5272,5274],{"class":97,"line":882},[95,5262,823],{"class":201},[95,5264,5265],{"class":108}," createUser",[95,5267,1216],{"class":101},[95,5269,273],{"class":370},[95,5271,1651],{"class":201},[95,5273,5210],{"class":108},[95,5275,2324],{"class":101},[95,5277,5278],{"class":97,"line":1788},[95,5279,4093],{"class":382},[95,5281,5282],{"class":97,"line":1793},[95,5283,174],{"class":101},[11,5285,5286],{},"This becomes increasingly valuable as functions grow.",[11,5288,5289],{},"A function with two arguments might be perfectly readable.",[11,5291,5292],{},"A function with seven arguments is usually asking for trouble.",[11,5294,5295],{},"TypeScript gives us a convenient way to make complex function APIs much more explicit.",[11,5297,5298],{},"Use it.",[75,5300,5302],{"id":5301},"_8-organize-types-around-the-domain","8. Organize Types Around the Domain",[11,5304,5305,5306,5309],{},"A common pattern in large applications is a global ",[20,5307,5308],{},"types.ts"," file.",[11,5311,5312],{},"It starts small.",[11,5314,5315],{},"Then another developer adds a type.",[11,5317,5318],{},"Then another.",[11,5320,5321],{},"A year later:",[11,5323,5324,5326],{},[20,5325,5308],{}," contains hundreds of unrelated types.",[11,5328,5329],{},"Finding the type you need becomes a project-wide search exercise.",[11,5331,5332],{},"Instead, keep types close to the domain they describe.",[11,5334,1628],{},[86,5336,5339],{"className":5337,"code":5338,"language":632,"meta":91},[630],"users\u002F\n  user.types.ts\n  user.service.ts\n  user.repository.ts\n\npayments\u002F\n  payment.types.ts\n  payment.service.ts\n  payment.repository.ts\n\nfacilities\u002F\n  facility.types.ts\n  facility.service.ts\n  facility.repository.ts\n",[20,5340,5338],{"__ignoreMap":91},[11,5342,5343],{},"The exact architecture isn’t important.",[11,5345,5346],{},"The principle is.",[11,5348,5349],{},"Types belong to the domain they describe.",[11,5351,5352],{},"When you understand where the business logic lives, you should have a good idea where its types live too.",[75,5354,5356],{"id":5355},"_9-dont-let-the-type-system-become-more-complicated-than-the-business-logic","9. Don’t Let the Type System Become More Complicated Than the Business Logic",[11,5358,5359],{},"TypeScript can do some incredible things.",[11,5361,5362],{},"Mapped types.",[11,5364,5365],{},"Conditional types.",[11,5367,5368],{},"Template literal types.",[11,5370,5371],{},"Recursive types.",[11,5373,5374,73],{},[20,5375,5376],{},"infer",[11,5378,5379],{},"Distributive conditional types.",[11,5381,5382],{},"You can build almost anything.",[11,5384,5385],{},"That doesn’t mean you should.",[11,5387,5388],{},"Consider a type like:",[86,5390,5392],{"className":88,"code":5391,"language":90,"meta":91,"style":91},"type Magic\u003CT> =\n  T extends infer U\n    ? U extends Record\u003Cstring, unknown>\n      ? {\n          [K in keyof U as K extends `${string}Id`\n            ? K\n            : never]: U[K]\n        }\n      : never\n    : never\n",[20,5393,5394,5411,5423,5445,5452,5482,5490,5511,5516,5524],{"__ignoreMap":91},[95,5395,5396,5398,5401,5403,5405,5408],{"class":97,"line":98},[95,5397,4404],{"class":201},[95,5399,5400],{"class":108}," Magic",[95,5402,192],{"class":101},[95,5404,3794],{"class":108},[95,5406,5407],{"class":101},"> ",[95,5409,5410],{"class":201},"=\n",[95,5412,5413,5415,5417,5420],{"class":97,"line":105},[95,5414,4040],{"class":108},[95,5416,4043],{"class":201},[95,5418,5419],{"class":201}," infer",[95,5421,5422],{"class":108}," U\n",[95,5424,5425,5428,5431,5433,5435,5437,5439,5441,5443],{"class":97,"line":122},[95,5426,5427],{"class":201},"    ?",[95,5429,5430],{"class":108}," U",[95,5432,4043],{"class":201},[95,5434,4046],{"class":108},[95,5436,192],{"class":101},[95,5438,4051],{"class":338},[95,5440,909],{"class":101},[95,5442,4056],{"class":338},[95,5444,553],{"class":101},[95,5446,5447,5450],{"class":97,"line":134},[95,5448,5449],{"class":201},"      ?",[95,5451,353],{"class":101},[95,5453,5454,5457,5460,5462,5464,5466,5469,5472,5474,5477,5479],{"class":97,"line":147},[95,5455,5456],{"class":101},"          [",[95,5458,5459],{"class":108},"K",[95,5461,4311],{"class":201},[95,5463,4069],{"class":201},[95,5465,5430],{"class":108},[95,5467,5468],{"class":201}," as",[95,5470,5471],{"class":108}," K",[95,5473,4043],{"class":201},[95,5475,5476],{"class":115}," `${",[95,5478,4051],{"class":338},[95,5480,5481],{"class":115},"}Id`\n",[95,5483,5484,5487],{"class":97,"line":160},[95,5485,5486],{"class":201},"            ?",[95,5488,5489],{"class":108}," K\n",[95,5491,5492,5495,5497,5500,5502,5504,5507,5509],{"class":97,"line":171},[95,5493,5494],{"class":201},"            :",[95,5496,4605],{"class":338},[95,5498,5499],{"class":101},"]",[95,5501,1651],{"class":201},[95,5503,5430],{"class":108},[95,5505,5506],{"class":101},"[",[95,5508,5459],{"class":108},[95,5510,313],{"class":101},[95,5512,5513],{"class":97,"line":882},[95,5514,5515],{"class":101},"        }\n",[95,5517,5518,5521],{"class":97,"line":1788},[95,5519,5520],{"class":201},"      :",[95,5522,5523],{"class":338}," never\n",[95,5525,5526,5529],{"class":97,"line":1793},[95,5527,5528],{"class":201},"    :",[95,5530,5523],{"class":338},[11,5532,5533],{},"There are legitimate use cases for sophisticated type-level programming.",[11,5535,5536],{},"But there is also a point where you’ve created more complexity than you’ve removed.",[11,5538,5539],{},"If the next developer needs to understand a complicated type before they can understand the business logic, stop and ask:",[11,5541,5542],{},"Could this be simpler?",[11,5544,5545],{},"Sometimes the answer is no.",[11,5547,5548],{},"Sometimes the complexity is justified.",[11,5550,5551],{},"But don’t confuse cleverness with quality.",[11,5553,5554],{},"The best TypeScript is often boring.",[11,5556,5557],{},"Readable types beat impressive types.",[75,5559,5561],{"id":5560},"_10-take-tsconfig-seriously","10. Take tsconfig Seriously",[11,5563,5564],{},"One of the easiest ways to undermine TypeScript is to configure it so that it doesn’t catch the problems you’re relying on it to catch.",[11,5566,5567],{},"At minimum, understand options such as:",[86,5569,5573],{"className":5570,"code":5571,"language":5572,"meta":91,"style":91},"language-json shiki shiki-themes github-light github-dark","{\n  \"compilerOptions\": {\n    \"strict\": true,\n    \"noUncheckedIndexedAccess\": true,\n    \"exactOptionalPropertyTypes\": true\n  }\n}\n","json",[20,5574,5575,5579,5587,5598,5609,5619,5623],{"__ignoreMap":91},[95,5576,5577],{"class":97,"line":98},[95,5578,102],{"class":101},[95,5580,5581,5584],{"class":97,"line":105},[95,5582,5583],{"class":338},"  \"compilerOptions\"",[95,5585,5586],{"class":101},": {\n",[95,5588,5589,5592,5594,5596],{"class":97,"line":122},[95,5590,5591],{"class":338},"    \"strict\"",[95,5593,112],{"class":101},[95,5595,5180],{"class":338},[95,5597,119],{"class":101},[95,5599,5600,5603,5605,5607],{"class":97,"line":134},[95,5601,5602],{"class":338},"    \"noUncheckedIndexedAccess\"",[95,5604,112],{"class":101},[95,5606,5180],{"class":338},[95,5608,119],{"class":101},[95,5610,5611,5614,5616],{"class":97,"line":147},[95,5612,5613],{"class":338},"    \"exactOptionalPropertyTypes\"",[95,5615,112],{"class":101},[95,5617,5618],{"class":338},"true\n",[95,5620,5621],{"class":97,"line":160},[95,5622,1757],{"class":101},[95,5624,5625],{"class":97,"line":171},[95,5626,174],{"class":101},[11,5628,5629],{},"The exact configuration will depend on your project.",[11,5631,5632],{},"A legacy codebase may not be able to enable every strict option immediately.",[11,5634,4528],{},[11,5636,5637],{},"What’s important is understanding what your compiler is checking.",[11,5639,5640],{},"Especially:",[86,5642,5644],{"className":5570,"code":5643,"language":5572,"meta":91,"style":91},"\"strict\": true\n",[20,5645,5646],{"__ignoreMap":91},[95,5647,5648,5651,5653],{"class":97,"line":98},[95,5649,5650],{"class":115},"\"strict\"",[95,5652,112],{"class":101},[95,5654,5618],{"class":338},[11,5656,5657],{},"Strict mode isn’t about making TypeScript annoying.",[11,5659,5660],{},"It’s about asking the compiler to be more honest about uncertainty.",[11,5662,5663],{},"And the whole reason we use TypeScript is to catch problems before users do.",[75,5665,5667],{"id":5666},"the-bigger-picture","The Bigger Picture",[11,5669,5670],{},"None of these practices are particularly useful because they’re clever TypeScript tricks.",[11,5672,5673],{},"They’re useful because they make your codebase easier to reason about.",[11,5675,5676],{},"Consider what happens when a new developer joins your team.",[11,5678,5679],{},"They encounter:",[86,5681,5683],{"className":88,"code":5682,"language":90,"meta":91,"style":91},"type Payment =\n  | {\n      status: 'SUCCESS'\n      transactionId: string\n    }\n  | {\n      status: 'FAILED'\n      error: string\n    }\n",[20,5684,5685,5694,5700,5710,5719,5723,5729,5738,5747],{"__ignoreMap":91},[95,5686,5687,5689,5692],{"class":97,"line":98},[95,5688,4404],{"class":201},[95,5690,5691],{"class":108}," Payment",[95,5693,4410],{"class":201},[95,5695,5696,5698],{"class":97,"line":105},[95,5697,4415],{"class":201},[95,5699,353],{"class":101},[95,5701,5702,5705,5707],{"class":97,"line":122},[95,5703,5704],{"class":370},"      status",[95,5706,1651],{"class":201},[95,5708,5709],{"class":115}," 'SUCCESS'\n",[95,5711,5712,5715,5717],{"class":97,"line":134},[95,5713,5714],{"class":370},"      transactionId",[95,5716,1651],{"class":201},[95,5718,1654],{"class":338},[95,5720,5721],{"class":97,"line":147},[95,5722,2770],{"class":101},[95,5724,5725,5727],{"class":97,"line":160},[95,5726,4415],{"class":201},[95,5728,353],{"class":101},[95,5730,5731,5733,5735],{"class":97,"line":171},[95,5732,5704],{"class":370},[95,5734,1651],{"class":201},[95,5736,5737],{"class":115}," 'FAILED'\n",[95,5739,5740,5743,5745],{"class":97,"line":882},[95,5741,5742],{"class":370},"      error",[95,5744,1651],{"class":201},[95,5746,1654],{"class":338},[95,5748,5749],{"class":97,"line":1788},[95,5750,2770],{"class":101},[11,5752,5753],{},"They immediately know something about the domain.",[11,5755,5756],{},"A successful payment has a transaction ID.",[11,5758,5759],{},"A failed payment has an error.",[11,5761,5762],{},"The type itself communicates the business rule.",[11,5764,5765],{},"That’s what good TypeScript should do.",[11,5767,5768],{},"It shouldn’t just tell you:",[11,5770,5771],{},"“This property is a string.”",[11,5773,5774],{},"It should tell you:",[11,5776,5777],{},"“This is how this part of the system is supposed to behave.”",[75,5779,5781],{"id":5780},"a-practical-typescript-checklist","A Practical TypeScript Checklist",[11,5783,5784],{},"Before committing a piece of TypeScript, I like to ask:",[639,5786,5787,5797,5800,5803,5806,5809,5815,5818,5821,5824,5830,5833],{},[642,5788,5789,5790,5793,5794,5796],{},"Is there an ",[20,5791,5792],{},"any"," here that could be ",[20,5795,4056],{}," or a real type?",[642,5798,5799],{},"Am I writing a type annotation that TypeScript can already infer?",[642,5801,5802],{},"Are my types representing valid domain states?",[642,5804,5805],{},"Am I making something optional because it’s genuinely optional?",[642,5807,5808],{},"Could a union represent this state more accurately?",[642,5810,5811,5812,5814],{},"Am I using ",[20,5813,4182],{}," because the value is actually safe, or because I want the error to disappear?",[642,5816,5817],{},"Does this generic express a meaningful relationship?",[642,5819,5820],{},"Is this abstraction easier to understand than the code it replaced?",[642,5822,5823],{},"Does the function API communicate what its arguments mean?",[642,5825,5826,5827,5829],{},"Would ",[20,5828,4748],{}," make the intended ownership or immutability clearer?",[642,5831,5832],{},"Will the compiler tell me when this domain changes?",[642,5834,5835],{},"Can another developer understand this type without having to decode it?",[11,5837,5838],{},"That last question is perhaps the most important.",[75,5840,5842],{"id":5841},"typescript-is-a-design-tool","TypeScript Is a Design Tool",[11,5844,5845],{},"The more TypeScript you write, the less interesting the syntax becomes.",[11,5847,5848],{},"The interesting part is what you choose to express.",[11,5850,5851],{},"You can use TypeScript to describe:",[11,5853,5854],{},"“This is an object with some strings.”",[11,5856,5857],{},"Or you can use it to describe:",[11,5859,5860],{},"“This operation can be in one of four states, and each state guarantees a different set of properties.”",[11,5862,5863],{},"The second is much more valuable.",[11,5865,5866],{},"That’s why good TypeScript isn’t measured by how many advanced features a developer knows.",[11,5868,5869],{},"It’s measured by how effectively the type system helps the team understand, change, and maintain the software.",[11,5871,5872],{},"Types should make the codebase easier to reason about—not harder.",[11,5874,5875],{},"And when the compiler can enforce the rules your application already cares about, you’ve moved beyond using TypeScript as a safety net.",[11,5877,5878],{},"You’re using it as part of the architecture.",[11,5880,1378],{},[11,5882,5883],{},"If you found this article useful, consider sharing it with other developers.",[11,5885,1381],{},[1383,5887,5888],{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":91,"searchDepth":105,"depth":105,"links":5890},[5891,5892,5893,5894,5896,5898,5900,5901,5902,5903,5904,5905,5906],{"id":3764,"depth":105,"text":3765},{"id":4010,"depth":105,"text":4011},{"id":4158,"depth":105,"text":4159},{"id":4386,"depth":105,"text":5895},"4. Use never to Catch Incomplete Logic",{"id":4744,"depth":105,"text":5897},"5. Use readonly to Communicate Intent",{"id":4887,"depth":105,"text":5899},"6. Don’t Use Record\u003Cstring, unknown> as a Replacement for Real Types",{"id":5084,"depth":105,"text":5085},{"id":5301,"depth":105,"text":5302},{"id":5355,"depth":105,"text":5356},{"id":5560,"depth":105,"text":5561},{"id":5666,"depth":105,"text":5667},{"id":5780,"depth":105,"text":5781},{"id":5841,"depth":105,"text":5842},"2026-08-22",{"src":5909,"alt":5910},"\u002Fimages\u002Fblog\u002F10-typescript-practices-that-make-your-codebase-better-cover.jpg","Cover graphic for ten TypeScript practices that improve readability and maintainability in growing codebases",{},"\u002Fblog\u002F10-typescript-practices-that-make-your-codebase-better",{"title":3686,"description":3691},"blog\u002F10-typescript-practices-that-make-your-codebase-better",[5916,1417,1419,1418,5917],"Typescript","Typescript Tips","2026-08-25","ZTI9UM8SpwRt7j5TCwKwlrGKPYhcF2tCwkdU2jDlzZc",{"id":5921,"title":5922,"body":5923,"date":8311,"description":5927,"draft":1404,"extension":1405,"featured":1404,"image":8312,"meta":8315,"navigation":1410,"path":3700,"seo":8316,"stem":8317,"tags":8318,"updatedAt":5918,"__hash__":8320},"blog\u002Fblog\u002Fyou-know-typescript-but-do-you-know-how-to-model-your-code-with-it.md","You Know TypeScript. But Do You Know How to Model Your Code With It?",{"type":8,"value":5924,"toc":8285},[5925,5928,5931,5934,5937,5940,5943,5946,5949,5952,5958,5961,6013,6016,6019,6022,6138,6141,6144,6147,6150,6214,6217,6219,6221,6224,6227,6230,6233,6236,6243,6246,6272,6275,6278,6281,6284,6289,6292,6295,6301,6343,6346,6353,6376,6379,6418,6421,6424,6436,6439,6445,6449,6452,6455,6510,6513,6548,6551,6554,6557,6559,6614,6617,6620,6623,6626,6629,6637,6639,6677,6680,6685,6726,6728,6755,6758,6772,6775,6777,6875,6878,6881,6922,6925,6928,6932,6935,6937,6967,6969,7020,7023,7040,7043,7059,7062,7065,7068,7084,7087,7091,7094,7097,7100,7120,7123,7126,7189,7192,7195,7198,7201,7204,7207,7211,7214,7217,7220,7237,7240,7296,7299,7352,7355,7358,7361,7484,7487,7599,7602,7605,7608,7612,7615,7618,7620,7663,7666,7669,7672,7675,7718,7721,7724,7727,7730,7733,7744,7747,7749,7781,7784,7787,7789,7816,7819,7822,7825,7828,7850,7853,7856,7859,7868,7870,7873,7876,7879,7882,7890,7892,7930,7933,7977,7983,7986,7988,7991,8030,8037,8040,8052,8058,8065,8068,8071,8091,8094,8097,8120,8123,8126,8129,8149,8152,8155,8158,8161,8175,8178,8181,8185,8188,8191,8194,8197,8200,8202,8205,8207,8210,8212,8218,8220,8223,8226,8229,8232,8236,8239,8243,8246,8250,8253,8257,8260,8263,8266,8269,8272,8275,8282],[11,5926,5927],{},"The TypeScript practices that help you turn business rules into types—and make invalid states harder to represent.",[11,5929,5930],{},"TypeScript is easy to learn.",[11,5932,5933],{},"You learn interfaces.",[11,5935,5936],{},"Then type aliases.",[11,5938,5939],{},"Then unions, generics, utility types, and maybe a few mapped types.",[11,5941,5942],{},"Eventually, you can look at a JavaScript object and confidently add a type to it.",[11,5944,5945],{},"But there’s a point where TypeScript stops being about adding types.",[11,5947,5948],{},"It becomes about designing types.",[11,5950,5951],{},"And that’s a very different skill.",[11,5953,59,5954,73],{},[61,5955,66],{"href":5956,"rel":5957},"https:\u002F\u002Fakshattriv.medium.com\u002Fyou-know-typescript-but-do-you-know-how-to-model-your-code-with-it-d111ae9935e3",[65],[11,5959,5960],{},"Consider this:",[86,5962,5964],{"className":88,"code":5963,"language":90,"meta":91,"style":91},"type Payment = {\n  status: 'SUCCESS' | 'FAILED'\n  transactionId?: string\n  error?: string\n}\n",[20,5965,5966,5976,5990,6000,6009],{"__ignoreMap":91},[95,5967,5968,5970,5972,5974],{"class":97,"line":98},[95,5969,4404],{"class":201},[95,5971,5691],{"class":108},[95,5973,341],{"class":201},[95,5975,353],{"class":101},[95,5977,5978,5981,5983,5986,5988],{"class":97,"line":105},[95,5979,5980],{"class":370},"  status",[95,5982,1651],{"class":201},[95,5984,5985],{"class":115}," 'SUCCESS'",[95,5987,2403],{"class":201},[95,5989,5737],{"class":115},[95,5991,5992,5995,5998],{"class":97,"line":122},[95,5993,5994],{"class":370},"  transactionId",[95,5996,5997],{"class":201},"?:",[95,5999,1654],{"class":338},[95,6001,6002,6005,6007],{"class":97,"line":134},[95,6003,6004],{"class":370},"  error",[95,6006,5997],{"class":201},[95,6008,1654],{"class":338},[95,6010,6011],{"class":97,"line":147},[95,6012,174],{"class":101},[11,6014,6015],{},"It looks perfectly reasonable.",[11,6017,6018],{},"But what states does this type actually allow?",[11,6020,6021],{},"All of these:",[86,6023,6025],{"className":88,"code":6024,"language":90,"meta":91,"style":91},"{\n  status: 'SUCCESS'\n}\n\n{\n  status: 'SUCCESS',\n  error: 'Something went wrong'\n}\n\n{\n  status: 'FAILED',\n  transactionId: '123'\n}\n\n{\n  status: 'FAILED',\n  error: 'Something went wrong'\n}\n",[20,6026,6027,6031,6040,6044,6048,6052,6063,6072,6076,6080,6084,6095,6104,6108,6112,6116,6126,6134],{"__ignoreMap":91},[95,6028,6029],{"class":97,"line":98},[95,6030,102],{"class":101},[95,6032,6033,6035,6037],{"class":97,"line":105},[95,6034,5980],{"class":108},[95,6036,112],{"class":101},[95,6038,6039],{"class":115},"'SUCCESS'\n",[95,6041,6042],{"class":97,"line":122},[95,6043,174],{"class":101},[95,6045,6046],{"class":97,"line":134},[95,6047,1731],{"emptyLinePlaceholder":1410},[95,6049,6050],{"class":97,"line":147},[95,6051,102],{"class":101},[95,6053,6054,6056,6058,6061],{"class":97,"line":160},[95,6055,5980],{"class":108},[95,6057,112],{"class":101},[95,6059,6060],{"class":115},"'SUCCESS'",[95,6062,119],{"class":101},[95,6064,6065,6067,6069],{"class":97,"line":171},[95,6066,6004],{"class":108},[95,6068,112],{"class":101},[95,6070,6071],{"class":115},"'Something went wrong'\n",[95,6073,6074],{"class":97,"line":882},[95,6075,174],{"class":101},[95,6077,6078],{"class":97,"line":1788},[95,6079,1731],{"emptyLinePlaceholder":1410},[95,6081,6082],{"class":97,"line":1793},[95,6083,102],{"class":101},[95,6085,6086,6088,6090,6093],{"class":97,"line":1801},[95,6087,5980],{"class":108},[95,6089,112],{"class":101},[95,6091,6092],{"class":115},"'FAILED'",[95,6094,119],{"class":101},[95,6096,6097,6099,6101],{"class":97,"line":1820},[95,6098,5994],{"class":108},[95,6100,112],{"class":101},[95,6102,6103],{"class":115},"'123'\n",[95,6105,6106],{"class":97,"line":1829},[95,6107,174],{"class":101},[95,6109,6110],{"class":97,"line":1851},[95,6111,1731],{"emptyLinePlaceholder":1410},[95,6113,6114],{"class":97,"line":1858},[95,6115,102],{"class":101},[95,6117,6118,6120,6122,6124],{"class":97,"line":1863},[95,6119,5980],{"class":108},[95,6121,112],{"class":101},[95,6123,6092],{"class":115},[95,6125,119],{"class":101},[95,6127,6128,6130,6132],{"class":97,"line":1868},[95,6129,6004],{"class":108},[95,6131,112],{"class":101},[95,6133,6071],{"class":115},[95,6135,6136],{"class":97,"line":1887},[95,6137,174],{"class":101},[11,6139,6140],{},"The type doesn’t know which combinations make sense.",[11,6142,6143],{},"The problem isn’t that TypeScript isn’t powerful enough.",[11,6145,6146],{},"We’ve modeled the domain poorly.",[11,6148,6149],{},"A better model is:",[86,6151,6152],{"className":88,"code":5682,"language":90,"meta":91,"style":91},[20,6153,6154,6162,6168,6176,6184,6188,6194,6202,6210],{"__ignoreMap":91},[95,6155,6156,6158,6160],{"class":97,"line":98},[95,6157,4404],{"class":201},[95,6159,5691],{"class":108},[95,6161,4410],{"class":201},[95,6163,6164,6166],{"class":97,"line":105},[95,6165,4415],{"class":201},[95,6167,353],{"class":101},[95,6169,6170,6172,6174],{"class":97,"line":122},[95,6171,5704],{"class":370},[95,6173,1651],{"class":201},[95,6175,5709],{"class":115},[95,6177,6178,6180,6182],{"class":97,"line":134},[95,6179,5714],{"class":370},[95,6181,1651],{"class":201},[95,6183,1654],{"class":338},[95,6185,6186],{"class":97,"line":147},[95,6187,2770],{"class":101},[95,6189,6190,6192],{"class":97,"line":160},[95,6191,4415],{"class":201},[95,6193,353],{"class":101},[95,6195,6196,6198,6200],{"class":97,"line":171},[95,6197,5704],{"class":370},[95,6199,1651],{"class":201},[95,6201,5737],{"class":115},[95,6203,6204,6206,6208],{"class":97,"line":882},[95,6205,5742],{"class":370},[95,6207,1651],{"class":201},[95,6209,1654],{"class":338},[95,6211,6212],{"class":97,"line":1788},[95,6213,2770],{"class":101},[11,6215,6216],{},"Now the type system understands the business rule.",[11,6218,5756],{},[11,6220,5759],{},[11,6222,6223],{},"Invalid combinations become difficult—or impossible—to represent.",[11,6225,6226],{},"This is where TypeScript becomes much more interesting.",[11,6228,6229],{},"The goal isn’t to put types everywhere.",[11,6231,6232],{},"The goal is to make your types communicate the rules of your application.",[11,6234,6235],{},"Here are some practices that help you do exactly that.",[75,6237,6239,6240,6242],{"id":6238},"_1-stop-using-any-when-you-actually-mean-i-dont-know","1. Stop Using ",[20,6241,5792],{}," When You Actually Mean “I Don’t Know”",[11,6244,6245],{},"One of the easiest ways to make a TypeScript error disappear is:",[86,6247,6249],{"className":88,"code":6248,"language":90,"meta":91,"style":91},"const response: any = await fetchData()\n",[20,6250,6251],{"__ignoreMap":91},[95,6252,6253,6255,6258,6260,6263,6265,6267,6270],{"class":97,"line":98},[95,6254,335],{"class":201},[95,6256,6257],{"class":338}," response",[95,6259,1651],{"class":201},[95,6261,6262],{"class":338}," any",[95,6264,341],{"class":201},[95,6266,1812],{"class":201},[95,6268,6269],{"class":108}," fetchData",[95,6271,1726],{"class":101},[11,6273,6274],{},"And sometimes that’s exactly what happens.",[11,6276,6277],{},"You have an error.",[11,6279,6280],{},"You’re in the middle of implementing something.",[11,6282,6283],{},"You don’t immediately understand the type.",[11,6285,6286,6287,73],{},"So you write ",[20,6288,5792],{},[11,6290,6291],{},"The compiler stops complaining.",[11,6293,6294],{},"But you’ve also stopped TypeScript from helping you.",[11,6296,6297,6298,6300],{},"Once ",[20,6299,5792],{}," enters your code:",[86,6302,6304],{"className":88,"code":6303,"language":90,"meta":91,"style":91},"const response: any = await fetchData()\n\nresponse.user.profile.name \u002F\u002F not checked\nresponse.foo.bar.baz \u002F\u002F not checked\n",[20,6305,6306,6324,6328,6336],{"__ignoreMap":91},[95,6307,6308,6310,6312,6314,6316,6318,6320,6322],{"class":97,"line":98},[95,6309,335],{"class":201},[95,6311,6257],{"class":338},[95,6313,1651],{"class":201},[95,6315,6262],{"class":338},[95,6317,341],{"class":201},[95,6319,1812],{"class":201},[95,6321,6269],{"class":108},[95,6323,1726],{"class":101},[95,6325,6326],{"class":97,"line":105},[95,6327,1731],{"emptyLinePlaceholder":1410},[95,6329,6330,6333],{"class":97,"line":122},[95,6331,6332],{"class":101},"response.user.profile.name ",[95,6334,6335],{"class":382},"\u002F\u002F not checked\n",[95,6337,6338,6341],{"class":97,"line":134},[95,6339,6340],{"class":101},"response.foo.bar.baz ",[95,6342,6335],{"class":382},[11,6344,6345],{},"TypeScript won’t protect you from any of it.",[3471,6347,6349,6350,6352],{"id":6348},"use-unknown-when-the-value-is-genuinely-unknown","Use ",[20,6351,4056],{}," when the value is genuinely unknown",[86,6354,6356],{"className":88,"code":6355,"language":90,"meta":91,"style":91},"const response: unknown = await fetchData()\n",[20,6357,6358],{"__ignoreMap":91},[95,6359,6360,6362,6364,6366,6368,6370,6372,6374],{"class":97,"line":98},[95,6361,335],{"class":201},[95,6363,6257],{"class":338},[95,6365,1651],{"class":201},[95,6367,3973],{"class":338},[95,6369,341],{"class":201},[95,6371,1812],{"class":201},[95,6373,6269],{"class":108},[95,6375,1726],{"class":101},[11,6377,6378],{},"Now TypeScript forces you to establish what the value actually is.",[86,6380,6382],{"className":88,"code":6381,"language":90,"meta":91,"style":91},"if (typeof response === 'string') {\n  console.log(response.toUpperCase())\n}\n",[20,6383,6384,6401,6414],{"__ignoreMap":91},[95,6385,6386,6388,6390,6392,6395,6397,6399],{"class":97,"line":98},[95,6387,2934],{"class":201},[95,6389,1701],{"class":101},[95,6391,4204],{"class":201},[95,6393,6394],{"class":101}," response ",[95,6396,990],{"class":201},[95,6398,4211],{"class":115},[95,6400,2324],{"class":101},[95,6402,6403,6405,6407,6410,6412],{"class":97,"line":105},[95,6404,2168],{"class":101},[95,6406,2171],{"class":108},[95,6408,6409],{"class":101},"(response.",[95,6411,4225],{"class":108},[95,6413,4228],{"class":101},[95,6415,6416],{"class":97,"line":122},[95,6417,174],{"class":101},[11,6419,6420],{},"Or for more complex data, validate it at the boundary.",[11,6422,6423],{},"This distinction is important:",[639,6425,6426,6431],{},[642,6427,6428,6430],{},[20,6429,4056],{}," means “I don’t know yet.”",[642,6432,6433,6435],{},[20,6434,5792],{}," means “I don’t want TypeScript involved.”",[11,6437,6438],{},"Those are very different statements.",[11,6440,6441,6442,6444],{},"If you’re dealing with external data, ",[20,6443,4056],{}," is usually the more honest starting point.",[75,6446,6448],{"id":6447},"_2-dont-type-what-typescript-already-knows","2. Don’t Type What TypeScript Already Knows",[11,6450,6451],{},"Strongly typed code doesn’t mean explicitly annotating every variable.",[11,6453,6454],{},"This:",[86,6456,6458],{"className":88,"code":6457,"language":90,"meta":91,"style":91},"const name: string = 'Akshat'\nconst age: number = 30\nconst active: boolean = true\n",[20,6459,6460,6477,6493],{"__ignoreMap":91},[95,6461,6462,6464,6467,6469,6472,6474],{"class":97,"line":98},[95,6463,335],{"class":201},[95,6465,6466],{"class":338}," name",[95,6468,1651],{"class":201},[95,6470,6471],{"class":338}," string",[95,6473,341],{"class":201},[95,6475,6476],{"class":115}," 'Akshat'\n",[95,6478,6479,6481,6484,6486,6488,6490],{"class":97,"line":105},[95,6480,335],{"class":201},[95,6482,6483],{"class":338}," age",[95,6485,1651],{"class":201},[95,6487,2400],{"class":338},[95,6489,341],{"class":201},[95,6491,6492],{"class":338}," 30\n",[95,6494,6495,6497,6500,6502,6505,6507],{"class":97,"line":122},[95,6496,335],{"class":201},[95,6498,6499],{"class":338}," active",[95,6501,1651],{"class":201},[95,6503,6504],{"class":338}," boolean",[95,6506,341],{"class":201},[95,6508,6509],{"class":338}," true\n",[11,6511,6512],{},"isn’t necessarily better than:",[86,6514,6516],{"className":88,"code":6515,"language":90,"meta":91,"style":91},"const name = 'Akshat'\nconst age = 30\nconst active = true\n",[20,6517,6518,6528,6538],{"__ignoreMap":91},[95,6519,6520,6522,6524,6526],{"class":97,"line":98},[95,6521,335],{"class":201},[95,6523,6466],{"class":338},[95,6525,341],{"class":201},[95,6527,6476],{"class":115},[95,6529,6530,6532,6534,6536],{"class":97,"line":105},[95,6531,335],{"class":201},[95,6533,6483],{"class":338},[95,6535,341],{"class":201},[95,6537,6492],{"class":338},[95,6539,6540,6542,6544,6546],{"class":97,"line":122},[95,6541,335],{"class":201},[95,6543,6499],{"class":338},[95,6545,341],{"class":201},[95,6547,6509],{"class":338},[11,6549,6550],{},"TypeScript already knows the types.",[11,6552,6553],{},"Adding annotations everywhere creates noise without adding information.",[11,6555,6556],{},"Explicit types become valuable when they communicate something meaningful.",[11,6558,1628],{},[86,6560,6562],{"className":88,"code":6561,"language":90,"meta":91,"style":91},"function calculateTotal(\n  items: Product[],\n  discount: number,\n): number {\n  \u002F\u002F ...\n}\n",[20,6563,6564,6573,6585,6596,6606,6610],{"__ignoreMap":91},[95,6565,6566,6568,6571],{"class":97,"line":98},[95,6567,823],{"class":201},[95,6569,6570],{"class":108}," calculateTotal",[95,6572,976],{"class":101},[95,6574,6575,6578,6580,6582],{"class":97,"line":105},[95,6576,6577],{"class":370},"  items",[95,6579,1651],{"class":201},[95,6581,3910],{"class":108},[95,6583,6584],{"class":101},"[],\n",[95,6586,6587,6590,6592,6594],{"class":97,"line":122},[95,6588,6589],{"class":370},"  discount",[95,6591,1651],{"class":201},[95,6593,2400],{"class":338},[95,6595,119],{"class":101},[95,6597,6598,6600,6602,6604],{"class":97,"line":134},[95,6599,4259],{"class":101},[95,6601,1651],{"class":201},[95,6603,2400],{"class":338},[95,6605,353],{"class":101},[95,6607,6608],{"class":97,"line":147},[95,6609,4093],{"class":382},[95,6611,6612],{"class":97,"line":160},[95,6613,174],{"class":101},[11,6615,6616],{},"Here the function signature is part of the API contract.",[11,6618,6619],{},"That’s useful.",[11,6621,6622],{},"A good question to ask is:",[11,6624,6625],{},"Does this annotation communicate something TypeScript couldn’t infer?",[11,6627,6628],{},"If the answer is no, you probably don’t need it.",[75,6630,6632,6633,6636],{"id":6631},"_3-use-as-const-when-values-are-also-types","3. Use ",[20,6634,6635],{},"as const"," When Values Are Also Types",[11,6638,3777],{},[86,6640,6642],{"className":88,"code":6641,"language":90,"meta":91,"style":91},"const STATUS = {\n  ACTIVE: 'ACTIVE',\n  INACTIVE: 'INACTIVE',\n}\n",[20,6643,6644,6655,6664,6673],{"__ignoreMap":91},[95,6645,6646,6648,6651,6653],{"class":97,"line":98},[95,6647,335],{"class":201},[95,6649,6650],{"class":338}," STATUS",[95,6652,341],{"class":201},[95,6654,353],{"class":101},[95,6656,6657,6660,6662],{"class":97,"line":105},[95,6658,6659],{"class":101},"  ACTIVE: ",[95,6661,290],{"class":115},[95,6663,119],{"class":101},[95,6665,6666,6669,6671],{"class":97,"line":122},[95,6667,6668],{"class":101},"  INACTIVE: ",[95,6670,305],{"class":115},[95,6672,119],{"class":101},[95,6674,6675],{"class":97,"line":134},[95,6676,174],{"class":101},[11,6678,6679],{},"Sometimes you want those values to remain literal types.",[11,6681,4581,6682,6684],{},[20,6683,6635],{}," becomes useful:",[86,6686,6688],{"className":88,"code":6687,"language":90,"meta":91,"style":91},"const STATUS = {\n  ACTIVE: 'ACTIVE',\n  INACTIVE: 'INACTIVE',\n} as const\n",[20,6689,6690,6700,6708,6716],{"__ignoreMap":91},[95,6691,6692,6694,6696,6698],{"class":97,"line":98},[95,6693,335],{"class":201},[95,6695,6650],{"class":338},[95,6697,341],{"class":201},[95,6699,353],{"class":101},[95,6701,6702,6704,6706],{"class":97,"line":105},[95,6703,6659],{"class":101},[95,6705,290],{"class":115},[95,6707,119],{"class":101},[95,6709,6710,6712,6714],{"class":97,"line":122},[95,6711,6668],{"class":101},[95,6713,305],{"class":115},[95,6715,119],{"class":101},[95,6717,6718,6721,6723],{"class":97,"line":134},[95,6719,6720],{"class":101},"} ",[95,6722,4182],{"class":201},[95,6724,6725],{"class":201}," const\n",[11,6727,4799],{},[86,6729,6731],{"className":88,"code":6730,"language":90,"meta":91,"style":91},"type Status = typeof STATUS[keyof typeof STATUS]\n",[20,6732,6733],{"__ignoreMap":91},[95,6734,6735,6737,6739,6741,6744,6746,6749,6751,6753],{"class":97,"line":98},[95,6736,4404],{"class":201},[95,6738,4407],{"class":108},[95,6740,341],{"class":201},[95,6742,6743],{"class":201}," typeof",[95,6745,6650],{"class":338},[95,6747,6748],{"class":101},"[keyof ",[95,6750,4204],{"class":201},[95,6752,6650],{"class":338},[95,6754,313],{"class":101},[11,6756,6757],{},"produces:",[86,6759,6761],{"className":88,"code":6760,"language":90,"meta":91,"style":91},"'ACTIVE' | 'INACTIVE'\n",[20,6762,6763],{"__ignoreMap":91},[95,6764,6765,6767,6769],{"class":97,"line":98},[95,6766,290],{"class":115},[95,6768,2403],{"class":201},[95,6770,6771],{"class":115}," 'INACTIVE'\n",[11,6773,6774],{},"This pattern is especially useful when you need the values at runtime and the type at compile time.",[11,6776,1628],{},[86,6778,6780],{"className":88,"code":6779,"language":90,"meta":91,"style":91},"export const ALTERNATE_CODE_TYPES = {\n  CHARGE_CODE: 'CHARGE_CODE',\n  NFTP_MDG_CODE: 'NFTP_MDG_CODE',\n  FACT_MDG_CODE: 'FACT_MDG_CODE',\n  CW1_CHARGE_CODE: 'CW1_CHARGE_CODE',\n} as const\n\nexport type AlternateCodeType =\n  typeof ALTERNATE_CODE_TYPES[keyof typeof ALTERNATE_CODE_TYPES]\n",[20,6781,6782,6796,6806,6816,6826,6836,6844,6848,6860],{"__ignoreMap":91},[95,6783,6784,6786,6789,6792,6794],{"class":97,"line":98},[95,6785,3005],{"class":201},[95,6787,6788],{"class":201}," const",[95,6790,6791],{"class":338}," ALTERNATE_CODE_TYPES",[95,6793,341],{"class":201},[95,6795,353],{"class":101},[95,6797,6798,6801,6804],{"class":97,"line":105},[95,6799,6800],{"class":101},"  CHARGE_CODE: ",[95,6802,6803],{"class":115},"'CHARGE_CODE'",[95,6805,119],{"class":101},[95,6807,6808,6811,6814],{"class":97,"line":122},[95,6809,6810],{"class":101},"  NFTP_MDG_CODE: ",[95,6812,6813],{"class":115},"'NFTP_MDG_CODE'",[95,6815,119],{"class":101},[95,6817,6818,6821,6824],{"class":97,"line":134},[95,6819,6820],{"class":101},"  FACT_MDG_CODE: ",[95,6822,6823],{"class":115},"'FACT_MDG_CODE'",[95,6825,119],{"class":101},[95,6827,6828,6831,6834],{"class":97,"line":147},[95,6829,6830],{"class":101},"  CW1_CHARGE_CODE: ",[95,6832,6833],{"class":115},"'CW1_CHARGE_CODE'",[95,6835,119],{"class":101},[95,6837,6838,6840,6842],{"class":97,"line":160},[95,6839,6720],{"class":101},[95,6841,4182],{"class":201},[95,6843,6725],{"class":201},[95,6845,6846],{"class":97,"line":171},[95,6847,1731],{"emptyLinePlaceholder":1410},[95,6849,6850,6852,6855,6858],{"class":97,"line":882},[95,6851,3005],{"class":201},[95,6853,6854],{"class":201}," type",[95,6856,6857],{"class":108}," AlternateCodeType",[95,6859,4410],{"class":201},[95,6861,6862,6865,6867,6869,6871,6873],{"class":97,"line":1788},[95,6863,6864],{"class":201},"  typeof",[95,6866,6791],{"class":338},[95,6868,6748],{"class":101},[95,6870,4204],{"class":201},[95,6872,6791],{"class":338},[95,6874,313],{"class":101},[11,6876,6877],{},"Now the object and type come from the same source.",[11,6879,6880],{},"You don’t have to maintain:",[86,6882,6884],{"className":88,"code":6883,"language":90,"meta":91,"style":91},"type AlternateCodeType =\n  | 'CHARGE_CODE'\n  | 'NFTP_MDG_CODE'\n  | 'FACT_MDG_CODE'\n  | 'CW1_CHARGE_CODE'\n",[20,6885,6886,6894,6901,6908,6915],{"__ignoreMap":91},[95,6887,6888,6890,6892],{"class":97,"line":98},[95,6889,4404],{"class":201},[95,6891,6857],{"class":108},[95,6893,4410],{"class":201},[95,6895,6896,6898],{"class":97,"line":105},[95,6897,4415],{"class":201},[95,6899,6900],{"class":115}," 'CHARGE_CODE'\n",[95,6902,6903,6905],{"class":97,"line":122},[95,6904,4415],{"class":201},[95,6906,6907],{"class":115}," 'NFTP_MDG_CODE'\n",[95,6909,6910,6912],{"class":97,"line":134},[95,6911,4415],{"class":201},[95,6913,6914],{"class":115}," 'FACT_MDG_CODE'\n",[95,6916,6917,6919],{"class":97,"line":147},[95,6918,4415],{"class":201},[95,6920,6921],{"class":115}," 'CW1_CHARGE_CODE'\n",[11,6923,6924],{},"separately.",[11,6926,6927],{},"One source of truth is easier to maintain.",[75,6929,6931],{"id":6930},"_4-use-union-types-when-the-domain-has-a-fixed-set-of-states","4. Use Union Types When the Domain Has a Fixed Set of States",[11,6933,6934],{},"If something can only be one of a few values, make that explicit.",[11,6936,4947],{},[86,6938,6940],{"className":88,"code":6939,"language":90,"meta":91,"style":91},"function setStatus(status: string) {\n  \u002F\u002F ...\n}\n",[20,6941,6942,6959,6963],{"__ignoreMap":91},[95,6943,6944,6946,6949,6951,6953,6955,6957],{"class":97,"line":98},[95,6945,823],{"class":201},[95,6947,6948],{"class":108}," setStatus",[95,6950,1216],{"class":101},[95,6952,4452],{"class":370},[95,6954,1651],{"class":201},[95,6956,6471],{"class":338},[95,6958,2324],{"class":101},[95,6960,6961],{"class":97,"line":105},[95,6962,4093],{"class":382},[95,6964,6965],{"class":97,"line":122},[95,6966,174],{"class":101},[11,6968,4986],{},[86,6970,6972],{"className":88,"code":6971,"language":90,"meta":91,"style":91},"type Status = 'pending' | 'approved' | 'rejected'\n\nfunction setStatus(status: Status) {\n  \u002F\u002F ...\n}\n",[20,6973,6974,6992,6996,7012,7016],{"__ignoreMap":91},[95,6975,6976,6978,6980,6982,6984,6986,6988,6990],{"class":97,"line":98},[95,6977,4404],{"class":201},[95,6979,4407],{"class":108},[95,6981,341],{"class":201},[95,6983,4474],{"class":115},[95,6985,2403],{"class":201},[95,6987,4492],{"class":115},[95,6989,2403],{"class":201},[95,6991,4432],{"class":115},[95,6993,6994],{"class":97,"line":105},[95,6995,1731],{"emptyLinePlaceholder":1410},[95,6997,6998,7000,7002,7004,7006,7008,7010],{"class":97,"line":122},[95,6999,823],{"class":201},[95,7001,6948],{"class":108},[95,7003,1216],{"class":101},[95,7005,4452],{"class":370},[95,7007,1651],{"class":201},[95,7009,4407],{"class":108},[95,7011,2324],{"class":101},[95,7013,7014],{"class":97,"line":134},[95,7015,4093],{"class":382},[95,7017,7018],{"class":97,"line":147},[95,7019,174],{"class":101},[11,7021,7022],{},"Now this is valid:",[86,7024,7026],{"className":88,"code":7025,"language":90,"meta":91,"style":91},"setStatus('approved')\n",[20,7027,7028],{"__ignoreMap":91},[95,7029,7030,7033,7035,7038],{"class":97,"line":98},[95,7031,7032],{"class":108},"setStatus",[95,7034,1216],{"class":101},[95,7036,7037],{"class":115},"'approved'",[95,7039,1222],{"class":101},[11,7041,7042],{},"while this isn’t:",[86,7044,7046],{"className":88,"code":7045,"language":90,"meta":91,"style":91},"setStatus('something-else')\n",[20,7047,7048],{"__ignoreMap":91},[95,7049,7050,7052,7054,7057],{"class":97,"line":98},[95,7051,7032],{"class":108},[95,7053,1216],{"class":101},[95,7055,7056],{"class":115},"'something-else'",[95,7058,1222],{"class":101},[11,7060,7061],{},"The narrower your type, the more useful the compiler becomes.",[11,7063,7064],{},"This isn’t just about autocomplete.",[11,7066,7067],{},"It improves:",[639,7069,7070,7073,7075,7078,7081],{},[642,7071,7072],{},"refactoring",[642,7074,5066],{},[642,7076,7077],{},"error detection",[642,7079,7080],{},"API design",[642,7082,7083],{},"discoverability",[11,7085,7086],{},"If your business domain permits three values, don’t model it as an arbitrary string.",[75,7088,7090],{"id":7089},"_5-dont-reach-for-enums-automatically","5. Don’t Reach for Enums Automatically",[11,7092,7093],{},"Enums have their place.",[11,7095,7096],{},"But not every set of constants needs to be an enum.",[11,7098,7099],{},"If you only need a compile-time union:",[86,7101,7103],{"className":88,"code":7102,"language":90,"meta":91,"style":91},"type Status = 'ACTIVE' | 'INACTIVE'\n",[20,7104,7105],{"__ignoreMap":91},[95,7106,7107,7109,7111,7113,7116,7118],{"class":97,"line":98},[95,7108,4404],{"class":201},[95,7110,4407],{"class":108},[95,7112,341],{"class":201},[95,7114,7115],{"class":115}," 'ACTIVE'",[95,7117,2403],{"class":201},[95,7119,6771],{"class":115},[11,7121,7122],{},"may be enough.",[11,7124,7125],{},"If you need runtime values too:",[86,7127,7129],{"className":88,"code":7128,"language":90,"meta":91,"style":91},"const STATUS = {\n  ACTIVE: 'ACTIVE',\n  INACTIVE: 'INACTIVE',\n} as const\n\ntype Status = typeof STATUS[keyof typeof STATUS]\n",[20,7130,7131,7141,7149,7157,7165,7169],{"__ignoreMap":91},[95,7132,7133,7135,7137,7139],{"class":97,"line":98},[95,7134,335],{"class":201},[95,7136,6650],{"class":338},[95,7138,341],{"class":201},[95,7140,353],{"class":101},[95,7142,7143,7145,7147],{"class":97,"line":105},[95,7144,6659],{"class":101},[95,7146,290],{"class":115},[95,7148,119],{"class":101},[95,7150,7151,7153,7155],{"class":97,"line":122},[95,7152,6668],{"class":101},[95,7154,305],{"class":115},[95,7156,119],{"class":101},[95,7158,7159,7161,7163],{"class":97,"line":134},[95,7160,6720],{"class":101},[95,7162,4182],{"class":201},[95,7164,6725],{"class":201},[95,7166,7167],{"class":97,"line":147},[95,7168,1731],{"emptyLinePlaceholder":1410},[95,7170,7171,7173,7175,7177,7179,7181,7183,7185,7187],{"class":97,"line":160},[95,7172,4404],{"class":201},[95,7174,4407],{"class":108},[95,7176,341],{"class":201},[95,7178,6743],{"class":201},[95,7180,6650],{"class":338},[95,7182,6748],{"class":101},[95,7184,4204],{"class":201},[95,7186,6650],{"class":338},[95,7188,313],{"class":101},[11,7190,7191],{},"You now have both.",[11,7193,7194],{},"The important thing to understand is that a TypeScript type disappears at runtime.",[11,7196,7197],{},"An object does not.",[11,7199,7200],{},"So ask yourself:",[11,7202,7203],{},"Do I need runtime behaviour, or do I only need compile-time constraints?",[11,7205,7206],{},"Choose accordingly.",[75,7208,7210],{"id":7209},"_6-make-invalid-states-impossible-to-represent","6. Make Invalid States Impossible to Represent",[11,7212,7213],{},"This is perhaps the most important principle in this entire article.",[11,7215,7216],{},"Suppose you are building a form.",[11,7218,7219],{},"It can be:",[639,7221,7222,7225,7228,7231,7234],{},[642,7223,7224],{},"loading",[642,7226,7227],{},"ready",[642,7229,7230],{},"submitting",[642,7232,7233],{},"successful",[642,7235,7236],{},"failed",[11,7238,7239],{},"A common model is:",[86,7241,7243],{"className":88,"code":7242,"language":90,"meta":91,"style":91},"type FormState = {\n  loading: boolean\n  submitting: boolean\n  error?: string\n  data?: FormData\n}\n",[20,7244,7245,7256,7265,7274,7282,7292],{"__ignoreMap":91},[95,7246,7247,7249,7252,7254],{"class":97,"line":98},[95,7248,4404],{"class":201},[95,7250,7251],{"class":108}," FormState",[95,7253,341],{"class":201},[95,7255,353],{"class":101},[95,7257,7258,7261,7263],{"class":97,"line":105},[95,7259,7260],{"class":370},"  loading",[95,7262,1651],{"class":201},[95,7264,5241],{"class":338},[95,7266,7267,7270,7272],{"class":97,"line":122},[95,7268,7269],{"class":370},"  submitting",[95,7271,1651],{"class":201},[95,7273,5241],{"class":338},[95,7275,7276,7278,7280],{"class":97,"line":134},[95,7277,6004],{"class":370},[95,7279,5997],{"class":201},[95,7281,1654],{"class":338},[95,7283,7284,7287,7289],{"class":97,"line":147},[95,7285,7286],{"class":370},"  data",[95,7288,5997],{"class":201},[95,7290,7291],{"class":108}," FormData\n",[95,7293,7294],{"class":97,"line":160},[95,7295,174],{"class":101},[11,7297,7298],{},"Now you can accidentally represent:",[86,7300,7302],{"className":88,"code":7301,"language":90,"meta":91,"style":91},"{\n  loading: true,\n  submitting: true,\n  data: {...},\n  error: 'Something went wrong'\n}\n",[20,7303,7304,7308,7318,7328,7340,7348],{"__ignoreMap":91},[95,7305,7306],{"class":97,"line":98},[95,7307,102],{"class":101},[95,7309,7310,7312,7314,7316],{"class":97,"line":105},[95,7311,7260],{"class":108},[95,7313,112],{"class":101},[95,7315,5180],{"class":338},[95,7317,119],{"class":101},[95,7319,7320,7322,7324,7326],{"class":97,"line":122},[95,7321,7269],{"class":108},[95,7323,112],{"class":101},[95,7325,5180],{"class":338},[95,7327,119],{"class":101},[95,7329,7330,7332,7335,7337],{"class":97,"line":134},[95,7331,7286],{"class":108},[95,7333,7334],{"class":101},": {",[95,7336,1219],{"class":201},[95,7338,7339],{"class":101},"},\n",[95,7341,7342,7344,7346],{"class":97,"line":147},[95,7343,6004],{"class":108},[95,7345,112],{"class":101},[95,7347,6071],{"class":115},[95,7349,7350],{"class":97,"line":160},[95,7351,174],{"class":101},[11,7353,7354],{},"What does that state mean?",[11,7356,7357],{},"Your type doesn’t know.",[11,7359,7360],{},"A better design:",[86,7362,7364],{"className":88,"code":7363,"language":90,"meta":91,"style":91},"type FormState =\n  | { status: 'loading' }\n  | { status: 'ready'; data: FormData }\n  | { status: 'submitting'; data: FormData }\n  | { status: 'success'; data: FormData }\n  | { status: 'error'; error: string }\n",[20,7365,7366,7374,7389,7414,7437,7460],{"__ignoreMap":91},[95,7367,7368,7370,7372],{"class":97,"line":98},[95,7369,4404],{"class":201},[95,7371,7251],{"class":108},[95,7373,4410],{"class":201},[95,7375,7376,7378,7380,7382,7384,7387],{"class":97,"line":105},[95,7377,4415],{"class":201},[95,7379,3369],{"class":101},[95,7381,4452],{"class":370},[95,7383,1651],{"class":201},[95,7385,7386],{"class":115}," 'loading'",[95,7388,308],{"class":101},[95,7390,7391,7393,7395,7397,7399,7402,7405,7407,7409,7412],{"class":97,"line":122},[95,7392,4415],{"class":201},[95,7394,3369],{"class":101},[95,7396,4452],{"class":370},[95,7398,1651],{"class":201},[95,7400,7401],{"class":115}," 'ready'",[95,7403,7404],{"class":101},"; ",[95,7406,4912],{"class":370},[95,7408,1651],{"class":201},[95,7410,7411],{"class":108}," FormData",[95,7413,308],{"class":101},[95,7415,7416,7418,7420,7422,7424,7427,7429,7431,7433,7435],{"class":97,"line":134},[95,7417,4415],{"class":201},[95,7419,3369],{"class":101},[95,7421,4452],{"class":370},[95,7423,1651],{"class":201},[95,7425,7426],{"class":115}," 'submitting'",[95,7428,7404],{"class":101},[95,7430,4912],{"class":370},[95,7432,1651],{"class":201},[95,7434,7411],{"class":108},[95,7436,308],{"class":101},[95,7438,7439,7441,7443,7445,7447,7450,7452,7454,7456,7458],{"class":97,"line":147},[95,7440,4415],{"class":201},[95,7442,3369],{"class":101},[95,7444,4452],{"class":370},[95,7446,1651],{"class":201},[95,7448,7449],{"class":115}," 'success'",[95,7451,7404],{"class":101},[95,7453,4912],{"class":370},[95,7455,1651],{"class":201},[95,7457,7411],{"class":108},[95,7459,308],{"class":101},[95,7461,7462,7464,7466,7468,7470,7473,7475,7478,7480,7482],{"class":97,"line":160},[95,7463,4415],{"class":201},[95,7465,3369],{"class":101},[95,7467,4452],{"class":370},[95,7469,1651],{"class":201},[95,7471,7472],{"class":115}," 'error'",[95,7474,7404],{"class":101},[95,7476,7477],{"class":370},"error",[95,7479,1651],{"class":201},[95,7481,6471],{"class":338},[95,7483,308],{"class":101},[11,7485,7486],{},"Now each state describes exactly what is available.",[86,7488,7490],{"className":88,"code":7489,"language":90,"meta":91,"style":91},"function render(state: FormState) {\n  switch (state.status) {\n    case 'loading':\n      return 'Loading...'\n    case 'ready':\n      return state.data\n    case 'submitting':\n      return 'Submitting...'\n    case 'success':\n      return state.data\n    case 'error':\n      return state.error\n  }\n}\n",[20,7491,7492,7510,7517,7525,7532,7540,7547,7555,7562,7570,7576,7584,7591,7595],{"__ignoreMap":91},[95,7493,7494,7496,7499,7501,7504,7506,7508],{"class":97,"line":98},[95,7495,823],{"class":201},[95,7497,7498],{"class":108}," render",[95,7500,1216],{"class":101},[95,7502,7503],{"class":370},"state",[95,7505,1651],{"class":201},[95,7507,7251],{"class":108},[95,7509,2324],{"class":101},[95,7511,7512,7514],{"class":97,"line":105},[95,7513,4463],{"class":201},[95,7515,7516],{"class":101}," (state.status) {\n",[95,7518,7519,7521,7523],{"class":97,"line":122},[95,7520,4471],{"class":201},[95,7522,7386],{"class":115},[95,7524,4477],{"class":101},[95,7526,7527,7529],{"class":97,"line":134},[95,7528,4482],{"class":201},[95,7530,7531],{"class":115}," 'Loading...'\n",[95,7533,7534,7536,7538],{"class":97,"line":147},[95,7535,4471],{"class":201},[95,7537,7401],{"class":115},[95,7539,4477],{"class":101},[95,7541,7542,7544],{"class":97,"line":160},[95,7543,4482],{"class":201},[95,7545,7546],{"class":101}," state.data\n",[95,7548,7549,7551,7553],{"class":97,"line":171},[95,7550,4471],{"class":201},[95,7552,7426],{"class":115},[95,7554,4477],{"class":101},[95,7556,7557,7559],{"class":97,"line":882},[95,7558,4482],{"class":201},[95,7560,7561],{"class":115}," 'Submitting...'\n",[95,7563,7564,7566,7568],{"class":97,"line":1788},[95,7565,4471],{"class":201},[95,7567,7449],{"class":115},[95,7569,4477],{"class":101},[95,7571,7572,7574],{"class":97,"line":1793},[95,7573,4482],{"class":201},[95,7575,7546],{"class":101},[95,7577,7578,7580,7582],{"class":97,"line":1801},[95,7579,4471],{"class":201},[95,7581,7472],{"class":115},[95,7583,4477],{"class":101},[95,7585,7586,7588],{"class":97,"line":1820},[95,7587,4482],{"class":201},[95,7589,7590],{"class":101}," state.error\n",[95,7592,7593],{"class":97,"line":1829},[95,7594,1757],{"class":101},[95,7596,7597],{"class":97,"line":1851},[95,7598,174],{"class":101},[11,7600,7601],{},"This is the power of discriminated unions.",[11,7603,7604],{},"Instead of representing your application as a collection of independent booleans and optional properties, you represent it as a set of valid states.",[11,7606,7607],{},"That is a much stronger model.",[75,7609,7611],{"id":7610},"_7-be-careful-with-optional-properties","7. Be Careful With Optional Properties",[11,7613,7614],{},"Optional properties are useful.",[11,7616,7617],{},"But they are also an easy way to hide uncertainty.",[11,7619,3777],{},[86,7621,7623],{"className":88,"code":7622,"language":90,"meta":91,"style":91},"type User = {\n  id?: string\n  name?: string\n  email?: string\n}\n",[20,7624,7625,7635,7643,7651,7659],{"__ignoreMap":91},[95,7626,7627,7629,7631,7633],{"class":97,"line":98},[95,7628,4404],{"class":201},[95,7630,826],{"class":108},[95,7632,341],{"class":201},[95,7634,353],{"class":101},[95,7636,7637,7639,7641],{"class":97,"line":105},[95,7638,109],{"class":370},[95,7640,5997],{"class":201},[95,7642,1654],{"class":338},[95,7644,7645,7647,7649],{"class":97,"line":122},[95,7646,125],{"class":370},[95,7648,5997],{"class":201},[95,7650,1654],{"class":338},[95,7652,7653,7655,7657],{"class":97,"line":134},[95,7654,5227],{"class":370},[95,7656,5997],{"class":201},[95,7658,1654],{"class":338},[95,7660,7661],{"class":97,"line":147},[95,7662,174],{"class":101},[11,7664,7665],{},"Now every consumer has to account for the possibility that everything is missing.",[11,7667,7668],{},"But perhaps the actual domain says:",[11,7670,7671],{},"A User always has an ID, name, and email.",[11,7673,7674],{},"Then write:",[86,7676,7678],{"className":88,"code":7677,"language":90,"meta":91,"style":91},"type User = {\n  id: string\n  name: string\n  email: string\n}\n",[20,7679,7680,7690,7698,7706,7714],{"__ignoreMap":91},[95,7681,7682,7684,7686,7688],{"class":97,"line":98},[95,7683,4404],{"class":201},[95,7685,826],{"class":108},[95,7687,341],{"class":201},[95,7689,353],{"class":101},[95,7691,7692,7694,7696],{"class":97,"line":105},[95,7693,109],{"class":370},[95,7695,1651],{"class":201},[95,7697,1654],{"class":338},[95,7699,7700,7702,7704],{"class":97,"line":122},[95,7701,125],{"class":370},[95,7703,1651],{"class":201},[95,7705,1654],{"class":338},[95,7707,7708,7710,7712],{"class":97,"line":134},[95,7709,5227],{"class":370},[95,7711,1651],{"class":201},[95,7713,1654],{"class":338},[95,7715,7716],{"class":97,"line":147},[95,7717,174],{"class":101},[11,7719,7720],{},"Optional properties should represent actual optionality.",[11,7722,7723],{},"They shouldn’t be a substitute for saying:",[11,7725,7726],{},"“I’m not sure what this API returns.”",[11,7728,7729],{},"If the uncertainty comes from an external system, deal with it at the boundary.",[11,7731,7732],{},"Don’t make your entire application uncertain because one external system is.",[75,7734,7736,7737,7740,7741],{"id":7735},"_8-understand-null-vs-undefined","8. Understand ",[20,7738,7739],{},"null"," vs ",[20,7742,7743],{},"undefined",[11,7745,7746],{},"This distinction becomes surprisingly important in real applications.",[11,7748,3777],{},[86,7750,7752],{"className":88,"code":7751,"language":90,"meta":91,"style":91},"type User = {\n  middleName: string | null\n}\n",[20,7753,7754,7764,7777],{"__ignoreMap":91},[95,7755,7756,7758,7760,7762],{"class":97,"line":98},[95,7757,4404],{"class":201},[95,7759,826],{"class":108},[95,7761,341],{"class":201},[95,7763,353],{"class":101},[95,7765,7766,7769,7771,7773,7775],{"class":97,"line":105},[95,7767,7768],{"class":370},"  middleName",[95,7770,1651],{"class":201},[95,7772,6471],{"class":338},[95,7774,2403],{"class":201},[95,7776,2411],{"class":338},[95,7778,7779],{"class":97,"line":122},[95,7780,174],{"class":101},[11,7782,7783],{},"This could mean:",[11,7785,7786],{},"The property exists, but there is intentionally no middle name.",[11,7788,3953],{},[86,7790,7792],{"className":88,"code":7791,"language":90,"meta":91,"style":91},"type User = {\n  middleName?: string\n}\n",[20,7793,7794,7804,7812],{"__ignoreMap":91},[95,7795,7796,7798,7800,7802],{"class":97,"line":98},[95,7797,4404],{"class":201},[95,7799,826],{"class":108},[95,7801,341],{"class":201},[95,7803,353],{"class":101},[95,7805,7806,7808,7810],{"class":97,"line":105},[95,7807,7768],{"class":370},[95,7809,5997],{"class":201},[95,7811,1654],{"class":338},[95,7813,7814],{"class":97,"line":122},[95,7815,174],{"class":101},[11,7817,7818],{},"which often means:",[11,7820,7821],{},"The property itself may not exist.",[11,7823,7824],{},"This distinction matters especially for APIs.",[11,7826,7827],{},"Imagine a PATCH request:",[86,7829,7831],{"className":88,"code":7830,"language":90,"meta":91,"style":91},"{\n  middleName: null\n}\n",[20,7832,7833,7837,7846],{"__ignoreMap":91},[95,7834,7835],{"class":97,"line":98},[95,7836,102],{"class":101},[95,7838,7839,7841,7843],{"class":97,"line":105},[95,7840,7768],{"class":108},[95,7842,112],{"class":101},[95,7844,7845],{"class":338},"null\n",[95,7847,7848],{"class":97,"line":122},[95,7849,174],{"class":101},[11,7851,7852],{},"might mean:",[11,7854,7855],{},"Clear the middle name.",[11,7857,7858],{},"While:",[86,7860,7862],{"className":88,"code":7861,"language":90,"meta":91,"style":91},"{}\n",[20,7863,7864],{"__ignoreMap":91},[95,7865,7866],{"class":97,"line":98},[95,7867,7861],{"class":101},[11,7869,7852],{},[11,7871,7872],{},"Don’t change the middle name.",[11,7874,7875],{},"If your type doesn’t capture that distinction, subtle bugs can appear at the API boundary.",[11,7877,7878],{},"Types aren’t just compiler annotations.",[11,7880,7881],{},"They should communicate the semantics of your data.",[75,7883,7885,7886,7889],{"id":7884},"_9-use-satisfies-instead-of-blindly-asserting-types","9. Use ",[20,7887,7888],{},"satisfies"," Instead of Blindly Asserting Types",[11,7891,3777],{},[86,7893,7895],{"className":88,"code":7894,"language":90,"meta":91,"style":91},"type Config = {\n  timeout: number\n  retries: number\n}\n",[20,7896,7897,7908,7917,7926],{"__ignoreMap":91},[95,7898,7899,7901,7904,7906],{"class":97,"line":98},[95,7900,4404],{"class":201},[95,7902,7903],{"class":108}," Config",[95,7905,341],{"class":201},[95,7907,353],{"class":101},[95,7909,7910,7913,7915],{"class":97,"line":105},[95,7911,7912],{"class":370},"  timeout",[95,7914,1651],{"class":201},[95,7916,1664],{"class":338},[95,7918,7919,7922,7924],{"class":97,"line":122},[95,7920,7921],{"class":370},"  retries",[95,7923,1651],{"class":201},[95,7925,1664],{"class":338},[95,7927,7928],{"class":97,"line":134},[95,7929,174],{"class":101},[11,7931,7932],{},"You could write:",[86,7934,7936],{"className":88,"code":7935,"language":90,"meta":91,"style":91},"const config = {\n  timeout: 5000,\n  retries: 3,\n} as Config\n",[20,7937,7938,7949,7959,7968],{"__ignoreMap":91},[95,7939,7940,7942,7945,7947],{"class":97,"line":98},[95,7941,335],{"class":201},[95,7943,7944],{"class":338}," config",[95,7946,341],{"class":201},[95,7948,353],{"class":101},[95,7950,7951,7954,7957],{"class":97,"line":105},[95,7952,7953],{"class":101},"  timeout: ",[95,7955,7956],{"class":338},"5000",[95,7958,119],{"class":101},[95,7960,7961,7964,7966],{"class":97,"line":122},[95,7962,7963],{"class":101},"  retries: ",[95,7965,484],{"class":338},[95,7967,119],{"class":101},[95,7969,7970,7972,7974],{"class":97,"line":134},[95,7971,6720],{"class":101},[95,7973,4182],{"class":201},[95,7975,7976],{"class":108}," Config\n",[11,7978,7979,7980,7982],{},"But ",[20,7981,4182],{}," is an assertion.",[11,7984,7985],{},"You’re telling TypeScript:",[11,7987,4374],{},[11,7989,7990],{},"A better option in many cases is:",[86,7992,7994],{"className":88,"code":7993,"language":90,"meta":91,"style":91},"const config = {\n  timeout: 5000,\n  retries: 3,\n} satisfies Config\n",[20,7995,7996,8006,8014,8022],{"__ignoreMap":91},[95,7997,7998,8000,8002,8004],{"class":97,"line":98},[95,7999,335],{"class":201},[95,8001,7944],{"class":338},[95,8003,341],{"class":201},[95,8005,353],{"class":101},[95,8007,8008,8010,8012],{"class":97,"line":105},[95,8009,7953],{"class":101},[95,8011,7956],{"class":338},[95,8013,119],{"class":101},[95,8015,8016,8018,8020],{"class":97,"line":122},[95,8017,7963],{"class":101},[95,8019,484],{"class":338},[95,8021,119],{"class":101},[95,8023,8024,8026,8028],{"class":97,"line":134},[95,8025,6720],{"class":101},[95,8027,7888],{"class":201},[95,8029,7976],{"class":108},[11,8031,8032,8033,8036],{},"Now TypeScript verifies that the object conforms to ",[20,8034,8035],{},"Config"," while preserving the specific inferred type of the object.",[11,8038,8039],{},"A useful mental model:",[639,8041,8042,8047],{},[642,8043,8044,8046],{},[20,8045,4182],{}," — Treat this as this type.",[642,8048,8049,8051],{},[20,8050,7888],{}," — Verify that this value satisfies this type.",[11,8053,8054,8055,8057],{},"That distinction makes ",[20,8056,7888],{}," particularly useful for configuration objects, mappings, and lookup tables.",[75,8059,8061,8062,8064],{"id":8060},"_10-treat-as-as-a-boundary-not-a-default-tool","10. Treat ",[20,8063,4182],{}," as a Boundary, Not a Default Tool",[11,8066,8067],{},"Type assertions are sometimes necessary.",[11,8069,8070],{},"But this:",[86,8072,8074],{"className":88,"code":8073,"language":90,"meta":91,"style":91},"const user = response as User\n",[20,8075,8076],{"__ignoreMap":91},[95,8077,8078,8080,8082,8084,8086,8088],{"class":97,"line":98},[95,8079,335],{"class":201},[95,8081,3878],{"class":338},[95,8083,341],{"class":201},[95,8085,6394],{"class":101},[95,8087,4182],{"class":201},[95,8089,8090],{"class":108}," User\n",[11,8092,8093],{},"doesn’t validate anything.",[11,8095,8096],{},"If your API actually returned:",[86,8098,8100],{"className":88,"code":8099,"language":90,"meta":91,"style":91},"{\n  username: 'akshat'\n}\n",[20,8101,8102,8106,8116],{"__ignoreMap":91},[95,8103,8104],{"class":97,"line":98},[95,8105,102],{"class":101},[95,8107,8108,8111,8113],{"class":97,"line":105},[95,8109,8110],{"class":108},"  username",[95,8112,112],{"class":101},[95,8114,8115],{"class":115},"'akshat'\n",[95,8117,8118],{"class":97,"line":122},[95,8119,174],{"class":101},[11,8121,8122],{},"TypeScript isn’t going to complain about your assertion.",[11,8124,8125],{},"You’ve simply told the compiler to trust you.",[11,8127,8128],{},"This is especially dangerous around:",[639,8130,8131,8134,8137,8140,8143,8146],{},[642,8132,8133],{},"API responses",[642,8135,8136],{},"localStorage",[642,8138,8139],{},"URL parameters",[642,8141,8142],{},"environment variables",[642,8144,8145],{},"user input",[642,8147,8148],{},"third-party libraries",[11,8150,8151],{},"When data enters your application from outside TypeScript’s control, consider validating it.",[11,8153,8154],{},"For example, a runtime schema can establish:",[11,8156,8157],{},"“This data actually matches the shape my application expects.”",[11,8159,8160],{},"That’s a much stronger guarantee than:",[86,8162,8164],{"className":88,"code":8163,"language":90,"meta":91,"style":91},"value as User\n",[20,8165,8166],{"__ignoreMap":91},[95,8167,8168,8171,8173],{"class":97,"line":98},[95,8169,8170],{"class":101},"value ",[95,8172,4182],{"class":201},[95,8174,8090],{"class":108},[11,8176,8177],{},"TypeScript is excellent at compile-time guarantees.",[11,8179,8180],{},"It isn’t a runtime validation system.",[75,8182,8184],{"id":8183},"the-real-goal-model-the-domain","The Real Goal: Model the Domain",[11,8186,8187],{},"Once you start thinking this way, TypeScript becomes less about syntax.",[11,8189,8190],{},"You stop asking:",[11,8192,8193],{},"“How do I type this object?”",[11,8195,8196],{},"And start asking:",[11,8198,8199],{},"“What states can this object actually be in?”",[11,8201,8190],{},[11,8203,8204],{},"“Should I make this property optional?”",[11,8206,8196],{},[11,8208,8209],{},"“Is this property genuinely optional, or am I hiding uncertainty?”",[11,8211,8190],{},[11,8213,8214,8215,8217],{},"“Can I use ",[20,8216,4182],{}," here?”",[11,8219,8196],{},[11,8221,8222],{},"“Can I prove this value has the type I claim it has?”",[11,8224,8225],{},"That’s the shift.",[11,8227,8228],{},"Good TypeScript isn’t about adding more types.",[11,8230,8231],{},"It’s about making the types meaningful.",[75,8233,8235],{"id":8234},"a-simple-rule-to-remember","A Simple Rule to Remember",[11,8237,8238],{},"When designing a type, ask yourself three questions:",[3471,8240,8242],{"id":8241},"_1-what-states-are-actually-valid","1. What states are actually valid?",[11,8244,8245],{},"If you can represent invalid states, your model probably needs improvement.",[3471,8247,8249],{"id":8248},"_2-what-does-the-compiler-already-know","2. What does the compiler already know?",[11,8251,8252],{},"Don’t add annotations just for the sake of adding annotations.",[3471,8254,8256],{"id":8255},"_3-where-does-this-data-become-trustworthy","3. Where does this data become trustworthy?",[11,8258,8259],{},"The farther you get from an external boundary, the more confident your types should become.",[11,8261,8262],{},"The best TypeScript code is not the code with the most complicated types.",[11,8264,8265],{},"It’s the code where the types make the correct implementation feel natural—and the incorrect implementation difficult to write.",[11,8267,8268],{},"And once you start designing types this way, TypeScript stops feeling like a layer on top of JavaScript.",[11,8270,8271],{},"It starts becoming part of your application’s architecture.",[8273,8274],"hr",{},[11,8276,8277,8278,8281],{},"This is Part 1 of a two-part series on writing better TypeScript. In ",[61,8279,8280],{"href":5912},"Part 2",", we look at practical patterns for using TypeScript to make large codebases more maintainable.",[1383,8283,8284],{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":91,"searchDepth":105,"depth":105,"links":8286},[8287,8292,8293,8295,8296,8297,8298,8299,8301,8303,8305,8306],{"id":6238,"depth":105,"text":8288,"children":8289},"1. Stop Using any When You Actually Mean “I Don’t Know”",[8290],{"id":6348,"depth":122,"text":8291},"Use unknown when the value is genuinely unknown",{"id":6447,"depth":105,"text":6448},{"id":6631,"depth":105,"text":8294},"3. Use as const When Values Are Also Types",{"id":6930,"depth":105,"text":6931},{"id":7089,"depth":105,"text":7090},{"id":7209,"depth":105,"text":7210},{"id":7610,"depth":105,"text":7611},{"id":7735,"depth":105,"text":8300},"8. Understand null vs undefined",{"id":7884,"depth":105,"text":8302},"9. Use satisfies Instead of Blindly Asserting Types",{"id":8060,"depth":105,"text":8304},"10. Treat as as a Boundary, Not a Default Tool",{"id":8183,"depth":105,"text":8184},{"id":8234,"depth":105,"text":8235,"children":8307},[8308,8309,8310],{"id":8241,"depth":122,"text":8242},{"id":8248,"depth":122,"text":8249},{"id":8255,"depth":122,"text":8256},"2026-08-13",{"src":8313,"alt":8314},"\u002Fimages\u002Fblog\u002Fyou-know-typescript-but-do-you-know-how-to-model-your-code-with-it-cover.jpg","Cover graphic about modeling TypeScript domains with discriminated unions and meaningful types",{},{"title":5922,"description":5927},"blog\u002Fyou-know-typescript-but-do-you-know-how-to-model-your-code-with-it",[5916,1417,1418,1419,8319],"Architecture","qlM1eC3xViPXHeBz9-E9Jtw5UmAg7C6NDPzMhFlUkvc",{"id":8322,"title":8323,"body":8324,"date":8781,"description":8782,"draft":1404,"extension":1405,"featured":1404,"image":8783,"meta":8786,"navigation":1410,"path":8787,"seo":8788,"stem":8789,"tags":8790,"updatedAt":5918,"__hash__":8792},"blog\u002Fblog\u002Fwhen-vue-transitions-meet-web-components-a-hidden-edge-case.md","When Vue Transitions Meet Web Components: A Hidden Edge Case",{"type":8,"value":8325,"toc":8773},[8326,8341,8344,8347,8350,8356,8360,8363,8390,8404,8407,8410,8413,8419,8422,8432,8436,8439,8442,8479,8482,8485,8488,8491,8495,8502,8509,8512,8518,8524,8534,8537,8541,8550,8559,8711,8714,8717,8720,8724,8730,8747,8753,8755,8758,8761,8764,8767,8770],[11,8327,8328,8329,8332,8333,8336,8337,73],{},"In my previous article, I showed how to build a reusable ",[20,8330,8331],{},"TransitionExpand"," component for Vue that smoothly animates an element’s height using Vue’s built-in ",[20,8334,8335],{},"\u003CTransition>"," component. If you haven’t read it yet, I recommend starting there before continuing with this article: ",[61,8338,8340],{"href":8339},"\u002Fblog\u002Fbuilding-a-reusable-expand-collapse-transition-component-in-vue-3-with-smooth-height-animation","Building a Reusable Expand\u002FCollapse Transition Component in Vue 3",[11,8342,8343],{},"After publishing it, I integrated the component into an application that uses dynamically loaded Web Components and discovered an interesting edge case that wasn’t immediately obvious.",[11,8345,8346],{},"The original implementation worked perfectly with native HTML elements and Vue components, but failed when used with dynamically loaded Web Components.",[11,8348,8349],{},"Let’s look at why.",[11,8351,59,8352,73],{},[61,8353,66],{"href":8354,"rel":8355},"https:\u002F\u002Fakshattriv.medium.com\u002Fwhen-vue-transitions-meet-web-components-a-hidden-production-edge-case-d5681325f025",[65],[75,8357,8359],{"id":8358},"the-problem","The Problem",[11,8361,8362],{},"Imagine you have a notification component built as a Web Component.",[86,8364,8366],{"className":183,"code":8365,"language":185,"meta":91,"style":91},"\u003Ctransition-expand>\n  \u003Cnotification-wrapper v-if=\"showError\" \u002F>\n\u003C\u002Ftransition-expand>\n",[20,8367,8368,8377,8382],{"__ignoreMap":91},[95,8369,8370,8372,8375],{"class":97,"line":98},[95,8371,192],{"class":101},[95,8373,8374],{"class":195},"transition-expand",[95,8376,553],{"class":101},[95,8378,8379],{"class":97,"line":105},[95,8380,8381],{"class":101},"  \u003Cnotification-wrapper v-if=\"showError\" \u002F>\n",[95,8383,8384,8386,8388],{"class":97,"line":122},[95,8385,568],{"class":101},[95,8387,8374],{"class":195},[95,8389,553],{"class":101},[11,8391,8392,8393,8396,8397,8399,8400,8403],{},"When ",[20,8394,8395],{},"showError"," becomes ",[20,8398,5180],{},", Vue inserts the component into the DOM and immediately calls the ",[20,8401,8402],{},"enter"," transition hook.",[11,8405,8406],{},"For regular Vue components, that’s exactly what we want.",[11,8408,8409],{},"However, many Web Components are upgraded asynchronously.",[11,8411,8412],{},"The sequence actually looks like this:",[86,8414,8417],{"className":8415,"code":8416,"language":632,"meta":91},[630],"v-if becomes true\n        ↓\nVue inserts the element\n        ↓\nTransition enter() runs\n        ↓\nHeight is still 0px\n        ↓\nWeb Component finishes rendering\n        ↓\nActual height becomes 160px\n",[20,8418,8416],{"__ignoreMap":91},[11,8420,8421],{},"Our transition measures the height before the component has finished rendering.",[11,8423,8424,8425,8428,8429,8431],{},"As a result, the animation goes from ",[20,8426,8427],{},"0px"," to ",[20,8430,8427],{},", making it look like the transition never happened.",[75,8433,8435],{"id":8434},"the-first-thought","The First Thought",[11,8437,8438],{},"A common solution is to repeatedly check whether the element has a height.",[11,8440,8441],{},"Adding something like this before we calculate the height:",[86,8443,8445],{"className":88,"code":8444,"language":90,"meta":91,"style":91},"while (element.scrollHeight === 0) {\n  await new Promise(requestAnimationFrame)\n}\n",[20,8446,8447,8462,8475],{"__ignoreMap":91},[95,8448,8449,8452,8455,8457,8460],{"class":97,"line":98},[95,8450,8451],{"class":201},"while",[95,8453,8454],{"class":101}," (element.scrollHeight ",[95,8456,990],{"class":201},[95,8458,8459],{"class":338}," 0",[95,8461,2324],{"class":101},[95,8463,8464,8467,8469,8472],{"class":97,"line":105},[95,8465,8466],{"class":201},"  await",[95,8468,4621],{"class":201},[95,8470,8471],{"class":338}," Promise",[95,8473,8474],{"class":101},"(requestAnimationFrame)\n",[95,8476,8477],{"class":97,"line":122},[95,8478,174],{"class":101},[11,8480,8481],{},"This works.",[11,8483,8484],{},"But we’re continuously asking the browser the same question every frame:",[11,8486,8487],{},"“Has the height changed yet?”",[11,8489,8490],{},"It gets the job done, but there’s a cleaner approach.",[75,8492,8494],{"id":8493},"refactoring-the-original-code","Refactoring the Original Code",[11,8496,8497,8498,8501],{},"Before we introduce ",[20,8499,8500],{},"ResizeObserver",", we need to make one small change to the original implementation.",[11,8503,8504,8505,8508],{},"In the previous article, the ",[20,8506,8507],{},"enter()"," function was responsible for both measuring the element’s height and performing the animation.",[11,8510,8511],{},"Since we may now need to wait until the content has finished rendering, it’s cleaner to separate those responsibilities.",[11,8513,8514,8515,73],{},"The animation logic itself doesn’t change—we simply move it into a new helper function called ",[20,8516,8517],{},"animateEnter()",[11,8519,8520,8521,8523],{},"Our new ",[20,8522,8507],{}," function now has a single responsibility:",[639,8525,8526,8529],{},[642,8527,8528],{},"If the element already has a measurable height, start the animation immediately.",[642,8530,8531,8532,73],{},"Otherwise, wait until the element is ready, then call ",[20,8533,8517],{},[11,8535,8536],{},"This small refactor makes the code easier to read while keeping the original animation logic completely unchanged.",[75,8538,8540],{"id":8539},"a-better-solution-resizeobserver","A Better Solution: ResizeObserver",[11,8542,8543,8544,73],{},"The browser already provides an API that tells us when an element changes size: ",[61,8545,8548],{"href":8546,"rel":8547},"https:\u002F\u002Fdeveloper.mozilla.org\u002Fen-US\u002Fdocs\u002FWeb\u002FAPI\u002FResizeObserver",[65],[20,8549,8500],{},[11,8551,8552,8553,8555,8556,8558],{},"Instead of polling every frame, we simply wait until the browser tells us the element has a measurable height. With the animation logic extracted into ",[20,8554,8517],{},", the new ",[20,8557,8507],{}," function becomes much simpler.",[86,8560,8562],{"className":88,"code":8561,"language":90,"meta":91,"style":91},"function enter(element: HTMLElement) {\n  if (element.scrollHeight > 0) {\n    animateEnter(element)\n    return\n  }\n\n  const observer = new ResizeObserver(() => {\n    if (element.scrollHeight === 0)\n      return\n\n    observer.disconnect()\n    requestAnimationFrame(() => {\n      animateEnter(element)\n    })\n  })\n\n  observer.observe(element)\n}\n",[20,8563,8564,8583,8596,8604,8608,8612,8616,8636,8648,8652,8656,8666,8677,8684,8689,8693,8697,8707],{"__ignoreMap":91},[95,8565,8566,8568,8571,8573,8576,8578,8581],{"class":97,"line":98},[95,8567,823],{"class":201},[95,8569,8570],{"class":108}," enter",[95,8572,1216],{"class":101},[95,8574,8575],{"class":370},"element",[95,8577,1651],{"class":201},[95,8579,8580],{"class":108}," HTMLElement",[95,8582,2324],{"class":101},[95,8584,8585,8587,8589,8592,8594],{"class":97,"line":105},[95,8586,1736],{"class":201},[95,8588,8454],{"class":101},[95,8590,8591],{"class":201},">",[95,8593,8459],{"class":338},[95,8595,2324],{"class":101},[95,8597,8598,8601],{"class":97,"line":122},[95,8599,8600],{"class":108},"    animateEnter",[95,8602,8603],{"class":101},"(element)\n",[95,8605,8606],{"class":97,"line":134},[95,8607,2702],{"class":201},[95,8609,8610],{"class":97,"line":147},[95,8611,1757],{"class":101},[95,8613,8614],{"class":97,"line":160},[95,8615,1731],{"emptyLinePlaceholder":1410},[95,8617,8618,8620,8623,8625,8627,8630,8632,8634],{"class":97,"line":171},[95,8619,1715],{"class":201},[95,8621,8622],{"class":338}," observer",[95,8624,341],{"class":201},[95,8626,4621],{"class":201},[95,8628,8629],{"class":108}," ResizeObserver",[95,8631,347],{"class":101},[95,8633,350],{"class":201},[95,8635,353],{"class":101},[95,8637,8638,8640,8642,8644,8646],{"class":97,"line":882},[95,8639,2751],{"class":201},[95,8641,8454],{"class":101},[95,8643,990],{"class":201},[95,8645,8459],{"class":338},[95,8647,1222],{"class":101},[95,8649,8650],{"class":97,"line":1788},[95,8651,3144],{"class":201},[95,8653,8654],{"class":97,"line":1793},[95,8655,1731],{"emptyLinePlaceholder":1410},[95,8657,8658,8661,8664],{"class":97,"line":1801},[95,8659,8660],{"class":101},"    observer.",[95,8662,8663],{"class":108},"disconnect",[95,8665,1726],{"class":101},[95,8667,8668,8671,8673,8675],{"class":97,"line":1820},[95,8669,8670],{"class":108},"    requestAnimationFrame",[95,8672,347],{"class":101},[95,8674,350],{"class":201},[95,8676,353],{"class":101},[95,8678,8679,8682],{"class":97,"line":1829},[95,8680,8681],{"class":108},"      animateEnter",[95,8683,8603],{"class":101},[95,8685,8686],{"class":97,"line":1851},[95,8687,8688],{"class":101},"    })\n",[95,8690,8691],{"class":97,"line":1858},[95,8692,398],{"class":101},[95,8694,8695],{"class":97,"line":1863},[95,8696,1731],{"emptyLinePlaceholder":1410},[95,8698,8699,8702,8705],{"class":97,"line":1868},[95,8700,8701],{"class":101},"  observer.",[95,8703,8704],{"class":108},"observe",[95,8706,8603],{"class":101},[95,8708,8709],{"class":97,"line":1887},[95,8710,174],{"class":101},[11,8712,8713],{},"If the element already has a height, we animate immediately.",[11,8715,8716],{},"If not, we wait for the browser to notify us that its size has changed before starting the transition.",[11,8718,8719],{},"The actual animation logic doesn’t change at all—only the moment when it begins.",[75,8721,8723],{"id":8722},"why-this-is-better","Why This Is Better",[11,8725,8726,8727,8729],{},"Using ",[20,8728,8500],{}," has a few advantages:",[639,8731,8732,8735,8738,8741,8744],{},[642,8733,8734],{},"No polling or animation frame loops",[642,8736,8737],{},"Works with asynchronously upgraded Web Components",[642,8739,8740],{},"Works with lazy-loaded Vue components",[642,8742,8743],{},"Works with images or other content that changes size after rendering",[642,8745,8746],{},"Keeps the transition component completely generic",[11,8748,8749,8750,8752],{},"This means your ",[20,8751,8331],{}," component doesn’t need to know what it’s rendering. It simply waits until the content has a measurable size before animating.",[75,8754,3581],{"id":3580},[11,8756,8757],{},"The original implementation from my previous article is still perfectly valid for most Vue applications.",[11,8759,8760],{},"This improvement simply makes the component more resilient when working with asynchronously rendered content, where layout isn’t always ready the moment Vue inserts an element into the DOM.",[11,8762,8763],{},"It was a small change in code, but a significant improvement in reliability.",[11,8765,8766],{},"One of the things I enjoy most about building reusable components is discovering these real-world edge cases. Every time one appears, the component becomes a little more robust—not just for the current project, but for every future one as well.",[11,8768,8769],{},"If you’ve encountered similar transition issues with Web Components or other asynchronous content, I’d love to hear how you solved them.",[1383,8771,8772],{},"html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s9eBZ, html code.shiki .s9eBZ{--shiki-default:#22863A;--shiki-dark:#85E89D}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}",{"title":91,"searchDepth":105,"depth":105,"links":8774},[8775,8776,8777,8778,8779,8780],{"id":8358,"depth":105,"text":8359},{"id":8434,"depth":105,"text":8435},{"id":8493,"depth":105,"text":8494},{"id":8539,"depth":105,"text":8540},{"id":8722,"depth":105,"text":8723},{"id":3580,"depth":105,"text":3581},"2026-08-03","Why a Vue height expand transition can measure 0px with async Web Components—and how ResizeObserver makes TransitionExpand wait until content has a real size.",{"src":8784,"alt":8785},"\u002Fimages\u002Fblog\u002Fwhen-vue-transitions-meet-web-components-a-hidden-edge-case-cover.jpg","Cover comparing a broken Vue expand animation at 0px height with a ResizeObserver fix for Web Components",{},"\u002Fblog\u002Fwhen-vue-transitions-meet-web-components-a-hidden-edge-case",{"title":8323,"description":8782},"blog\u002Fwhen-vue-transitions-meet-web-components-a-hidden-edge-case",[1415,1417,8791,1419,3681],"Web Components","reNH84dymokUDAhFCv_QaJS_K3UK-rVgVqxQG1i24mc",{"id":8794,"title":8795,"body":8796,"date":10131,"description":10132,"draft":1404,"extension":1405,"featured":1404,"image":10133,"meta":10136,"navigation":1410,"path":10137,"seo":10138,"stem":10139,"tags":10140,"updatedAt":5918,"__hash__":10145},"blog\u002Fblog\u002Fnuxt-4-azure-msal-authentication-authorization-and-ssr-pitfalls.md","Nuxt 4 + Azure MSAL: Authentication, Authorization, and SSR Pitfalls",{"type":8,"value":8797,"toc":10111},[8798,8801,8804,8807,8830,8833,8839,8843,8846,8849,8852,8858,8865,8884,8887,8890,8893,8899,8903,8906,8909,8915,8921,8928,9084,9091,9114,9117,9121,9124,9127,9133,9136,9156,9159,9204,9207,9211,9214,9217,9220,9226,9229,9383,9386,9414,9417,9421,9424,9427,9430,9435,9438,9443,9446,9449,9452,9456,9459,9462,9468,9471,9477,9480,9486,9489,9494,9498,9501,9503,9580,9583,9586,9589,9596,9600,9603,9606,9612,9615,9618,9672,9675,9679,9682,9685,9688,9705,9708,9712,9723,9726,9743,9746,9797,9800,9845,9848,9862,9865,9912,9916,9922,9925,9950,9954,9958,9961,9976,9979,9982,9988,9992,9994,10011,10014,10017,10021,10023,10029,10032,10036,10038,10054,10057,10065,10068,10070,10076,10079,10094,10097,10100,10103,10106,10108],[11,8799,8800],{},"Authentication in Single Page Applications is usually straightforward: initialize your authentication library, redirect the user to sign in, and store the resulting tokens.",[11,8802,8803],{},"With Nuxt 4, however, things become more interesting.",[11,8805,8806],{},"Nuxt is not just a client-side framework. It is an SSR (Server-Side Rendering) framework, which means your code can execute on both the server and the browser. If you treat authentication the same way you would in a Vue SPA, you’ll quickly run into issues such as:",[639,8808,8809,8814,8819,8824,8827],{},[642,8810,8811],{},[20,8812,8813],{},"window is not defined",[642,8815,8816],{},[20,8817,8818],{},"localStorage is not defined",[642,8820,8821],{},[20,8822,8823],{},"non_browser_environment",[642,8825,8826],{},"MSAL initialization failures",[642,8828,8829],{},"Authentication state not being available during SSR",[11,8831,8832],{},"This article walks through a production-ready approach for implementing both authentication and authorization in a Nuxt 4 application using Azure MSAL, while ensuring your server-side APIs are protected as well.",[11,8834,59,8835,73],{},[61,8836,66],{"href":8837,"rel":8838},"https:\u002F\u002Fakshattriv.medium.com\u002Fnuxt-4-azure-msal-authentication-authorization-and-ssr-pitfalls-4b19bc213f3d",[65],[75,8840,8842],{"id":8841},"understanding-the-ssr-challenge","Understanding the SSR Challenge",[11,8844,8845],{},"One of the most common misconceptions when starting with Nuxt is assuming that all application code runs in the browser.",[11,8847,8848],{},"In reality, Nuxt renders pages on the server first and then hydrates them on the client.",[11,8850,8851],{},"The lifecycle looks something like this:",[86,8853,8856],{"className":8854,"code":8855,"language":632,"meta":91},[630],"Browser Request\n       ↓\nNuxt Server\n       ↓\nPage Rendered on Server\n       ↓\nHTML Sent to Browser\n       ↓\nClient Hydration\n       ↓\nBrowser-Specific Code Executes\n",[20,8857,8855],{"__ignoreMap":91},[11,8859,8860,8861,8864],{},"The Azure MSAL Browser SDK (",[20,8862,8863],{},"@azure\u002Fmsal-browser",") depends heavily on browser APIs such as:",[639,8866,8867,8872,8876,8881],{},[642,8868,8869],{},[20,8870,8871],{},"window",[642,8873,8874],{},[20,8875,8136],{},[642,8877,8878],{},[20,8879,8880],{},"sessionStorage",[642,8882,8883],{},"redirects",[11,8885,8886],{},"These APIs do not exist during SSR.",[11,8888,8889],{},"If MSAL initialization happens anywhere that Nuxt attempts to execute on the server, authentication will fail before the application even reaches the browser.",[11,8891,8892],{},"The solution is to ensure that MSAL is initialized exclusively on the client side.",[8894,8895],"article-figure",{"alt":8896,"caption":8897,"src":8898},"Architecture diagram of authentication and authorization flow from browser through MSAL and Azure AD to Nuxt API, JOSE token validation, and protected resources","Authentication and authorization flow in Nuxt 4 with Azure MSAL: secure access from browser to protected resources.","\u002Fimages\u002Fblog\u002Fnuxt-4-azure-msal-authentication-authorization-and-ssr-pitfalls\u002Fauthentication-authorization-flow.png",[75,8900,8902],{"id":8901},"creating-a-client-side-msal-plugin","Creating a Client-Side MSAL Plugin",[11,8904,8905],{},"Nuxt plugins provide an excellent mechanism for creating shared services.",[11,8907,8908],{},"Because MSAL must never run on the server, we create a client-only plugin:",[86,8910,8913],{"className":8911,"code":8912,"language":632,"meta":91},[630],"app\u002F\n └── plugins\u002F\n     └── msal.client.ts\n",[20,8914,8912],{"__ignoreMap":91},[11,8916,1671,8917,8920],{},[20,8918,8919],{},".client.ts"," suffix guarantees that Nuxt only loads this file in the browser.",[11,8922,8923,8924,8927],{},"The plugin creates and initializes a single ",[20,8925,8926],{},"PublicClientApplication"," instance:",[86,8929,8931],{"className":88,"code":8930,"language":90,"meta":91,"style":91},"import { PublicClientApplication } from '@azure\u002Fmsal-browser'\n\nexport default defineNuxtPlugin(async () => {\n  const auth = getMSALEnvironment()\n  const msalConfig = {\n    auth,\n    cache: {\n      cacheLocation: 'localStorage',\n    },\n  }\n  const msalInstance = new PublicClientApplication(msalConfig)\n  await msalInstance.initialize()\n\n  return {\n    provide: {\n      msal: msalInstance,\n    },\n  }\n})\n",[20,8932,8933,8945,8949,8969,8983,8994,8999,9004,9014,9019,9023,9040,9052,9056,9062,9067,9072,9076,9080],{"__ignoreMap":91},[95,8934,8935,8937,8940,8942],{"class":97,"line":98},[95,8936,2987],{"class":201},[95,8938,8939],{"class":101}," { PublicClientApplication } ",[95,8941,2993],{"class":201},[95,8943,8944],{"class":115}," '@azure\u002Fmsal-browser'\n",[95,8946,8947],{"class":97,"line":105},[95,8948,1731],{"emptyLinePlaceholder":1410},[95,8950,8951,8953,8956,8959,8961,8963,8965,8967],{"class":97,"line":122},[95,8952,3005],{"class":201},[95,8954,8955],{"class":201}," default",[95,8957,8958],{"class":108}," defineNuxtPlugin",[95,8960,1216],{"class":101},[95,8962,1698],{"class":201},[95,8964,3110],{"class":101},[95,8966,350],{"class":201},[95,8968,353],{"class":101},[95,8970,8971,8973,8976,8978,8981],{"class":97,"line":134},[95,8972,1715],{"class":201},[95,8974,8975],{"class":338}," auth",[95,8977,341],{"class":201},[95,8979,8980],{"class":108}," getMSALEnvironment",[95,8982,1726],{"class":101},[95,8984,8985,8987,8990,8992],{"class":97,"line":147},[95,8986,1715],{"class":201},[95,8988,8989],{"class":338}," msalConfig",[95,8991,341],{"class":201},[95,8993,353],{"class":101},[95,8995,8996],{"class":97,"line":160},[95,8997,8998],{"class":101},"    auth,\n",[95,9000,9001],{"class":97,"line":171},[95,9002,9003],{"class":101},"    cache: {\n",[95,9005,9006,9009,9012],{"class":97,"line":882},[95,9007,9008],{"class":101},"      cacheLocation: ",[95,9010,9011],{"class":115},"'localStorage'",[95,9013,119],{"class":101},[95,9015,9016],{"class":97,"line":1788},[95,9017,9018],{"class":101},"    },\n",[95,9020,9021],{"class":97,"line":1793},[95,9022,1757],{"class":101},[95,9024,9025,9027,9030,9032,9034,9037],{"class":97,"line":1801},[95,9026,1715],{"class":201},[95,9028,9029],{"class":338}," msalInstance",[95,9031,341],{"class":201},[95,9033,4621],{"class":201},[95,9035,9036],{"class":108}," PublicClientApplication",[95,9038,9039],{"class":101},"(msalConfig)\n",[95,9041,9042,9044,9047,9050],{"class":97,"line":1820},[95,9043,8466],{"class":201},[95,9045,9046],{"class":101}," msalInstance.",[95,9048,9049],{"class":108},"initialize",[95,9051,1726],{"class":101},[95,9053,9054],{"class":97,"line":1829},[95,9055,1731],{"emptyLinePlaceholder":1410},[95,9057,9058,9060],{"class":97,"line":1851},[95,9059,358],{"class":201},[95,9061,353],{"class":101},[95,9063,9064],{"class":97,"line":1858},[95,9065,9066],{"class":101},"    provide: {\n",[95,9068,9069],{"class":97,"line":1863},[95,9070,9071],{"class":101},"      msal: msalInstance,\n",[95,9073,9074],{"class":97,"line":1868},[95,9075,9018],{"class":101},[95,9077,9078],{"class":97,"line":1887},[95,9079,1757],{"class":101},[95,9081,9082],{"class":97,"line":1894},[95,9083,403],{"class":101},[11,9085,9086,9087,9090],{},"By returning the instance through ",[20,9088,9089],{},"provide",", it becomes accessible anywhere in the application through:",[86,9092,9094],{"className":88,"code":9093,"language":90,"meta":91,"style":91},"const { $msal } = useNuxtApp()\n",[20,9095,9096],{"__ignoreMap":91},[95,9097,9098,9100,9102,9105,9107,9109,9112],{"class":97,"line":98},[95,9099,335],{"class":201},[95,9101,3369],{"class":101},[95,9103,9104],{"class":338},"$msal",[95,9106,3375],{"class":101},[95,9108,205],{"class":201},[95,9110,9111],{"class":108}," useNuxtApp",[95,9113,1726],{"class":101},[11,9115,9116],{},"This creates a single source of truth for authentication across the application.",[75,9118,9120],{"id":9119},"encapsulating-authentication-logic-in-a-composable","Encapsulating Authentication Logic in a Composable",[11,9122,9123],{},"Authentication logic tends to spread across components very quickly.",[11,9125,9126],{},"A better pattern is to centralize everything into a composable.",[86,9128,9131],{"className":9129,"code":9130,"language":632,"meta":91},[630],"app\u002F\n └── composables\u002F\n     └── authentication.ts\n",[20,9132,9130],{"__ignoreMap":91},[11,9134,9135],{},"This composable becomes responsible for:",[639,9137,9138,9141,9144,9147,9150,9153],{},[642,9139,9140],{},"Sign-in",[642,9142,9143],{},"Sign-out",[642,9145,9146],{},"Account selection",[642,9148,9149],{},"Token acquisition",[642,9151,9152],{},"User profile management",[642,9154,9155],{},"Redirect handling",[11,9157,9158],{},"Instead of every component directly interacting with MSAL, they simply consume:",[86,9160,9162],{"className":88,"code":9161,"language":90,"meta":91,"style":91},"const {\n  signIn,\n  logOut,\n  getAccessToken,\n} = await useAuth()\n",[20,9163,9164,9170,9177,9184,9191],{"__ignoreMap":91},[95,9165,9166,9168],{"class":97,"line":98},[95,9167,335],{"class":201},[95,9169,353],{"class":101},[95,9171,9172,9175],{"class":97,"line":105},[95,9173,9174],{"class":338},"  signIn",[95,9176,119],{"class":101},[95,9178,9179,9182],{"class":97,"line":122},[95,9180,9181],{"class":338},"  logOut",[95,9183,119],{"class":101},[95,9185,9186,9189],{"class":97,"line":134},[95,9187,9188],{"class":338},"  getAccessToken",[95,9190,119],{"class":101},[95,9192,9193,9195,9197,9199,9202],{"class":97,"line":147},[95,9194,6720],{"class":101},[95,9196,205],{"class":201},[95,9198,1812],{"class":201},[95,9200,9201],{"class":108}," useAuth",[95,9203,1726],{"class":101},[11,9205,9206],{},"This separation significantly improves maintainability and testability.",[75,9208,9210],{"id":9209},"handling-redirect-responses-correctly","Handling Redirect Responses Correctly",[11,9212,9213],{},"One of the most frequent MSAL mistakes is forgetting to process redirect responses.",[11,9215,9216],{},"After Azure AD redirects users back to your application, MSAL stores authentication information that must be processed.",[11,9218,9219],{},"In Nuxt, a global route middleware is an excellent place for this logic.",[86,9221,9224],{"className":9222,"code":9223,"language":632,"meta":91},[630],"app\u002F\n └── middleware\u002F\n     └── auth.global.ts\n",[20,9225,9223],{"__ignoreMap":91},[11,9227,9228],{},"Example:",[86,9230,9232],{"className":88,"code":9231,"language":90,"meta":91,"style":91},"export default defineNuxtRouteMiddleware(async () => {\n  if (!import.meta.client) {\n    return\n  }\n\n  const { initializeAuth, handleMSALResponse } = await useAuth()\n  const msal = initializeAuth()\n  try {\n    const response = await msal.handleRedirectPromise()\n    handleMSALResponse(response)\n  }\n  catch (error) {\n    handleMSALResponse(null)\n  }\n})\n",[20,9233,9234,9253,9270,9274,9278,9282,9306,9320,9327,9345,9353,9357,9365,9375,9379],{"__ignoreMap":91},[95,9235,9236,9238,9240,9243,9245,9247,9249,9251],{"class":97,"line":98},[95,9237,3005],{"class":201},[95,9239,8955],{"class":201},[95,9241,9242],{"class":108}," defineNuxtRouteMiddleware",[95,9244,1216],{"class":101},[95,9246,1698],{"class":201},[95,9248,3110],{"class":101},[95,9250,350],{"class":201},[95,9252,353],{"class":101},[95,9254,9255,9257,9259,9262,9264,9267],{"class":97,"line":105},[95,9256,1736],{"class":201},[95,9258,1701],{"class":101},[95,9260,9261],{"class":201},"!import",[95,9263,73],{"class":101},[95,9265,9266],{"class":338},"meta",[95,9268,9269],{"class":101},".client) {\n",[95,9271,9272],{"class":97,"line":122},[95,9273,2702],{"class":201},[95,9275,9276],{"class":97,"line":134},[95,9277,1757],{"class":101},[95,9279,9280],{"class":97,"line":147},[95,9281,1731],{"emptyLinePlaceholder":1410},[95,9283,9284,9286,9288,9291,9293,9296,9298,9300,9302,9304],{"class":97,"line":160},[95,9285,1715],{"class":201},[95,9287,3369],{"class":101},[95,9289,9290],{"class":338},"initializeAuth",[95,9292,909],{"class":101},[95,9294,9295],{"class":338},"handleMSALResponse",[95,9297,3375],{"class":101},[95,9299,205],{"class":201},[95,9301,1812],{"class":201},[95,9303,9201],{"class":108},[95,9305,1726],{"class":101},[95,9307,9308,9310,9313,9315,9318],{"class":97,"line":171},[95,9309,1715],{"class":201},[95,9311,9312],{"class":338}," msal",[95,9314,341],{"class":201},[95,9316,9317],{"class":108}," initializeAuth",[95,9319,1726],{"class":101},[95,9321,9322,9325],{"class":97,"line":882},[95,9323,9324],{"class":201},"  try",[95,9326,353],{"class":101},[95,9328,9329,9331,9333,9335,9337,9340,9343],{"class":97,"line":1788},[95,9330,1804],{"class":201},[95,9332,6257],{"class":338},[95,9334,341],{"class":201},[95,9336,1812],{"class":201},[95,9338,9339],{"class":101}," msal.",[95,9341,9342],{"class":108},"handleRedirectPromise",[95,9344,1726],{"class":101},[95,9346,9347,9350],{"class":97,"line":1793},[95,9348,9349],{"class":108},"    handleMSALResponse",[95,9351,9352],{"class":101},"(response)\n",[95,9354,9355],{"class":97,"line":1801},[95,9356,1757],{"class":101},[95,9358,9359,9362],{"class":97,"line":1820},[95,9360,9361],{"class":201},"  catch",[95,9363,9364],{"class":101}," (error) {\n",[95,9366,9367,9369,9371,9373],{"class":97,"line":1829},[95,9368,9349],{"class":108},[95,9370,1216],{"class":101},[95,9372,7739],{"class":338},[95,9374,1222],{"class":101},[95,9376,9377],{"class":97,"line":1851},[95,9378,1757],{"class":101},[95,9380,9381],{"class":97,"line":1858},[95,9382,403],{"class":101},[11,9384,9385],{},"Notice the critical guard:",[86,9387,9389],{"className":88,"code":9388,"language":90,"meta":91,"style":91},"if (!import.meta.client) {\n  return\n}\n",[20,9390,9391,9405,9410],{"__ignoreMap":91},[95,9392,9393,9395,9397,9399,9401,9403],{"class":97,"line":98},[95,9394,2934],{"class":201},[95,9396,1701],{"class":101},[95,9398,9261],{"class":201},[95,9400,73],{"class":101},[95,9402,9266],{"class":338},[95,9404,9269],{"class":101},[95,9406,9407],{"class":97,"line":105},[95,9408,9409],{"class":201},"  return\n",[95,9411,9412],{"class":97,"line":122},[95,9413,174],{"class":101},[11,9415,9416],{},"Without this check, Nuxt would attempt to execute redirect handling during SSR.",[75,9418,9420],{"id":9419},"authentication-vs-authorization","Authentication vs Authorization",[11,9422,9423],{},"Many applications stop after user authentication.",[11,9425,9426],{},"This is a serious mistake.",[11,9428,9429],{},"Authentication answers:",[43,9431,9432],{},[11,9433,9434],{},"Who is the user?",[11,9436,9437],{},"Authorization answers:",[43,9439,9440],{},[11,9441,9442],{},"Is the user allowed to access this resource?",[11,9444,9445],{},"A user successfully logging into Azure AD does not automatically mean your server APIs are protected.",[11,9447,9448],{},"Every server endpoint must validate incoming tokens independently.",[11,9450,9451],{},"Never trust authentication performed solely on the client.",[75,9453,9455],{"id":9454},"creating-an-api-scope-in-azure-app-registration","Creating an API Scope in Azure App Registration",[11,9457,9458],{},"To authorize requests against your backend APIs, Azure AD must issue an access token intended for your application.",[11,9460,9461],{},"Navigate to:",[86,9463,9466],{"className":9464,"code":9465,"language":632,"meta":91},[630],"Azure Portal\n  → App Registrations\n  → Expose an API\n",[20,9467,9465],{"__ignoreMap":91},[11,9469,9470],{},"Create a scope such as:",[86,9472,9475],{"className":9473,"code":9474,"language":632,"meta":91},[630],"access_as_user\n",[20,9476,9474],{"__ignoreMap":91},[11,9478,9479],{},"Azure generates an identifier similar to:",[86,9481,9484],{"className":9482,"code":9483,"language":632,"meta":91},[630],"api:\u002F\u002F\u003Cclient-id>\u002Faccess_as_user\n",[20,9485,9483],{"__ignoreMap":91},[11,9487,9488],{},"This scope tells Azure AD:",[43,9490,9491],{},[11,9492,9493],{},"Issue a token that this backend API can validate.",[75,9495,9497],{"id":9496},"requesting-api-tokens-from-the-client","Requesting API Tokens from the Client",[11,9499,9500],{},"When calling backend APIs, the client should request the custom API scope instead of Microsoft Graph scopes.",[11,9502,9228],{},[86,9504,9506],{"className":88,"code":9505,"language":90,"meta":91,"style":91},"const tokenRequest = {\n  scopes: [`api:\u002F\u002F${clientId}\u002Faccess_as_user`],\n  account,\n}\n\nconst response = await $msal.acquireTokenSilent(tokenRequest)\n\nreturn response.accessToken\n",[20,9507,9508,9519,9536,9541,9545,9549,9568,9572],{"__ignoreMap":91},[95,9509,9510,9512,9515,9517],{"class":97,"line":98},[95,9511,335],{"class":201},[95,9513,9514],{"class":338}," tokenRequest",[95,9516,341],{"class":201},[95,9518,353],{"class":101},[95,9520,9521,9524,9527,9530,9533],{"class":97,"line":105},[95,9522,9523],{"class":101},"  scopes: [",[95,9525,9526],{"class":115},"`api:\u002F\u002F${",[95,9528,9529],{"class":101},"clientId",[95,9531,9532],{"class":115},"}\u002Faccess_as_user`",[95,9534,9535],{"class":101},"],\n",[95,9537,9538],{"class":97,"line":122},[95,9539,9540],{"class":101},"  account,\n",[95,9542,9543],{"class":97,"line":134},[95,9544,174],{"class":101},[95,9546,9547],{"class":97,"line":147},[95,9548,1731],{"emptyLinePlaceholder":1410},[95,9550,9551,9553,9555,9557,9559,9562,9565],{"class":97,"line":160},[95,9552,335],{"class":201},[95,9554,6257],{"class":338},[95,9556,341],{"class":201},[95,9558,1812],{"class":201},[95,9560,9561],{"class":101}," $msal.",[95,9563,9564],{"class":108},"acquireTokenSilent",[95,9566,9567],{"class":101},"(tokenRequest)\n",[95,9569,9570],{"class":97,"line":171},[95,9571,1731],{"emptyLinePlaceholder":1410},[95,9573,9574,9577],{"class":97,"line":882},[95,9575,9576],{"class":201},"return",[95,9578,9579],{"class":101}," response.accessToken\n",[11,9581,9582],{},"This produces an access token whose audience is your application rather than Microsoft Graph.",[11,9584,9585],{},"That distinction is important.",[11,9587,9588],{},"A Graph token should not be accepted by your backend.",[11,9590,9591,9592,9595],{},"Your backend should only accept tokens specifically issued for your API. Now you can use this token for calling your Nuxt server—the best way I believe is creating an Axios instance and then adding a request interceptor that fetches the token from ",[20,9593,9594],{},"authentication.ts"," and attaches it to the request header.",[75,9597,9599],{"id":9598},"securing-nuxt-server-routes","Securing Nuxt Server Routes",[11,9601,9602],{},"Once authentication works on the client, we need to protect server endpoints.",[11,9604,9605],{},"A clean approach is creating a server middleware:",[86,9607,9610],{"className":9608,"code":9609,"language":632,"meta":91},[630],"server\u002F\n └── middleware\u002F\n     └── auth.ts\n",[20,9611,9609],{"__ignoreMap":91},[11,9613,9614],{},"The middleware intercepts incoming requests before route handlers execute.",[11,9616,9617],{},"We only want protection for backend APIs:",[86,9619,9621],{"className":88,"code":9620,"language":90,"meta":91,"style":91},"if (\n  baseUrl.startsWith('\u002Fapi')\n  && baseUrl !== '\u002Fapi\u002Fhealth'\n  && baseUrl !== '\u002Fapi\u002Fprometheus'\n)\n",[20,9622,9623,9629,9644,9657,9668],{"__ignoreMap":91},[95,9624,9625,9627],{"class":97,"line":98},[95,9626,2934],{"class":201},[95,9628,836],{"class":101},[95,9630,9631,9634,9637,9639,9642],{"class":97,"line":105},[95,9632,9633],{"class":101},"  baseUrl.",[95,9635,9636],{"class":108},"startsWith",[95,9638,1216],{"class":101},[95,9640,9641],{"class":115},"'\u002Fapi'",[95,9643,1222],{"class":101},[95,9645,9646,9649,9652,9654],{"class":97,"line":122},[95,9647,9648],{"class":201},"  &&",[95,9650,9651],{"class":101}," baseUrl ",[95,9653,4299],{"class":201},[95,9655,9656],{"class":115}," '\u002Fapi\u002Fhealth'\n",[95,9658,9659,9661,9663,9665],{"class":97,"line":134},[95,9660,9648],{"class":201},[95,9662,9651],{"class":101},[95,9664,4299],{"class":201},[95,9666,9667],{"class":115}," '\u002Fapi\u002Fprometheus'\n",[95,9669,9670],{"class":97,"line":147},[95,9671,1222],{"class":101},[11,9673,9674],{},"This keeps public endpoints accessible while protecting application APIs.",[75,9676,9678],{"id":9677},"why-not-trust-the-token-blindly","Why Not Trust the Token Blindly?",[11,9680,9681],{},"An access token is simply a JWT.",[11,9683,9684],{},"Anyone can send a JWT.",[11,9686,9687],{},"Your server must verify:",[639,9689,9690,9693,9696,9699,9702],{},[642,9691,9692],{},"Signature",[642,9694,9695],{},"Issuer",[642,9697,9698],{},"Audience",[642,9700,9701],{},"Expiration",[642,9703,9704],{},"Tenant",[11,9706,9707],{},"If any of these checks fail, access should be denied.",[75,9709,9711],{"id":9710},"verifying-tokens-with-jose","Verifying Tokens with JOSE",[11,9713,9714,9715,9718,9719,9722],{},"While ",[20,9716,9717],{},"@azure\u002Fmsal-node"," can be used for server-side authentication flows and token acquisition, token validation is often simpler and more lightweight with the ",[20,9720,9721],{},"jose"," package.",[11,9724,9725],{},"The middleware uses Microsoft’s public signing keys:",[86,9727,9729],{"className":88,"code":9728,"language":90,"meta":91,"style":91},"import { createRemoteJWKSet, jwtVerify } from 'jose'\n",[20,9730,9731],{"__ignoreMap":91},[95,9732,9733,9735,9738,9740],{"class":97,"line":98},[95,9734,2987],{"class":201},[95,9736,9737],{"class":101}," { createRemoteJWKSet, jwtVerify } ",[95,9739,2993],{"class":201},[95,9741,9742],{"class":115}," 'jose'\n",[11,9744,9745],{},"Create a remote JWK set:",[86,9747,9749],{"className":88,"code":9748,"language":90,"meta":91,"style":91},"const JWKS = createRemoteJWKSet(\n  new URL(\n    `https:\u002F\u002Flogin.microsoftonline.com\u002F${tenantId}\u002Fdiscovery\u002Fv2.0\u002Fkeys`,\n  ),\n)\n",[20,9750,9751,9765,9775,9788,9793],{"__ignoreMap":91},[95,9752,9753,9755,9758,9760,9763],{"class":97,"line":98},[95,9754,335],{"class":201},[95,9756,9757],{"class":338}," JWKS",[95,9759,341],{"class":201},[95,9761,9762],{"class":108}," createRemoteJWKSet",[95,9764,976],{"class":101},[95,9766,9767,9770,9773],{"class":97,"line":105},[95,9768,9769],{"class":201},"  new",[95,9771,9772],{"class":108}," URL",[95,9774,976],{"class":101},[95,9776,9777,9780,9783,9786],{"class":97,"line":122},[95,9778,9779],{"class":115},"    `https:\u002F\u002Flogin.microsoftonline.com\u002F${",[95,9781,9782],{"class":101},"tenantId",[95,9784,9785],{"class":115},"}\u002Fdiscovery\u002Fv2.0\u002Fkeys`",[95,9787,119],{"class":101},[95,9789,9790],{"class":97,"line":134},[95,9791,9792],{"class":101},"  ),\n",[95,9794,9795],{"class":97,"line":147},[95,9796,1222],{"class":101},[11,9798,9799],{},"Then verify the token:",[86,9801,9803],{"className":88,"code":9802,"language":90,"meta":91,"style":91},"await jwtVerify(token, JWKS, {\n  issuer: `https:\u002F\u002Flogin.microsoftonline.com\u002F${tenantId}\u002Fv2.0`,\n  audience: clientId,\n})\n",[20,9804,9805,9821,9836,9841],{"__ignoreMap":91},[95,9806,9807,9809,9812,9815,9818],{"class":97,"line":98},[95,9808,1902],{"class":201},[95,9810,9811],{"class":108}," jwtVerify",[95,9813,9814],{"class":101},"(token, ",[95,9816,9817],{"class":338},"JWKS",[95,9819,9820],{"class":101},", {\n",[95,9822,9823,9826,9829,9831,9834],{"class":97,"line":105},[95,9824,9825],{"class":101},"  issuer: ",[95,9827,9828],{"class":115},"`https:\u002F\u002Flogin.microsoftonline.com\u002F${",[95,9830,9782],{"class":101},[95,9832,9833],{"class":115},"}\u002Fv2.0`",[95,9835,119],{"class":101},[95,9837,9838],{"class":97,"line":122},[95,9839,9840],{"class":101},"  audience: clientId,\n",[95,9842,9843],{"class":97,"line":134},[95,9844,403],{"class":101},[11,9846,9847],{},"This ensures:",[639,9849,9850,9853,9856,9859],{},[642,9851,9852],{},"Microsoft issued the token",[642,9854,9855],{},"The token wasn’t modified",[642,9857,9858],{},"The token belongs to your tenant",[642,9860,9861],{},"The token was intended for your application",[11,9863,9864],{},"If verification fails:",[86,9866,9868],{"className":88,"code":9867,"language":90,"meta":91,"style":91},"setResponseStatus(event, 401)\nreturn {\n  statusCode: 401,\n  message: 'Unauthorized access',\n}\n",[20,9869,9870,9883,9889,9898,9908],{"__ignoreMap":91},[95,9871,9872,9875,9878,9881],{"class":97,"line":98},[95,9873,9874],{"class":108},"setResponseStatus",[95,9876,9877],{"class":101},"(event, ",[95,9879,9880],{"class":338},"401",[95,9882,1222],{"class":101},[95,9884,9885,9887],{"class":97,"line":105},[95,9886,9576],{"class":201},[95,9888,353],{"class":101},[95,9890,9891,9894,9896],{"class":97,"line":122},[95,9892,9893],{"class":101},"  statusCode: ",[95,9895,9880],{"class":338},[95,9897,119],{"class":101},[95,9899,9900,9903,9906],{"class":97,"line":134},[95,9901,9902],{"class":101},"  message: ",[95,9904,9905],{"class":115},"'Unauthorized access'",[95,9907,119],{"class":101},[95,9909,9910],{"class":97,"line":147},[95,9911,174],{"class":101},[75,9913,9915],{"id":9914},"recommended-directory-structure","Recommended Directory Structure",[86,9917,9920],{"className":9918,"code":9919,"language":632,"meta":91},[630],"app\u002F\n├── composables\u002F\n│   └── authentication.ts\n│\n├── middleware\u002F\n│   └── auth.global.ts\n│\n└── plugins\u002F\n    └── msal.client.ts\n\nserver\u002F\n└── middleware\u002F\n    └── auth.ts\n",[20,9921,9919],{"__ignoreMap":91},[11,9923,9924],{},"Responsibilities:",[639,9926,9927,9933,9938,9944],{},[642,9928,9929,9932],{},[20,9930,9931],{},"msal.client.ts"," → Initialize MSAL in browser only",[642,9934,9935,9937],{},[20,9936,9594],{}," → Centralize authentication logic",[642,9939,9940,9943],{},[20,9941,9942],{},"auth.global.ts"," → Process redirects and user session",[642,9945,9946,9949],{},[20,9947,9948],{},"server\u002Fmiddleware\u002Fauth.ts"," → Validate JWTs for API requests",[75,9951,9953],{"id":9952},"common-pitfalls","Common Pitfalls",[3471,9955,9957],{"id":9956},"initializing-msal-during-ssr","Initializing MSAL During SSR",[11,9959,9960],{},"Bad:",[86,9962,9964],{"className":88,"code":9963,"language":90,"meta":91,"style":91},"import { PublicClientApplication } from '@azure\u002Fmsal-browser'\n",[20,9965,9966],{"__ignoreMap":91},[95,9967,9968,9970,9972,9974],{"class":97,"line":98},[95,9969,2987],{"class":201},[95,9971,8939],{"class":101},[95,9973,2993],{"class":201},[95,9975,8944],{"class":115},[11,9977,9978],{},"inside shared server-executed code.",[11,9980,9981],{},"Result:",[86,9983,9986],{"className":9984,"code":9985,"language":632,"meta":91},[630],"window is not defined\n",[20,9987,9985],{"__ignoreMap":91},[3471,9989,9991],{"id":9990},"using-graph-tokens-for-backend-apis","Using Graph Tokens for Backend APIs",[11,9993,9960],{},[86,9995,9997],{"className":88,"code":9996,"language":90,"meta":91,"style":91},"scopes: ['user.read']\n",[20,9998,9999],{"__ignoreMap":91},[95,10000,10001,10004,10006,10009],{"class":97,"line":98},[95,10002,10003],{"class":108},"scopes",[95,10005,756],{"class":101},[95,10007,10008],{"class":115},"'user.read'",[95,10010,313],{"class":101},[11,10012,10013],{},"for API authorization.",[11,10015,10016],{},"Your backend should validate tokens issued specifically for your API scope.",[3471,10018,10020],{"id":10019},"trusting-client-authentication","Trusting Client Authentication",[11,10022,9960],{},[86,10024,10027],{"className":10025,"code":10026,"language":632,"meta":91},[630],"User logged in = API access granted\n",[20,10028,10026],{"__ignoreMap":91},[11,10030,10031],{},"Client-side authentication alone does not protect server endpoints. Your server endpoints can be accessed outside of the client, by another application or Postman\u002FBruno.",[3471,10033,10035],{"id":10034},"skipping-audience-validation","Skipping Audience Validation",[11,10037,9960],{},[86,10039,10041],{"className":88,"code":10040,"language":90,"meta":91,"style":91},"jwtVerify(token, JWKS)\n",[20,10042,10043],{"__ignoreMap":91},[95,10044,10045,10048,10050,10052],{"class":97,"line":98},[95,10046,10047],{"class":108},"jwtVerify",[95,10049,9814],{"class":101},[95,10051,9817],{"class":338},[95,10053,1222],{"class":101},[11,10055,10056],{},"Always validate:",[639,10058,10059,10062],{},[642,10060,10061],{},"issuer",[642,10063,10064],{},"audience",[11,10066,10067],{},"to ensure the token was intended for your application.",[75,10069,3581],{"id":3580},[11,10071,10072,10073,10075],{},"Implementing Azure AD authentication in Nuxt 4 requires understanding that Nuxt is not purely a browser application. The server executes code before the browser ever sees the page, and browser-specific libraries such as ",[20,10074,8863],{}," must be isolated carefully.",[11,10077,10078],{},"A robust setup typically follows four principles:",[10080,10081,10082,10085,10088,10091],"ol",{},[642,10083,10084],{},"Initialize MSAL only on the client.",[642,10086,10087],{},"Expose a single MSAL instance through a Nuxt plugin.",[642,10089,10090],{},"Centralize authentication logic in composables and route middleware.",[642,10092,10093],{},"Independently verify access tokens on the server before allowing API access.",[11,10095,10096],{},"When both client-side authentication and server-side authorization are implemented correctly, you gain a secure architecture where Azure AD identities are trusted, API access is validated, and Nuxt SSR works seamlessly without the common MSAL pitfalls.",[11,10098,10099],{},"Thank you for taking the time to read this article.",[11,10101,10102],{},"I hope it helped clarify the challenges of implementing authentication and authorization in a Nuxt 4 application using Azure MSAL, especially in an SSR environment. If you’ve encountered similar issues or have a different approach, I’d love to hear about your experience.",[11,10104,10105],{},"If you found this article useful, consider sharing it with other developers who are working with Nuxt, Azure AD, or enterprise authentication workflows.",[11,10107,1381],{},[1383,10109,10110],{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":91,"searchDepth":105,"depth":105,"links":10112},[10113,10114,10115,10116,10117,10118,10119,10120,10121,10122,10123,10124,10130],{"id":8841,"depth":105,"text":8842},{"id":8901,"depth":105,"text":8902},{"id":9119,"depth":105,"text":9120},{"id":9209,"depth":105,"text":9210},{"id":9419,"depth":105,"text":9420},{"id":9454,"depth":105,"text":9455},{"id":9496,"depth":105,"text":9497},{"id":9598,"depth":105,"text":9599},{"id":9677,"depth":105,"text":9678},{"id":9710,"depth":105,"text":9711},{"id":9914,"depth":105,"text":9915},{"id":9952,"depth":105,"text":9953,"children":10125},[10126,10127,10128,10129],{"id":9956,"depth":122,"text":9957},{"id":9990,"depth":122,"text":9991},{"id":10019,"depth":122,"text":10020},{"id":10034,"depth":122,"text":10035},{"id":3580,"depth":105,"text":3581},"2026-07-27","Implement Azure MSAL in Nuxt 4 without SSR crashes—client-only plugins, composables, redirect middleware, API scopes, and JOSE JWT validation on server routes.",{"src":10134,"alt":10135},"\u002Fimages\u002Fblog\u002Fnuxt-4-azure-msal-authentication-authorization-and-ssr-pitfalls-cover.jpg","Cover graphic for Nuxt 4 Azure MSAL authentication covering SSR pitfalls and API authorization",{},"\u002Fblog\u002Fnuxt-4-azure-msal-authentication-authorization-and-ssr-pitfalls",{"title":8795,"description":10132},"blog\u002Fnuxt-4-azure-msal-authentication-authorization-and-ssr-pitfalls",[10141,1415,10142,10143,10144],"Nuxtjs","Authentication","Software Engineering","Full Stack Developer","DE_BfoEngPZ3CFTLhF-DfGSfC0Q7BCxSDgRMnE6BsGI",{"id":10147,"title":10148,"body":10149,"date":10803,"description":10804,"draft":1404,"extension":1405,"featured":1404,"image":10805,"meta":10808,"navigation":1410,"path":10809,"seo":10810,"stem":10811,"tags":10812,"updatedAt":5918,"__hash__":10815},"blog\u002Fblog\u002Fstop-fighting-screen-sizes-why-i-switched-from-px-to-rem-for-enterprise-applications.md","Stop Fighting Screen Sizes: Why I Switched from px to rem for Enterprise Applications",{"type":8,"value":10150,"toc":10793},[10151,10154,10161,10164,10167,10170,10173,10182,10188,10192,10195,10242,10247,10288,10291,10324,10327,10354,10357,10361,10364,10367,10533,10536,10539,10542,10546,10549,10552,10555,10558,10561,10581,10584,10587,10601,10604,10607,10611,10614,10617,10620,10623,10626,10629,10633,10636,10639,10645,10648,10662,10665,10669,10672,10675,10681,10684,10687,10693,10696,10702,10705,10708,10712,10715,10718,10732,10735,10749,10755,10757,10760,10763,10766,10772,10775,10778,10781,10784,10787,10790],[11,10152,10153],{},"For years, I built applications the same way most front-end developers do: everything in pixels.",[11,10155,10156,10157,10160],{},"Font sizes, spacing, component dimensions, layouts—",[20,10158,10159],{},"px"," everywhere.",[11,10162,10163],{},"Then I started leading the development of enterprise portals used by thousands of internal users across different regions, departments, and hardware setups. That’s when I discovered a problem that modern web development discussions don’t talk about enough:",[11,10165,10166],{},"Most enterprise users are not working on large 4K monitors.",[11,10168,10169],{},"Many are working on 13-inch and 14-inch corporate laptops with limited screen real estate. A portal that looks perfectly fine on a designer’s 27-inch monitor can quickly feel cramped, oversized, and frustrating on smaller screens.",[11,10171,10172],{},"My solution wasn’t more breakpoints.",[11,10174,10175,10176,10178,10179,73],{},"It was replacing ",[20,10177,10159],{}," with ",[20,10180,10181],{},"rem",[11,10183,59,10184,73],{},[61,10185,66],{"href":10186,"rel":10187},"https:\u002F\u002Fakshattriv.medium.com\u002Fstop-fighting-screen-sizes-why-i-switched-from-px-to-rem-for-enterprise-applications-a4320184feed",[65],[75,10189,10191],{"id":10190},"the-simple-idea","The Simple Idea",[11,10193,10194],{},"Instead of defining sizes directly in pixels:",[86,10196,10200],{"className":10197,"code":10198,"language":10199,"meta":91,"style":91},"language-css shiki shiki-themes github-light github-dark",".title {\n  font-size: 24px;\n  margin-bottom: 16px;\n}\n","css",[20,10201,10202,10209,10224,10238],{"__ignoreMap":91},[95,10203,10204,10207],{"class":97,"line":98},[95,10205,10206],{"class":108},".title",[95,10208,353],{"class":101},[95,10210,10211,10214,10216,10219,10221],{"class":97,"line":105},[95,10212,10213],{"class":338},"  font-size",[95,10215,112],{"class":101},[95,10217,10218],{"class":338},"24",[95,10220,10159],{"class":201},[95,10222,10223],{"class":101},";\n",[95,10225,10226,10229,10231,10234,10236],{"class":97,"line":122},[95,10227,10228],{"class":338},"  margin-bottom",[95,10230,112],{"class":101},[95,10232,10233],{"class":338},"16",[95,10235,10159],{"class":201},[95,10237,10223],{"class":101},[95,10239,10240],{"class":97,"line":134},[95,10241,174],{"class":101},[11,10243,10244,10245,1651],{},"I started defining almost everything in ",[20,10246,10181],{},[86,10248,10250],{"className":10197,"code":10249,"language":10199,"meta":91,"style":91},".title {\n  font-size: 2.4rem;\n  margin-bottom: 1.6rem;\n}\n",[20,10251,10252,10258,10271,10284],{"__ignoreMap":91},[95,10253,10254,10256],{"class":97,"line":98},[95,10255,10206],{"class":108},[95,10257,353],{"class":101},[95,10259,10260,10262,10264,10267,10269],{"class":97,"line":105},[95,10261,10213],{"class":338},[95,10263,112],{"class":101},[95,10265,10266],{"class":338},"2.4",[95,10268,10181],{"class":201},[95,10270,10223],{"class":101},[95,10272,10273,10275,10277,10280,10282],{"class":97,"line":122},[95,10274,10228],{"class":338},[95,10276,112],{"class":101},[95,10278,10279],{"class":338},"1.6",[95,10281,10181],{"class":201},[95,10283,10223],{"class":101},[95,10285,10286],{"class":97,"line":134},[95,10287,174],{"class":101},[11,10289,10290],{},"And then controlled the scale of the entire application through the root font size:",[86,10292,10294],{"className":10197,"code":10293,"language":10199,"meta":91,"style":91},"html {\n  font-size: 62.5%; \u002F* 1rem = 10px *\u002F\n}\n",[20,10295,10296,10303,10320],{"__ignoreMap":91},[95,10297,10298,10301],{"class":97,"line":98},[95,10299,10300],{"class":195},"html",[95,10302,353],{"class":101},[95,10304,10305,10307,10309,10312,10315,10317],{"class":97,"line":105},[95,10306,10213],{"class":338},[95,10308,112],{"class":101},[95,10310,10311],{"class":338},"62.5",[95,10313,10314],{"class":201},"%",[95,10316,7404],{"class":101},[95,10318,10319],{"class":382},"\u002F* 1rem = 10px *\u002F\n",[95,10321,10322],{"class":97,"line":122},[95,10323,174],{"class":101},[11,10325,10326],{},"This makes sizing calculations incredibly simple:",[639,10328,10329,10338,10346],{},[642,10330,10331,10334,10335],{},[20,10332,10333],{},"1.6rem"," = ",[20,10336,10337],{},"16px",[642,10339,10340,10334,10343],{},[20,10341,10342],{},"2.4rem",[20,10344,10345],{},"24px",[642,10347,10348,10334,10351],{},[20,10349,10350],{},"4rem",[20,10352,10353],{},"40px",[11,10355,10356],{},"No mental math required.",[75,10358,10360],{"id":10359},"the-real-superpower","The Real Superpower",[11,10362,10363],{},"The real benefit appears when supporting different screen sizes.",[11,10365,10366],{},"Instead of writing dozens of component-level media queries, I only adjust the root font size.",[86,10368,10370],{"className":10197,"code":10369,"language":10199,"meta":91,"style":91},"html {\n  font-size: 62.5%; \u002F* 1rem = 10px *\u002F\n}\n\n@media only screen and (min-width: 1280px) and (max-width: 1440px) {\n  html {\n    font-size: 56.25%; \u002F* 1rem = 9px *\u002F\n  }\n}\n\n@media only screen and (max-width: 1280px) {\n  html {\n    font-size: 50%; \u002F* 1rem = 8px *\u002F\n  }\n}\n",[20,10371,10372,10378,10392,10396,10400,10445,10452,10469,10473,10477,10481,10503,10509,10525,10529],{"__ignoreMap":91},[95,10373,10374,10376],{"class":97,"line":98},[95,10375,10300],{"class":195},[95,10377,353],{"class":101},[95,10379,10380,10382,10384,10386,10388,10390],{"class":97,"line":105},[95,10381,10213],{"class":338},[95,10383,112],{"class":101},[95,10385,10311],{"class":338},[95,10387,10314],{"class":201},[95,10389,7404],{"class":101},[95,10391,10319],{"class":382},[95,10393,10394],{"class":97,"line":122},[95,10395,174],{"class":101},[95,10397,10398],{"class":97,"line":134},[95,10399,1731],{"emptyLinePlaceholder":1410},[95,10401,10402,10405,10408,10411,10414,10416,10419,10421,10424,10426,10428,10431,10433,10436,10438,10441,10443],{"class":97,"line":147},[95,10403,10404],{"class":201},"@media",[95,10406,10407],{"class":201}," only",[95,10409,10410],{"class":338}," screen",[95,10412,10413],{"class":201}," and",[95,10415,1701],{"class":101},[95,10417,10418],{"class":338},"min-width",[95,10420,112],{"class":101},[95,10422,10423],{"class":338},"1280",[95,10425,10159],{"class":201},[95,10427,373],{"class":101},[95,10429,10430],{"class":201},"and",[95,10432,1701],{"class":101},[95,10434,10435],{"class":338},"max-width",[95,10437,112],{"class":101},[95,10439,10440],{"class":338},"1440",[95,10442,10159],{"class":201},[95,10444,2324],{"class":101},[95,10446,10447,10450],{"class":97,"line":160},[95,10448,10449],{"class":195},"  html",[95,10451,353],{"class":101},[95,10453,10454,10457,10459,10462,10464,10466],{"class":97,"line":171},[95,10455,10456],{"class":338},"    font-size",[95,10458,112],{"class":101},[95,10460,10461],{"class":338},"56.25",[95,10463,10314],{"class":201},[95,10465,7404],{"class":101},[95,10467,10468],{"class":382},"\u002F* 1rem = 9px *\u002F\n",[95,10470,10471],{"class":97,"line":882},[95,10472,1757],{"class":101},[95,10474,10475],{"class":97,"line":1788},[95,10476,174],{"class":101},[95,10478,10479],{"class":97,"line":1793},[95,10480,1731],{"emptyLinePlaceholder":1410},[95,10482,10483,10485,10487,10489,10491,10493,10495,10497,10499,10501],{"class":97,"line":1801},[95,10484,10404],{"class":201},[95,10486,10407],{"class":201},[95,10488,10410],{"class":338},[95,10490,10413],{"class":201},[95,10492,1701],{"class":101},[95,10494,10435],{"class":338},[95,10496,112],{"class":101},[95,10498,10423],{"class":338},[95,10500,10159],{"class":201},[95,10502,2324],{"class":101},[95,10504,10505,10507],{"class":97,"line":1820},[95,10506,10449],{"class":195},[95,10508,353],{"class":101},[95,10510,10511,10513,10515,10518,10520,10522],{"class":97,"line":1829},[95,10512,10456],{"class":338},[95,10514,112],{"class":101},[95,10516,10517],{"class":338},"50",[95,10519,10314],{"class":201},[95,10521,7404],{"class":101},[95,10523,10524],{"class":382},"\u002F* 1rem = 8px *\u002F\n",[95,10526,10527],{"class":97,"line":1851},[95,10528,1757],{"class":101},[95,10530,10531],{"class":97,"line":1858},[95,10532,174],{"class":101},[11,10534,10535],{},"That’s it.",[11,10537,10538],{},"Every font, margin, padding, gap, width, height, card, table, and form element scales proportionally across the application.",[11,10540,10541],{},"Instead of maintaining hundreds of responsive adjustments, the entire portal adapts through a few lines of CSS.",[75,10543,10545],{"id":10544},"why-this-works-so-well-for-enterprise-applications","Why This Works So Well for Enterprise Applications",[11,10547,10548],{},"Consumer websites often prioritize mobile responsiveness.",[11,10550,10551],{},"Enterprise applications have a different challenge.",[11,10553,10554],{},"Users spend eight hours a day inside the same portal.",[11,10556,10557],{},"The goal is not just responsiveness—it’s information density.",[11,10559,10560],{},"A procurement dashboard, facility management system, master data portal, or analytics application often needs to display:",[639,10562,10563,10566,10569,10572,10575,10578],{},[642,10564,10565],{},"Large tables",[642,10567,10568],{},"Filters",[642,10570,10571],{},"Side panels",[642,10573,10574],{},"Forms",[642,10576,10577],{},"Navigation menus",[642,10579,10580],{},"Data visualizations",[11,10582,10583],{},"all on the same screen.",[11,10585,10586],{},"When every element is sized in pixels, smaller laptops frequently suffer from:",[639,10588,10589,10592,10595,10598],{},[642,10590,10591],{},"Excessive scrolling",[642,10593,10594],{},"Truncated content",[642,10596,10597],{},"Reduced visible data",[642,10599,10600],{},"Poor workspace utilization",[11,10602,10603],{},"By scaling the root font size, the entire interface becomes more compact while maintaining visual consistency.",[11,10605,10606],{},"The experience feels intentional rather than squeezed.",[75,10608,10610],{"id":10609},"my-experience","My Experience",[11,10612,10613],{},"I first adopted this approach while working on enterprise master data management portals.",[11,10615,10616],{},"Initially, the goal was simple: make the application more usable on smaller corporate laptops without creating endless responsive rules.",[11,10618,10619],{},"The results were surprisingly effective.",[11,10621,10622],{},"Instead of designing and maintaining multiple layouts, the entire portal scaled naturally. Tables displayed more rows, forms required less scrolling, and users could see more information without sacrificing readability.",[11,10624,10625],{},"The amount of responsive CSS we had to maintain dropped significantly.",[11,10627,10628],{},"What started as an experiment quickly became my preferred approach for desktop-focused enterprise systems.",[75,10630,10632],{"id":10631},"the-catch-third-party-libraries","The Catch: Third-Party Libraries",[11,10634,10635],{},"Nothing comes for free.",[11,10637,10638],{},"The biggest challenge appears when integrating third-party UI libraries.",[11,10640,10641,10642,10644],{},"If your application uses ",[20,10643,10181],{}," but a component library is heavily based on fixed pixel values, scaling becomes inconsistent.",[11,10646,10647],{},"You may notice issues like:",[639,10649,10650,10653,10656,10659],{},[642,10651,10652],{},"Text scales but icons don’t",[642,10654,10655],{},"Containers resize but internal spacing remains fixed",[642,10657,10658],{},"Dialogs look oversized or undersized",[642,10660,10661],{},"Layout alignment becomes inconsistent",[11,10663,10664],{},"The application still works, but the visual harmony starts breaking down.",[75,10666,10668],{"id":10667},"a-lesson-from-our-design-system","A Lesson from Our Design System",[11,10670,10671],{},"I ran into this exact problem with my company’s design system.",[11,10673,10674],{},"The design system was delivered as web components and most of its styling was implemented using pixel units.",[11,10676,10677,10678,10680],{},"As our applications adopted a ",[20,10679,10181],{},"-based scaling strategy, some components stopped scaling consistently with the rest of the portal.",[11,10682,10683],{},"Rather than implementing local overrides everywhere, I discussed the issue with the design system maintainers.",[11,10685,10686],{},"The proposal was straightforward:",[11,10688,10689,10690,10692],{},"Provide an alternative stylesheet using ",[20,10691,10181],{}," units.",[11,10694,10695],{},"The maintainers agreed that the approach made sense for enterprise applications, especially those targeting desktop environments with varying screen sizes.",[11,10697,10698,10699,10701],{},"Eventually, support was added and teams could opt into a ",[20,10700,10181],{},"-based stylesheet whenever scaling support was required.",[11,10703,10704],{},"That experience reinforced an important lesson:",[11,10706,10707],{},"A design system becomes significantly more adaptable when it avoids hard-coded pixel assumptions.",[75,10709,10711],{"id":10710},"is-rem-better-than-px","Is rem Better Than px?",[11,10713,10714],{},"Not always.",[11,10716,10717],{},"Pixels still make sense for:",[639,10719,10720,10723,10726,10729],{},[642,10721,10722],{},"Borders",[642,10724,10725],{},"Hairline separators",[642,10727,10728],{},"Certain icons",[642,10730,10731],{},"Precise graphical elements",[11,10733,10734],{},"But for most application-level styling:",[639,10736,10737,10740,10743,10746],{},[642,10738,10739],{},"Typography",[642,10741,10742],{},"Spacing",[642,10744,10745],{},"Layout dimensions",[642,10747,10748],{},"Component sizing",[11,10750,10751,10752,10754],{},"I have found ",[20,10753,10181],{}," to be far more maintainable and scalable.",[75,10756,3581],{"id":3580},[11,10758,10759],{},"Responsive design is often treated as a problem solved through more media queries.",[11,10761,10762],{},"My experience has been the opposite.",[11,10764,10765],{},"The best responsive strategy I’ve used for enterprise desktop applications was reducing media queries, not adding more.",[11,10767,10768,10769,10771],{},"A ",[20,10770,10181],{},"-based design system allows the entire application to scale through a few root font-size adjustments, making it easier to support the reality of corporate hardware: smaller laptops, limited screen space, and users who spend their entire workday inside your application.",[11,10773,10774],{},"If you’re building enterprise software and your CSS is full of breakpoint-specific overrides, it might be worth asking a simple question:",[11,10776,10777],{},"What if the problem isn’t your layout, but your unit of measurement?",[11,10779,10780],{},"Thanks for reading all the way to the end.",[11,10782,10783],{},"I hope this article gave you a different perspective on building responsive and user-friendly applications, especially for enterprise environments where screen space is often limited.",[11,10785,10786],{},"If you’ve used a similar approach—or have a different take on responsive design—I’d love to hear your thoughts.",[11,10788,10789],{},"Until next time, happy coding.",[1383,10791,10792],{},"html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s9eBZ, html code.shiki .s9eBZ{--shiki-default:#22863A;--shiki-dark:#85E89D}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":91,"searchDepth":105,"depth":105,"links":10794},[10795,10796,10797,10798,10799,10800,10801,10802],{"id":10190,"depth":105,"text":10191},{"id":10359,"depth":105,"text":10360},{"id":10544,"depth":105,"text":10545},{"id":10609,"depth":105,"text":10610},{"id":10631,"depth":105,"text":10632},{"id":10667,"depth":105,"text":10668},{"id":10710,"depth":105,"text":10711},{"id":3580,"depth":105,"text":3581},"2026-07-19","Scale enterprise UIs by sizing in rem and adjusting the root font-size—fewer media queries, better density on 13–14 inch corporate laptops, and clearer design-system tradeoffs.",{"src":10806,"alt":10807},"\u002Fimages\u002Fblog\u002Fstop-fighting-screen-sizes-why-i-switched-from-px-to-rem-for-enterprise-applications-cover.jpg","Cover comparing a cramped pixel-based enterprise UI with a rem-scaled layout that fits more content on a laptop screen",{},"\u002Fblog\u002Fstop-fighting-screen-sizes-why-i-switched-from-px-to-rem-for-enterprise-applications",{"title":10148,"description":10804},"blog\u002Fstop-fighting-screen-sizes-why-i-switched-from-px-to-rem-for-enterprise-applications",[3681,10813,1419,10814,10143],"CSS","Responsive Design","flCr6-zrgTvJc8pkiQA4EJAo0v-4Rv6zzzUkRCXn5AQ",{"id":10817,"title":10818,"body":10819,"date":11441,"description":11442,"draft":1404,"extension":1405,"featured":1404,"image":11443,"meta":11446,"navigation":1410,"path":11447,"seo":11448,"stem":11449,"tags":11450,"updatedAt":1420,"__hash__":11454},"blog\u002Fblog\u002Fstop-writing-duplicate-css-build-scalable-light-and-dark-themes-with-sass-maps-mixins.md","Stop Writing Duplicate CSS: Build Scalable Light & Dark Themes with Sass Maps + Mixins",{"type":8,"value":10820,"toc":11424},[10821,10824,10827,10835,10839,10842,10917,10920,10924,10927,10941,10945,10948,11028,11037,11041,11044,11056,11058,11075,11078,11084,11087,11091,11094,11136,11139,11142,11165,11168,11179,11183,11186,11243,11252,11256,11259,11306,11309,11313,11317,11320,11324,11327,11349,11361,11365,11372,11376,11383,11395,11398,11402,11409,11412,11415,11418,11421],[11,10822,10823],{},"Implementing light and dark themes used to be messy: duplicate styles, endless overrides, and hardcoded colors scattered across files. With Sass maps and mixins, you can build a clean, scalable theming system that stays maintainable and developer-friendly.",[11,10825,10826],{},"In this article, we’ll break down a practical approach to theming step by step—in a way beginners can follow.",[11,10828,10829,10830,73],{},"You’ll need Sass in your project. Sass is a CSS preprocessor that makes writing and managing styles more structured. See the docs: ",[61,10831,10834],{"href":10832,"rel":10833},"https:\u002F\u002Fsass-lang.com\u002F",[65],"Sass Lang: CSS with superpowers",[75,10836,10838],{"id":10837},"the-problem-with-traditional-theming","The problem with traditional theming",[11,10840,10841],{},"Most developers start like this:",[86,10843,10845],{"className":10197,"code":10844,"language":10199,"meta":91,"style":91},".light {\n  background: white;\n  color: black;\n}\n\n.dark {\n  background: black;\n  color: white;\n}\n",[20,10846,10847,10854,10866,10878,10882,10886,10893,10903,10913],{"__ignoreMap":91},[95,10848,10849,10852],{"class":97,"line":98},[95,10850,10851],{"class":108},".light",[95,10853,353],{"class":101},[95,10855,10856,10859,10861,10864],{"class":97,"line":105},[95,10857,10858],{"class":338},"  background",[95,10860,112],{"class":101},[95,10862,10863],{"class":338},"white",[95,10865,10223],{"class":101},[95,10867,10868,10871,10873,10876],{"class":97,"line":122},[95,10869,10870],{"class":338},"  color",[95,10872,112],{"class":101},[95,10874,10875],{"class":338},"black",[95,10877,10223],{"class":101},[95,10879,10880],{"class":97,"line":134},[95,10881,174],{"class":101},[95,10883,10884],{"class":97,"line":147},[95,10885,1731],{"emptyLinePlaceholder":1410},[95,10887,10888,10891],{"class":97,"line":160},[95,10889,10890],{"class":108},".dark",[95,10892,353],{"class":101},[95,10894,10895,10897,10899,10901],{"class":97,"line":171},[95,10896,10858],{"class":338},[95,10898,112],{"class":101},[95,10900,10875],{"class":338},[95,10902,10223],{"class":101},[95,10904,10905,10907,10909,10911],{"class":97,"line":882},[95,10906,10870],{"class":338},[95,10908,112],{"class":101},[95,10910,10863],{"class":338},[95,10912,10223],{"class":101},[95,10914,10915],{"class":97,"line":1788},[95,10916,174],{"class":101},[11,10918,10919],{},"This works… until your app grows. Imagine 50+ colors, multiple components, and ongoing design updates—and suddenly you’re managing chaos.",[75,10921,10923],{"id":10922},"the-idea-centralize-everything","The idea: centralize everything",[11,10925,10926],{},"Instead of scattering styles, we:",[10080,10928,10929,10932,10935,10938],{},[642,10930,10931],{},"Define all theme values in one place",[642,10933,10934],{},"Use Sass maps to store them",[642,10936,10937],{},"Use a mixin to generate CSS variables",[642,10939,10940],{},"Switch themes by toggling a class",[75,10942,10944],{"id":10943},"step-1-understanding-sass-maps","Step 1: Understanding Sass maps",[11,10946,10947],{},"A Sass map is like a JavaScript object:",[86,10949,10953],{"className":10950,"code":10951,"language":10952,"meta":91,"style":91},"language-scss shiki shiki-themes github-light github-dark","$light-theme: (\n  text-color-1: #141414,\n  surface-bg-color-1: #ffffff,\n);\n\n$dark-theme: (\n  text-color-1: #f0f0f0,\n  surface-bg-color-1: #141414,\n);\n","scss",[20,10954,10955,10963,10975,10987,10992,10996,11003,11014,11024],{"__ignoreMap":91},[95,10956,10957,10960],{"class":97,"line":98},[95,10958,10959],{"class":370},"$light-theme",[95,10961,10962],{"class":101},": (\n",[95,10964,10965,10968,10970,10973],{"class":97,"line":105},[95,10966,10967],{"class":338},"  text-color-1",[95,10969,112],{"class":101},[95,10971,10972],{"class":338},"#141414",[95,10974,119],{"class":101},[95,10976,10977,10980,10982,10985],{"class":97,"line":122},[95,10978,10979],{"class":338},"  surface-bg-color-1",[95,10981,112],{"class":101},[95,10983,10984],{"class":338},"#ffffff",[95,10986,119],{"class":101},[95,10988,10989],{"class":97,"line":134},[95,10990,10991],{"class":101},");\n",[95,10993,10994],{"class":97,"line":147},[95,10995,1731],{"emptyLinePlaceholder":1410},[95,10997,10998,11001],{"class":97,"line":160},[95,10999,11000],{"class":370},"$dark-theme",[95,11002,10962],{"class":101},[95,11004,11005,11007,11009,11012],{"class":97,"line":171},[95,11006,10967],{"class":338},[95,11008,112],{"class":101},[95,11010,11011],{"class":338},"#f0f0f0",[95,11013,119],{"class":101},[95,11015,11016,11018,11020,11022],{"class":97,"line":882},[95,11017,10979],{"class":338},[95,11019,112],{"class":101},[95,11021,10972],{"class":338},[95,11023,119],{"class":101},[95,11025,11026],{"class":97,"line":1788},[95,11027,10991],{"class":101},[11,11029,11030,11031,11033,11034,11036],{},"Think of it as a dictionary of design tokens—a single source of truth. We’ve created two maps, ",[20,11032,10959],{}," and ",[20,11035,11000],{},". Each can hold text colors, background colors, border colors, shadows, and semantic colors (success, warning, error). Everything stays organized in one place.",[75,11038,11040],{"id":11039},"step-2-accessing-map-values","Step 2: Accessing map values",[11,11042,11043],{},"Sass provides a function:",[86,11045,11047],{"className":10950,"code":11046,"language":10952,"meta":91,"style":91},"map.get(\u003Ctheme-map-name>, \u003Ckey>)\n",[20,11048,11049],{"__ignoreMap":91},[95,11050,11051,11053],{"class":97,"line":98},[95,11052,364],{"class":195},[95,11054,11055],{"class":101},".get(\u003Ctheme-map-name>, \u003Ckey>)\n",[11,11057,9228],{},[86,11059,11061],{"className":10950,"code":11060,"language":10952,"meta":91,"style":91},"map.get($light-theme, text-color-1)\n",[20,11062,11063],{"__ignoreMap":91},[95,11064,11065,11067,11070,11072],{"class":97,"line":98},[95,11066,364],{"class":195},[95,11068,11069],{"class":101},".get(",[95,11071,10959],{"class":370},[95,11073,11074],{"class":101},", text-color-1)\n",[11,11076,11077],{},"This returns:",[86,11079,11082],{"className":11080,"code":11081,"language":632,"meta":91},[630],"#141414\n",[20,11083,11081],{"__ignoreMap":91},[11,11085,11086],{},"So instead of hardcoding values, you query your theme.",[75,11088,11090],{"id":11089},"step-3-the-power-of-mixins","Step 3: The power of mixins",[11,11092,11093],{},"Now comes the real magic—our mixin:",[86,11095,11097],{"className":10950,"code":11096,"language":10952,"meta":91,"style":91},"@mixin apply-theme($theme-map) {\n  --text-color-1: #{map.get($theme-map, text-color-1)};\n}\n",[20,11098,11099,11114,11132],{"__ignoreMap":91},[95,11100,11101,11104,11107,11109,11112],{"class":97,"line":98},[95,11102,11103],{"class":201},"@mixin",[95,11105,11106],{"class":108}," apply-theme",[95,11108,1216],{"class":101},[95,11110,11111],{"class":370},"$theme-map",[95,11113,2324],{"class":101},[95,11115,11116,11119,11121,11124,11127,11130],{"class":97,"line":105},[95,11117,11118],{"class":370},"  --text-color-1",[95,11120,112],{"class":101},[95,11122,11123],{"class":370},"#{map.",[95,11125,11126],{"class":338},"get",[95,11128,11129],{"class":370},"($theme-map, text-color-1)}",[95,11131,10223],{"class":101},[95,11133,11134],{"class":97,"line":122},[95,11135,174],{"class":101},[11,11137,11138],{},"What’s happening here?",[11,11140,11141],{},"We’re converting Sass values into CSS custom properties. Each theme map gets transformed into a set of variables like:",[86,11143,11145],{"className":10197,"code":11144,"language":10199,"meta":91,"style":91},"--text-color-1: #141414;\n--surface-bg-color-1: #ffffff;\n",[20,11146,11147,11157],{"__ignoreMap":91},[95,11148,11149,11152,11155],{"class":97,"line":98},[95,11150,11151],{"class":101},"--text-color-1: ",[95,11153,10972],{"class":11154},"s7hpK",[95,11156,10223],{"class":101},[95,11158,11159,11162],{"class":97,"line":105},[95,11160,11161],{"class":101},"--surface-bg-color-1: ",[95,11163,11164],{"class":11154},"#ffffff;\n",[11,11166,11167],{},"Why is this powerful? Because CSS variables:",[639,11169,11170,11173,11176],{},[642,11171,11172],{},"Work at runtime",[642,11174,11175],{},"Can be changed dynamically",[642,11177,11178],{},"Are accessible anywhere in your app",[75,11180,11182],{"id":11181},"step-4-applying-themes","Step 4: Applying themes",[11,11184,11185],{},"We apply the mixin like this:",[86,11187,11189],{"className":10950,"code":11188,"language":10952,"meta":91,"style":91},"body {\n  @include apply-theme($light-theme);\n}\n\nbody.dark {\n  @include apply-theme($dark-theme);\n}\n",[20,11190,11191,11198,11211,11215,11219,11227,11239],{"__ignoreMap":91},[95,11192,11193,11196],{"class":97,"line":98},[95,11194,11195],{"class":195},"body",[95,11197,353],{"class":101},[95,11199,11200,11203,11205,11207,11209],{"class":97,"line":105},[95,11201,11202],{"class":201},"  @include",[95,11204,11106],{"class":108},[95,11206,1216],{"class":101},[95,11208,10959],{"class":370},[95,11210,10991],{"class":101},[95,11212,11213],{"class":97,"line":122},[95,11214,174],{"class":101},[95,11216,11217],{"class":97,"line":134},[95,11218,1731],{"emptyLinePlaceholder":1410},[95,11220,11221,11223,11225],{"class":97,"line":147},[95,11222,11195],{"class":195},[95,11224,10890],{"class":108},[95,11226,353],{"class":101},[95,11228,11229,11231,11233,11235,11237],{"class":97,"line":160},[95,11230,11202],{"class":201},[95,11232,11106],{"class":108},[95,11234,1216],{"class":101},[95,11236,11000],{"class":370},[95,11238,10991],{"class":101},[95,11240,11241],{"class":97,"line":171},[95,11242,174],{"class":101},[11,11244,11245,11246,11248,11249,11251],{},"This sets the light theme as the default. When the ",[20,11247,10890],{}," class is added to ",[20,11250,11195],{},", the dark theme is applied instantly.",[75,11253,11255],{"id":11254},"step-5-using-the-variables","Step 5: Using the variables",[11,11257,11258],{},"In components, we don’t reach for Sass maps or Sass variables anymore. We use CSS variables:",[86,11260,11262],{"className":10197,"code":11261,"language":10199,"meta":91,"style":91},".card {\n  background: var(--surface-bg-color-1);\n  color: var(--text-color-1);\n}\n",[20,11263,11264,11271,11287,11302],{"__ignoreMap":91},[95,11265,11266,11269],{"class":97,"line":98},[95,11267,11268],{"class":108},".card",[95,11270,353],{"class":101},[95,11272,11273,11275,11277,11280,11282,11285],{"class":97,"line":105},[95,11274,10858],{"class":338},[95,11276,112],{"class":101},[95,11278,11279],{"class":338},"var",[95,11281,1216],{"class":101},[95,11283,11284],{"class":370},"--surface-bg-color-1",[95,11286,10991],{"class":101},[95,11288,11289,11291,11293,11295,11297,11300],{"class":97,"line":122},[95,11290,10870],{"class":338},[95,11292,112],{"class":101},[95,11294,11279],{"class":338},[95,11296,1216],{"class":101},[95,11298,11299],{"class":370},"--text-color-1",[95,11301,10991],{"class":101},[95,11303,11304],{"class":97,"line":134},[95,11305,174],{"class":101},[11,11307,11308],{},"That separation is the key: Sass maps store theme values; the mixin converts those maps into CSS variables.",[75,11310,11312],{"id":11311},"why-this-approach-scales","Why this approach scales",[3471,11314,11316],{"id":11315},"_1-single-source-of-truth","1. Single source of truth",[11,11318,11319],{},"All colors live in one place.",[3471,11321,11323],{"id":11322},"_2-easy-theme-switching","2. Easy theme switching",[11,11325,11326],{},"Toggle a class:",[86,11328,11332],{"className":11329,"code":11330,"language":11331,"meta":91,"style":91},"language-js shiki shiki-themes github-light github-dark","document.body.classList.toggle('dark')\n","js",[20,11333,11334],{"__ignoreMap":91},[95,11335,11336,11339,11342,11344,11347],{"class":97,"line":98},[95,11337,11338],{"class":101},"document.body.classList.",[95,11340,11341],{"class":108},"toggle",[95,11343,1216],{"class":101},[95,11345,11346],{"class":115},"'dark'",[95,11348,1222],{"class":101},[11,11350,11351,11352,11355,11356,73],{},"Or use another library for the same idea. In Vue projects, I use the ",[20,11353,11354],{},"useDark()"," composable from ",[61,11357,11360],{"href":11358,"rel":11359},"https:\u002F\u002Fvueuse.org\u002Fcore\u002FuseDark\u002F",[65],"VueUse",[3471,11362,11364],{"id":11363},"_3-zero-style-duplication","3. Zero style duplication",[11,11366,11367,11368,11371],{},"No ",[20,11369,11370],{},".dark .card {}"," overrides needed.",[3471,11373,11375],{"id":11374},"_4-semantic-design-system","4. Semantic design system",[11,11377,11378,11379,11382],{},"We’re not sprinkling random colors like ",[20,11380,11381],{},"#fff",". We’re using names such as:",[639,11384,11385,11390],{},[642,11386,11387],{},[20,11388,11389],{},"text-color-error",[642,11391,11392],{},[20,11393,11394],{},"surface-bg-color-success-light",[11,11396,11397],{},"That keeps the UI consistent, readable, and design-friendly.",[3471,11399,11401],{"id":11400},"_5-future-proof","5. Future-proof",[11,11403,11404,11405,11408],{},"Adding a new theme? Define ",[20,11406,11407],{},"$special-theme: ( ... );"," and plug it into the same mixin.",[75,11410,11411],{"id":3580},"Final thoughts",[11,11413,11414],{},"This pattern shows up in design systems, component libraries, and large-scale frontend apps because it reduces bugs, improves maintainability, and keeps design and code aligned.",[11,11416,11417],{},"What we’ve built here is more than light\u002Fdark mode—it’s a scalable theming architecture. When you’re building modern web apps, this approach isn’t just helpful—it’s essential.",[11,11419,11420],{},"Thanks for reading—I hope you learned something new. See you next time.",[1383,11422,11423],{},"html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .s9eBZ, html code.shiki .s9eBZ{--shiki-default:#22863A;--shiki-dark:#85E89D}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s7hpK, html code.shiki .s7hpK{--shiki-default:#B31D28;--shiki-default-font-style:italic;--shiki-dark:#FDAEB7;--shiki-dark-font-style:italic}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":91,"searchDepth":105,"depth":105,"links":11425},[11426,11427,11428,11429,11430,11431,11432,11433,11440],{"id":10837,"depth":105,"text":10838},{"id":10922,"depth":105,"text":10923},{"id":10943,"depth":105,"text":10944},{"id":11039,"depth":105,"text":11040},{"id":11089,"depth":105,"text":11090},{"id":11181,"depth":105,"text":11182},{"id":11254,"depth":105,"text":11255},{"id":11311,"depth":105,"text":11312,"children":11434},[11435,11436,11437,11438,11439],{"id":11315,"depth":122,"text":11316},{"id":11322,"depth":122,"text":11323},{"id":11363,"depth":122,"text":11364},{"id":11374,"depth":122,"text":11375},{"id":11400,"depth":122,"text":11401},{"id":3580,"depth":105,"text":11411},"2026-04-26","Centralize light and dark theme tokens in Sass maps, generate CSS variables with a mixin, and switch themes by toggling a class—without duplicating component styles.",{"src":11444,"alt":11445},"\u002Fimages\u002Fblog\u002Fstop-writing-duplicate-css-build-scalable-light-and-dark-themes-with-sass-maps-mixins-cover.jpg","Split light and dark UI with Sass theme maps and a theme toggle between modes",{},"\u002Fblog\u002Fstop-writing-duplicate-css-build-scalable-light-and-dark-themes-with-sass-maps-mixins",{"title":10818,"description":11442},"blog\u002Fstop-writing-duplicate-css-build-scalable-light-and-dark-themes-with-sass-maps-mixins",[11451,10813,11452,3681,11453],"Sass","Theming","Vue","4XO_2fGTAf3XDA97_nBhTII4_lTcVD_j79acRmkZMgo",{"id":11456,"title":11457,"body":11458,"date":13631,"description":13632,"draft":1404,"extension":1405,"featured":1410,"image":13633,"meta":13636,"navigation":1410,"path":8339,"seo":13637,"stem":13638,"tags":13639,"updatedAt":5918,"__hash__":13641},"blog\u002Fblog\u002Fbuilding-a-reusable-expand-collapse-transition-component-in-vue-3-with-smooth-height-animation.md","Building a Reusable Expand\u002FCollapse Transition Component in Vue 3 (with Smooth Height Animation)",{"type":8,"value":11459,"toc":13613},[11460,11470,11473,11479,11486,11493,11499,11502,11522,11525,11529,11536,11555,11558,11562,11565,11663,11666,11684,11697,11733,11736,11747,11754,11763,12036,12041,12058,12061,12065,12106,12116,12120,12188,12194,12198,12210,12213,12216,12220,12256,12259,12267,12301,12307,12315,12322,12453,12466,12470,12554,12557,12613,12616,12620,12685,12699,12705,12709,13465,13468,13556,13560,13563,13584,13588,13604,13607,13610],[11,11461,11462,11463,11033,11466,11469],{},"Animating height in the browser is one of those things that sounds simple but quickly turns messy. CSS doesn’t transition well between ",[20,11464,11465],{},"height: 0",[20,11467,11468],{},"height: auto",", which is exactly what you need for expand\u002Fcollapse UI patterns like accordions, dropdowns, or dynamic content blocks.",[11,11471,11472],{},"In this article, we’ll break down a reusable Vue 3 Transition Component that solves this problem cleanly and globally—so you can plug it in anywhere and get smooth expand\u002Fcollapse animations.",[11,11474,59,11475,73],{},[61,11476,66],{"href":11477,"rel":11478},"https:\u002F\u002Fakshattriv.medium.com\u002Fbuilding-a-reusable-expand-collapse-transition-component-in-vue-3-with-smooth-height-animation-d03d41149f51",[65],[75,11480,11482,11483,11485],{"id":11481},"the-problem-why-height-auto-wont-animate","The Problem: Why ",[20,11484,11468],{}," Won’t Animate",[11,11487,11488,11489,11492],{},"CSS transitions require explicit numeric values. But ",[20,11490,11491],{},"auto"," isn’t a number—it’s computed dynamically by the browser. So this won’t work:",[86,11494,11497],{"className":11495,"code":11496,"language":632,"meta":91},[630],"height: 0 → height: auto ❌\n",[20,11498,11496],{"__ignoreMap":91},[11,11500,11501],{},"To animate height properly, we need to:",[10080,11503,11504,11507,11516],{},[642,11505,11506],{},"Measure the element’s actual height",[642,11508,11509,11510,11512,11513],{},"Animate from ",[20,11511,8427],{}," → ",[20,11514,11515],{},"\u003Cmeasured height>",[642,11517,11518,11519,11521],{},"Reset back to ",[20,11520,11491],{}," after animation completes",[11,11523,11524],{},"That’s exactly what this component does.",[75,11526,11528],{"id":11527},"core-idea-of-the-component","Core Idea of the Component",[11,11530,11531,11532,11535],{},"We leverage Vue’s ",[20,11533,11534],{},"\u003Ctransition>"," hooks:",[639,11537,11538,11543,11549],{},[642,11539,11540,11542],{},[20,11541,8402],{}," → when element is inserted",[642,11544,11545,11548],{},[20,11546,11547],{},"afterEnter"," → after expand animation completes",[642,11550,11551,11554],{},[20,11552,11553],{},"leave"," → when element is removed",[11,11556,11557],{},"These hooks give us full control over DOM styles during animation.",[75,11559,11561],{"id":11560},"the-component-code-explained","The Component Code (Explained)",[11,11563,11564],{},"Here’s the template from our component:",[86,11566,11568],{"className":183,"code":11567,"language":185,"meta":91,"style":91},"\u003Ctemplate>\n  \u003Ctransition\n    name=\"expand\"\n    @enter=\"enter($el)\"\n    @after-enter=\"afterEnter($el)\"\n    @leave=\"leave($el)\"\n  >\n    \u003Cslot \u002F>\n  \u003C\u002Ftransition>\n\u003C\u002Ftemplate>\n",[20,11569,11570,11579,11587,11597,11607,11617,11627,11632,11645,11655],{"__ignoreMap":91},[95,11571,11572,11574,11577],{"class":97,"line":98},[95,11573,192],{"class":101},[95,11575,11576],{"class":195},"template",[95,11578,553],{"class":101},[95,11580,11581,11584],{"class":97,"line":105},[95,11582,11583],{"class":101},"  \u003C",[95,11585,11586],{"class":195},"transition\n",[95,11588,11589,11592,11594],{"class":97,"line":122},[95,11590,11591],{"class":108},"    name",[95,11593,205],{"class":101},[95,11595,11596],{"class":115},"\"expand\"\n",[95,11598,11599,11602,11604],{"class":97,"line":134},[95,11600,11601],{"class":108},"    @enter",[95,11603,205],{"class":101},[95,11605,11606],{"class":115},"\"enter($el)\"\n",[95,11608,11609,11612,11614],{"class":97,"line":147},[95,11610,11611],{"class":108},"    @after-enter",[95,11613,205],{"class":101},[95,11615,11616],{"class":115},"\"afterEnter($el)\"\n",[95,11618,11619,11622,11624],{"class":97,"line":160},[95,11620,11621],{"class":108},"    @leave",[95,11623,205],{"class":101},[95,11625,11626],{"class":115},"\"leave($el)\"\n",[95,11628,11629],{"class":97,"line":171},[95,11630,11631],{"class":101},"  >\n",[95,11633,11634,11637,11640,11643],{"class":97,"line":882},[95,11635,11636],{"class":101},"    \u003C",[95,11638,11639],{"class":195},"slot",[95,11641,11642],{"class":11154}," \u002F",[95,11644,553],{"class":101},[95,11646,11647,11650,11653],{"class":97,"line":1788},[95,11648,11649],{"class":101},"  \u003C\u002F",[95,11651,11652],{"class":195},"transition",[95,11654,553],{"class":101},[95,11656,11657,11659,11661],{"class":97,"line":1793},[95,11658,568],{"class":101},[95,11660,11576],{"class":195},[95,11662,553],{"class":101},[11,11664,11665],{},"Let’s break this down.",[11,11667,11668,11669,11675,11676,909,11678,909,11681,11683],{},"Vue provides a built-in ",[61,11670,11673],{"href":11671,"rel":11672},"https:\u002F\u002Fvuejs.org\u002Fguide\u002Fbuilt-ins\u002Ftransition",[65],[20,11674,11534],{}," component that allows you to animate elements when they enter the DOM and leave the DOM. Instead of manually adding\u002Fremoving classes, Vue gives us lifecycle hooks like ",[20,11677,8402],{},[20,11679,11680],{},"after-enter",[20,11682,11553],{},", etc. We are using these hooks to control the animation.",[11,11685,11686,11687,11692,11693,11696],{},"And we are using Vue ",[61,11688,11691],{"href":11689,"rel":11690},"https:\u002F\u002Fvuejs.org\u002Fguide\u002Fcomponents\u002Fslots",[65],"slots"," for making this fully reusable. For anyone who is not familiar with slots, ",[20,11694,11695],{},"\u003Cslot \u002F>"," is one of the most powerful concepts in Vue. It acts like a placeholder for whatever content you pass into this component. Example usage:",[86,11698,11700],{"className":183,"code":11699,"language":185,"meta":91,"style":91},"\u003CTransitionExpand>\n  \u003Cdiv v-if=\"isOpen\">\n    Hello, I will expand!\n  \u003C\u002Fdiv>\n\u003C\u002FTransitionExpand>\n",[20,11701,11702,11710,11715,11720,11725],{"__ignoreMap":91},[95,11703,11704,11706,11708],{"class":97,"line":98},[95,11705,192],{"class":101},[95,11707,8331],{"class":195},[95,11709,553],{"class":101},[95,11711,11712],{"class":97,"line":105},[95,11713,11714],{"class":101},"  \u003Cdiv v-if=\"isOpen\">\n",[95,11716,11717],{"class":97,"line":122},[95,11718,11719],{"class":101},"    Hello, I will expand!\n",[95,11721,11722],{"class":97,"line":134},[95,11723,11724],{"class":101},"  \u003C\u002Fdiv>\n",[95,11726,11727,11729,11731],{"class":97,"line":147},[95,11728,568],{"class":101},[95,11730,8331],{"class":195},[95,11732,553],{"class":101},[11,11734,11735],{},"Now writing the animation logic in script setup. I am using TypeScript, but if you are using JavaScript that’s totally fine—just ignore the types.",[11,11737,11738,11739,909,11741,11743,11744,11746],{},"We need to define three functions: ",[20,11740,8402],{},[20,11742,11547],{},", and ",[20,11745,11553],{},". As their names suggest, these functions define what to do when the element enters the DOM, what changes to perform after it enters, and finally the actions we need to perform once it leaves the DOM.",[3471,11748,11750,11751,11753],{"id":11749},"function-1-enter-when-element-appears","Function 1: ",[20,11752,8507],{}," — When Element Appears",[11,11755,11756,11757,8396,11760,11762],{},"Like I said before, this function runs when the element is inserted into the DOM (i.e., when ",[20,11758,11759],{},"v-if",[20,11761,5180],{},").",[86,11764,11766],{"className":88,"code":11765,"language":90,"meta":91,"style":91},"function enter(element: HTMLElement) {\n  const width = getComputedStyle(element).width\n\n  element.style.width = width\n  element.style.position = 'absolute'\n  element.style.visibility = 'hidden'\n  element.style.height = 'auto'\n\n  const { height, padding } = getComputedStyle(element)\n\n  element.style.removeProperty('position')\n  element.style.removeProperty('visibility')\n  element.style.removeProperty('width')\n  element.style.height = '0'\n  element.style.margin = '0'\n  element.style.padding = '0'\n\n  \u002F\u002F Force repaint to make sure the\n  \u002F\u002F animation is triggered correctly.\n  \u002F\u002F oxlint-disable-next-line no-unused-expressions\n  getComputedStyle(element).height\n\n  \u002F\u002F Trigger the animation.\n  \u002F\u002F We use `requestAnimationFrame` because we need\n  \u002F\u002F to make sure the browser has finished\n  \u002F\u002F painting after setting the `height`\n  \u002F\u002F to `0` in the line above.\n  requestAnimationFrame(() => {\n    element.style.height = height\n    element.style.padding = padding\n  })\n}\n",[20,11767,11768,11784,11799,11803,11813,11823,11833,11843,11847,11869,11873,11888,11901,11914,11923,11932,11941,11945,11950,11955,11960,11968,11972,11977,11982,11987,11992,11997,12008,12018,12028,12032],{"__ignoreMap":91},[95,11769,11770,11772,11774,11776,11778,11780,11782],{"class":97,"line":98},[95,11771,823],{"class":201},[95,11773,8570],{"class":108},[95,11775,1216],{"class":101},[95,11777,8575],{"class":370},[95,11779,1651],{"class":201},[95,11781,8580],{"class":108},[95,11783,2324],{"class":101},[95,11785,11786,11788,11791,11793,11796],{"class":97,"line":105},[95,11787,1715],{"class":201},[95,11789,11790],{"class":338}," width",[95,11792,341],{"class":201},[95,11794,11795],{"class":108}," getComputedStyle",[95,11797,11798],{"class":101},"(element).width\n",[95,11800,11801],{"class":97,"line":122},[95,11802,1731],{"emptyLinePlaceholder":1410},[95,11804,11805,11808,11810],{"class":97,"line":134},[95,11806,11807],{"class":101},"  element.style.width ",[95,11809,205],{"class":201},[95,11811,11812],{"class":101}," width\n",[95,11814,11815,11818,11820],{"class":97,"line":147},[95,11816,11817],{"class":101},"  element.style.position ",[95,11819,205],{"class":201},[95,11821,11822],{"class":115}," 'absolute'\n",[95,11824,11825,11828,11830],{"class":97,"line":160},[95,11826,11827],{"class":101},"  element.style.visibility ",[95,11829,205],{"class":201},[95,11831,11832],{"class":115}," 'hidden'\n",[95,11834,11835,11838,11840],{"class":97,"line":171},[95,11836,11837],{"class":101},"  element.style.height ",[95,11839,205],{"class":201},[95,11841,11842],{"class":115}," 'auto'\n",[95,11844,11845],{"class":97,"line":882},[95,11846,1731],{"emptyLinePlaceholder":1410},[95,11848,11849,11851,11853,11856,11858,11861,11863,11865,11867],{"class":97,"line":1788},[95,11850,1715],{"class":201},[95,11852,3369],{"class":101},[95,11854,11855],{"class":338},"height",[95,11857,909],{"class":101},[95,11859,11860],{"class":338},"padding",[95,11862,3375],{"class":101},[95,11864,205],{"class":201},[95,11866,11795],{"class":108},[95,11868,8603],{"class":101},[95,11870,11871],{"class":97,"line":1793},[95,11872,1731],{"emptyLinePlaceholder":1410},[95,11874,11875,11878,11881,11883,11886],{"class":97,"line":1801},[95,11876,11877],{"class":101},"  element.style.",[95,11879,11880],{"class":108},"removeProperty",[95,11882,1216],{"class":101},[95,11884,11885],{"class":115},"'position'",[95,11887,1222],{"class":101},[95,11889,11890,11892,11894,11896,11899],{"class":97,"line":1820},[95,11891,11877],{"class":101},[95,11893,11880],{"class":108},[95,11895,1216],{"class":101},[95,11897,11898],{"class":115},"'visibility'",[95,11900,1222],{"class":101},[95,11902,11903,11905,11907,11909,11912],{"class":97,"line":1829},[95,11904,11877],{"class":101},[95,11906,11880],{"class":108},[95,11908,1216],{"class":101},[95,11910,11911],{"class":115},"'width'",[95,11913,1222],{"class":101},[95,11915,11916,11918,11920],{"class":97,"line":1851},[95,11917,11837],{"class":101},[95,11919,205],{"class":201},[95,11921,11922],{"class":115}," '0'\n",[95,11924,11925,11928,11930],{"class":97,"line":1858},[95,11926,11927],{"class":101},"  element.style.margin ",[95,11929,205],{"class":201},[95,11931,11922],{"class":115},[95,11933,11934,11937,11939],{"class":97,"line":1863},[95,11935,11936],{"class":101},"  element.style.padding ",[95,11938,205],{"class":201},[95,11940,11922],{"class":115},[95,11942,11943],{"class":97,"line":1868},[95,11944,1731],{"emptyLinePlaceholder":1410},[95,11946,11947],{"class":97,"line":1887},[95,11948,11949],{"class":382},"  \u002F\u002F Force repaint to make sure the\n",[95,11951,11952],{"class":97,"line":1894},[95,11953,11954],{"class":382},"  \u002F\u002F animation is triggered correctly.\n",[95,11956,11957],{"class":97,"line":3201},[95,11958,11959],{"class":382},"  \u002F\u002F oxlint-disable-next-line no-unused-expressions\n",[95,11961,11962,11965],{"class":97,"line":3210},[95,11963,11964],{"class":108},"  getComputedStyle",[95,11966,11967],{"class":101},"(element).height\n",[95,11969,11970],{"class":97,"line":3215},[95,11971,1731],{"emptyLinePlaceholder":1410},[95,11973,11974],{"class":97,"line":3220},[95,11975,11976],{"class":382},"  \u002F\u002F Trigger the animation.\n",[95,11978,11979],{"class":97,"line":3225},[95,11980,11981],{"class":382},"  \u002F\u002F We use `requestAnimationFrame` because we need\n",[95,11983,11984],{"class":97,"line":3237},[95,11985,11986],{"class":382},"  \u002F\u002F to make sure the browser has finished\n",[95,11988,11989],{"class":97,"line":3247},[95,11990,11991],{"class":382},"  \u002F\u002F painting after setting the `height`\n",[95,11993,11994],{"class":97,"line":3255},[95,11995,11996],{"class":382},"  \u002F\u002F to `0` in the line above.\n",[95,11998,11999,12002,12004,12006],{"class":97,"line":3261},[95,12000,12001],{"class":108},"  requestAnimationFrame",[95,12003,347],{"class":101},[95,12005,350],{"class":201},[95,12007,353],{"class":101},[95,12009,12010,12013,12015],{"class":97,"line":3267},[95,12011,12012],{"class":101},"    element.style.height ",[95,12014,205],{"class":201},[95,12016,12017],{"class":101}," height\n",[95,12019,12020,12023,12025],{"class":97,"line":3272},[95,12021,12022],{"class":101},"    element.style.padding ",[95,12024,205],{"class":201},[95,12026,12027],{"class":101}," padding\n",[95,12029,12030],{"class":97,"line":3277},[95,12031,398],{"class":101},[95,12033,12034],{"class":97,"line":3289},[95,12035,174],{"class":101},[12037,12038,12040],"h4",{"id":12039},"capture-the-width","Capture the width",[86,12042,12044],{"className":88,"code":12043,"language":90,"meta":91,"style":91},"const width = getComputedStyle(element).width\n",[20,12045,12046],{"__ignoreMap":91},[95,12047,12048,12050,12052,12054,12056],{"class":97,"line":98},[95,12049,335],{"class":201},[95,12051,11790],{"class":338},[95,12053,341],{"class":201},[95,12055,11795],{"class":108},[95,12057,11798],{"class":101},[11,12059,12060],{},"We store the width because when we temporarily change styles, layout might break—and fixing width ensures stable measurement.",[12037,12062,12064],{"id":12063},"prepare-for-measurement","Prepare for measurement",[86,12066,12068],{"className":88,"code":12067,"language":90,"meta":91,"style":91},"element.style.width = width\nelement.style.position = 'absolute'\nelement.style.visibility = 'hidden'\nelement.style.height = 'auto'\n",[20,12069,12070,12079,12088,12097],{"__ignoreMap":91},[95,12071,12072,12075,12077],{"class":97,"line":98},[95,12073,12074],{"class":101},"element.style.width ",[95,12076,205],{"class":201},[95,12078,11812],{"class":101},[95,12080,12081,12084,12086],{"class":97,"line":105},[95,12082,12083],{"class":101},"element.style.position ",[95,12085,205],{"class":201},[95,12087,11822],{"class":115},[95,12089,12090,12093,12095],{"class":97,"line":122},[95,12091,12092],{"class":101},"element.style.visibility ",[95,12094,205],{"class":201},[95,12096,11832],{"class":115},[95,12098,12099,12102,12104],{"class":97,"line":134},[95,12100,12101],{"class":101},"element.style.height ",[95,12103,205],{"class":201},[95,12105,11842],{"class":115},[11,12107,12108,12109,12111,12112,12115],{},"We make the element absolute so it gets removed from normal layout, set visibility to hidden so the user doesn’t see the element, and set height to ",[20,12110,11491],{}," so the browser calculates the height for us. In the next line, ",[20,12113,12114],{},"const { height, padding } = getComputedStyle(element)"," we capture the height.",[12037,12117,12119],{"id":12118},"reset-styles-to-start-animation","Reset styles to start animation",[86,12121,12123],{"className":88,"code":12122,"language":90,"meta":91,"style":91},"element.style.removeProperty('position')\nelement.style.removeProperty('visibility')\nelement.style.removeProperty('width')\nelement.style.height = '0'\nelement.style.margin = '0'\nelement.style.padding = '0'\n",[20,12124,12125,12138,12150,12162,12170,12179],{"__ignoreMap":91},[95,12126,12127,12130,12132,12134,12136],{"class":97,"line":98},[95,12128,12129],{"class":101},"element.style.",[95,12131,11880],{"class":108},[95,12133,1216],{"class":101},[95,12135,11885],{"class":115},[95,12137,1222],{"class":101},[95,12139,12140,12142,12144,12146,12148],{"class":97,"line":105},[95,12141,12129],{"class":101},[95,12143,11880],{"class":108},[95,12145,1216],{"class":101},[95,12147,11898],{"class":115},[95,12149,1222],{"class":101},[95,12151,12152,12154,12156,12158,12160],{"class":97,"line":122},[95,12153,12129],{"class":101},[95,12155,11880],{"class":108},[95,12157,1216],{"class":101},[95,12159,11911],{"class":115},[95,12161,1222],{"class":101},[95,12163,12164,12166,12168],{"class":97,"line":134},[95,12165,12101],{"class":101},[95,12167,205],{"class":201},[95,12169,11922],{"class":115},[95,12171,12172,12175,12177],{"class":97,"line":147},[95,12173,12174],{"class":101},"element.style.margin ",[95,12176,205],{"class":201},[95,12178,11922],{"class":115},[95,12180,12181,12184,12186],{"class":97,"line":160},[95,12182,12183],{"class":101},"element.style.padding ",[95,12185,205],{"class":201},[95,12187,11922],{"class":115},[11,12189,12190,12191,12193],{},"This brings the element back to normal flow, collapses it to ",[20,12192,11465],{},", and removes the spacing. This becomes our starting point of the animation.",[12037,12195,12197],{"id":12196},"force-reflow","Force reflow",[86,12199,12201],{"className":88,"code":12200,"language":90,"meta":91,"style":91},"getComputedStyle(element).height\n",[20,12202,12203],{"__ignoreMap":91},[95,12204,12205,12208],{"class":97,"line":98},[95,12206,12207],{"class":108},"getComputedStyle",[95,12209,11967],{"class":101},[11,12211,12212],{},"This line looks useless—but it’s critical.",[11,12214,12215],{},"It forces the browser to apply all style changes immediately. Without this, the browser might skip the animation and everything could jump directly to the final state.",[12037,12217,12219],{"id":12218},"animate-to-full-height","Animate to full height",[86,12221,12223],{"className":88,"code":12222,"language":90,"meta":91,"style":91},"requestAnimationFrame(() => {\n  element.style.height = height\n  element.style.padding = padding\n})\n",[20,12224,12225,12236,12244,12252],{"__ignoreMap":91},[95,12226,12227,12230,12232,12234],{"class":97,"line":98},[95,12228,12229],{"class":108},"requestAnimationFrame",[95,12231,347],{"class":101},[95,12233,350],{"class":201},[95,12235,353],{"class":101},[95,12237,12238,12240,12242],{"class":97,"line":105},[95,12239,11837],{"class":101},[95,12241,205],{"class":201},[95,12243,12017],{"class":101},[95,12245,12246,12248,12250],{"class":97,"line":122},[95,12247,11936],{"class":101},[95,12249,205],{"class":201},[95,12251,12027],{"class":101},[95,12253,12254],{"class":97,"line":134},[95,12255,403],{"class":101},[11,12257,12258],{},"We wait for the next frame, then set the height to the actual value and restore padding.",[3471,12260,12262,12263,12266],{"id":12261},"function-2-afterenter-cleanup-after-animation","Function 2: ",[20,12264,12265],{},"afterEnter()"," — Cleanup After Animation",[86,12268,12270],{"className":88,"code":12269,"language":90,"meta":91,"style":91},"function afterEnter(element: HTMLElement) {\n  element.style.height = 'auto'\n}\n",[20,12271,12272,12289,12297],{"__ignoreMap":91},[95,12273,12274,12276,12279,12281,12283,12285,12287],{"class":97,"line":98},[95,12275,823],{"class":201},[95,12277,12278],{"class":108}," afterEnter",[95,12280,1216],{"class":101},[95,12282,8575],{"class":370},[95,12284,1651],{"class":201},[95,12286,8580],{"class":108},[95,12288,2324],{"class":101},[95,12290,12291,12293,12295],{"class":97,"line":105},[95,12292,11837],{"class":101},[95,12294,205],{"class":201},[95,12296,11842],{"class":115},[95,12298,12299],{"class":97,"line":122},[95,12300,174],{"class":101},[11,12302,12303,12304,12306],{},"Once animation finishes, we reset height to ",[20,12305,11491],{},". The reason we do this is that future content changes should adjust normally—a fixed height would break responsiveness.",[3471,12308,12310,12311,12314],{"id":12309},"function-3-leave-when-element-disappears","Function 3: ",[20,12312,12313],{},"leave()"," — When Element Disappears",[11,12316,12317,12318,8396,12320,73],{},"This runs when ",[20,12319,11759],{},[20,12321,3052],{},[86,12323,12325],{"className":88,"code":12324,"language":90,"meta":91,"style":91},"function leave(element: HTMLElement) {\n  const { height, padding } = getComputedStyle(element)\n\n  element.style.height = height\n  element.style.padding = padding\n\n  \u002F\u002F Force repaint to make sure the\n  \u002F\u002F animation is triggered correctly.\n  \u002F\u002F oxlint-disable-next-line no-unused-expressions\n  getComputedStyle(element).height\n\n  requestAnimationFrame(() => {\n    element.style.height = '0'\n    element.style.margin = '0'\n    element.style.padding = '0'\n  })\n}\n",[20,12326,12327,12344,12364,12368,12376,12384,12388,12392,12396,12400,12406,12410,12420,12428,12437,12445,12449],{"__ignoreMap":91},[95,12328,12329,12331,12334,12336,12338,12340,12342],{"class":97,"line":98},[95,12330,823],{"class":201},[95,12332,12333],{"class":108}," leave",[95,12335,1216],{"class":101},[95,12337,8575],{"class":370},[95,12339,1651],{"class":201},[95,12341,8580],{"class":108},[95,12343,2324],{"class":101},[95,12345,12346,12348,12350,12352,12354,12356,12358,12360,12362],{"class":97,"line":105},[95,12347,1715],{"class":201},[95,12349,3369],{"class":101},[95,12351,11855],{"class":338},[95,12353,909],{"class":101},[95,12355,11860],{"class":338},[95,12357,3375],{"class":101},[95,12359,205],{"class":201},[95,12361,11795],{"class":108},[95,12363,8603],{"class":101},[95,12365,12366],{"class":97,"line":122},[95,12367,1731],{"emptyLinePlaceholder":1410},[95,12369,12370,12372,12374],{"class":97,"line":134},[95,12371,11837],{"class":101},[95,12373,205],{"class":201},[95,12375,12017],{"class":101},[95,12377,12378,12380,12382],{"class":97,"line":147},[95,12379,11936],{"class":101},[95,12381,205],{"class":201},[95,12383,12027],{"class":101},[95,12385,12386],{"class":97,"line":160},[95,12387,1731],{"emptyLinePlaceholder":1410},[95,12389,12390],{"class":97,"line":171},[95,12391,11949],{"class":382},[95,12393,12394],{"class":97,"line":882},[95,12395,11954],{"class":382},[95,12397,12398],{"class":97,"line":1788},[95,12399,11959],{"class":382},[95,12401,12402,12404],{"class":97,"line":1793},[95,12403,11964],{"class":108},[95,12405,11967],{"class":101},[95,12407,12408],{"class":97,"line":1801},[95,12409,1731],{"emptyLinePlaceholder":1410},[95,12411,12412,12414,12416,12418],{"class":97,"line":1820},[95,12413,12001],{"class":108},[95,12415,347],{"class":101},[95,12417,350],{"class":201},[95,12419,353],{"class":101},[95,12421,12422,12424,12426],{"class":97,"line":1829},[95,12423,12012],{"class":101},[95,12425,205],{"class":201},[95,12427,11922],{"class":115},[95,12429,12430,12433,12435],{"class":97,"line":1851},[95,12431,12432],{"class":101},"    element.style.margin ",[95,12434,205],{"class":201},[95,12436,11922],{"class":115},[95,12438,12439,12441,12443],{"class":97,"line":1858},[95,12440,12022],{"class":101},[95,12442,205],{"class":201},[95,12444,11922],{"class":115},[95,12446,12447],{"class":97,"line":1863},[95,12448,398],{"class":101},[95,12450,12451],{"class":97,"line":1868},[95,12452,174],{"class":101},[11,12454,12455,12456,12458,12459,12462,12463,12465],{},"This is very similar to the ",[20,12457,8402],{}," function but just reversed: we capture the current state, explicitly set the height and padding so animation starts from the exact visible state, force a reflow again with ",[20,12460,12461],{},"getComputedStyle(element).height"," (same reason as before), and finally animate the collapse in the next frame by setting everything to ",[20,12464,3069],{},". This creates the smooth collapse effect.",[75,12467,12469],{"id":12468},"css-that-makes-it-smooth","CSS That Makes It Smooth",[86,12471,12473],{"className":10950,"code":12472,"language":10952,"meta":91,"style":91},".expand-enter-active,\n.expand-leave-active {\n  transition:\n    height 0.3s ease-in-out,\n    opacity 0.3s ease-in-out,\n    margin 0.3s ease-in-out;\n  overflow: hidden;\n}\n",[20,12474,12475,12482,12489,12496,12512,12525,12538,12550],{"__ignoreMap":91},[95,12476,12477,12480],{"class":97,"line":98},[95,12478,12479],{"class":108},".expand-enter-active",[95,12481,119],{"class":101},[95,12483,12484,12487],{"class":97,"line":105},[95,12485,12486],{"class":108},".expand-leave-active",[95,12488,353],{"class":101},[95,12490,12491,12494],{"class":97,"line":122},[95,12492,12493],{"class":338},"  transition",[95,12495,4477],{"class":101},[95,12497,12498,12501,12504,12507,12510],{"class":97,"line":134},[95,12499,12500],{"class":338},"    height",[95,12502,12503],{"class":338}," 0.3",[95,12505,12506],{"class":201},"s",[95,12508,12509],{"class":338}," ease-in-out",[95,12511,119],{"class":101},[95,12513,12514,12517,12519,12521,12523],{"class":97,"line":147},[95,12515,12516],{"class":338},"    opacity",[95,12518,12503],{"class":338},[95,12520,12506],{"class":201},[95,12522,12509],{"class":338},[95,12524,119],{"class":101},[95,12526,12527,12530,12532,12534,12536],{"class":97,"line":160},[95,12528,12529],{"class":338},"    margin",[95,12531,12503],{"class":338},[95,12533,12506],{"class":201},[95,12535,12509],{"class":338},[95,12537,10223],{"class":101},[95,12539,12540,12543,12545,12548],{"class":97,"line":171},[95,12541,12542],{"class":338},"  overflow",[95,12544,112],{"class":101},[95,12546,12547],{"class":338},"hidden",[95,12549,10223],{"class":101},[95,12551,12552],{"class":97,"line":882},[95,12553,174],{"class":101},[11,12555,12556],{},"This defines what properties to animate, the duration, and easing.",[86,12558,12560],{"className":10950,"code":12559,"language":10952,"meta":91,"style":91},".expand-enter,\n.expand-leave-to {\n  height: 0;\n  opacity: 0;\n  margin: 0;\n}\n",[20,12561,12562,12569,12576,12587,12598,12609],{"__ignoreMap":91},[95,12563,12564,12567],{"class":97,"line":98},[95,12565,12566],{"class":108},".expand-enter",[95,12568,119],{"class":101},[95,12570,12571,12574],{"class":97,"line":105},[95,12572,12573],{"class":108},".expand-leave-to",[95,12575,353],{"class":101},[95,12577,12578,12581,12583,12585],{"class":97,"line":122},[95,12579,12580],{"class":338},"  height",[95,12582,112],{"class":101},[95,12584,3069],{"class":338},[95,12586,10223],{"class":101},[95,12588,12589,12592,12594,12596],{"class":97,"line":134},[95,12590,12591],{"class":338},"  opacity",[95,12593,112],{"class":101},[95,12595,3069],{"class":338},[95,12597,10223],{"class":101},[95,12599,12600,12603,12605,12607],{"class":97,"line":147},[95,12601,12602],{"class":338},"  margin",[95,12604,112],{"class":101},[95,12606,3069],{"class":338},[95,12608,10223],{"class":101},[95,12610,12611],{"class":97,"line":160},[95,12612,174],{"class":101},[11,12614,12615],{},"This defines the initial and final collapse state.",[75,12617,12619],{"id":12618},"performance-optimisations","Performance Optimisations",[86,12621,12623],{"className":183,"code":12622,"language":185,"meta":91,"style":91},"\u003Cstyle lang=\"scss\" scoped>\n* {\n  will-change: height;\n  backface-visibility: hidden;\n}\n\u003C\u002Fstyle>\n",[20,12624,12625,12644,12651,12662,12673,12677],{"__ignoreMap":91},[95,12626,12627,12629,12631,12634,12636,12639,12642],{"class":97,"line":98},[95,12628,192],{"class":101},[95,12630,1383],{"class":195},[95,12632,12633],{"class":108}," lang",[95,12635,205],{"class":101},[95,12637,12638],{"class":115},"\"scss\"",[95,12640,12641],{"class":108}," scoped",[95,12643,553],{"class":101},[95,12645,12646,12649],{"class":97,"line":105},[95,12647,12648],{"class":195},"*",[95,12650,353],{"class":101},[95,12652,12653,12656,12658,12660],{"class":97,"line":122},[95,12654,12655],{"class":338},"  will-change",[95,12657,112],{"class":101},[95,12659,11855],{"class":338},[95,12661,10223],{"class":101},[95,12663,12664,12667,12669,12671],{"class":97,"line":134},[95,12665,12666],{"class":338},"  backface-visibility",[95,12668,112],{"class":101},[95,12670,12547],{"class":338},[95,12672,10223],{"class":101},[95,12674,12675],{"class":97,"line":147},[95,12676,174],{"class":101},[95,12678,12679,12681,12683],{"class":97,"line":160},[95,12680,568],{"class":101},[95,12682,1383],{"class":195},[95,12684,553],{"class":101},[639,12686,12687,12693],{},[642,12688,12689,12692],{},[20,12690,12691],{},"will-change"," → tells the browser to prepare for animation",[642,12694,12695,12698],{},[20,12696,12697],{},"backface-visibility"," → reduces rendering glitches",[11,12700,12701,12702,12704],{},"We have marked it scoped and used ",[20,12703,12648],{}," to apply this to everything in the component.",[75,12706,12708],{"id":12707},"final-code-usage-example","Final Code & Usage Example",[86,12710,12712],{"className":183,"code":12711,"language":185,"meta":91,"style":91},"\u003Cscript lang=\"ts\" setup>\nfunction enter(element: HTMLElement) {\n  const width = getComputedStyle(element).width\n\n  element.style.width = width\n  element.style.position = 'absolute'\n  element.style.visibility = 'hidden'\n  element.style.height = 'auto'\n\n  const { height, padding } = getComputedStyle(element)\n\n  element.style.removeProperty('position')\n  element.style.removeProperty('visibility')\n  element.style.removeProperty('width')\n  element.style.height = '0'\n  element.style.margin = '0'\n  element.style.padding = '0'\n\n  \u002F\u002F Force repaint to make sure the\n  \u002F\u002F animation is triggered correctly.\n  \u002F\u002F oxlint-disable-next-line no-unused-expressions\n  getComputedStyle(element).height\n\n  \u002F\u002F Trigger the animation.\n  \u002F\u002F We use `requestAnimationFrame` because we need\n  \u002F\u002F to make sure the browser has finished\n  \u002F\u002F painting after setting the `height`\n  \u002F\u002F to `0` in the line above.\n  requestAnimationFrame(() => {\n    element.style.height = height\n    element.style.padding = padding\n  })\n}\n\nfunction afterEnter(element: HTMLElement) {\n  element.style.height = 'auto'\n}\n\nfunction leave(element: HTMLElement) {\n  const { height, padding } = getComputedStyle(element)\n\n  element.style.height = height\n  element.style.padding = padding\n\n  \u002F\u002F Force repaint to make sure the\n  \u002F\u002F animation is triggered correctly.\n  \u002F\u002F oxlint-disable-next-line no-unused-expressions\n  getComputedStyle(element).height\n\n  requestAnimationFrame(() => {\n    element.style.height = '0'\n    element.style.margin = '0'\n    element.style.padding = '0'\n  })\n}\n\u003C\u002Fscript>\n\n\u003Ctemplate>\n  \u003Ctransition\n    name=\"expand\"\n    @enter=\"enter($el)\"\n    @after-enter=\"afterEnter($el)\"\n    @leave=\"leave($el)\"\n  >\n    \u003Cslot \u002F>\n  \u003C\u002Ftransition>\n\u003C\u002Ftemplate>\n\n\u003Cstyle lang=\"scss\">\n.expand-enter-active,\n.expand-leave-active {\n  transition:\n    height 0.3s ease-in-out,\n    opacity 0.3s ease-in-out,\n    margin 0.3s ease-in-out;\n  overflow: hidden;\n}\n\n.expand-enter,\n.expand-leave-to {\n  height: 0;\n  opacity: 0;\n  margin: 0;\n}\n\u003C\u002Fstyle>\n\n\u003Cstyle lang=\"scss\" scoped>\n* {\n  will-change: height;\n  backface-visibility: hidden;\n}\n\u003C\u002Fstyle>\n",[20,12713,12714,12733,12749,12761,12765,12773,12781,12789,12797,12801,12821,12825,12837,12849,12861,12869,12877,12885,12889,12893,12897,12901,12907,12911,12915,12919,12923,12927,12931,12941,12949,12957,12961,12965,12969,12985,12993,12997,13001,13017,13037,13041,13049,13058,13063,13068,13073,13078,13085,13090,13101,13110,13119,13128,13133,13138,13147,13152,13161,13168,13177,13186,13195,13204,13209,13220,13229,13238,13243,13258,13265,13272,13279,13292,13305,13318,13329,13334,13339,13346,13353,13364,13375,13386,13391,13400,13405,13422,13429,13440,13451,13456],{"__ignoreMap":91},[95,12715,12716,12718,12721,12723,12725,12728,12731],{"class":97,"line":98},[95,12717,192],{"class":101},[95,12719,12720],{"class":195},"script",[95,12722,12633],{"class":108},[95,12724,205],{"class":101},[95,12726,12727],{"class":115},"\"ts\"",[95,12729,12730],{"class":108}," setup",[95,12732,553],{"class":101},[95,12734,12735,12737,12739,12741,12743,12745,12747],{"class":97,"line":105},[95,12736,823],{"class":201},[95,12738,8570],{"class":108},[95,12740,1216],{"class":101},[95,12742,8575],{"class":370},[95,12744,1651],{"class":201},[95,12746,8580],{"class":108},[95,12748,2324],{"class":101},[95,12750,12751,12753,12755,12757,12759],{"class":97,"line":122},[95,12752,1715],{"class":201},[95,12754,11790],{"class":338},[95,12756,341],{"class":201},[95,12758,11795],{"class":108},[95,12760,11798],{"class":101},[95,12762,12763],{"class":97,"line":134},[95,12764,1731],{"emptyLinePlaceholder":1410},[95,12766,12767,12769,12771],{"class":97,"line":147},[95,12768,11807],{"class":101},[95,12770,205],{"class":201},[95,12772,11812],{"class":101},[95,12774,12775,12777,12779],{"class":97,"line":160},[95,12776,11817],{"class":101},[95,12778,205],{"class":201},[95,12780,11822],{"class":115},[95,12782,12783,12785,12787],{"class":97,"line":171},[95,12784,11827],{"class":101},[95,12786,205],{"class":201},[95,12788,11832],{"class":115},[95,12790,12791,12793,12795],{"class":97,"line":882},[95,12792,11837],{"class":101},[95,12794,205],{"class":201},[95,12796,11842],{"class":115},[95,12798,12799],{"class":97,"line":1788},[95,12800,1731],{"emptyLinePlaceholder":1410},[95,12802,12803,12805,12807,12809,12811,12813,12815,12817,12819],{"class":97,"line":1793},[95,12804,1715],{"class":201},[95,12806,3369],{"class":101},[95,12808,11855],{"class":338},[95,12810,909],{"class":101},[95,12812,11860],{"class":338},[95,12814,3375],{"class":101},[95,12816,205],{"class":201},[95,12818,11795],{"class":108},[95,12820,8603],{"class":101},[95,12822,12823],{"class":97,"line":1801},[95,12824,1731],{"emptyLinePlaceholder":1410},[95,12826,12827,12829,12831,12833,12835],{"class":97,"line":1820},[95,12828,11877],{"class":101},[95,12830,11880],{"class":108},[95,12832,1216],{"class":101},[95,12834,11885],{"class":115},[95,12836,1222],{"class":101},[95,12838,12839,12841,12843,12845,12847],{"class":97,"line":1829},[95,12840,11877],{"class":101},[95,12842,11880],{"class":108},[95,12844,1216],{"class":101},[95,12846,11898],{"class":115},[95,12848,1222],{"class":101},[95,12850,12851,12853,12855,12857,12859],{"class":97,"line":1851},[95,12852,11877],{"class":101},[95,12854,11880],{"class":108},[95,12856,1216],{"class":101},[95,12858,11911],{"class":115},[95,12860,1222],{"class":101},[95,12862,12863,12865,12867],{"class":97,"line":1858},[95,12864,11837],{"class":101},[95,12866,205],{"class":201},[95,12868,11922],{"class":115},[95,12870,12871,12873,12875],{"class":97,"line":1863},[95,12872,11927],{"class":101},[95,12874,205],{"class":201},[95,12876,11922],{"class":115},[95,12878,12879,12881,12883],{"class":97,"line":1868},[95,12880,11936],{"class":101},[95,12882,205],{"class":201},[95,12884,11922],{"class":115},[95,12886,12887],{"class":97,"line":1887},[95,12888,1731],{"emptyLinePlaceholder":1410},[95,12890,12891],{"class":97,"line":1894},[95,12892,11949],{"class":382},[95,12894,12895],{"class":97,"line":3201},[95,12896,11954],{"class":382},[95,12898,12899],{"class":97,"line":3210},[95,12900,11959],{"class":382},[95,12902,12903,12905],{"class":97,"line":3215},[95,12904,11964],{"class":108},[95,12906,11967],{"class":101},[95,12908,12909],{"class":97,"line":3220},[95,12910,1731],{"emptyLinePlaceholder":1410},[95,12912,12913],{"class":97,"line":3225},[95,12914,11976],{"class":382},[95,12916,12917],{"class":97,"line":3237},[95,12918,11981],{"class":382},[95,12920,12921],{"class":97,"line":3247},[95,12922,11986],{"class":382},[95,12924,12925],{"class":97,"line":3255},[95,12926,11991],{"class":382},[95,12928,12929],{"class":97,"line":3261},[95,12930,11996],{"class":382},[95,12932,12933,12935,12937,12939],{"class":97,"line":3267},[95,12934,12001],{"class":108},[95,12936,347],{"class":101},[95,12938,350],{"class":201},[95,12940,353],{"class":101},[95,12942,12943,12945,12947],{"class":97,"line":3272},[95,12944,12012],{"class":101},[95,12946,205],{"class":201},[95,12948,12017],{"class":101},[95,12950,12951,12953,12955],{"class":97,"line":3277},[95,12952,12022],{"class":101},[95,12954,205],{"class":201},[95,12956,12027],{"class":101},[95,12958,12959],{"class":97,"line":3289},[95,12960,398],{"class":101},[95,12962,12963],{"class":97,"line":3299},[95,12964,174],{"class":101},[95,12966,12967],{"class":97,"line":3306},[95,12968,1731],{"emptyLinePlaceholder":1410},[95,12970,12971,12973,12975,12977,12979,12981,12983],{"class":97,"line":3311},[95,12972,823],{"class":201},[95,12974,12278],{"class":108},[95,12976,1216],{"class":101},[95,12978,8575],{"class":370},[95,12980,1651],{"class":201},[95,12982,8580],{"class":108},[95,12984,2324],{"class":101},[95,12986,12987,12989,12991],{"class":97,"line":3316},[95,12988,11837],{"class":101},[95,12990,205],{"class":201},[95,12992,11842],{"class":115},[95,12994,12995],{"class":97,"line":3321},[95,12996,174],{"class":101},[95,12998,12999],{"class":97,"line":3326},[95,13000,1731],{"emptyLinePlaceholder":1410},[95,13002,13003,13005,13007,13009,13011,13013,13015],{"class":97,"line":3333},[95,13004,823],{"class":201},[95,13006,12333],{"class":108},[95,13008,1216],{"class":101},[95,13010,8575],{"class":370},[95,13012,1651],{"class":201},[95,13014,8580],{"class":108},[95,13016,2324],{"class":101},[95,13018,13019,13021,13023,13025,13027,13029,13031,13033,13035],{"class":97,"line":3339},[95,13020,1715],{"class":201},[95,13022,3369],{"class":101},[95,13024,11855],{"class":338},[95,13026,909],{"class":101},[95,13028,11860],{"class":338},[95,13030,3375],{"class":101},[95,13032,205],{"class":201},[95,13034,11795],{"class":108},[95,13036,8603],{"class":101},[95,13038,13039],{"class":97,"line":3345},[95,13040,1731],{"emptyLinePlaceholder":1410},[95,13042,13043,13045,13047],{"class":97,"line":3350},[95,13044,11837],{"class":101},[95,13046,205],{"class":201},[95,13048,12017],{"class":101},[95,13050,13052,13054,13056],{"class":97,"line":13051},43,[95,13053,11936],{"class":101},[95,13055,205],{"class":201},[95,13057,12027],{"class":101},[95,13059,13061],{"class":97,"line":13060},44,[95,13062,1731],{"emptyLinePlaceholder":1410},[95,13064,13066],{"class":97,"line":13065},45,[95,13067,11949],{"class":382},[95,13069,13071],{"class":97,"line":13070},46,[95,13072,11954],{"class":382},[95,13074,13076],{"class":97,"line":13075},47,[95,13077,11959],{"class":382},[95,13079,13081,13083],{"class":97,"line":13080},48,[95,13082,11964],{"class":108},[95,13084,11967],{"class":101},[95,13086,13088],{"class":97,"line":13087},49,[95,13089,1731],{"emptyLinePlaceholder":1410},[95,13091,13093,13095,13097,13099],{"class":97,"line":13092},50,[95,13094,12001],{"class":108},[95,13096,347],{"class":101},[95,13098,350],{"class":201},[95,13100,353],{"class":101},[95,13102,13104,13106,13108],{"class":97,"line":13103},51,[95,13105,12012],{"class":101},[95,13107,205],{"class":201},[95,13109,11922],{"class":115},[95,13111,13113,13115,13117],{"class":97,"line":13112},52,[95,13114,12432],{"class":101},[95,13116,205],{"class":201},[95,13118,11922],{"class":115},[95,13120,13122,13124,13126],{"class":97,"line":13121},53,[95,13123,12022],{"class":101},[95,13125,205],{"class":201},[95,13127,11922],{"class":115},[95,13129,13131],{"class":97,"line":13130},54,[95,13132,398],{"class":101},[95,13134,13136],{"class":97,"line":13135},55,[95,13137,174],{"class":101},[95,13139,13141,13143,13145],{"class":97,"line":13140},56,[95,13142,568],{"class":101},[95,13144,12720],{"class":195},[95,13146,553],{"class":101},[95,13148,13150],{"class":97,"line":13149},57,[95,13151,1731],{"emptyLinePlaceholder":1410},[95,13153,13155,13157,13159],{"class":97,"line":13154},58,[95,13156,192],{"class":101},[95,13158,11576],{"class":195},[95,13160,553],{"class":101},[95,13162,13164,13166],{"class":97,"line":13163},59,[95,13165,11583],{"class":101},[95,13167,11586],{"class":195},[95,13169,13171,13173,13175],{"class":97,"line":13170},60,[95,13172,11591],{"class":108},[95,13174,205],{"class":101},[95,13176,11596],{"class":115},[95,13178,13180,13182,13184],{"class":97,"line":13179},61,[95,13181,11601],{"class":108},[95,13183,205],{"class":101},[95,13185,11606],{"class":115},[95,13187,13189,13191,13193],{"class":97,"line":13188},62,[95,13190,11611],{"class":108},[95,13192,205],{"class":101},[95,13194,11616],{"class":115},[95,13196,13198,13200,13202],{"class":97,"line":13197},63,[95,13199,11621],{"class":108},[95,13201,205],{"class":101},[95,13203,11626],{"class":115},[95,13205,13207],{"class":97,"line":13206},64,[95,13208,11631],{"class":101},[95,13210,13212,13214,13216,13218],{"class":97,"line":13211},65,[95,13213,11636],{"class":101},[95,13215,11639],{"class":195},[95,13217,11642],{"class":11154},[95,13219,553],{"class":101},[95,13221,13223,13225,13227],{"class":97,"line":13222},66,[95,13224,11649],{"class":101},[95,13226,11652],{"class":195},[95,13228,553],{"class":101},[95,13230,13232,13234,13236],{"class":97,"line":13231},67,[95,13233,568],{"class":101},[95,13235,11576],{"class":195},[95,13237,553],{"class":101},[95,13239,13241],{"class":97,"line":13240},68,[95,13242,1731],{"emptyLinePlaceholder":1410},[95,13244,13246,13248,13250,13252,13254,13256],{"class":97,"line":13245},69,[95,13247,192],{"class":101},[95,13249,1383],{"class":195},[95,13251,12633],{"class":108},[95,13253,205],{"class":101},[95,13255,12638],{"class":115},[95,13257,553],{"class":101},[95,13259,13261,13263],{"class":97,"line":13260},70,[95,13262,12479],{"class":108},[95,13264,119],{"class":101},[95,13266,13268,13270],{"class":97,"line":13267},71,[95,13269,12486],{"class":108},[95,13271,353],{"class":101},[95,13273,13275,13277],{"class":97,"line":13274},72,[95,13276,12493],{"class":338},[95,13278,4477],{"class":101},[95,13280,13282,13284,13286,13288,13290],{"class":97,"line":13281},73,[95,13283,12500],{"class":338},[95,13285,12503],{"class":338},[95,13287,12506],{"class":201},[95,13289,12509],{"class":338},[95,13291,119],{"class":101},[95,13293,13295,13297,13299,13301,13303],{"class":97,"line":13294},74,[95,13296,12516],{"class":338},[95,13298,12503],{"class":338},[95,13300,12506],{"class":201},[95,13302,12509],{"class":338},[95,13304,119],{"class":101},[95,13306,13308,13310,13312,13314,13316],{"class":97,"line":13307},75,[95,13309,12529],{"class":338},[95,13311,12503],{"class":338},[95,13313,12506],{"class":201},[95,13315,12509],{"class":338},[95,13317,10223],{"class":101},[95,13319,13321,13323,13325,13327],{"class":97,"line":13320},76,[95,13322,12542],{"class":338},[95,13324,112],{"class":101},[95,13326,12547],{"class":338},[95,13328,10223],{"class":101},[95,13330,13332],{"class":97,"line":13331},77,[95,13333,174],{"class":101},[95,13335,13337],{"class":97,"line":13336},78,[95,13338,1731],{"emptyLinePlaceholder":1410},[95,13340,13342,13344],{"class":97,"line":13341},79,[95,13343,12566],{"class":108},[95,13345,119],{"class":101},[95,13347,13349,13351],{"class":97,"line":13348},80,[95,13350,12573],{"class":108},[95,13352,353],{"class":101},[95,13354,13356,13358,13360,13362],{"class":97,"line":13355},81,[95,13357,12580],{"class":338},[95,13359,112],{"class":101},[95,13361,3069],{"class":338},[95,13363,10223],{"class":101},[95,13365,13367,13369,13371,13373],{"class":97,"line":13366},82,[95,13368,12591],{"class":338},[95,13370,112],{"class":101},[95,13372,3069],{"class":338},[95,13374,10223],{"class":101},[95,13376,13378,13380,13382,13384],{"class":97,"line":13377},83,[95,13379,12602],{"class":338},[95,13381,112],{"class":101},[95,13383,3069],{"class":338},[95,13385,10223],{"class":101},[95,13387,13389],{"class":97,"line":13388},84,[95,13390,174],{"class":101},[95,13392,13394,13396,13398],{"class":97,"line":13393},85,[95,13395,568],{"class":101},[95,13397,1383],{"class":195},[95,13399,553],{"class":101},[95,13401,13403],{"class":97,"line":13402},86,[95,13404,1731],{"emptyLinePlaceholder":1410},[95,13406,13408,13410,13412,13414,13416,13418,13420],{"class":97,"line":13407},87,[95,13409,192],{"class":101},[95,13411,1383],{"class":195},[95,13413,12633],{"class":108},[95,13415,205],{"class":101},[95,13417,12638],{"class":115},[95,13419,12641],{"class":108},[95,13421,553],{"class":101},[95,13423,13425,13427],{"class":97,"line":13424},88,[95,13426,12648],{"class":195},[95,13428,353],{"class":101},[95,13430,13432,13434,13436,13438],{"class":97,"line":13431},89,[95,13433,12655],{"class":338},[95,13435,112],{"class":101},[95,13437,11855],{"class":338},[95,13439,10223],{"class":101},[95,13441,13443,13445,13447,13449],{"class":97,"line":13442},90,[95,13444,12666],{"class":338},[95,13446,112],{"class":101},[95,13448,12547],{"class":338},[95,13450,10223],{"class":101},[95,13452,13454],{"class":97,"line":13453},91,[95,13455,174],{"class":101},[95,13457,13459,13461,13463],{"class":97,"line":13458},92,[95,13460,568],{"class":101},[95,13462,1383],{"class":195},[95,13464,553],{"class":101},[11,13466,13467],{},"Usage:",[86,13469,13471],{"className":183,"code":13470,"language":185,"meta":91,"style":91},"\u003Ctemplate>\n  \u003Cbutton @click=\"isOpen = !isOpen\">\n    Toggle\n  \u003C\u002Fbutton>\n  \u003CTransitionExpand>\n    \u003Cdiv v-if=\"isOpen\">This content expands smoothly\u003C\u002Fdiv>\n  \u003C\u002FTransitionExpand>\n\u003C\u002Ftemplate>\n",[20,13472,13473,13481,13498,13503,13511,13519,13540,13548],{"__ignoreMap":91},[95,13474,13475,13477,13479],{"class":97,"line":98},[95,13476,192],{"class":101},[95,13478,11576],{"class":195},[95,13480,553],{"class":101},[95,13482,13483,13485,13488,13491,13493,13496],{"class":97,"line":105},[95,13484,11583],{"class":101},[95,13486,13487],{"class":195},"button",[95,13489,13490],{"class":108}," @click",[95,13492,205],{"class":101},[95,13494,13495],{"class":115},"\"isOpen = !isOpen\"",[95,13497,553],{"class":101},[95,13499,13500],{"class":97,"line":122},[95,13501,13502],{"class":101},"    Toggle\n",[95,13504,13505,13507,13509],{"class":97,"line":134},[95,13506,11649],{"class":101},[95,13508,13487],{"class":195},[95,13510,553],{"class":101},[95,13512,13513,13515,13517],{"class":97,"line":147},[95,13514,11583],{"class":101},[95,13516,8331],{"class":195},[95,13518,553],{"class":101},[95,13520,13521,13523,13525,13528,13530,13533,13536,13538],{"class":97,"line":160},[95,13522,11636],{"class":101},[95,13524,571],{"class":195},[95,13526,13527],{"class":108}," v-if",[95,13529,205],{"class":101},[95,13531,13532],{"class":115},"\"isOpen\"",[95,13534,13535],{"class":101},">This content expands smoothly\u003C\u002F",[95,13537,571],{"class":195},[95,13539,553],{"class":101},[95,13541,13542,13544,13546],{"class":97,"line":171},[95,13543,11649],{"class":101},[95,13545,8331],{"class":195},[95,13547,553],{"class":101},[95,13549,13550,13552,13554],{"class":97,"line":882},[95,13551,568],{"class":101},[95,13553,11576],{"class":195},[95,13555,553],{"class":101},[75,13557,13559],{"id":13558},"final-understanding","Final Understanding",[11,13561,13562],{},"Here’s the full mental model:",[10080,13564,13565,13568,13573,13576,13578],{},[642,13566,13567],{},"Measure actual height (hidden)",[642,13569,13570,13571],{},"Collapse element to ",[20,13572,3069],{},[642,13574,13575],{},"Force browser to apply styles",[642,13577,12219],{},[642,13579,13580,13581,13583],{},"Reset to ",[20,13582,11491],{}," after animation",[75,13585,13587],{"id":13586},"why-this-pattern-is-powerful","Why This Pattern is Powerful",[639,13589,13590,13593,13598,13601],{},[642,13591,13592],{},"Works with any dynamic content",[642,13594,13595,13596],{},"Fully reusable via ",[20,13597,11695],{},[642,13599,13600],{},"Gives smooth, professional UI",[642,13602,13603],{},"No external libraries needed",[11,13605,13606],{},"Once you understand this, you’ve basically unlocked a core frontend animation technique used in real-world production apps.",[11,13608,13609],{},"Thanks for reading—hope you learned something new. If you liked this article, please consider following me; I’ll be writing more such articles on Vue, front-end, and back-end. If you have any thoughts, share them in the comments, and see you next time.",[1383,13611,13612],{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s9eBZ, html code.shiki .s9eBZ{--shiki-default:#22863A;--shiki-dark:#85E89D}html pre.shiki code .s7hpK, html code.shiki .s7hpK{--shiki-default:#B31D28;--shiki-default-font-style:italic;--shiki-dark:#FDAEB7;--shiki-dark-font-style:italic}",{"title":91,"searchDepth":105,"depth":105,"links":13614},[13615,13617,13618,13626,13627,13628,13629,13630],{"id":11481,"depth":105,"text":13616},"The Problem: Why height: auto Won’t Animate",{"id":11527,"depth":105,"text":11528},{"id":11560,"depth":105,"text":11561,"children":13619},[13620,13622,13624],{"id":11749,"depth":122,"text":13621},"Function 1: enter() — When Element Appears",{"id":12261,"depth":122,"text":13623},"Function 2: afterEnter() — Cleanup After Animation",{"id":12309,"depth":122,"text":13625},"Function 3: leave() — When Element Disappears",{"id":12468,"depth":105,"text":12469},{"id":12618,"depth":105,"text":12619},{"id":12707,"depth":105,"text":12708},{"id":13558,"depth":105,"text":13559},{"id":13586,"depth":105,"text":13587},"2026-04-25","Build a reusable Vue 3 TransitionExpand component that animates height from 0 to auto using transition hooks, measured heights, and requestAnimationFrame.",{"src":13634,"alt":13635},"\u002Fimages\u002Fblog\u002Fbuilding-a-reusable-expand-collapse-transition-component-in-vue-3-with-smooth-height-animation-cover.jpg","Cover graphic for a reusable Vue 3 expand and collapse transition with smooth height animation",{},{"title":11457,"description":13632},"blog\u002Fbuilding-a-reusable-expand-collapse-transition-component-in-vue-3-with-smooth-height-animation",[1415,13640,1417,1419,10813],"Vue 3","eTh53rXaT-rb5TVkp_rF8uIL-DKpRw5IUNsggbhzLoA",{"id":13643,"title":13644,"body":13645,"date":13631,"description":15356,"draft":1404,"extension":1405,"featured":1404,"image":15357,"meta":15360,"navigation":1410,"path":15361,"seo":15362,"stem":15363,"tags":15364,"updatedAt":1420,"__hash__":15368},"blog\u002Fblog\u002Fhandling-multipart-form-data-using-multer.md","Handling Multipart Form Data Using Multer",{"type":8,"value":13646,"toc":15341},[13647,13675,13679,13686,13737,13783,13804,13808,13815,13820,13826,13831,13834,13839,13843,13852,13931,13936,13945,13990,13993,13998,14008,14029,14035,14071,14085,14091,14163,14169,14181,14200,14206,14211,14214,14219,14222,14227,14230,14235,14238,14242,14252,14381,14388,14398,14403,14508,14519,14535,14538,14543,14548,14553,14558,14565,14570,14573,14577,14580,14591,14600,14796,14799,14847,14850,14856,14859,14863,14866,15001,15004,15008,15090,15099,15102,15109,15151,15158,15264,15268,15271,15304,15306,15309,15323,15326,15335,15338],[11,13648,13649,13650,13653,13654,13657,13658,13661,13662,13665,13666,13671,13672,13674],{},"In this article you’ll learn how to use Multer to accept and process multipart form data. ",[20,13651,13652],{},"multipart\u002Fform-data"," is one of the values for the form ",[20,13655,13656],{},"enctype"," attribute—multipart means the payload is split and sent in multiple parts because it includes a file upload. We’ll create a route that listens for ",[20,13659,13660],{},"POST"," requests at ",[20,13663,13664],{},"\u002FsaveFormData",", using ",[61,13667,13670],{"href":13668,"rel":13669},"https:\u002F\u002Fgithub.com\u002Fexpressjs\u002Fmulter",[65],"Multer",", middleware for handling ",[20,13673,13652],{}," (primarily for file uploads).",[75,13676,13678],{"id":13677},"prerequisites","Prerequisites",[11,13680,13681,13682,13685],{},"There are a few things you should know about forms before we start. The ",[20,13683,13684],{},"form"," tag has many attributes, but three matter most here—you should have a clear idea of what they do:",[86,13687,13690],{"className":13688,"code":13689,"language":10300,"meta":91,"style":91},"language-html shiki shiki-themes github-light github-dark","\u003Cform action=\"\u002FsaveFormData\" method=\"post\" enctype=\"multipart\u002Fform-data\">\n  ...\n\u003C\u002Fform>\n",[20,13691,13692,13724,13729],{"__ignoreMap":91},[95,13693,13694,13696,13698,13701,13703,13706,13709,13711,13714,13717,13719,13722],{"class":97,"line":98},[95,13695,192],{"class":101},[95,13697,13684],{"class":195},[95,13699,13700],{"class":108}," action",[95,13702,205],{"class":101},[95,13704,13705],{"class":115},"\"\u002FsaveFormData\"",[95,13707,13708],{"class":108}," method",[95,13710,205],{"class":101},[95,13712,13713],{"class":115},"\"post\"",[95,13715,13716],{"class":108}," enctype",[95,13718,205],{"class":101},[95,13720,13721],{"class":115},"\"multipart\u002Fform-data\"",[95,13723,553],{"class":101},[95,13725,13726],{"class":97,"line":105},[95,13727,13728],{"class":101},"  ...\n",[95,13730,13731,13733,13735],{"class":97,"line":122},[95,13732,568],{"class":101},[95,13734,13684],{"class":195},[95,13736,553],{"class":101},[639,13738,13739,13748,13768],{},[642,13740,13741,13747],{},[13742,13743,13744],"strong",{},[20,13745,13746],{},"action"," — where the form data should be sent.",[642,13749,13750,13755,13756,909,13758,13761,13762,13765,13766,73],{},[13742,13751,13752],{},[20,13753,13754],{},"method"," — the HTTP method for the request. When sending data it is typically ",[20,13757,13660],{},[20,13759,13760],{},"PUT",", or ",[20,13763,13764],{},"PATCH","; for a create-style request we use ",[20,13767,13660],{},[642,13769,13770,13774,13775,909,13778,13761,13780,73],{},[13742,13771,13772],{},[20,13773,13656],{}," — how the form data should be encoded when submitted. It can be ",[20,13776,13777],{},"application\u002Fx-www-form-urlencoded",[20,13779,13652],{},[20,13781,13782],{},"text\u002Fplain",[11,13784,13785,13786,13788,13789,13791,13792,13794,13795,13798,13799,13801,13802,73],{},"I’ve never needed ",[20,13787,13782],{},". If you omit ",[20,13790,13656],{},", it defaults to ",[20,13793,13777],{},", and you ",[13742,13796,13797],{},"cannot"," send file data with that encoding. If the form has a file input, ",[20,13800,13656],{}," must be ",[20,13803,13652],{},[75,13805,13807],{"id":13806},"handling-form-data","Handling form data",[11,13809,13810,13811,13814],{},"Let’s start simple: handle a normal form with text and checkbox fields first. At this point ",[20,13812,13813],{},"app.js"," is a minimal Express server that serves an HTML page.",[8894,13816],{"alt":13817,"caption":13818,"src":13819},"VS Code showing a basic Express app.js that serves index.html and listens on port 3000","A minimal Express server before Multer—serving the HTML page and static assets.","\u002Fimages\u002Fblog\u002Fhandling-multipart-form-data-using-multer\u002Fapp-js-starter.png",[11,13821,1671,13822,13825],{},[20,13823,13824],{},"index.html"," page contains a form with one text input and two checkboxes (the file input is commented out for now). We’ll look at sending that data to the backend and reading it there.",[8894,13827],{"alt":13828,"caption":13829,"src":13830},"VS Code showing index.html with a form for name, Developer and Designer checkboxes, and a commented-out file input","The starter form: text and checkboxes only; the file input is still commented out.","\u002Fimages\u002Fblog\u002Fhandling-multipart-form-data-using-multer\u002Findex-html-form.png",[11,13832,13833],{},"And the web page looks like this:",[8894,13835],{"alt":13836,"caption":13837,"src":13838},"Browser at 127.0.0.1:3000 showing Hello World and an empty form with name, checkboxes, and Submit","The form in the browser before any data is entered.","\u002Fimages\u002Fblog\u002Fhandling-multipart-form-data-using-multer\u002Fbrowser-empty-form.png",[3471,13840,13842],{"id":13841},"sending-form-data-with-html-attributes","Sending form data with HTML attributes",[11,13844,13845,13846,13848,13849,13851],{},"To handle incoming ",[20,13847,13660],{}," requests, create a ",[20,13850,13660],{}," route:",[86,13853,13855],{"className":11329,"code":13854,"language":11331,"meta":91,"style":91},"app.post('\u002FsaveFormData', (req, res) => {\n  console.log(req.body)\n  res.status(200).json({ status: 'success', message: 'Data successfully uploaded.' })\n})\n",[20,13856,13857,13887,13896,13927],{"__ignoreMap":91},[95,13858,13859,13862,13865,13867,13870,13873,13876,13878,13881,13883,13885],{"class":97,"line":98},[95,13860,13861],{"class":101},"app.",[95,13863,13864],{"class":108},"post",[95,13866,1216],{"class":101},[95,13868,13869],{"class":115},"'\u002FsaveFormData'",[95,13871,13872],{"class":101},", (",[95,13874,13875],{"class":370},"req",[95,13877,909],{"class":101},[95,13879,13880],{"class":370},"res",[95,13882,373],{"class":101},[95,13884,350],{"class":201},[95,13886,353],{"class":101},[95,13888,13889,13891,13893],{"class":97,"line":105},[95,13890,2168],{"class":101},[95,13892,2171],{"class":108},[95,13894,13895],{"class":101},"(req.body)\n",[95,13897,13898,13901,13903,13905,13908,13910,13912,13915,13918,13921,13924],{"class":97,"line":122},[95,13899,13900],{"class":101},"  res.",[95,13902,4452],{"class":108},[95,13904,1216],{"class":101},[95,13906,13907],{"class":338},"200",[95,13909,11762],{"class":101},[95,13911,5572],{"class":108},[95,13913,13914],{"class":101},"({ status: ",[95,13916,13917],{"class":115},"'success'",[95,13919,13920],{"class":101},", message: ",[95,13922,13923],{"class":115},"'Data successfully uploaded.'",[95,13925,13926],{"class":101}," })\n",[95,13928,13929],{"class":97,"line":134},[95,13930,403],{"class":101},[8894,13932],{"alt":13933,"caption":13934,"src":13935},"VS Code showing app.js with a POST \u002FsaveFormData route that logs req.body and returns JSON","The \u002FsaveFormData route before any body-parsing middleware.","\u002Fimages\u002Fblog\u002Fhandling-multipart-form-data-using-multer\u002Fapp-js-post-route.png",[11,13937,13938,13939,13941,13942,13944],{},"We’ve created a ",[20,13940,13664],{}," route that logs the request body and returns JSON. In ",[20,13943,13824],{},", point the form at that route:",[86,13946,13948],{"className":13688,"code":13947,"language":10300,"meta":91,"style":91},"\u003Cform method=\"post\" action=\"\u002FsaveFormData\" class=\"form\">\n  ...\n\u003C\u002Fform>\n",[20,13949,13950,13978,13982],{"__ignoreMap":91},[95,13951,13952,13954,13956,13958,13960,13962,13964,13966,13968,13971,13973,13976],{"class":97,"line":98},[95,13953,192],{"class":101},[95,13955,13684],{"class":195},[95,13957,13708],{"class":108},[95,13959,205],{"class":101},[95,13961,13713],{"class":115},[95,13963,13700],{"class":108},[95,13965,205],{"class":101},[95,13967,13705],{"class":115},[95,13969,13970],{"class":108}," class",[95,13972,205],{"class":101},[95,13974,13975],{"class":115},"\"form\"",[95,13977,553],{"class":101},[95,13979,13980],{"class":97,"line":105},[95,13981,13728],{"class":101},[95,13983,13984,13986,13988],{"class":97,"line":122},[95,13985,568],{"class":101},[95,13987,13684],{"class":195},[95,13989,553],{"class":101},[11,13991,13992],{},"Submit the form and you should see the returned JSON from the server:",[8894,13994],{"alt":13995,"caption":13996,"src":13997},"Browser at 127.0.0.1:3000\u002FsaveFormData showing JSON status success and Data successfully uploaded","The JSON response after submitting the form.","\u002Fimages\u002Fblog\u002Fhandling-multipart-form-data-using-multer\u002Fbrowser-json-response-first.png",[11,13999,14000,14001,14004,14005,14007],{},"So our form data is going to the place we wanted, but we can’t seem to access it in ",[20,14002,14003],{},"req.body","—the console logs ",[20,14006,7743],{}," because the body is empty. That’s because we need to parse the form data before we can read it, and that’s why we need Multer. Install it:",[86,14009,14013],{"className":14010,"code":14011,"language":14012,"meta":91,"style":91},"language-bash shiki shiki-themes github-light github-dark","npm i multer --save\n","bash",[20,14014,14015],{"__ignoreMap":91},[95,14016,14017,14020,14023,14026],{"class":97,"line":98},[95,14018,14019],{"class":108},"npm",[95,14021,14022],{"class":115}," i",[95,14024,14025],{"class":115}," multer",[95,14027,14028],{"class":338}," --save\n",[11,14030,14031,14032,14034],{},"Import it in ",[20,14033,13813],{}," and create an upload instance:",[86,14036,14038],{"className":11329,"code":14037,"language":11331,"meta":91,"style":91},"const multer = require('multer')\nconst upload = multer()\n",[20,14039,14040,14058],{"__ignoreMap":91},[95,14041,14042,14044,14046,14048,14051,14053,14056],{"class":97,"line":98},[95,14043,335],{"class":201},[95,14045,14025],{"class":338},[95,14047,341],{"class":201},[95,14049,14050],{"class":108}," require",[95,14052,1216],{"class":101},[95,14054,14055],{"class":115},"'multer'",[95,14057,1222],{"class":101},[95,14059,14060,14062,14065,14067,14069],{"class":97,"line":105},[95,14061,335],{"class":201},[95,14063,14064],{"class":338}," upload",[95,14066,341],{"class":201},[95,14068,14025],{"class":108},[95,14070,1726],{"class":101},[11,14072,14073,14074,14077,14078,14081,14082,73],{},"Calling ",[20,14075,14076],{},"multer()"," returns an object we store as ",[20,14079,14080],{},"upload","—similar to importing Express and calling it to get ",[20,14083,14084],{},"app",[11,14086,14087,14088,1651],{},"Route handlers can chain middleware. Before the handler function, add ",[20,14089,14090],{},"upload.none()",[86,14092,14094],{"className":11329,"code":14093,"language":11331,"meta":91,"style":91},"app.post('\u002FsaveFormData', upload.none(), (req, res) => {\n  console.log(req.body)\n  res.status(200).json({ status: 'success', message: 'Data successfully uploaded.' })\n})\n",[20,14095,14096,14127,14135,14159],{"__ignoreMap":91},[95,14097,14098,14100,14102,14104,14106,14109,14112,14115,14117,14119,14121,14123,14125],{"class":97,"line":98},[95,14099,13861],{"class":101},[95,14101,13864],{"class":108},[95,14103,1216],{"class":101},[95,14105,13869],{"class":115},[95,14107,14108],{"class":101},", upload.",[95,14110,14111],{"class":108},"none",[95,14113,14114],{"class":101},"(), (",[95,14116,13875],{"class":370},[95,14118,909],{"class":101},[95,14120,13880],{"class":370},[95,14122,373],{"class":101},[95,14124,350],{"class":201},[95,14126,353],{"class":101},[95,14128,14129,14131,14133],{"class":97,"line":105},[95,14130,2168],{"class":101},[95,14132,2171],{"class":108},[95,14134,13895],{"class":101},[95,14136,14137,14139,14141,14143,14145,14147,14149,14151,14153,14155,14157],{"class":97,"line":122},[95,14138,13900],{"class":101},[95,14140,4452],{"class":108},[95,14142,1216],{"class":101},[95,14144,13907],{"class":338},[95,14146,11762],{"class":101},[95,14148,5572],{"class":108},[95,14150,13914],{"class":101},[95,14152,13917],{"class":115},[95,14154,13920],{"class":101},[95,14156,13923],{"class":115},[95,14158,13926],{"class":101},[95,14160,14161],{"class":97,"line":134},[95,14162,403],{"class":101},[11,14164,14165,14168],{},[20,14166,14167],{},".none()"," is for when you aren’t uploading files but still want Multer to handle the text fields.",[11,14170,14171,14172,14174,14175,14177,14178,14180],{},"Submit again and you’ll get the JSON response, but ",[20,14173,14003],{}," may still be empty. The default ",[20,14176,13656],{}," is ",[20,14179,13777],{},", so the data is URL-encoded and needs a different parser. Express provides that middleware—add:",[86,14182,14184],{"className":11329,"code":14183,"language":11331,"meta":91,"style":91},"app.use(express.urlencoded())\n",[20,14185,14186],{"__ignoreMap":91},[95,14187,14188,14190,14192,14195,14198],{"class":97,"line":98},[95,14189,13861],{"class":101},[95,14191,1693],{"class":108},[95,14193,14194],{"class":101},"(express.",[95,14196,14197],{"class":108},"urlencoded",[95,14199,4228],{"class":101},[11,14201,14202,14203,14205],{},"After everything, ",[20,14204,13813],{}," looks something like this:",[8894,14207],{"alt":14208,"caption":14209,"src":14210},"VS Code showing app.js with multer, upload.none on the POST route, and express.urlencoded middleware","Multer with upload.none() plus express.urlencoded() for text-only forms.","\u002Fimages\u002Fblog\u002Fhandling-multipart-form-data-using-multer\u002Fapp-js-multer-none.png",[11,14212,14213],{},"Fill in the form and submit:",[8894,14215],{"alt":14216,"caption":14217,"src":14218},"Browser form filled with Akshat Trivedi and both Developer and Designer checkboxes checked","Submitting text fields and checkboxes after urlencoded parsing is in place.","\u002Fimages\u002Fblog\u002Fhandling-multipart-form-data-using-multer\u002Fbrowser-filled-form.png",[11,14220,14221],{},"When I hit submit, I get the following response from the server:",[8894,14223],{"alt":14224,"caption":14225,"src":14226},"Browser at 127.0.0.1:3000\u002FsaveFormData showing the success JSON response again","The same success JSON after the body parsers are wired up.","\u002Fimages\u002Fblog\u002Fhandling-multipart-form-data-using-multer\u002Fbrowser-json-response-parsed.png",[11,14228,14229],{},"And my console looks like this, meaning the request body is getting populated with form data:",[8894,14231],{"alt":14232,"caption":14233,"src":14234},"VS Code terminal logging req.body with name Akshat Trivedi and skills developer and designer","req.body populated after express.urlencoded() parses the form.","\u002Fimages\u002Fblog\u002Fhandling-multipart-form-data-using-multer\u002Fterminal-req-body.png",[11,14236,14237],{},"Now we can start with uploading a file.",[3471,14239,14241],{"id":14240},"uploading-a-file","Uploading a file",[11,14243,14244,14245,8428,14247,14249,14250,1651],{},"To send a file, set ",[20,14246,13656],{},[20,14248,13652],{},". In ",[20,14251,13824],{},[86,14253,14255],{"className":13688,"code":14254,"language":10300,"meta":91,"style":91},"\u003Cform\n  action=\"\u002FsaveFormData\"\n  method=\"post\"\n  class=\"form\"\n  enctype=\"multipart\u002Fform-data\"\n>\n  \u003Cinput type=\"text\" name=\"name\" placeholder=\"Enter your name\" \u002F>\n  \u003C!-- checkboxes and other fields -->\n  \u003Cinput type=\"file\" name=\"profilePic\" id=\"profilePic\" \u002F>\n\u003C\u002Fform>\n",[20,14256,14257,14264,14274,14284,14294,14304,14308,14340,14345,14373],{"__ignoreMap":91},[95,14258,14259,14261],{"class":97,"line":98},[95,14260,192],{"class":101},[95,14262,14263],{"class":195},"form\n",[95,14265,14266,14269,14271],{"class":97,"line":105},[95,14267,14268],{"class":108},"  action",[95,14270,205],{"class":101},[95,14272,14273],{"class":115},"\"\u002FsaveFormData\"\n",[95,14275,14276,14279,14281],{"class":97,"line":122},[95,14277,14278],{"class":108},"  method",[95,14280,205],{"class":101},[95,14282,14283],{"class":115},"\"post\"\n",[95,14285,14286,14289,14291],{"class":97,"line":134},[95,14287,14288],{"class":108},"  class",[95,14290,205],{"class":101},[95,14292,14293],{"class":115},"\"form\"\n",[95,14295,14296,14299,14301],{"class":97,"line":147},[95,14297,14298],{"class":108},"  enctype",[95,14300,205],{"class":101},[95,14302,14303],{"class":115},"\"multipart\u002Fform-data\"\n",[95,14305,14306],{"class":97,"line":160},[95,14307,553],{"class":101},[95,14309,14310,14312,14315,14317,14319,14322,14324,14326,14329,14332,14334,14337],{"class":97,"line":171},[95,14311,11583],{"class":101},[95,14313,14314],{"class":195},"input",[95,14316,6854],{"class":108},[95,14318,205],{"class":101},[95,14320,14321],{"class":115},"\"text\"",[95,14323,6466],{"class":108},[95,14325,205],{"class":101},[95,14327,14328],{"class":115},"\"name\"",[95,14330,14331],{"class":108}," placeholder",[95,14333,205],{"class":101},[95,14335,14336],{"class":115},"\"Enter your name\"",[95,14338,14339],{"class":101}," \u002F>\n",[95,14341,14342],{"class":97,"line":882},[95,14343,14344],{"class":382},"  \u003C!-- checkboxes and other fields -->\n",[95,14346,14347,14349,14351,14353,14355,14358,14360,14362,14365,14367,14369,14371],{"class":97,"line":1788},[95,14348,11583],{"class":101},[95,14350,14314],{"class":195},[95,14352,6854],{"class":108},[95,14354,205],{"class":101},[95,14356,14357],{"class":115},"\"file\"",[95,14359,6466],{"class":108},[95,14361,205],{"class":101},[95,14363,14364],{"class":115},"\"profilePic\"",[95,14366,4780],{"class":108},[95,14368,205],{"class":101},[95,14370,14364],{"class":115},[95,14372,14339],{"class":101},[95,14374,14375,14377,14379],{"class":97,"line":1793},[95,14376,568],{"class":101},[95,14378,13684],{"class":195},[95,14380,553],{"class":101},[11,14382,14383,14384,14387],{},"Specify a ",[20,14385,14386],{},"name"," on every input—that becomes the property name on the request body (and for files, the field name Multer looks up).",[11,14389,14390,14391,14394,14395,14397],{},"You can remove ",[20,14392,14393],{},"app.use(express.urlencoded())"," for this flow; with ",[20,14396,13652],{}," the payload isn’t URL-encoded the same way.",[11,14399,14400,14401,1651],{},"Update ",[20,14402,13813],{},[86,14404,14406],{"className":11329,"code":14405,"language":11331,"meta":91,"style":91},"const upload = multer({ dest: path.join(__dirname, 'public') })\n\napp.post('\u002FsaveFormData', upload.single('profilePic'), (req, res) => {\n  console.log(req.body)\n  res.status(200).json({ status: 'success', message: 'Data successfully uploaded.' })\n})\n",[20,14407,14408,14433,14437,14472,14480,14504],{"__ignoreMap":91},[95,14409,14410,14412,14414,14416,14418,14421,14424,14427,14430],{"class":97,"line":98},[95,14411,335],{"class":201},[95,14413,14064],{"class":338},[95,14415,341],{"class":201},[95,14417,14025],{"class":108},[95,14419,14420],{"class":101},"({ dest: path.",[95,14422,14423],{"class":108},"join",[95,14425,14426],{"class":101},"(__dirname, ",[95,14428,14429],{"class":115},"'public'",[95,14431,14432],{"class":101},") })\n",[95,14434,14435],{"class":97,"line":105},[95,14436,1731],{"emptyLinePlaceholder":1410},[95,14438,14439,14441,14443,14445,14447,14449,14452,14454,14457,14460,14462,14464,14466,14468,14470],{"class":97,"line":122},[95,14440,13861],{"class":101},[95,14442,13864],{"class":108},[95,14444,1216],{"class":101},[95,14446,13869],{"class":115},[95,14448,14108],{"class":101},[95,14450,14451],{"class":108},"single",[95,14453,1216],{"class":101},[95,14455,14456],{"class":115},"'profilePic'",[95,14458,14459],{"class":101},"), (",[95,14461,13875],{"class":370},[95,14463,909],{"class":101},[95,14465,13880],{"class":370},[95,14467,373],{"class":101},[95,14469,350],{"class":201},[95,14471,353],{"class":101},[95,14473,14474,14476,14478],{"class":97,"line":134},[95,14475,2168],{"class":101},[95,14477,2171],{"class":108},[95,14479,13895],{"class":101},[95,14481,14482,14484,14486,14488,14490,14492,14494,14496,14498,14500,14502],{"class":97,"line":147},[95,14483,13900],{"class":101},[95,14485,4452],{"class":108},[95,14487,1216],{"class":101},[95,14489,13907],{"class":338},[95,14491,11762],{"class":101},[95,14493,5572],{"class":108},[95,14495,13914],{"class":101},[95,14497,13917],{"class":115},[95,14499,13920],{"class":101},[95,14501,13923],{"class":115},[95,14503,13926],{"class":101},[95,14505,14506],{"class":97,"line":160},[95,14507,403],{"class":101},[11,14509,14510,14511,14514,14515,14518],{},"We pass Multer an options object with ",[20,14512,14513],{},"dest",": the path where files should be saved (here, a ",[20,14516,14517],{},"public"," folder next to the app).",[11,14520,14521,14522,8396,14524,14527,14528,14531,14532,73],{},"In the route, ",[20,14523,14090],{},[20,14525,14526],{},"upload.single('profilePic')",". That accepts a single file whose field name is ",[20,14529,14530],{},"profilePic","—matching ",[20,14533,14534],{},"\u003Cinput type=\"file\" name=\"profilePic\" id=\"profilePic\" \u002F>",[11,14536,14537],{},"I’m uploading an image file:",[8894,14539],{"alt":14540,"caption":14541,"src":14542},"Browser form open with a Windows file picker selecting goku from Pictures","Choosing an image to upload through the file input.","\u002Fimages\u002Fblog\u002Fhandling-multipart-form-data-using-multer\u002Fbrowser-file-picker.png",[8894,14544],{"alt":14545,"caption":14546,"src":14547},"Browser form with name Akshat, both skills checked, and goku.jpg selected","The form ready to submit with text fields and goku.jpg.","\u002Fimages\u002Fblog\u002Fhandling-multipart-form-data-using-multer\u002Fbrowser-form-with-file.png",[11,14549,14550,14551,1651],{},"When I hit submit, I again see the JSON response on the client side, but in the editor a file with a weird name appears inside ",[20,14552,14517],{},[8894,14554],{"alt":14555,"caption":14556,"src":14557},"VS Code showing a random hex-named file in public that cannot be displayed as text, with req.body logged in the terminal","Multer’s default dest naming: a random hash in public, plus the text fields in req.body.","\u002Fimages\u002Fblog\u002Fhandling-multipart-form-data-using-multer\u002Fpublic-random-upload-file.png",[11,14559,14560,14561,14564],{},"I cannot open it as text, but if I rename it with the extension ",[20,14562,14563],{},".png"," I see that this is the same image I uploaded from the front end:",[8894,14566],{"alt":14567,"caption":14568,"src":14569},"VS Code image preview of the renamed upload showing a stylized Goku illustration","After adding a .png extension, the uploaded file opens as the original image.","\u002Fimages\u002Fblog\u002Fhandling-multipart-form-data-using-multer\u002Fpublic-renamed-upload-preview.png",[11,14571,14572],{},"So the file did get uploaded—but we don’t just want to upload it. We want a unique, identifiable name, and we cannot allow any file type. Next: custom storage and a file filter.",[75,14574,14576],{"id":14575},"customizing-file-storage-and-naming","Customizing file storage and naming",[11,14578,14579],{},"Right now Multer saves files with random names. In most apps you want control over:",[639,14581,14582,14585,14588],{},[642,14583,14584],{},"File name",[642,14586,14587],{},"File format \u002F extension",[642,14589,14590],{},"Storage location",[11,14592,14593,14594,14596,14597,1651],{},"Multer’s storage engine covers that. Instead of the simple ",[20,14595,14513],{}," option, use ",[20,14598,14599],{},"diskStorage",[86,14601,14603],{"className":11329,"code":14602,"language":11331,"meta":91,"style":91},"const storage = multer.diskStorage({\n  destination: function (req, file, cb) {\n    cb(null, path.join(__dirname, 'public'))\n  },\n  filename: function (req, file, cb) {\n    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9)\n    cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname))\n  },\n})\n\nconst upload = multer({ storage: storage })\n",[20,14604,14605,14621,14646,14667,14672,14695,14740,14771,14775,14779,14783],{"__ignoreMap":91},[95,14606,14607,14609,14612,14614,14617,14619],{"class":97,"line":98},[95,14608,335],{"class":201},[95,14610,14611],{"class":338}," storage",[95,14613,341],{"class":201},[95,14615,14616],{"class":101}," multer.",[95,14618,14599],{"class":108},[95,14620,5152],{"class":101},[95,14622,14623,14626,14628,14630,14632,14634,14636,14639,14641,14644],{"class":97,"line":105},[95,14624,14625],{"class":108},"  destination",[95,14627,112],{"class":101},[95,14629,823],{"class":201},[95,14631,1701],{"class":101},[95,14633,13875],{"class":370},[95,14635,909],{"class":101},[95,14637,14638],{"class":370},"file",[95,14640,909],{"class":101},[95,14642,14643],{"class":370},"cb",[95,14645,2324],{"class":101},[95,14647,14648,14651,14653,14655,14658,14660,14662,14664],{"class":97,"line":122},[95,14649,14650],{"class":108},"    cb",[95,14652,1216],{"class":101},[95,14654,7739],{"class":338},[95,14656,14657],{"class":101},", path.",[95,14659,14423],{"class":108},[95,14661,14426],{"class":101},[95,14663,14429],{"class":115},[95,14665,14666],{"class":101},"))\n",[95,14668,14669],{"class":97,"line":134},[95,14670,14671],{"class":101},"  },\n",[95,14673,14674,14677,14679,14681,14683,14685,14687,14689,14691,14693],{"class":97,"line":147},[95,14675,14676],{"class":108},"  filename",[95,14678,112],{"class":101},[95,14680,823],{"class":201},[95,14682,1701],{"class":101},[95,14684,13875],{"class":370},[95,14686,909],{"class":101},[95,14688,14638],{"class":370},[95,14690,909],{"class":101},[95,14692,14643],{"class":370},[95,14694,2324],{"class":101},[95,14696,14697,14699,14702,14704,14706,14708,14710,14713,14716,14719,14722,14725,14728,14731,14733,14735,14738],{"class":97,"line":160},[95,14698,1804],{"class":201},[95,14700,14701],{"class":338}," uniqueSuffix",[95,14703,341],{"class":201},[95,14705,1773],{"class":101},[95,14707,1776],{"class":108},[95,14709,1779],{"class":101},[95,14711,14712],{"class":201},"+",[95,14714,14715],{"class":115}," '-'",[95,14717,14718],{"class":201}," +",[95,14720,14721],{"class":101}," Math.",[95,14723,14724],{"class":108},"round",[95,14726,14727],{"class":101},"(Math.",[95,14729,14730],{"class":108},"random",[95,14732,1779],{"class":101},[95,14734,12648],{"class":201},[95,14736,14737],{"class":338}," 1e9",[95,14739,1222],{"class":101},[95,14741,14742,14744,14746,14748,14751,14753,14755,14757,14760,14762,14765,14768],{"class":97,"line":171},[95,14743,14650],{"class":108},[95,14745,1216],{"class":101},[95,14747,7739],{"class":338},[95,14749,14750],{"class":101},", file.fieldname ",[95,14752,14712],{"class":201},[95,14754,14715],{"class":115},[95,14756,14718],{"class":201},[95,14758,14759],{"class":101}," uniqueSuffix ",[95,14761,14712],{"class":201},[95,14763,14764],{"class":101}," path.",[95,14766,14767],{"class":108},"extname",[95,14769,14770],{"class":101},"(file.originalname))\n",[95,14772,14773],{"class":97,"line":882},[95,14774,14671],{"class":101},[95,14776,14777],{"class":97,"line":1788},[95,14778,403],{"class":101},[95,14780,14781],{"class":97,"line":1793},[95,14782,1731],{"emptyLinePlaceholder":1410},[95,14784,14785,14787,14789,14791,14793],{"class":97,"line":1801},[95,14786,335],{"class":201},[95,14788,14064],{"class":338},[95,14790,341],{"class":201},[95,14792,14025],{"class":108},[95,14794,14795],{"class":101},"({ storage: storage })\n",[11,14797,14798],{},"What’s going on:",[639,14800,14801,14817,14831,14839],{},[642,14802,14803,14808,14809,909,14811,14813,14814,14816],{},[13742,14804,14805],{},[20,14806,14807],{},"destination"," — where the file is stored. It receives ",[20,14810,13875],{},[20,14812,14638],{},", and a callback. The first callback argument is an error (",[20,14815,7739],{}," if none); the second is the destination path.",[642,14818,14819,14824,14825,14827,14828,14830],{},[13742,14820,14821],{},[20,14822,14823],{},"filename"," — how the file is named. Same parameters as ",[20,14826,14807],{},". Here we build a unique name, then call the callback with ",[20,14829,7739],{}," and that name.",[642,14832,14833,14838],{},[13742,14834,14835],{},[20,14836,14837],{},"file.originalname"," — the original client filename.",[642,14840,14841,14846],{},[13742,14842,14843],{},[20,14844,14845],{},"path.extname()"," — extracts the extension.",[11,14848,14849],{},"Uploaded files then look more like:",[86,14851,14854],{"className":14852,"code":14853,"language":632,"meta":91},[630],"profilePic-1683029381234-123456789.png\n",[20,14855,14853],{"__ignoreMap":91},[11,14857,14858],{},"Much better than an opaque random string with no extension.",[75,14860,14862],{"id":14861},"filtering-file-types","Filtering file types",[11,14864,14865],{},"Never blindly accept every upload. That invites security risk, malicious files, and storage abuse. Restrict uploads to images:",[86,14867,14869],{"className":11329,"code":14868,"language":11331,"meta":91,"style":91},"const fileFilter = (req, file, cb) => {\n  if (file.mimetype.startsWith('image\u002F')) {\n    cb(null, true)\n  }\n  else {\n    cb(new Error('Only image files are allowed!'), false)\n  }\n}\n\nconst upload = multer({\n  storage: storage,\n  fileFilter: fileFilter,\n})\n",[20,14870,14871,14898,14915,14929,14933,14940,14963,14967,14971,14975,14987,14992,14997],{"__ignoreMap":91},[95,14872,14873,14875,14878,14880,14882,14884,14886,14888,14890,14892,14894,14896],{"class":97,"line":98},[95,14874,335],{"class":201},[95,14876,14877],{"class":108}," fileFilter",[95,14879,341],{"class":201},[95,14881,1701],{"class":101},[95,14883,13875],{"class":370},[95,14885,909],{"class":101},[95,14887,14638],{"class":370},[95,14889,909],{"class":101},[95,14891,14643],{"class":370},[95,14893,373],{"class":101},[95,14895,350],{"class":201},[95,14897,353],{"class":101},[95,14899,14900,14902,14905,14907,14909,14912],{"class":97,"line":105},[95,14901,1736],{"class":201},[95,14903,14904],{"class":101}," (file.mimetype.",[95,14906,9636],{"class":108},[95,14908,1216],{"class":101},[95,14910,14911],{"class":115},"'image\u002F'",[95,14913,14914],{"class":101},")) {\n",[95,14916,14917,14919,14921,14923,14925,14927],{"class":97,"line":122},[95,14918,14650],{"class":108},[95,14920,1216],{"class":101},[95,14922,7739],{"class":338},[95,14924,909],{"class":101},[95,14926,5180],{"class":338},[95,14928,1222],{"class":101},[95,14930,14931],{"class":97,"line":134},[95,14932,1757],{"class":101},[95,14934,14935,14938],{"class":97,"line":147},[95,14936,14937],{"class":201},"  else",[95,14939,353],{"class":101},[95,14941,14942,14944,14946,14949,14951,14953,14956,14959,14961],{"class":97,"line":160},[95,14943,14650],{"class":108},[95,14945,1216],{"class":101},[95,14947,14948],{"class":201},"new",[95,14950,4624],{"class":108},[95,14952,1216],{"class":101},[95,14954,14955],{"class":115},"'Only image files are allowed!'",[95,14957,14958],{"class":101},"), ",[95,14960,3052],{"class":338},[95,14962,1222],{"class":101},[95,14964,14965],{"class":97,"line":171},[95,14966,1757],{"class":101},[95,14968,14969],{"class":97,"line":882},[95,14970,174],{"class":101},[95,14972,14973],{"class":97,"line":1788},[95,14974,1731],{"emptyLinePlaceholder":1410},[95,14976,14977,14979,14981,14983,14985],{"class":97,"line":1793},[95,14978,335],{"class":201},[95,14980,14064],{"class":338},[95,14982,341],{"class":201},[95,14984,14025],{"class":108},[95,14986,5152],{"class":101},[95,14988,14989],{"class":97,"line":1801},[95,14990,14991],{"class":101},"  storage: storage,\n",[95,14993,14994],{"class":97,"line":1820},[95,14995,14996],{"class":101},"  fileFilter: fileFilter,\n",[95,14998,14999],{"class":97,"line":1829},[95,15000,403],{"class":101},[11,15002,15003],{},"Only image MIME types pass. Anything else makes the server reject the upload with an error.",[75,15005,15007],{"id":15006},"final-route-with-file-upload","Final route with file upload",[86,15009,15011],{"className":11329,"code":15010,"language":11331,"meta":91,"style":91},"app.post('\u002FsaveFormData', upload.single('profilePic'), (req, res) => {\n  console.log(req.body)\n  console.log(req.file)\n  res.status(200).json({ status: 'success', message: 'Data successfully uploaded.' })\n})\n",[20,15012,15013,15045,15053,15062,15086],{"__ignoreMap":91},[95,15014,15015,15017,15019,15021,15023,15025,15027,15029,15031,15033,15035,15037,15039,15041,15043],{"class":97,"line":98},[95,15016,13861],{"class":101},[95,15018,13864],{"class":108},[95,15020,1216],{"class":101},[95,15022,13869],{"class":115},[95,15024,14108],{"class":101},[95,15026,14451],{"class":108},[95,15028,1216],{"class":101},[95,15030,14456],{"class":115},[95,15032,14459],{"class":101},[95,15034,13875],{"class":370},[95,15036,909],{"class":101},[95,15038,13880],{"class":370},[95,15040,373],{"class":101},[95,15042,350],{"class":201},[95,15044,353],{"class":101},[95,15046,15047,15049,15051],{"class":97,"line":105},[95,15048,2168],{"class":101},[95,15050,2171],{"class":108},[95,15052,13895],{"class":101},[95,15054,15055,15057,15059],{"class":97,"line":122},[95,15056,2168],{"class":101},[95,15058,2171],{"class":108},[95,15060,15061],{"class":101},"(req.file)\n",[95,15063,15064,15066,15068,15070,15072,15074,15076,15078,15080,15082,15084],{"class":97,"line":134},[95,15065,13900],{"class":101},[95,15067,4452],{"class":108},[95,15069,1216],{"class":101},[95,15071,13907],{"class":338},[95,15073,11762],{"class":101},[95,15075,5572],{"class":108},[95,15077,13914],{"class":101},[95,15079,13917],{"class":115},[95,15081,13920],{"class":101},[95,15083,13923],{"class":115},[95,15085,13926],{"class":101},[95,15087,15088],{"class":97,"line":147},[95,15089,403],{"class":101},[3471,15091,15093,15094,11033,15097],{"id":15092},"understanding-reqfile-and-reqbody","Understanding ",[20,15095,15096],{},"req.file",[20,15098,14003],{},[11,15100,15101],{},"After a successful upload:",[11,15103,15104,15108],{},[13742,15105,15106],{},[20,15107,14003],{}," holds the text fields, for example:",[86,15110,15112],{"className":5570,"code":15111,"language":5572,"meta":91,"style":91},"{\n  \"name\": \"Akshat\",\n  \"skills\": [\"developer\", \"designer\"]\n}\n",[20,15113,15114,15118,15130,15147],{"__ignoreMap":91},[95,15115,15116],{"class":97,"line":98},[95,15117,102],{"class":101},[95,15119,15120,15123,15125,15128],{"class":97,"line":105},[95,15121,15122],{"class":338},"  \"name\"",[95,15124,112],{"class":101},[95,15126,15127],{"class":115},"\"Akshat\"",[95,15129,119],{"class":101},[95,15131,15132,15135,15137,15140,15142,15145],{"class":97,"line":122},[95,15133,15134],{"class":338},"  \"skills\"",[95,15136,756],{"class":101},[95,15138,15139],{"class":115},"\"developer\"",[95,15141,909],{"class":101},[95,15143,15144],{"class":115},"\"designer\"",[95,15146,313],{"class":101},[95,15148,15149],{"class":97,"line":134},[95,15150,174],{"class":101},[11,15152,15153,15157],{},[13742,15154,15155],{},[20,15156,15096],{}," holds file metadata:",[86,15159,15161],{"className":5570,"code":15160,"language":5572,"meta":91,"style":91},"{\n  \"fieldname\": \"profilePic\",\n  \"originalname\": \"image.png\",\n  \"encoding\": \"7bit\",\n  \"mimetype\": \"image\u002Fpng\",\n  \"destination\": \"public\u002F\",\n  \"filename\": \"profilePic-1683029381234-123456789.png\",\n  \"path\": \"public\u002FprofilePic-1683029381234-123456789.png\",\n  \"size\": 34567\n}\n",[20,15162,15163,15167,15178,15190,15202,15214,15226,15238,15250,15260],{"__ignoreMap":91},[95,15164,15165],{"class":97,"line":98},[95,15166,102],{"class":101},[95,15168,15169,15172,15174,15176],{"class":97,"line":105},[95,15170,15171],{"class":338},"  \"fieldname\"",[95,15173,112],{"class":101},[95,15175,14364],{"class":115},[95,15177,119],{"class":101},[95,15179,15180,15183,15185,15188],{"class":97,"line":122},[95,15181,15182],{"class":338},"  \"originalname\"",[95,15184,112],{"class":101},[95,15186,15187],{"class":115},"\"image.png\"",[95,15189,119],{"class":101},[95,15191,15192,15195,15197,15200],{"class":97,"line":134},[95,15193,15194],{"class":338},"  \"encoding\"",[95,15196,112],{"class":101},[95,15198,15199],{"class":115},"\"7bit\"",[95,15201,119],{"class":101},[95,15203,15204,15207,15209,15212],{"class":97,"line":147},[95,15205,15206],{"class":338},"  \"mimetype\"",[95,15208,112],{"class":101},[95,15210,15211],{"class":115},"\"image\u002Fpng\"",[95,15213,119],{"class":101},[95,15215,15216,15219,15221,15224],{"class":97,"line":160},[95,15217,15218],{"class":338},"  \"destination\"",[95,15220,112],{"class":101},[95,15222,15223],{"class":115},"\"public\u002F\"",[95,15225,119],{"class":101},[95,15227,15228,15231,15233,15236],{"class":97,"line":171},[95,15229,15230],{"class":338},"  \"filename\"",[95,15232,112],{"class":101},[95,15234,15235],{"class":115},"\"profilePic-1683029381234-123456789.png\"",[95,15237,119],{"class":101},[95,15239,15240,15243,15245,15248],{"class":97,"line":882},[95,15241,15242],{"class":338},"  \"path\"",[95,15244,112],{"class":101},[95,15246,15247],{"class":115},"\"public\u002FprofilePic-1683029381234-123456789.png\"",[95,15249,119],{"class":101},[95,15251,15252,15255,15257],{"class":97,"line":1788},[95,15253,15254],{"class":338},"  \"size\"",[95,15256,112],{"class":101},[95,15258,15259],{"class":338},"34567\n",[95,15261,15262],{"class":97,"line":1793},[95,15263,174],{"class":101},[75,15265,15267],{"id":15266},"summary","Summary",[11,15269,15270],{},"In this article you learned:",[639,15272,15273,15279,15282,15291,15297],{},[642,15274,15275,15276,15278],{},"What ",[20,15277,13652],{}," is and why file uploads need it",[642,15280,15281],{},"How to use Multer as Express middleware",[642,15283,15284,15285,15287,15288],{},"How to handle text-only forms with ",[20,15286,14090],{}," and a single file with ",[20,15289,15290],{},"upload.single()",[642,15292,15293,15294,15296],{},"How to customize storage with ",[20,15295,14599],{},", generate unique names, and filter file types",[642,15298,15299,15300,11033,15302],{},"How to read uploaded data from ",[20,15301,14003],{},[20,15303,15096],{},[75,15305,11411],{"id":3580},[11,15307,15308],{},"Multer is simple on the surface and powerful when used carefully. In production you might go further by:",[639,15310,15311,15314,15317,15320],{},[642,15312,15313],{},"Uploading to cloud storage (AWS S3, Cloudinary)",[642,15315,15316],{},"Enforcing file size limits",[642,15318,15319],{},"Scanning uploads for malware",[642,15321,15322],{},"Storing file metadata in a database",[11,15324,15325],{},"What you’ve covered here is the foundation—and where many developers get stuck at first. Once this is comfortable, file uploads become routine.",[11,15327,15328,15329,15334],{},"Companion code: ",[61,15330,15333],{"href":15331,"rel":15332},"https:\u002F\u002Fgithub.com\u002Fakshat-triv\u002Fmedium_articles\u002F",[65],"github.com\u002Fakshat-triv\u002Fmedium_articles"," (see the Multipart Form Data folder).",[11,15336,15337],{},"Thanks for reading—see you next time.",[1383,15339,15340],{},"html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s9eBZ, html code.shiki .s9eBZ{--shiki-default:#22863A;--shiki-dark:#85E89D}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":91,"searchDepth":105,"depth":105,"links":15342},[15343,15344,15348,15349,15350,15354,15355],{"id":13677,"depth":105,"text":13678},{"id":13806,"depth":105,"text":13807,"children":15345},[15346,15347],{"id":13841,"depth":122,"text":13842},{"id":14240,"depth":122,"text":14241},{"id":14575,"depth":105,"text":14576},{"id":14861,"depth":105,"text":14862},{"id":15006,"depth":105,"text":15007,"children":15351},[15352],{"id":15092,"depth":122,"text":15353},"Understanding req.file and req.body",{"id":15266,"depth":105,"text":15267},{"id":3580,"depth":105,"text":11411},"Learn how to accept multipart\u002Fform-data in Express with Multer—parse text fields, upload a single file, customize disk storage and naming, and filter uploads to images only.",{"src":15358,"alt":15359},"\u002Fimages\u002Fblog\u002Fhandling-multipart-form-data-using-multer-cover.jpg","Cover graphic for handling multipart form data with Multer, showing upload flow from client through middleware to a Node.js server",{},"\u002Fblog\u002Fhandling-multipart-form-data-using-multer",{"title":13644,"description":15356},"blog\u002Fhandling-multipart-form-data-using-multer",[15365,15366,13670,15367],"Node.js","Express","Backend","3-kgBk3kQZELbh95NrEJ5NdaP2BoW1nQ48l6emwGiPs",{"id":15370,"title":15371,"body":15372,"date":16912,"description":16913,"draft":1404,"extension":1405,"featured":1410,"image":16914,"meta":16917,"navigation":1410,"path":16918,"seo":16919,"stem":16920,"tags":16921,"updatedAt":1420,"__hash__":16924},"blog\u002Fblog\u002Fcreating-a-customized-alert-and-confirm-modal-in-vue-3.md","Creating a customized alert and confirm function modal component which can stop function execution until the user responds in Vue 3",{"type":8,"value":15373,"toc":16903},[15374,15377,15394,15403,15409,15414,15420,15424,15438,15446,15449,15453,15465,15469,15561,15572,16269,16282,16288,16310,16337,16343,16365,16386,16390,16393,16406,16460,16467,16887,16892,16898,16900],[11,15375,15376],{},"Hi, that was probably the longest title I’ve ever written before. But it was necessary—there’s no clean way of summarizing the task.",[11,15378,15379,15380,11033,15383,15386,15387,15389,15390,15393],{},"In modern web development we make a lot of use of modals for alerting users and confirming actions. And of course, no one likes using the ",[20,15381,15382],{},"alert",[20,15384,15385],{},"confirm"," functions the browser APIs give us in JavaScript—browser default modals look horrible. But there’s one thing about those functions that intrigued me: they can stop script execution until they receive a user response. I wanted something similar—a way to ",[20,15388,1902],{}," the user response from my own ",[20,15391,15392],{},"alertConfirm"," modal.",[11,15395,15396,15397,15400,15401,15393],{},"The reason for doing this could be that you don’t want to leave your current function, and you wish to perform certain actions based on the user response inside that function. The best use case I encountered was inside a ",[20,15398,15399],{},"beforeLeave"," route guard: I didn’t want to leave the route if the user clicked “No” in my ",[20,15402,15392],{},[11,15404,59,15405,73],{},[61,15406,66],{"href":15407,"rel":15408},"https:\u002F\u002Fmedium.com\u002Fcodetodeploy\u002Fcreating-a-customized-alert-and-confirm-function-modal-component-which-can-stop-function-execution-bb26914da78a",[65],[8894,15410],{"alt":15411,"caption":15412,"src":15413},"TypeScript openAlertConfirm function that awaits alertConfirm.value.openModal with an alert or confirm message","Calling openModal from a parent component and awaiting the boolean result.","\u002Fimages\u002Fblog\u002Fcreating-a-customized-alert-and-confirm-modal-in-vue-3\u002Fopen-modal-usage.png",[11,15415,15416,15417,15419],{},"The above image is an example of how this modal component will be used inside a view or any other component. The first parameter the function takes is the message you wish to display inside the modal. The second parameter is a Boolean—if ",[20,15418,5180],{},", the modal will be an alert and will have only one button.",[75,15421,15423],{"id":15422},"ui-screenshots-from-the-project","UI Screenshots from the project",[15425,15426,15428,15433],"article-gallery",{"label":15427},"Alert and confirm modal screenshots",[8894,15429],{"alt":15430,"caption":15431,"src":15432},"Dark-themed alert modal with a single Okay button and a checkmark icon","Alert mode: one Okay button.","\u002Fimages\u002Fblog\u002Fcreating-a-customized-alert-and-confirm-modal-in-vue-3\u002Falert-modal.png",[8894,15434],{"alt":15435,"caption":15436,"src":15437},"Dark-themed confirm modal with Yes and No buttons","Confirm mode: Yes and No buttons.","\u002Fimages\u002Fblog\u002Fcreating-a-customized-alert-and-confirm-modal-in-vue-3\u002Fconfirm-modal.png",[11,15439,15440,15441,73],{},"Project link: ",[61,15442,15445],{"href":15443,"rel":15444},"https:\u002F\u002Fgithub.com\u002Fakshat-triv\u002FCustom-Vue-Alert-Confirm-Modal",[65],"akshat-triv\u002FCustom-Vue-Alert-Confirm-Modal",[11,15447,15448],{},"I’m using Vite and TypeScript in my project, but that shouldn’t affect you even if you don’t know TypeScript—you can ignore the types.",[75,15450,15452],{"id":15451},"creating-the-alertconfirm-modal-component","Creating the AlertConfirm modal component",[11,15454,15455,15456,15459,15460,11033,15462,15464],{},"I’m using the Composition API and ",[20,15457,15458],{},"\u003Cscript setup>"," way of writing Vue SFCs. In the setup I’ve imported my button component, along with the icon components I want inside my buttons. I’ve also imported ",[20,15461,22],{},[20,15463,29],{}," from Vue.",[3471,15466,15468],{"id":15467},"refs-variables-and-computed-properties","Refs, variables, and computed properties",[10080,15470,15471,15481,15503,15511,15524,15535],{},[642,15472,15473,15478,15479,73],{},[13742,15474,15475],{},[20,15476,15477],{},"showConfirm"," — makes the modal visible when the value is ",[20,15480,5180],{},[642,15482,15483,15488,15489,15492,15493,15496,15497,15499,15500,15502],{},[13742,15484,15485],{},[20,15486,15487],{},"modalType"," — can only be ",[20,15490,15491],{},"\"confirm\""," or ",[20,15494,15495],{},"\"alert\"",". If the value is ",[20,15498,15491],{},", the modal has two buttons with the text “Yes” and “No”. If the value is ",[20,15501,15495],{},", the modal has only one button with the text “Okay”.",[642,15504,15505,15510],{},[13742,15506,15507],{},[20,15508,15509],{},"alertConfirmMessage"," — stores and displays the message text inside the modal.",[642,15512,15513,15518,15519,15521,15522,73],{},[13742,15514,15515],{},[20,15516,15517],{},"trueBtnText"," — a computed property for the button that returns ",[20,15520,5180],{},"; the label changes based on ",[20,15523,15487],{},[642,15525,15526,15531,15532,15534],{},[13742,15527,15528],{},[20,15529,15530],{},"falseBtnText"," — a ref for the text on the button that returns ",[20,15533,3052],{}," when clicked.",[642,15536,15537,15542,15543,15545,15546,15549,15550,15553,15554,15557,15558,73],{},[13742,15538,15539],{},[20,15540,15541],{},"resolvePromise"," — a ",[20,15544,2392],{}," variable that later stores the ",[20,15547,15548],{},"resolve"," function’s reference. Don’t worry too much about this now—you’ll see why it matters. Its type is a function that takes a ",[20,15551,15552],{},"boolean"," (or ",[20,15555,15556],{},"PromiseLike\u003Cboolean>",") and returns ",[20,15559,15560],{},"void",[11,15562,15563,15564,15567,15568,15571],{},"Here’s the component (",[20,15565,15566],{},"AlertConfirm.vue"," in the article; the companion repo names the file ",[20,15569,15570],{},"AlertConfirmModal.vue","):",[86,15573,15575],{"className":183,"code":15574,"language":185,"meta":91,"style":91},"\u003Cscript lang=\"ts\" setup>\nimport { ref, computed } from 'vue'\n\nimport CommonButton from '@\u002Fcomponents\u002FCommonButton.vue'\nimport CheckIcon from '@\u002Fcomponents\u002Ficons\u002FIconCheck.vue'\nimport CrossIcon from '@\u002Fcomponents\u002Ficons\u002FIconCancel.vue'\n\nconst showConfirm = ref(false)\nlet resolvePromise: (value: PromiseLike\u003Cboolean> | boolean) => void\n\nconst modalType = ref\u003C'confirm' | 'alert'>('confirm')\n\nconst alertConfirmMessage = ref\u003Cnull | string>(null)\nconst trueBtnText = computed(() =>\n  modalType.value === 'confirm' ? 'Yes' : 'Okay',\n)\nconst falseBtnText = ref('No')\n\nfunction openModal(message: string, alertModal: boolean) {\n  alertConfirmMessage.value = message\n  modalType.value = alertModal ? 'alert' : 'confirm'\n\n  showConfirm.value = true\n\n  return new Promise((resolve) => {\n    resolvePromise = resolve\n  })\n}\n\nfunction handleUserInput(value: boolean) {\n  if (!resolvePromise) return\n\n  resolvePromise(value)\n  showConfirm.value = false\n}\n\ndefineExpose({ openModal })\n\u003C\u002Fscript>\n\n\u003Ctemplate>\n  \u003Ctransition name=\"fade-up\">\n    \u003Cdiv\n      v-if=\"showConfirm\"\n      class=\"alert-box\"\n    >\n      \u003Cdiv class=\"alert-box-header\">\n        {{ alertConfirmMessage }}\n      \u003C\u002Fdiv>\n      \u003Cdiv class=\"alert-box-buttons\">\n        \u003Ccommon-button\n          :button-text=\"trueBtnText\"\n          :primary=\"true\"\n          @click=\"handleUserInput(true)\"\n        >\n          \u003Ccheck-icon \u002F>\n        \u003C\u002Fcommon-button>\n        \u003Ccommon-button\n          v-if=\"modalType === 'confirm'\"\n          :button-text=\"falseBtnText\"\n          @click=\"handleUserInput(false)\"\n        >\n          \u003Ccross-icon \u002F>\n        \u003C\u002Fcommon-button>\n      \u003C\u002Fdiv>\n    \u003C\u002Fdiv>\n  \u003C\u002Ftransition>\n\u003C\u002Ftemplate>\n",[20,15576,15577,15593,15604,15608,15620,15632,15644,15648,15665,15701,15705,15732,15736,15761,15777,15801,15805,15823,15827,15854,15864,15883,15887,15896,15900,15918,15928,15932,15936,15940,15957,15971,15975,15983,15992,15996,16000,16008,16016,16020,16028,16043,16049,16059,16069,16074,16089,16094,16103,16118,16126,16136,16146,16156,16161,16171,16181,16187,16197,16206,16215,16219,16228,16236,16244,16253,16261],{"__ignoreMap":91},[95,15578,15579,15581,15583,15585,15587,15589,15591],{"class":97,"line":98},[95,15580,192],{"class":101},[95,15582,12720],{"class":195},[95,15584,12633],{"class":108},[95,15586,205],{"class":101},[95,15588,12727],{"class":115},[95,15590,12730],{"class":108},[95,15592,553],{"class":101},[95,15594,15595,15597,15600,15602],{"class":97,"line":105},[95,15596,2987],{"class":201},[95,15598,15599],{"class":101}," { ref, computed } ",[95,15601,2993],{"class":201},[95,15603,2996],{"class":115},[95,15605,15606],{"class":97,"line":122},[95,15607,1731],{"emptyLinePlaceholder":1410},[95,15609,15610,15612,15615,15617],{"class":97,"line":134},[95,15611,2987],{"class":201},[95,15613,15614],{"class":101}," CommonButton ",[95,15616,2993],{"class":201},[95,15618,15619],{"class":115}," '@\u002Fcomponents\u002FCommonButton.vue'\n",[95,15621,15622,15624,15627,15629],{"class":97,"line":147},[95,15623,2987],{"class":201},[95,15625,15626],{"class":101}," CheckIcon ",[95,15628,2993],{"class":201},[95,15630,15631],{"class":115}," '@\u002Fcomponents\u002Ficons\u002FIconCheck.vue'\n",[95,15633,15634,15636,15639,15641],{"class":97,"line":160},[95,15635,2987],{"class":201},[95,15637,15638],{"class":101}," CrossIcon ",[95,15640,2993],{"class":201},[95,15642,15643],{"class":115}," '@\u002Fcomponents\u002Ficons\u002FIconCancel.vue'\n",[95,15645,15646],{"class":97,"line":171},[95,15647,1731],{"emptyLinePlaceholder":1410},[95,15649,15650,15652,15655,15657,15659,15661,15663],{"class":97,"line":882},[95,15651,335],{"class":201},[95,15653,15654],{"class":338}," showConfirm",[95,15656,341],{"class":201},[95,15658,433],{"class":108},[95,15660,1216],{"class":101},[95,15662,3052],{"class":338},[95,15664,1222],{"class":101},[95,15666,15667,15669,15672,15674,15676,15678,15680,15683,15685,15687,15689,15692,15694,15696,15698],{"class":97,"line":1788},[95,15668,2392],{"class":201},[95,15670,15671],{"class":108}," resolvePromise",[95,15673,1651],{"class":201},[95,15675,1701],{"class":101},[95,15677,4252],{"class":370},[95,15679,1651],{"class":201},[95,15681,15682],{"class":108}," PromiseLike",[95,15684,192],{"class":101},[95,15686,15552],{"class":338},[95,15688,5407],{"class":101},[95,15690,15691],{"class":201},"|",[95,15693,6504],{"class":338},[95,15695,373],{"class":101},[95,15697,350],{"class":201},[95,15699,15700],{"class":338}," void\n",[95,15702,15703],{"class":97,"line":1793},[95,15704,1731],{"emptyLinePlaceholder":1410},[95,15706,15707,15709,15712,15714,15716,15718,15721,15723,15726,15728,15730],{"class":97,"line":1801},[95,15708,335],{"class":201},[95,15710,15711],{"class":338}," modalType",[95,15713,341],{"class":201},[95,15715,433],{"class":108},[95,15717,192],{"class":101},[95,15719,15720],{"class":115},"'confirm'",[95,15722,2403],{"class":201},[95,15724,15725],{"class":115}," 'alert'",[95,15727,3797],{"class":101},[95,15729,15720],{"class":115},[95,15731,1222],{"class":101},[95,15733,15734],{"class":97,"line":1820},[95,15735,1731],{"emptyLinePlaceholder":1410},[95,15737,15738,15740,15743,15745,15747,15749,15751,15753,15755,15757,15759],{"class":97,"line":1829},[95,15739,335],{"class":201},[95,15741,15742],{"class":338}," alertConfirmMessage",[95,15744,341],{"class":201},[95,15746,433],{"class":108},[95,15748,192],{"class":101},[95,15750,7739],{"class":338},[95,15752,2403],{"class":201},[95,15754,6471],{"class":338},[95,15756,3797],{"class":101},[95,15758,7739],{"class":338},[95,15760,1222],{"class":101},[95,15762,15763,15765,15768,15770,15772,15774],{"class":97,"line":1851},[95,15764,335],{"class":201},[95,15766,15767],{"class":338}," trueBtnText",[95,15769,341],{"class":201},[95,15771,344],{"class":108},[95,15773,347],{"class":101},[95,15775,15776],{"class":201},"=>\n",[95,15778,15779,15782,15784,15787,15790,15793,15796,15799],{"class":97,"line":1858},[95,15780,15781],{"class":101},"  modalType.value ",[95,15783,990],{"class":201},[95,15785,15786],{"class":115}," 'confirm'",[95,15788,15789],{"class":201}," ?",[95,15791,15792],{"class":115}," 'Yes'",[95,15794,15795],{"class":201}," :",[95,15797,15798],{"class":115}," 'Okay'",[95,15800,119],{"class":101},[95,15802,15803],{"class":97,"line":1863},[95,15804,1222],{"class":101},[95,15806,15807,15809,15812,15814,15816,15818,15821],{"class":97,"line":1868},[95,15808,335],{"class":201},[95,15810,15811],{"class":338}," falseBtnText",[95,15813,341],{"class":201},[95,15815,433],{"class":108},[95,15817,1216],{"class":101},[95,15819,15820],{"class":115},"'No'",[95,15822,1222],{"class":101},[95,15824,15825],{"class":97,"line":1887},[95,15826,1731],{"emptyLinePlaceholder":1410},[95,15828,15829,15831,15834,15836,15839,15841,15843,15845,15848,15850,15852],{"class":97,"line":1894},[95,15830,823],{"class":201},[95,15832,15833],{"class":108}," openModal",[95,15835,1216],{"class":101},[95,15837,15838],{"class":370},"message",[95,15840,1651],{"class":201},[95,15842,6471],{"class":338},[95,15844,909],{"class":101},[95,15846,15847],{"class":370},"alertModal",[95,15849,1651],{"class":201},[95,15851,6504],{"class":338},[95,15853,2324],{"class":101},[95,15855,15856,15859,15861],{"class":97,"line":3201},[95,15857,15858],{"class":101},"  alertConfirmMessage.value ",[95,15860,205],{"class":201},[95,15862,15863],{"class":101}," message\n",[95,15865,15866,15868,15870,15873,15876,15878,15880],{"class":97,"line":3210},[95,15867,15781],{"class":101},[95,15869,205],{"class":201},[95,15871,15872],{"class":101}," alertModal ",[95,15874,15875],{"class":201},"?",[95,15877,15725],{"class":115},[95,15879,15795],{"class":201},[95,15881,15882],{"class":115}," 'confirm'\n",[95,15884,15885],{"class":97,"line":3215},[95,15886,1731],{"emptyLinePlaceholder":1410},[95,15888,15889,15892,15894],{"class":97,"line":3220},[95,15890,15891],{"class":101},"  showConfirm.value ",[95,15893,205],{"class":201},[95,15895,6509],{"class":338},[95,15897,15898],{"class":97,"line":3225},[95,15899,1731],{"emptyLinePlaceholder":1410},[95,15901,15902,15904,15906,15908,15910,15912,15914,15916],{"class":97,"line":3237},[95,15903,358],{"class":201},[95,15905,4621],{"class":201},[95,15907,8471],{"class":338},[95,15909,367],{"class":101},[95,15911,15548],{"class":370},[95,15913,373],{"class":101},[95,15915,350],{"class":201},[95,15917,353],{"class":101},[95,15919,15920,15923,15925],{"class":97,"line":3247},[95,15921,15922],{"class":101},"    resolvePromise ",[95,15924,205],{"class":201},[95,15926,15927],{"class":101}," resolve\n",[95,15929,15930],{"class":97,"line":3255},[95,15931,398],{"class":101},[95,15933,15934],{"class":97,"line":3261},[95,15935,174],{"class":101},[95,15937,15938],{"class":97,"line":3267},[95,15939,1731],{"emptyLinePlaceholder":1410},[95,15941,15942,15944,15947,15949,15951,15953,15955],{"class":97,"line":3272},[95,15943,823],{"class":201},[95,15945,15946],{"class":108}," handleUserInput",[95,15948,1216],{"class":101},[95,15950,4252],{"class":370},[95,15952,1651],{"class":201},[95,15954,6504],{"class":338},[95,15956,2324],{"class":101},[95,15958,15959,15961,15963,15965,15968],{"class":97,"line":3277},[95,15960,1736],{"class":201},[95,15962,1701],{"class":101},[95,15964,1741],{"class":201},[95,15966,15967],{"class":101},"resolvePromise) ",[95,15969,15970],{"class":201},"return\n",[95,15972,15973],{"class":97,"line":3289},[95,15974,1731],{"emptyLinePlaceholder":1410},[95,15976,15977,15980],{"class":97,"line":3299},[95,15978,15979],{"class":108},"  resolvePromise",[95,15981,15982],{"class":101},"(value)\n",[95,15984,15985,15987,15989],{"class":97,"line":3306},[95,15986,15891],{"class":101},[95,15988,205],{"class":201},[95,15990,15991],{"class":338}," false\n",[95,15993,15994],{"class":97,"line":3311},[95,15995,174],{"class":101},[95,15997,15998],{"class":97,"line":3316},[95,15999,1731],{"emptyLinePlaceholder":1410},[95,16001,16002,16005],{"class":97,"line":3321},[95,16003,16004],{"class":108},"defineExpose",[95,16006,16007],{"class":101},"({ openModal })\n",[95,16009,16010,16012,16014],{"class":97,"line":3326},[95,16011,568],{"class":101},[95,16013,12720],{"class":195},[95,16015,553],{"class":101},[95,16017,16018],{"class":97,"line":3333},[95,16019,1731],{"emptyLinePlaceholder":1410},[95,16021,16022,16024,16026],{"class":97,"line":3339},[95,16023,192],{"class":101},[95,16025,11576],{"class":195},[95,16027,553],{"class":101},[95,16029,16030,16032,16034,16036,16038,16041],{"class":97,"line":3345},[95,16031,11583],{"class":101},[95,16033,11652],{"class":195},[95,16035,6466],{"class":108},[95,16037,205],{"class":101},[95,16039,16040],{"class":115},"\"fade-up\"",[95,16042,553],{"class":101},[95,16044,16045,16047],{"class":97,"line":3350},[95,16046,11636],{"class":101},[95,16048,516],{"class":195},[95,16050,16051,16054,16056],{"class":97,"line":13051},[95,16052,16053],{"class":108},"      v-if",[95,16055,205],{"class":101},[95,16057,16058],{"class":115},"\"showConfirm\"\n",[95,16060,16061,16064,16066],{"class":97,"line":13060},[95,16062,16063],{"class":108},"      class",[95,16065,205],{"class":101},[95,16067,16068],{"class":115},"\"alert-box\"\n",[95,16070,16071],{"class":97,"line":13065},[95,16072,16073],{"class":101},"    >\n",[95,16075,16076,16078,16080,16082,16084,16087],{"class":97,"line":13070},[95,16077,846],{"class":101},[95,16079,571],{"class":195},[95,16081,13970],{"class":108},[95,16083,205],{"class":101},[95,16085,16086],{"class":115},"\"alert-box-header\"",[95,16088,553],{"class":101},[95,16090,16091],{"class":97,"line":13075},[95,16092,16093],{"class":101},"        {{ alertConfirmMessage }}\n",[95,16095,16096,16099,16101],{"class":97,"line":13080},[95,16097,16098],{"class":101},"      \u003C\u002F",[95,16100,571],{"class":195},[95,16102,553],{"class":101},[95,16104,16105,16107,16109,16111,16113,16116],{"class":97,"line":13087},[95,16106,846],{"class":101},[95,16108,571],{"class":195},[95,16110,13970],{"class":108},[95,16112,205],{"class":101},[95,16114,16115],{"class":115},"\"alert-box-buttons\"",[95,16117,553],{"class":101},[95,16119,16120,16123],{"class":97,"line":13092},[95,16121,16122],{"class":101},"        \u003C",[95,16124,16125],{"class":195},"common-button\n",[95,16127,16128,16131,16133],{"class":97,"line":13103},[95,16129,16130],{"class":108},"          :button-text",[95,16132,205],{"class":101},[95,16134,16135],{"class":115},"\"trueBtnText\"\n",[95,16137,16138,16141,16143],{"class":97,"line":13112},[95,16139,16140],{"class":108},"          :primary",[95,16142,205],{"class":101},[95,16144,16145],{"class":115},"\"true\"\n",[95,16147,16148,16151,16153],{"class":97,"line":13121},[95,16149,16150],{"class":108},"          @click",[95,16152,205],{"class":101},[95,16154,16155],{"class":115},"\"handleUserInput(true)\"\n",[95,16157,16158],{"class":97,"line":13130},[95,16159,16160],{"class":101},"        >\n",[95,16162,16163,16166,16169],{"class":97,"line":13135},[95,16164,16165],{"class":101},"          \u003C",[95,16167,16168],{"class":195},"check-icon",[95,16170,14339],{"class":101},[95,16172,16173,16176,16179],{"class":97,"line":13140},[95,16174,16175],{"class":101},"        \u003C\u002F",[95,16177,16178],{"class":195},"common-button",[95,16180,553],{"class":101},[95,16182,16183,16185],{"class":97,"line":13149},[95,16184,16122],{"class":101},[95,16186,16125],{"class":195},[95,16188,16189,16192,16194],{"class":97,"line":13154},[95,16190,16191],{"class":108},"          v-if",[95,16193,205],{"class":101},[95,16195,16196],{"class":115},"\"modalType === 'confirm'\"\n",[95,16198,16199,16201,16203],{"class":97,"line":13163},[95,16200,16130],{"class":108},[95,16202,205],{"class":101},[95,16204,16205],{"class":115},"\"falseBtnText\"\n",[95,16207,16208,16210,16212],{"class":97,"line":13170},[95,16209,16150],{"class":108},[95,16211,205],{"class":101},[95,16213,16214],{"class":115},"\"handleUserInput(false)\"\n",[95,16216,16217],{"class":97,"line":13179},[95,16218,16160],{"class":101},[95,16220,16221,16223,16226],{"class":97,"line":13188},[95,16222,16165],{"class":101},[95,16224,16225],{"class":195},"cross-icon",[95,16227,14339],{"class":101},[95,16229,16230,16232,16234],{"class":97,"line":13197},[95,16231,16175],{"class":101},[95,16233,16178],{"class":195},[95,16235,553],{"class":101},[95,16237,16238,16240,16242],{"class":97,"line":13206},[95,16239,16098],{"class":101},[95,16241,571],{"class":195},[95,16243,553],{"class":101},[95,16245,16246,16249,16251],{"class":97,"line":13211},[95,16247,16248],{"class":101},"    \u003C\u002F",[95,16250,571],{"class":195},[95,16252,553],{"class":101},[95,16254,16255,16257,16259],{"class":97,"line":13222},[95,16256,11649],{"class":101},[95,16258,11652],{"class":195},[95,16260,553],{"class":101},[95,16262,16263,16265,16267],{"class":97,"line":13231},[95,16264,568],{"class":101},[95,16266,11576],{"class":195},[95,16268,553],{"class":101},[11,16270,16271,16272,16274,16275,16277,16278,14177,16280,73],{},"I have two buttons inside the component: one returns ",[20,16273,5180],{}," when clicked and the other returns ",[20,16276,3052],{},". The false-returning button is hidden when ",[20,16279,15487],{},[20,16281,15495],{},[3471,16283,16285],{"id":16284},"openmodal",[20,16286,16287],{},"openModal",[11,16289,16290,16291,16293,16294,8428,16296,16298,16299,8428,16301,16303,16304,16306,16307,16309],{},"This function takes two arguments: the message to display, and a Boolean—if ",[20,16292,5180],{},", we set ",[20,16295,15487],{},[20,16297,15495],{},". At the start we set ",[20,16300,15477],{},[20,16302,5180],{},", set ",[20,16305,15509],{}," to the first argument, and update ",[20,16308,15487],{}," from the second argument when needed.",[11,16311,16312,16313,16316,16317,16319,16320,16322,16323,16325,16326,10178,16328,16330,16331,16333,16334,16336],{},"After that we return a new ",[20,16314,16315],{},"Promise",", and inside the executor we assign the ",[20,16318,15548],{}," function to the ",[20,16321,15541],{}," variable declared earlier. We don’t call ",[20,16324,15548],{}," inside this component for the open path. Instead we expose ",[20,16327,16287],{},[20,16329,16004],{}," so parent components can call it and ",[20,16332,1902],{}," the user’s response. We can use ",[20,16335,1902],{}," because we’re returning a Promise.",[3471,16338,16340],{"id":16339},"handleuserinput",[20,16341,16342],{},"handleUserInput",[11,16344,16345,16346,15492,16348,16350,16351,16353,16354,16356,16357,16359,16360,8428,16362,16364],{},"When the user clicks a button inside the modal, we call this function with ",[20,16347,5180],{},[20,16349,3052],{},"—",[20,16352,5180],{}," for the true button, ",[20,16355,3052],{}," for the false button (hidden in alert mode). Inside the function we check that ",[20,16358,15541],{}," is defined (to keep TypeScript happy), call it with the boolean we received, then set ",[20,16361,15477],{},[20,16363,3052],{}," to close the modal.",[11,16366,16367,16369,16370,14177,16372,16374,16375,16377,16378,16380,16381,16383,16384,73],{},[20,16368,16342],{}," only runs when ",[20,16371,15477],{},[20,16373,5180],{},", and that only happens after ",[20,16376,16287],{}," has been called—so ",[20,16379,15541],{}," will already hold the ",[20,16382,15548],{}," from the Promise returned by ",[20,16385,16287],{},[75,16387,16389],{"id":16388},"using-alertconfirm-inside-other-components","Using AlertConfirm inside other components",[11,16391,16392],{},"Follow these steps:",[10080,16394,16395,16398,16403],{},[642,16396,16397],{},"Import the component.",[642,16399,16400,16401,11762],{},"Add it to your template, and bind a template ref (for example ",[20,16402,15392],{},[642,16404,16405],{},"When you want to open the modal:",[86,16407,16409],{"className":88,"code":16408,"language":90,"meta":91,"style":91},"\u002F\u002F Alert (one button)\nawait alertConfirm.value?.openModal('This will be an alert', true)\n\n\u002F\u002F Confirm (Yes \u002F No)\nawait alertConfirm.value?.openModal('This will be a confirm')\n",[20,16410,16411,16416,16436,16440,16445],{"__ignoreMap":91},[95,16412,16413],{"class":97,"line":98},[95,16414,16415],{"class":382},"\u002F\u002F Alert (one button)\n",[95,16417,16418,16420,16423,16425,16427,16430,16432,16434],{"class":97,"line":105},[95,16419,1902],{"class":201},[95,16421,16422],{"class":101}," alertConfirm.value?.",[95,16424,16287],{"class":108},[95,16426,1216],{"class":101},[95,16428,16429],{"class":115},"'This will be an alert'",[95,16431,909],{"class":101},[95,16433,5180],{"class":338},[95,16435,1222],{"class":101},[95,16437,16438],{"class":97,"line":122},[95,16439,1731],{"emptyLinePlaceholder":1410},[95,16441,16442],{"class":97,"line":134},[95,16443,16444],{"class":382},"\u002F\u002F Confirm (Yes \u002F No)\n",[95,16446,16447,16449,16451,16453,16455,16458],{"class":97,"line":147},[95,16448,1902],{"class":201},[95,16450,16422],{"class":101},[95,16452,16287],{"class":108},[95,16454,1216],{"class":101},[95,16456,16457],{"class":115},"'This will be a confirm'",[95,16459,1222],{"class":101},[11,16461,16462,16463,16466],{},"Here’s how I wired it in ",[20,16464,16465],{},"App.vue","—two buttons, one for alert and one for confirm:",[86,16468,16470],{"className":183,"code":16469,"language":185,"meta":91,"style":91},"\u003Cscript lang=\"ts\" setup>\nimport { ref } from 'vue'\n\nimport CommonButton from '.\u002Fcomponents\u002FCommonButton.vue'\nimport AlertConfirmModal from '.\u002Fcomponents\u002FAlertConfirmModal.vue'\n\nconst alertConfirm = ref\u003CInstanceType\u003Ctypeof AlertConfirmModal> | null>(null)\n\nasync function openAlertConfirm(alertModal = false) {\n  const alertMessage =\n    'Hi!, You created a custom async alert modal. This was amazing.'\n\n  const confirmMessage = 'It was quite interesting and elegant right?'\n\n  const userInput = await alertConfirm.value?.openModal(\n    alertModal ? alertMessage : confirmMessage,\n    alertModal,\n  )\n\n  console.log('This was the user input', userInput)\n}\n\u003C\u002Fscript>\n\n\u003Ctemplate>\n  \u003Cdiv class=\"buttons-wrapper\">\n    \u003CCommonButton\n      button-text=\"Open Alert\"\n      :primary=\"true\"\n      @click=\"openAlertConfirm(true)\"\n    \u002F>\n    \u003CCommonButton\n      button-text=\"Open Confirm\"\n      :primary=\"true\"\n      @click=\"openAlertConfirm(false)\"\n    \u002F>\n  \u003C\u002Fdiv>\n  \u003Cdiv class=\"alert-modal\">\n    \u003CAlertConfirmModal ref=\"alertConfirm\" \u002F>\n  \u003C\u002Fdiv>\n\u003C\u002Ftemplate>\n",[20,16471,16472,16488,16499,16503,16514,16526,16530,16563,16567,16587,16596,16601,16605,16617,16621,16638,16653,16658,16662,16666,16680,16684,16692,16696,16704,16719,16726,16736,16751,16774,16779,16785,16794,16808,16828,16832,16840,16855,16871,16879],{"__ignoreMap":91},[95,16473,16474,16476,16478,16480,16482,16484,16486],{"class":97,"line":98},[95,16475,192],{"class":101},[95,16477,12720],{"class":195},[95,16479,12633],{"class":108},[95,16481,205],{"class":101},[95,16483,12727],{"class":115},[95,16485,12730],{"class":108},[95,16487,553],{"class":101},[95,16489,16490,16492,16495,16497],{"class":97,"line":105},[95,16491,2987],{"class":201},[95,16493,16494],{"class":101}," { ref } ",[95,16496,2993],{"class":201},[95,16498,2996],{"class":115},[95,16500,16501],{"class":97,"line":122},[95,16502,1731],{"emptyLinePlaceholder":1410},[95,16504,16505,16507,16509,16511],{"class":97,"line":134},[95,16506,2987],{"class":201},[95,16508,15614],{"class":101},[95,16510,2993],{"class":201},[95,16512,16513],{"class":115}," '.\u002Fcomponents\u002FCommonButton.vue'\n",[95,16515,16516,16518,16521,16523],{"class":97,"line":147},[95,16517,2987],{"class":201},[95,16519,16520],{"class":101}," AlertConfirmModal ",[95,16522,2993],{"class":201},[95,16524,16525],{"class":115}," '.\u002Fcomponents\u002FAlertConfirmModal.vue'\n",[95,16527,16528],{"class":97,"line":160},[95,16529,1731],{"emptyLinePlaceholder":1410},[95,16531,16532,16534,16537,16539,16541,16543,16546,16548,16550,16553,16555,16557,16559,16561],{"class":97,"line":171},[95,16533,335],{"class":201},[95,16535,16536],{"class":338}," alertConfirm",[95,16538,341],{"class":201},[95,16540,433],{"class":108},[95,16542,192],{"class":101},[95,16544,16545],{"class":108},"InstanceType",[95,16547,192],{"class":101},[95,16549,4204],{"class":201},[95,16551,16552],{"class":101}," AlertConfirmModal> ",[95,16554,15691],{"class":201},[95,16556,2406],{"class":338},[95,16558,3797],{"class":101},[95,16560,7739],{"class":338},[95,16562,1222],{"class":101},[95,16564,16565],{"class":97,"line":882},[95,16566,1731],{"emptyLinePlaceholder":1410},[95,16568,16569,16571,16573,16576,16578,16580,16582,16585],{"class":97,"line":1788},[95,16570,1698],{"class":201},[95,16572,3008],{"class":201},[95,16574,16575],{"class":108}," openAlertConfirm",[95,16577,1216],{"class":101},[95,16579,15847],{"class":370},[95,16581,341],{"class":201},[95,16583,16584],{"class":338}," false",[95,16586,2324],{"class":101},[95,16588,16589,16591,16594],{"class":97,"line":1793},[95,16590,1715],{"class":201},[95,16592,16593],{"class":338}," alertMessage",[95,16595,4410],{"class":201},[95,16597,16598],{"class":97,"line":1801},[95,16599,16600],{"class":115},"    'Hi!, You created a custom async alert modal. This was amazing.'\n",[95,16602,16603],{"class":97,"line":1820},[95,16604,1731],{"emptyLinePlaceholder":1410},[95,16606,16607,16609,16612,16614],{"class":97,"line":1829},[95,16608,1715],{"class":201},[95,16610,16611],{"class":338}," confirmMessage",[95,16613,341],{"class":201},[95,16615,16616],{"class":115}," 'It was quite interesting and elegant right?'\n",[95,16618,16619],{"class":97,"line":1851},[95,16620,1731],{"emptyLinePlaceholder":1410},[95,16622,16623,16625,16628,16630,16632,16634,16636],{"class":97,"line":1858},[95,16624,1715],{"class":201},[95,16626,16627],{"class":338}," userInput",[95,16629,341],{"class":201},[95,16631,1812],{"class":201},[95,16633,16422],{"class":101},[95,16635,16287],{"class":108},[95,16637,976],{"class":101},[95,16639,16640,16643,16645,16648,16650],{"class":97,"line":1863},[95,16641,16642],{"class":101},"    alertModal ",[95,16644,15875],{"class":201},[95,16646,16647],{"class":101}," alertMessage ",[95,16649,1651],{"class":201},[95,16651,16652],{"class":101}," confirmMessage,\n",[95,16654,16655],{"class":97,"line":1868},[95,16656,16657],{"class":101},"    alertModal,\n",[95,16659,16660],{"class":97,"line":1887},[95,16661,879],{"class":101},[95,16663,16664],{"class":97,"line":1894},[95,16665,1731],{"emptyLinePlaceholder":1410},[95,16667,16668,16670,16672,16674,16677],{"class":97,"line":3201},[95,16669,2168],{"class":101},[95,16671,2171],{"class":108},[95,16673,1216],{"class":101},[95,16675,16676],{"class":115},"'This was the user input'",[95,16678,16679],{"class":101},", userInput)\n",[95,16681,16682],{"class":97,"line":3210},[95,16683,174],{"class":101},[95,16685,16686,16688,16690],{"class":97,"line":3215},[95,16687,568],{"class":101},[95,16689,12720],{"class":195},[95,16691,553],{"class":101},[95,16693,16694],{"class":97,"line":3220},[95,16695,1731],{"emptyLinePlaceholder":1410},[95,16697,16698,16700,16702],{"class":97,"line":3225},[95,16699,192],{"class":101},[95,16701,11576],{"class":195},[95,16703,553],{"class":101},[95,16705,16706,16708,16710,16712,16714,16717],{"class":97,"line":3237},[95,16707,11583],{"class":101},[95,16709,571],{"class":195},[95,16711,13970],{"class":108},[95,16713,205],{"class":101},[95,16715,16716],{"class":115},"\"buttons-wrapper\"",[95,16718,553],{"class":101},[95,16720,16721,16723],{"class":97,"line":3247},[95,16722,11636],{"class":101},[95,16724,16725],{"class":195},"CommonButton\n",[95,16727,16728,16731,16733],{"class":97,"line":3255},[95,16729,16730],{"class":108},"      button-text",[95,16732,205],{"class":101},[95,16734,16735],{"class":115},"\"Open Alert\"\n",[95,16737,16738,16740,16743,16745,16747,16749],{"class":97,"line":3261},[95,16739,5520],{"class":101},[95,16741,16742],{"class":108},"primary",[95,16744,205],{"class":101},[95,16746,208],{"class":115},[95,16748,5180],{"class":338},[95,16750,220],{"class":115},[95,16752,16753,16756,16759,16761,16763,16766,16768,16770,16772],{"class":97,"line":3267},[95,16754,16755],{"class":101},"      @",[95,16757,16758],{"class":108},"click",[95,16760,205],{"class":101},[95,16762,208],{"class":115},[95,16764,16765],{"class":108},"openAlertConfirm",[95,16767,1216],{"class":101},[95,16769,5180],{"class":338},[95,16771,4259],{"class":101},[95,16773,220],{"class":115},[95,16775,16776],{"class":97,"line":3272},[95,16777,16778],{"class":101},"    \u002F>\n",[95,16780,16781,16783],{"class":97,"line":3277},[95,16782,11636],{"class":101},[95,16784,16725],{"class":195},[95,16786,16787,16789,16791],{"class":97,"line":3289},[95,16788,16730],{"class":108},[95,16790,205],{"class":101},[95,16792,16793],{"class":115},"\"Open Confirm\"\n",[95,16795,16796,16798,16800,16802,16804,16806],{"class":97,"line":3299},[95,16797,5520],{"class":101},[95,16799,16742],{"class":108},[95,16801,205],{"class":101},[95,16803,208],{"class":115},[95,16805,5180],{"class":338},[95,16807,220],{"class":115},[95,16809,16810,16812,16814,16816,16818,16820,16822,16824,16826],{"class":97,"line":3306},[95,16811,16755],{"class":101},[95,16813,16758],{"class":108},[95,16815,205],{"class":101},[95,16817,208],{"class":115},[95,16819,16765],{"class":108},[95,16821,1216],{"class":101},[95,16823,3052],{"class":338},[95,16825,4259],{"class":101},[95,16827,220],{"class":115},[95,16829,16830],{"class":97,"line":3311},[95,16831,16778],{"class":101},[95,16833,16834,16836,16838],{"class":97,"line":3316},[95,16835,11649],{"class":101},[95,16837,571],{"class":195},[95,16839,553],{"class":101},[95,16841,16842,16844,16846,16848,16850,16853],{"class":97,"line":3321},[95,16843,11583],{"class":101},[95,16845,571],{"class":195},[95,16847,13970],{"class":108},[95,16849,205],{"class":101},[95,16851,16852],{"class":115},"\"alert-modal\"",[95,16854,553],{"class":101},[95,16856,16857,16859,16862,16864,16866,16869],{"class":97,"line":3326},[95,16858,11636],{"class":101},[95,16860,16861],{"class":195},"AlertConfirmModal",[95,16863,433],{"class":108},[95,16865,205],{"class":101},[95,16867,16868],{"class":115},"\"alertConfirm\"",[95,16870,14339],{"class":101},[95,16872,16873,16875,16877],{"class":97,"line":3333},[95,16874,11649],{"class":101},[95,16876,571],{"class":195},[95,16878,553],{"class":101},[95,16880,16881,16883,16885],{"class":97,"line":3339},[95,16882,568],{"class":101},[95,16884,11576],{"class":195},[95,16886,553],{"class":101},[11,16888,16889,16890,73],{},"And that’s it—you have a way to write alert and confirm modals that you can ",[20,16891,1902],{},[11,16893,16894,16895,73],{},"If you want the full project, it’s on GitHub: ",[61,16896,15445],{"href":15443,"rel":16897},[65],[11,16899,15337],{},[1383,16901,16902],{},"html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s9eBZ, html code.shiki .s9eBZ{--shiki-default:#22863A;--shiki-dark:#85E89D}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":91,"searchDepth":105,"depth":105,"links":16904},[16905,16906,16911],{"id":15422,"depth":105,"text":15423},{"id":15451,"depth":105,"text":15452,"children":16907},[16908,16909,16910],{"id":15467,"depth":122,"text":15468},{"id":16284,"depth":122,"text":16287},{"id":16339,"depth":122,"text":16342},{"id":16388,"depth":105,"text":16389},"2022-11-07","Build a Vue 3 AlertConfirm modal that returns a Promise so you can await the user’s Yes\u002FNo or Okay response—useful in route guards and other in-function confirmations.",{"src":16915,"alt":16916},"\u002Fimages\u002Fblog\u002Fcreating-a-customized-alert-and-confirm-modal-in-vue-3-cover.jpg","Vue-themed cover showing function execution pausing for an alert or confirm modal, then continuing after the user responds",{},"\u002Fblog\u002Fcreating-a-customized-alert-and-confirm-modal-in-vue-3",{"title":15371,"description":16913},"blog\u002Fcreating-a-customized-alert-and-confirm-modal-in-vue-3",[13640,11453,1417,16922,16923],"Promises","Asyncawait","DiFFuqyErLXVNcKD0X2TjVPLqn2FhqwhO6Ud_JVyrzY",{"id":16926,"title":16927,"body":16928,"date":17639,"description":17640,"draft":1404,"extension":1405,"featured":1404,"image":17641,"meta":17644,"navigation":1410,"path":17645,"seo":17646,"stem":17647,"tags":17648,"updatedAt":1420,"__hash__":17653},"blog\u002Fblog\u002Fdeploying-a-node-js-application-using-heroku-and-git.md","Deploying a Node.js Application Using Heroku and Git",{"type":8,"value":16929,"toc":17617},[16930,16933,16944,16955,16957,16960,16969,16973,16981,16985,16994,16997,17002,17006,17017,17021,17024,17035,17038,17107,17111,17125,17128,17234,17239,17243,17254,17364,17368,17372,17375,17388,17393,17404,17414,17433,17436,17441,17448,17463,17475,17479,17482,17507,17513,17517,17520,17548,17558,17563,17567,17570,17574,17585,17589,17597,17600,17612,17614],[11,16931,16932],{},"Heroku is a platform as a service (PaaS) that lets developers build, run, and operate applications entirely in the cloud. It supports many languages—Ruby, Java, Scala, Python, PHP, and more. Today we’ll look at deploying a Node application on Heroku: what to change in the code, how to configure Heroku, and the pros and cons of the free tier as it existed when this was written.",[43,16934,16935],{},[11,16936,16937,16938,16943],{},"Note: Heroku’s free tier was ",[61,16939,16942],{"href":16940,"rel":16941},"https:\u002F\u002Fblog.heroku.com\u002Fnext-chapter",[65],"discontinued in late 2022",". The deploy workflow below still applies on paid plans; the free-tier sleep behavior in the pros\u002Fcons section is historical.",[11,16945,59,16946,67,16950,73],{},[61,16947,66],{"href":16948,"rel":16949},"https:\u002F\u002Fmedium.com\u002Ffaun\u002Fdeploying-a-node-js-application-using-heroku-and-git-getting-started-with-node-js-c658a670c8d8",[65],[61,16951,16954],{"href":16952,"rel":16953},"https:\u002F\u002Ffaun.pub\u002Fdeploying-a-node-js-application-using-heroku-and-git-getting-started-with-node-js-c658a670c8d8",[65],"FAUN.dev()",[75,16956,13678],{"id":13677},[11,16958,16959],{},"We’ll cover the Heroku steps here, but you should know the basics of Git and GitHub, and Git should already be set up in your project. If you need that first, read this and come back:",[43,16961,16962],{},[11,16963,16964,16965],{},"Previous article: ",[61,16966,16968],{"href":16967},"\u002Fblog\u002Finitializing-git-and-pushing-code-to-a-remote-repository","Initializing Git and Pushing Code to a Remote Repository",[3471,16970,16972],{"id":16971},"install-the-heroku-cli","Install the Heroku CLI",[11,16974,16975,16976,73],{},"Download and install the Heroku CLI from the ",[61,16977,16980],{"href":16978,"rel":16979},"https:\u002F\u002Fdevcenter.heroku.com\u002Farticles\u002Fheroku-cli#download-and-install",[65],"Heroku Dev Center",[3471,16982,16984],{"id":16983},"create-a-heroku-account","Create a Heroku account",[11,16986,16987,16988,16993],{},"Go to ",[61,16989,16992],{"href":16990,"rel":16991},"https:\u002F\u002Fsignup.heroku.com\u002F",[65],"Heroku’s signup page",", create an account, confirm your email, and accept the terms on the dashboard. If you already have an account, skip ahead.",[11,16995,16996],{},"Once the CLI is installed and you’re signed up, the dashboard looks something like this:",[8894,16998],{"alt":16999,"caption":17000,"src":17001},"Heroku dashboard welcome screen with Create new app and Create a team options","A new Heroku account dashboard after signup.","\u002Fimages\u002Fblog\u002Fdeploying-a-node-js-application-using-heroku-and-git\u002Fheroku-dashboard.png",[3471,17003,17005],{"id":17004},"log-in-to-the-heroku-cli","Log in to the Heroku CLI",[11,17007,17008,17009,17012,17013,17016],{},"Open your terminal, run ",[20,17010,17011],{},"heroku login",", and press Enter. You’ll be asked to press any key to open a login window in your default browser (or ",[20,17014,17015],{},"q"," to exit). Enter your credentials if you aren’t already logged in in the browser; if you are, just confirm the login.",[75,17018,17020],{"id":17019},"changes-required-in-our-code","Changes required in our code",[11,17022,17023],{},"Open the project in your editor—we need a few updates before deploy.",[3471,17025,17027,17028,17031,17032],{"id":17026},"specifying-the-engines-property-in-packagejson","Specifying the ",[20,17029,17030],{},"engines"," property in ",[20,17033,17034],{},"package.json",[11,17036,17037],{},"Tell Heroku which Node version the project should run on. I was on 14, so I specified 14:",[86,17039,17041],{"className":5570,"code":17040,"language":5572,"meta":91,"style":91},"{\n  \"name\": \"projectName\",\n  \"version\": \"1.0.0\",\n  \"description\": \"Too cool for writing descriptions\",\n  \"engines\": {\n    \"node\": \"^14\"\n  }\n}\n",[20,17042,17043,17047,17058,17070,17082,17089,17099,17103],{"__ignoreMap":91},[95,17044,17045],{"class":97,"line":98},[95,17046,102],{"class":101},[95,17048,17049,17051,17053,17056],{"class":97,"line":105},[95,17050,15122],{"class":338},[95,17052,112],{"class":101},[95,17054,17055],{"class":115},"\"projectName\"",[95,17057,119],{"class":101},[95,17059,17060,17063,17065,17068],{"class":97,"line":122},[95,17061,17062],{"class":338},"  \"version\"",[95,17064,112],{"class":101},[95,17066,17067],{"class":115},"\"1.0.0\"",[95,17069,119],{"class":101},[95,17071,17072,17075,17077,17080],{"class":97,"line":134},[95,17073,17074],{"class":338},"  \"description\"",[95,17076,112],{"class":101},[95,17078,17079],{"class":115},"\"Too cool for writing descriptions\"",[95,17081,119],{"class":101},[95,17083,17084,17087],{"class":97,"line":147},[95,17085,17086],{"class":338},"  \"engines\"",[95,17088,5586],{"class":101},[95,17090,17091,17094,17096],{"class":97,"line":160},[95,17092,17093],{"class":338},"    \"node\"",[95,17095,112],{"class":101},[95,17097,17098],{"class":115},"\"^14\"\n",[95,17100,17101],{"class":97,"line":171},[95,17102,1757],{"class":101},[95,17104,17105],{"class":97,"line":882},[95,17106,174],{"class":101},[3471,17108,17110],{"id":17109},"responding-to-a-sigterm-signal","Responding to a SIGTERM signal",[11,17112,17113,17114,17117,17118,17121,17122,17124],{},"Heroku emits ",[20,17115,17116],{},"SIGTERM"," from time to time to tell the app that the ",[13742,17119,17120],{},"dyno"," (Heroku’s name for the container running your app) is about to restart. That happens about every 24 hours so the process can stay healthy. Roughly 30 seconds before restart, ",[20,17123,17116],{}," is sent—an alarm to finish in-flight requests and shut down gracefully.",[11,17126,17127],{},"In the file where you start the server, add this at the end:",[86,17129,17131],{"className":11329,"code":17130,"language":11331,"meta":91,"style":91},"const server = app.listen(port, () => {\n  \u002F\u002F ...\n})\n\nprocess.on('SIGTERM', () => {\n  console.log('SIGTERM received')\n  server.close(() => {\n    console.log('Process terminated')\n  })\n})\n",[20,17132,17133,17155,17159,17163,17167,17186,17199,17213,17226,17230],{"__ignoreMap":91},[95,17134,17135,17137,17140,17142,17145,17148,17151,17153],{"class":97,"line":98},[95,17136,335],{"class":201},[95,17138,17139],{"class":338}," server",[95,17141,341],{"class":201},[95,17143,17144],{"class":101}," app.",[95,17146,17147],{"class":108},"listen",[95,17149,17150],{"class":101},"(port, () ",[95,17152,350],{"class":201},[95,17154,353],{"class":101},[95,17156,17157],{"class":97,"line":105},[95,17158,4093],{"class":382},[95,17160,17161],{"class":97,"line":122},[95,17162,403],{"class":101},[95,17164,17165],{"class":97,"line":134},[95,17166,1731],{"emptyLinePlaceholder":1410},[95,17168,17169,17172,17175,17177,17180,17182,17184],{"class":97,"line":147},[95,17170,17171],{"class":101},"process.",[95,17173,17174],{"class":108},"on",[95,17176,1216],{"class":101},[95,17178,17179],{"class":115},"'SIGTERM'",[95,17181,2305],{"class":101},[95,17183,350],{"class":201},[95,17185,353],{"class":101},[95,17187,17188,17190,17192,17194,17197],{"class":97,"line":160},[95,17189,2168],{"class":101},[95,17191,2171],{"class":108},[95,17193,1216],{"class":101},[95,17195,17196],{"class":115},"'SIGTERM received'",[95,17198,1222],{"class":101},[95,17200,17201,17204,17207,17209,17211],{"class":97,"line":171},[95,17202,17203],{"class":101},"  server.",[95,17205,17206],{"class":108},"close",[95,17208,347],{"class":101},[95,17210,350],{"class":201},[95,17212,353],{"class":101},[95,17214,17215,17217,17219,17221,17224],{"class":97,"line":882},[95,17216,2530],{"class":101},[95,17218,2171],{"class":108},[95,17220,1216],{"class":101},[95,17222,17223],{"class":115},"'Process terminated'",[95,17225,1222],{"class":101},[95,17227,17228],{"class":97,"line":1788},[95,17229,398],{"class":101},[95,17231,17232],{"class":97,"line":1793},[95,17233,403],{"class":101},[11,17235,17236,17238],{},[20,17237,17116],{}," is just an event the process can listen for, then take an action such as closing the server. That’s the code change—we’re ready to deploy.",[3471,17240,17242],{"id":17241},"common-gotchas","Common gotchas",[10080,17244,17245,17248],{},[642,17246,17247],{},"Don’t hardcode the port. Heroku provides it as an environment variable, and the server must listen on that port.",[642,17249,17250,17251,73],{},"Either omit the hostname or set it to ",[20,17252,17253],{},"0.0.0.0",[86,17255,17257],{"className":11329,"code":17256,"language":11331,"meta":91,"style":91},"const port = process.env.PORT || 3000 \u002F\u002F Use this\n\u002F\u002F const port = 3000 \u002F\u002F Not this\n\napp.listen(port, '0.0.0.0', () => {\n  console.log(`Server is running on port ${port}`)\n})\n\n\u002F\u002F or don't specify the hostname at all\napp.listen(port, () => {\n  \u002F\u002F ...\n})\n",[20,17258,17259,17283,17291,17295,17313,17331,17335,17339,17344,17356,17360],{"__ignoreMap":91},[95,17260,17261,17263,17266,17268,17271,17274,17277,17280],{"class":97,"line":98},[95,17262,335],{"class":201},[95,17264,17265],{"class":338}," port",[95,17267,341],{"class":201},[95,17269,17270],{"class":101}," process.env.",[95,17272,17273],{"class":338},"PORT",[95,17275,17276],{"class":201}," ||",[95,17278,17279],{"class":338}," 3000",[95,17281,17282],{"class":382}," \u002F\u002F Use this\n",[95,17284,17285,17288],{"class":97,"line":105},[95,17286,17287],{"class":382},"\u002F\u002F const port = 3000",[95,17289,17290],{"class":382}," \u002F\u002F Not this\n",[95,17292,17293],{"class":97,"line":122},[95,17294,1731],{"emptyLinePlaceholder":1410},[95,17296,17297,17299,17301,17304,17307,17309,17311],{"class":97,"line":134},[95,17298,13861],{"class":101},[95,17300,17147],{"class":108},[95,17302,17303],{"class":101},"(port, ",[95,17305,17306],{"class":115},"'0.0.0.0'",[95,17308,2305],{"class":101},[95,17310,350],{"class":201},[95,17312,353],{"class":101},[95,17314,17315,17317,17319,17321,17324,17327,17329],{"class":97,"line":147},[95,17316,2168],{"class":101},[95,17318,2171],{"class":108},[95,17320,1216],{"class":101},[95,17322,17323],{"class":115},"`Server is running on port ${",[95,17325,17326],{"class":101},"port",[95,17328,4634],{"class":115},[95,17330,1222],{"class":101},[95,17332,17333],{"class":97,"line":160},[95,17334,403],{"class":101},[95,17336,17337],{"class":97,"line":171},[95,17338,1731],{"emptyLinePlaceholder":1410},[95,17340,17341],{"class":97,"line":882},[95,17342,17343],{"class":382},"\u002F\u002F or don't specify the hostname at all\n",[95,17345,17346,17348,17350,17352,17354],{"class":97,"line":1788},[95,17347,13861],{"class":101},[95,17349,17147],{"class":108},[95,17351,17150],{"class":101},[95,17353,350],{"class":201},[95,17355,353],{"class":101},[95,17357,17358],{"class":97,"line":1793},[95,17359,4093],{"class":382},[95,17361,17362],{"class":97,"line":1801},[95,17363,403],{"class":101},[75,17365,17367],{"id":17366},"deploying-our-application","Deploying our application",[3471,17369,17371],{"id":17370},"creating-a-new-app","Creating a new app",[11,17373,17374],{},"From the project folder, run:",[86,17376,17378],{"className":14010,"code":17377,"language":14012,"meta":91,"style":91},"heroku create\n",[20,17379,17380],{"__ignoreMap":91},[95,17381,17382,17385],{"class":97,"line":98},[95,17383,17384],{"class":108},"heroku",[95,17386,17387],{"class":115}," create\n",[8894,17389],{"alt":17390,"caption":17391,"src":17392},"VS Code terminal after heroku create showing app pure-ocean-63386 and its Git remote","heroku create assigns a random app name and adds a heroku Git remote.","\u002Fimages\u002Fblog\u002Fdeploying-a-node-js-application-using-heroku-and-git\u002Fheroku-create.png",[11,17394,17395,17396,17399,17400,17403],{},"Heroku generates a random name—mine was ",[20,17397,17398],{},"pure-ocean-63386","—and the app URL looks like ",[20,17401,17402],{},"https:\u002F\u002F\u003Cname-of-project>.herokuapp.com",". We’ll rename it later; that also updates the domain.",[11,17405,17406,17407,17409,17410,17413],{},"Heroku also adds a Git remote named ",[20,17408,17384],{}," (for me, ",[20,17411,17412],{},"https:\u002F\u002Fgit.heroku.com\u002Fpure-ocean-63386.git","). To deploy, push to that remote:",[86,17415,17417],{"className":14010,"code":17416,"language":14012,"meta":91,"style":91},"git push heroku master\n",[20,17418,17419],{"__ignoreMap":91},[95,17420,17421,17424,17427,17430],{"class":97,"line":98},[95,17422,17423],{"class":108},"git",[95,17425,17426],{"class":115}," push",[95,17428,17429],{"class":115}," heroku",[95,17431,17432],{"class":115}," master\n",[11,17434,17435],{},"After you run that, Heroku logs a lot to the terminal—installing dependencies, build succeeded, launching—and when it’s done the terminal looks something like this:",[8894,17437],{"alt":17438,"caption":17439,"src":17440},"VS Code terminal showing a successful Heroku build and release to pure-ocean-63386.herokuapp.com","A successful git push heroku master with Build succeeded and the live app URL.","\u002Fimages\u002Fblog\u002Fdeploying-a-node-js-application-using-heroku-and-git\u002Fgit-push-heroku.png",[11,17442,17443,17444,17447],{},"Open the URL (",[20,17445,17446],{},"https:\u002F\u002Fpure-ocean-63386.herokuapp.com"," for me). If you didn’t rely on environment variables and everything above is correct, the app should be live. If you get an application error page that says to check the logs, run:",[86,17449,17451],{"className":14010,"code":17450,"language":14012,"meta":91,"style":91},"heroku logs --tail\n",[20,17452,17453],{"__ignoreMap":91},[95,17454,17455,17457,17460],{"class":97,"line":98},[95,17456,17384],{"class":108},[95,17458,17459],{"class":115}," logs",[95,17461,17462],{"class":338}," --tail\n",[11,17464,17465,17466,17470,17471,17474],{},"If you ",[17467,17468,17469],"em",{},"did"," use environment variables, they’re probably undefined: we don’t push ",[20,17472,17473],{},".env"," files, so those values must be set on the Heroku dyno.",[3471,17476,17478],{"id":17477},"setting-environment-variables-on-heroku","Setting environment variables on Heroku",[11,17480,17481],{},"Set config vars from the terminal:",[86,17483,17485],{"className":14010,"code":17484,"language":14012,"meta":91,"style":91},"heroku config:set FIELD_NAME=VALUE\n# e.g. heroku config:set DBPASSWORD=9786ACCBG21IJK\n# heroku config:set NODE_ENV=production  # Heroku often sets this by default\n",[20,17486,17487,17497,17502],{"__ignoreMap":91},[95,17488,17489,17491,17494],{"class":97,"line":98},[95,17490,17384],{"class":108},[95,17492,17493],{"class":115}," config:set",[95,17495,17496],{"class":115}," FIELD_NAME=VALUE\n",[95,17498,17499],{"class":97,"line":105},[95,17500,17501],{"class":382},"# e.g. heroku config:set DBPASSWORD=9786ACCBG21IJK\n",[95,17503,17504],{"class":97,"line":122},[95,17505,17506],{"class":382},"# heroku config:set NODE_ENV=production  # Heroku often sets this by default\n",[11,17508,17509,17510,17512],{},"After code changes, stage and commit, push again to the ",[20,17511,17384],{}," remote, then open the URL. If it’s still broken, check the logs and debug from there.",[3471,17514,17516],{"id":17515},"renaming-the-application","Renaming the application",[11,17518,17519],{},"The random name isn’t related to the project. Rename it to something clearer:",[86,17521,17523],{"className":14010,"code":17522,"language":14012,"meta":91,"style":91},"heroku apps:rename \u003Cnewname>\n# heroku apps:rename akshattrivedi001\n",[20,17524,17525,17543],{"__ignoreMap":91},[95,17526,17527,17529,17532,17535,17538,17541],{"class":97,"line":98},[95,17528,17384],{"class":108},[95,17530,17531],{"class":115}," apps:rename",[95,17533,17534],{"class":201}," \u003C",[95,17536,17537],{"class":115},"newnam",[95,17539,17540],{"class":101},"e",[95,17542,553],{"class":201},[95,17544,17545],{"class":97,"line":105},[95,17546,17547],{"class":382},"# heroku apps:rename akshattrivedi001\n",[11,17549,17550,17551,17554,17555,73],{},"That renames the app (for example to ",[20,17552,17553],{},"akshattrivedi001",") and updates the domain to ",[20,17556,17557],{},"akshattrivedi001.herokuapp.com",[43,17559,17560],{},[11,17561,17562],{},"The name must be unique across Heroku.",[75,17564,17566],{"id":17565},"pros-and-cons-of-herokus-free-tier","Pros and cons of Heroku’s free tier",[11,17568,17569],{},"As noted above, this section reflects the free tier as it worked in 2021.",[3471,17571,17573],{"id":17572},"pros","Pros",[10080,17575,17576,17579,17582],{},[642,17577,17578],{},"Easy to deploy with Git.",[642,17580,17581],{},"No need to manage server configuration yourself.",[642,17583,17584],{},"Good for personal practice projects.",[3471,17586,17588],{"id":17587},"cons","Cons",[10080,17590,17591,17594],{},[642,17592,17593],{},"With no traffic for 30 minutes, Heroku put the app to sleep. The next visitor could wait 10–15 seconds for it to wake.",[642,17595,17596],{},"That cold start was the main free-tier downside—most people won’t wait ten seconds for a site to open.",[11,17598,17599],{},"That’s the deploy path. For a shortcut instead of copying the URL into a browser, open the app from the terminal with:",[86,17601,17603],{"className":14010,"code":17602,"language":14012,"meta":91,"style":91},"heroku open\n",[20,17604,17605],{"__ignoreMap":91},[95,17606,17607,17609],{"class":97,"line":98},[95,17608,17384],{"class":108},[95,17610,17611],{"class":115}," open\n",[11,17613,15337],{},[1383,17615,17616],{},"html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":91,"searchDepth":105,"depth":105,"links":17618},[17619,17624,17630,17635],{"id":13677,"depth":105,"text":13678,"children":17620},[17621,17622,17623],{"id":16971,"depth":122,"text":16972},{"id":16983,"depth":122,"text":16984},{"id":17004,"depth":122,"text":17005},{"id":17019,"depth":105,"text":17020,"children":17625},[17626,17628,17629],{"id":17026,"depth":122,"text":17627},"Specifying the engines property in package.json",{"id":17109,"depth":122,"text":17110},{"id":17241,"depth":122,"text":17242},{"id":17366,"depth":105,"text":17367,"children":17631},[17632,17633,17634],{"id":17370,"depth":122,"text":17371},{"id":17477,"depth":122,"text":17478},{"id":17515,"depth":122,"text":17516},{"id":17565,"depth":105,"text":17566,"children":17636},[17637,17638],{"id":17572,"depth":122,"text":17573},{"id":17587,"depth":122,"text":17588},"2021-09-27","Continue the Node.js series—prepare an Express app for Heroku, push with Git, set config vars, and understand free-tier tradeoffs from when this was written.",{"src":17642,"alt":17643},"\u002Fimages\u002Fblog\u002Fdeploying-a-node-js-application-using-heroku-and-git-cover.jpg","Getting started with Node.js cover showing deploy flow from local app through Git to Heroku",{},"\u002Fblog\u002Fdeploying-a-node-js-application-using-heroku-and-git",{"title":16927,"description":17640},"blog\u002Fdeploying-a-node-js-application-using-heroku-and-git",[15365,17649,17650,17651,17652],"Heroku","Git","Deployment","Getting Started","TSKTtiMs_I241dtZkQvoB5n0rVsk19C9mkKG6LePBDI",{"id":17655,"title":17656,"body":17657,"date":17754,"description":17755,"draft":1404,"extension":1405,"featured":1404,"image":17756,"meta":17759,"navigation":1410,"path":17760,"seo":17761,"stem":17762,"tags":17763,"updatedAt":1420,"__hash__":17766},"blog\u002Fblog\u002Fintroduction-to-front-end-development-what-is-html-and-css.md","Introduction to Front-end Development: What Is HTML and CSS?",{"type":8,"value":17658,"toc":17747},[17659,17664,17667,17673,17677,17680,17685,17688,17691,17700,17704,17707,17712,17716,17719,17724,17727,17730,17734,17737,17742,17745],[43,17660,17661],{},[11,17662,17663],{},"I wrote this article about a year before publishing it, but I lost the original images. I finally remade them and put this out.",[11,17665,17666],{},"Web development can be divided into two parts: front-end and back-end. Front-end focuses on the client side of a website—the appearance and interactivity of a page. At a high level, it mainly consists of HTML, CSS, and JavaScript. This article covers the basics of front-end development.",[11,17668,59,17669,73],{},[61,17670,66],{"href":17671,"rel":17672},"https:\u002F\u002Fakshattriv.medium.com\u002Fintroduction-to-front-end-development-what-is-html-and-css-871d1f3c29ed",[65],[75,17674,17676],{"id":17675},"what-front-end-development-really-is","What front-end development really is",[11,17678,17679],{},"Everything you can see on a website or webpage falls under front-end development. We call it the front end because it is the front of a website—what’s visible to the user.",[8894,17681],{"alt":17682,"caption":17683,"src":17684},"Diagram of browser, server, and database with HTML, CSS, and JavaScript on the browser side of the request–response cycle","The browser uses HTML, CSS, and JavaScript to display the page after the server responds.","\u002Fimages\u002Fblog\u002Fintroduction-to-front-end-development-what-is-html-and-css\u002Ffrontend-visible-layer.png",[11,17686,17687],{},"When you enter a URL, a request goes to the server, and the server sends a response—usually an HTML document. The browser reads that markup and the linked files. Based on the HTML, CSS, and JavaScript, it decides which elements appear on the page, in what order, how they look, and what happens on clicks, keypresses, and other events. Creating that HTML, CSS, and JavaScript is front-end development.",[11,17689,17690],{},"If you want a deeper walkthrough of the request–response path, start here:",[43,17692,17693],{},[11,17694,17695,17696],{},"Related: ",[61,17697,17699],{"href":17698},"\u002Fblog\u002Fwhat-happens-when-you-visit-a-webpage","What Happens When You Visit a Webpage?",[75,17701,17703],{"id":17702},"what-is-html","What is HTML?",[11,17705,17706],{},"HTML stands for Hypertext Markup Language. It is one of the three languages a browser understands. We use it to define the structure of a page and the elements that appear on it. We also give the browser metadata and point it at linked files that affect the page—stylesheets and scripts.",[8894,17708],{"alt":17709,"caption":17710,"src":17711},"Orange graphic with a white document-shaped HTML badge","HTML defines structure and the elements on the page.","\u002Fimages\u002Fblog\u002Fintroduction-to-front-end-development-what-is-html-and-css\u002Fhtml-structure.png",[3471,17713,17715],{"id":17714},"understanding-the-html","Understanding the HTML",[11,17717,17718],{},"In the image below you can see how HTML is written to create a specific layout.",[8894,17720],{"alt":17721,"caption":17722,"src":17723},"Medium editor navigation bar above the HTML nav markup that produces it","A rendered nav bar and the HTML tags that describe its structure.","\u002Fimages\u002Fblog\u002Fintroduction-to-front-end-development-what-is-html-and-css\u002Fhtml-to-rendered-page.png",[11,17725,17726],{},"Remember: the design shown above is the result of HTML and CSS together. HTML alone will not produce the same look. We generally do not style elements in HTML—we use CSS for that.",[11,17728,17729],{},"I keep saying “elements” without defining the word. Everything on a webpage is an element: a site header is a header element, a footer is a footer element, and so on. We write those elements in the HTML document as tags.",[75,17731,17733],{"id":17732},"what-is-css","What is CSS?",[11,17735,17736],{},"CSS stands for Cascading Style Sheets. The name says a lot: these are stylesheets—files that hold styling for our HTML elements. Styling means defining property–value pairs on those elements, and those pairs control how they appear on the page.",[8894,17738],{"alt":17739,"caption":17740,"src":17741},"Code editor showing a nav CSS rule with width, height, background-color, max-width, and color","CSS applies property–value pairs to HTML elements to control appearance.","\u002Fimages\u002Fblog\u002Fintroduction-to-front-end-development-what-is-html-and-css\u002Fcss-properties.png",[11,17743,17744],{},"In that example we set properties on HTML elements—common ones like height, width, and background color. There are many more, and you won’t believe what CSS can do until you try it yourself.",[11,17746,15337],{},{"title":91,"searchDepth":105,"depth":105,"links":17748},[17749,17750,17753],{"id":17675,"depth":105,"text":17676},{"id":17702,"depth":105,"text":17703,"children":17751},[17752],{"id":17714,"depth":122,"text":17715},{"id":17732,"depth":105,"text":17733},"2021-08-19","A beginner-friendly look at front-end development—what the client side is, how HTML structures a page, and how CSS styles it.",{"src":17757,"alt":17758},"\u002Fimages\u002Fblog\u002Fintroduction-to-front-end-development-what-is-html-and-css-cover.jpg","Introduction to front-end development cover with HTML and CSS logos beside a browser and code editor",{},"\u002Fblog\u002Fintroduction-to-front-end-development-what-is-html-and-css",{"title":17656,"description":17755},"blog\u002Fintroduction-to-front-end-development-what-is-html-and-css",[17764,17765,10813,17652,3681],"Front-end","HTML","C0EVu460ttuSWnAUr06fP_rRPCp2qTge9vl60O_yDtE",{"id":17768,"title":16968,"body":17769,"date":18243,"description":18244,"draft":1404,"extension":1405,"featured":1404,"image":18245,"meta":18248,"navigation":1410,"path":16967,"seo":18249,"stem":18250,"tags":18251,"updatedAt":1420,"__hash__":18254},"blog\u002Fblog\u002Finitializing-git-and-pushing-code-to-a-remote-repository.md",{"type":8,"value":17770,"toc":18235},[17771,17774,17784,17788,17791,17794,17797,17800,17804,17813,17830,17833,17838,17845,17848,17855,17860,17883,17886,17891,17894,17898,17901,17904,17916,17919,17922,17965,17969,17972,17984,17991,18005,18022,18033,18037,18042,18045,18060,18063,18077,18080,18083,18101,18105,18108,18125,18132,18136,18139,18158,18161,18176,18195,18198,18201,18215,18218,18232],[11,17772,17773],{},"So one day I was watching a YouTube video and there was this guy developing a web app. At the time I didn’t know much about development, so the first thing he did was type something into his terminal—and boom, lines started appearing and his whole codebase downloaded from somewhere on the internet. My jaw dropped. That was around three years ago. If you’ve had that moment too, stick with me: in this article I’ll cover what Git and GitHub are, how we use them to manage projects, and why they matter.",[11,17775,59,17776,67,17780,73],{},[61,17777,66],{"href":17778,"rel":17779},"https:\u002F\u002Fmedium.com\u002Ffaun\u002Finitializing-git-and-pushing-code-to-a-remote-repository-getting-started-with-git-and-github-f8ffa9785ecc",[65],[61,17781,16954],{"href":17782,"rel":17783},"https:\u002F\u002Ffaun.pub\u002Finitializing-git-and-pushing-code-to-a-remote-repository-getting-started-with-git-and-github-f8ffa9785ecc",[65],[75,17785,17787],{"id":17786},"whats-git-and-github","What’s Git and GitHub?",[11,17789,17790],{},"Git is version-control software. It’s used for tracking changes in a set of files—like the files in your codebase when you’re building an application—and is usually used so developers can coordinate work while building source code together.",[11,17792,17793],{},"Let me make that a bit simpler. Imagine you have four people on a team building a website. How do you work on the project at the same time? One option is to zip all the files, email the zip, then the next person makes changes, zips again, and sends it back. To avoid chaos, only one person can edit at a time—and even then it’s exhausting. Every small change means zipping everything and shipping it over email. That’s too much work for a developer.",[11,17795,17796],{},"That’s the need for Git and GitHub. Think of GitHub as an online folder where you keep your code. Anyone on the team can pull the code, make changes, commit them, and push back to that online folder. The next time you look, you can see who edited the code and what they changed or added.",[11,17798,17799],{},"Git and GitHub are tools that make development easier.",[75,17801,17803],{"id":17802},"sign-up-on-github","Sign up on GitHub",[11,17805,17806,17807,17812],{},"That’s the first thing to do. Visit ",[61,17808,17811],{"href":17809,"rel":17810},"https:\u002F\u002Fgithub.com",[65],"github.com",", click Sign up, then:",[10080,17814,17815,17818,17821,17824,17827],{},[642,17816,17817],{},"Enter your email",[642,17819,17820],{},"Set a password",[642,17822,17823],{},"Set a username",[642,17825,17826],{},"Solve a simple captcha puzzle",[642,17828,17829],{},"Verify your email",[11,17831,17832],{},"That’s it. You’ll be redirected to your GitHub dashboard.",[8894,17834],{"alt":17835,"caption":17836,"src":17837},"GitHub dashboard with a Create repository button in the left sidebar","A new GitHub account dashboard, ready to create the first repository.","\u002Fimages\u002Fblog\u002Finitializing-git-and-pushing-code-to-a-remote-repository\u002Fgithub-dashboard.png",[11,17839,17840,17841,17844],{},"As covered above, GitHub is where we store project code so we can access it from anywhere and share it with the world. We do that by creating ",[13742,17842,17843],{},"repositories","—think of them as folders that hold the files for each project.",[11,17846,17847],{},"If you have two projects, you create two repositories, and each project’s files live in its own repo.",[11,17849,17850,17851,17854],{},"Create a repo by clicking ",[13742,17852,17853],{},"Create repository"," on the left. You’ll get a form that looks something like this:",[8894,17856],{"alt":17857,"caption":17858,"src":17859},"GitHub Create a new repository form with name, description, and public or private options","The Create a new repository form on GitHub.","\u002Fimages\u002Fblog\u002Finitializing-git-and-pushing-code-to-a-remote-repository\u002Fcreate-repository.png",[10080,17861,17862,17869,17872,17875,17878],{},[642,17863,17864,17865,17868],{},"Give your repo a name. It must be unique among ",[17467,17866,17867],{},"your"," repositories—no two of yours can share a name.",[642,17870,17871],{},"Leave the description blank if you want; it’s optional.",[642,17873,17874],{},"Choose public (anyone can see and copy the code, but they can’t edit the repo without your permission) or private. Prefer public if you want to showcase the work.",[642,17876,17877],{},"Don’t check any of the boxes below; we’ll handle those later.",[642,17879,17880,17881,73],{},"Hit ",[13742,17882,17853],{},[11,17884,17885],{},"A page like the one below will appear:",[8894,17887],{"alt":17888,"caption":17889,"src":17890},"Empty GitHub repository quick setup page with HTTPS remote URL and command-line instructions","Quick setup page for a new empty repository, including the remote URL.","\u002Fimages\u002Fblog\u002Finitializing-git-and-pushing-code-to-a-remote-repository\u002Fempty-repo-setup.png",[11,17892,17893],{},"Leave this tab open—we’ll come back to it soon. First, download and install Git on your machine.",[75,17895,17897],{"id":17896},"installing-git","Installing Git",[11,17899,17900],{},"Search for “download Git,” open the first result, and download the installer for your operating system. Run the installer—keep clicking Next, then Install.",[11,17902,17903],{},"To check that Git installed correctly, run this in your terminal:",[86,17905,17907],{"className":14010,"code":17906,"language":14012,"meta":91,"style":91},"git --version\n",[20,17908,17909],{"__ignoreMap":91},[95,17910,17911,17913],{"class":97,"line":98},[95,17912,17423],{"class":108},[95,17914,17915],{"class":338}," --version\n",[11,17917,17918],{},"That prints the installed Git version.",[11,17920,17921],{},"Next, add your GitHub account details to the local Git config:",[86,17923,17925],{"className":14010,"code":17924,"language":14012,"meta":91,"style":91},"git config --global user.name \"Your Name\"\n# e.g. git config --global user.name \"Akshat Trivedi\"\n\ngit config --global user.email \"you@example.com\"\n",[20,17926,17927,17942,17947,17951],{"__ignoreMap":91},[95,17928,17929,17931,17933,17936,17939],{"class":97,"line":98},[95,17930,17423],{"class":108},[95,17932,7944],{"class":115},[95,17934,17935],{"class":338}," --global",[95,17937,17938],{"class":115}," user.name",[95,17940,17941],{"class":115}," \"Your Name\"\n",[95,17943,17944],{"class":97,"line":105},[95,17945,17946],{"class":382},"# e.g. git config --global user.name \"Akshat Trivedi\"\n",[95,17948,17949],{"class":97,"line":122},[95,17950,1731],{"emptyLinePlaceholder":1410},[95,17952,17953,17955,17957,17959,17962],{"class":97,"line":134},[95,17954,17423],{"class":108},[95,17956,7944],{"class":115},[95,17958,17935],{"class":338},[95,17960,17961],{"class":115}," user.email",[95,17963,17964],{"class":115}," \"you@example.com\"\n",[75,17966,17968],{"id":17967},"adding-git-to-a-project","Adding Git to a project",[11,17970,17971],{},"To initialize a project as a Git repository, run this from inside your project folder:",[86,17973,17975],{"className":14010,"code":17974,"language":14012,"meta":91,"style":91},"git init\n",[20,17976,17977],{"__ignoreMap":91},[95,17978,17979,17981],{"class":97,"line":98},[95,17980,17423],{"class":108},[95,17982,17983],{"class":115}," init\n",[11,17985,17986,17987,17990],{},"It’s similar to running ",[20,17988,17989],{},"npm init"," to start a Node project.",[11,17992,17993,17994,17997,17998,18000,18001,18004],{},"After that, create a file named ",[20,17995,17996],{},".gitignore",". It lists files and folders you don’t want Git to track—for example ",[20,17999,17473],{}," files and the ",[20,18002,18003],{},"node_modules"," folder.",[86,18006,18010],{"className":18007,"code":18008,"language":18009,"meta":91,"style":91},"language-gitignore shiki shiki-themes github-light github-dark","node_modules\u002F\n*.env\n","gitignore",[20,18011,18012,18017],{"__ignoreMap":91},[95,18013,18014],{"class":97,"line":98},[95,18015,18016],{},"node_modules\u002F\n",[95,18018,18019],{"class":97,"line":105},[95,18020,18021],{},"*.env\n",[11,18023,18024,18025,18027,18028,18030,18031,73],{},"Put that in ",[20,18026,17996],{}," for a Node project. With it in place, everything under ",[20,18029,18003],{}," is ignored, along with any file whose name ends in ",[20,18032,17473],{},[75,18034,18036],{"id":18035},"staging-and-committing-files","Staging and committing files",[8894,18038],{"alt":18039,"caption":18040,"src":18041},"Diagram of Working directory, Staging area, and repository with git add and git commit arrows","How changes move from the working directory to the staging area, then into the repository.","\u002Fimages\u002Fblog\u002Finitializing-git-and-pushing-code-to-a-remote-repository\u002Fgit-workflow.png",[11,18043,18044],{},"Here’s the basic flow. After you edit project files, you add them to the staging area—and you do that for each file in the working directory once you’re ready.",[86,18046,18048],{"className":14010,"code":18047,"language":14012,"meta":91,"style":91},"git add app.js\n",[20,18049,18050],{"__ignoreMap":91},[95,18051,18052,18054,18057],{"class":97,"line":98},[95,18053,17423],{"class":108},[95,18055,18056],{"class":115}," add",[95,18058,18059],{"class":115}," app.js\n",[11,18061,18062],{},"That’s how you stage one file. Staging every file one by one gets tiring, so use this to stage all untracked changes at once (files that were edited and have changes Git doesn’t know about yet):",[86,18064,18066],{"className":14010,"code":18065,"language":14012,"meta":91,"style":91},"git add .\n",[20,18067,18068],{"__ignoreMap":91},[95,18069,18070,18072,18074],{"class":97,"line":98},[95,18071,17423],{"class":108},[95,18073,18056],{"class":115},[95,18075,18076],{"class":115}," .\n",[11,18078,18079],{},"That adds every untracked file—or every file with untracked changes—to the staging area.",[11,18081,18082],{},"Once your edits are staged and you want Git to record them, commit with:",[86,18084,18086],{"className":14010,"code":18085,"language":14012,"meta":91,"style":91},"git commit -m \"message you want to give to this commit\"\n",[20,18087,18088],{"__ignoreMap":91},[95,18089,18090,18092,18095,18098],{"class":97,"line":98},[95,18091,17423],{"class":108},[95,18093,18094],{"class":115}," commit",[95,18096,18097],{"class":338}," -m",[95,18099,18100],{"class":115}," \"message you want to give to this commit\"\n",[75,18102,18104],{"id":18103},"adding-a-remote-repository","Adding a remote repository",[11,18106,18107],{},"Remember the repository we created on GitHub? Now we add that remote to the local project and push our code.",[86,18109,18111],{"className":14010,"code":18110,"language":14012,"meta":91,"style":91},"git remote add [name_you_want_to_give_this_repo] [link_to_your_remote_repo]\n",[20,18112,18113],{"__ignoreMap":91},[95,18114,18115,18117,18120,18122],{"class":97,"line":98},[95,18116,17423],{"class":108},[95,18118,18119],{"class":115}," remote",[95,18121,18056],{"class":115},[95,18123,18124],{"class":101}," [name_you_want_to_give_this_repo] [link_to_your_remote_repo]\n",[11,18126,18127,18128,18131],{},"You can name the remote whatever you like, but ",[20,18129,18130],{},"origin"," is the conventional name. Earlier I asked you to leave the new-repo tab open—go back to it now.",[8894,18133],{"alt":18134,"caption":18135,"src":17890},"Empty GitHub repository page highlighting the HTTPS remote URL for github_demo","Copy the HTTPS URL from the empty repository page to use as your remote.",[11,18137,18138],{},"In that screenshot you’ll see a link—that’s the URL of your remote repository. Copy it into the command. It ends up looking like this:",[86,18140,18142],{"className":14010,"code":18141,"language":14012,"meta":91,"style":91},"git remote add origin https:\u002F\u002Fgithub.com\u002Fakshat-triv\u002Fgithub_demo.git\n",[20,18143,18144],{"__ignoreMap":91},[95,18145,18146,18148,18150,18152,18155],{"class":97,"line":98},[95,18147,17423],{"class":108},[95,18149,18119],{"class":115},[95,18151,18056],{"class":115},[95,18153,18154],{"class":115}," origin",[95,18156,18157],{"class":115}," https:\u002F\u002Fgithub.com\u002Fakshat-triv\u002Fgithub_demo.git\n",[11,18159,18160],{},"That’s the setup. With your files already committed, push those commits to the remote with:",[86,18162,18164],{"className":14010,"code":18163,"language":14012,"meta":91,"style":91},"git push origin master\n",[20,18165,18166],{"__ignoreMap":91},[95,18167,18168,18170,18172,18174],{"class":97,"line":98},[95,18169,17423],{"class":108},[95,18171,17426],{"class":115},[95,18173,18154],{"class":115},[95,18175,17432],{"class":115},[11,18177,18178,18180,18181,18184,18185,18188,18189,18191,18192,18194],{},[20,18179,18130],{}," is the remote you’re pushing to, and ",[20,18182,18183],{},"master"," is your current branch. Don’t worry too much about branches yet—we can cover that in another article. (Newer GitHub repos often use ",[20,18186,18187],{},"main"," instead of ",[20,18190,18183],{},"; if yours does, push to ",[20,18193,18187],{},".)",[11,18196,18197],{},"That’s it—congratulations on your first push. The ongoing loop is the same: write code, stage the files when a change is ready, commit, and push those commits to the remote when you’re done.",[11,18199,18200],{},"One more useful command:",[86,18202,18204],{"className":14010,"code":18203,"language":14012,"meta":91,"style":91},"git clone https:\u002F\u002Fgithub.com\u002Fakshat-triv\u002Fgithub_demo.git\n",[20,18205,18206],{"__ignoreMap":91},[95,18207,18208,18210,18213],{"class":97,"line":98},[95,18209,17423],{"class":108},[95,18211,18212],{"class":115}," clone",[95,18214,18157],{"class":115},[11,18216,18217],{},"That’s how you clone a remote repository onto your machine—and that’s the command the developer in the YouTube video used at the start of this story.",[11,18219,18220,18221,15492,18226,18231],{},"Thanks for reading. There’s a lot here; if something’s unclear, dig into the official ",[61,18222,18225],{"href":18223,"rel":18224},"https:\u002F\u002Fgit-scm.com\u002Fdoc",[65],"Git documentation",[61,18227,18230],{"href":18228,"rel":18229},"https:\u002F\u002Fdocs.github.com\u002F",[65],"GitHub Docs"," while you practice the commands above.",[1383,18233,18234],{},"html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":91,"searchDepth":105,"depth":105,"links":18236},[18237,18238,18239,18240,18241,18242],{"id":17786,"depth":105,"text":17787},{"id":17802,"depth":105,"text":17803},{"id":17896,"depth":105,"text":17897},{"id":17967,"depth":105,"text":17968},{"id":18035,"depth":105,"text":18036},{"id":18103,"depth":105,"text":18104},"2021-08-15","Get started with Git and GitHub—create a repository, install Git, stage and commit changes, add a remote, and push your first project.",{"src":18246,"alt":18247},"\u002Fimages\u002Fblog\u002Finitializing-git-and-pushing-code-to-a-remote-repository-cover.jpg","Getting started with Git and GitHub cover showing init, add, commit, and push commands",{},{"title":16968,"description":18244},"blog\u002Finitializing-git-and-pushing-code-to-a-remote-repository",[17650,18252,17652,18253],"GitHub","Version Control","l1bPx8SY8CT7hv3na83rce1p4RJYRogPD69nlpb5GEY",{"id":18256,"title":18257,"body":18258,"date":19019,"description":19020,"draft":1404,"extension":1405,"featured":1404,"image":19021,"meta":19024,"navigation":1410,"path":19025,"seo":19026,"stem":19027,"tags":19028,"updatedAt":1420,"__hash__":19029},"blog\u002Fblog\u002Fsending-html-file-in-response-serving-static-files-using-express.md","Sending an HTML File in Response and Serving Static Files with Express",{"type":8,"value":18259,"toc":19009},[18260,18263,18271,18281,18291,18417,18423,18428,18444,18450,18458,18467,18472,18479,18497,18506,18525,18545,18551,18646,18649,18661,18668,18673,18676,18681,18684,18691,18700,18727,18734,18739,18744,18747,18771,18774,18796,18805,18814,18820,19003,19006],[11,18261,18262],{},"Hi there—in this article I’ll show you how to send an HTML file in response to a request and how to serve static files from your Node server. This continues the previous piece on building a simple server; if you haven’t read that yet, start here:",[43,18264,18265],{},[11,18266,16964,18267],{},[61,18268,18270],{"href":18269},"\u002Fblog\u002Fcreating-a-simple-node-js-server-using-express","Creating a Simple Node.js Server Using Express",[11,18272,59,18273,67,18277,73],{},[61,18274,66],{"href":18275,"rel":18276},"https:\u002F\u002Fmedium.com\u002Ffaun\u002Fsending-html-file-in-response-serving-static-files-using-express-js-getting-started-with-node-js-5b43b5718d94",[65],[61,18278,16954],{"href":18279,"rel":18280},"https:\u002F\u002Ffaun.pub\u002Fsending-html-file-in-response-serving-static-files-using-express-js-getting-started-with-node-js-5b43b5718d94",[65],[11,18282,18283,18284,18286,18287,18290],{},"This is how ",[20,18285,13813],{}," looked at the end of the previous article—nothing fancy: Express is initialized, a server listens on port 9000, and a GET handler on ",[20,18288,18289],{},"\u002F"," sends back a message.",[86,18292,18294],{"className":11329,"code":18293,"language":11331,"meta":91,"style":91},"const express = require('express')\nconst app = express()\n\napp.get('\u002F', (req, res) => {\n  res.end('Hi there, Server is working.')\n})\n\napp.listen(9000, '127.0.0.1', () => {\n  console.log('Server is running on port 9000')\n})\n",[20,18295,18296,18314,18327,18331,18356,18370,18374,18378,18400,18413],{"__ignoreMap":91},[95,18297,18298,18300,18303,18305,18307,18309,18312],{"class":97,"line":98},[95,18299,335],{"class":201},[95,18301,18302],{"class":338}," express",[95,18304,341],{"class":201},[95,18306,14050],{"class":108},[95,18308,1216],{"class":101},[95,18310,18311],{"class":115},"'express'",[95,18313,1222],{"class":101},[95,18315,18316,18318,18321,18323,18325],{"class":97,"line":105},[95,18317,335],{"class":201},[95,18319,18320],{"class":338}," app",[95,18322,341],{"class":201},[95,18324,18302],{"class":108},[95,18326,1726],{"class":101},[95,18328,18329],{"class":97,"line":122},[95,18330,1731],{"emptyLinePlaceholder":1410},[95,18332,18333,18335,18337,18339,18342,18344,18346,18348,18350,18352,18354],{"class":97,"line":134},[95,18334,13861],{"class":101},[95,18336,11126],{"class":108},[95,18338,1216],{"class":101},[95,18340,18341],{"class":115},"'\u002F'",[95,18343,13872],{"class":101},[95,18345,13875],{"class":370},[95,18347,909],{"class":101},[95,18349,13880],{"class":370},[95,18351,373],{"class":101},[95,18353,350],{"class":201},[95,18355,353],{"class":101},[95,18357,18358,18360,18363,18365,18368],{"class":97,"line":147},[95,18359,13900],{"class":101},[95,18361,18362],{"class":108},"end",[95,18364,1216],{"class":101},[95,18366,18367],{"class":115},"'Hi there, Server is working.'",[95,18369,1222],{"class":101},[95,18371,18372],{"class":97,"line":160},[95,18373,403],{"class":101},[95,18375,18376],{"class":97,"line":171},[95,18377,1731],{"emptyLinePlaceholder":1410},[95,18379,18380,18382,18384,18386,18389,18391,18394,18396,18398],{"class":97,"line":882},[95,18381,13861],{"class":101},[95,18383,17147],{"class":108},[95,18385,1216],{"class":101},[95,18387,18388],{"class":338},"9000",[95,18390,909],{"class":101},[95,18392,18393],{"class":115},"'127.0.0.1'",[95,18395,2305],{"class":101},[95,18397,350],{"class":201},[95,18399,353],{"class":101},[95,18401,18402,18404,18406,18408,18411],{"class":97,"line":1788},[95,18403,2168],{"class":101},[95,18405,2171],{"class":108},[95,18407,1216],{"class":101},[95,18409,18410],{"class":115},"'Server is running on port 9000'",[95,18412,1222],{"class":101},[95,18414,18415],{"class":97,"line":1793},[95,18416,403],{"class":101},[75,18418,18420,18421],{"id":18419},"serving-a-portfolio-from","Serving a portfolio from ",[20,18422,18289],{},[11,18424,18425,18426,73],{},"Say you’ve built a portfolio: an HTML file, CSS, images, and maybe a JS file. We’ll serve that portfolio when someone hits ",[20,18427,18289],{},[11,18429,18430,18431,18433,18434,18436,18437,18439,18440,18443],{},"Put ",[20,18432,13824],{}," in your Node app’s root directory (the project folder that contains ",[20,18435,17034],{},"). Create a ",[20,18438,14517],{}," folder for CSS, JS, images, and any other static assets the markup needs. After that, the folder structure looks something like this—",[20,18441,18442],{},"first_node_server"," is the project folder name:",[86,18445,18448],{"className":18446,"code":18447,"language":632,"meta":91},[630],"first_node_server\u002F\n├── app.js\n├── index.html\n├── package.json\n└── public\u002F\n    ├── css\u002F\n    ├── fav\u002F        # favicons\n    ├── jquery\u002F     # jQuery scripts used by the portfolio\n    ├── images\u002F\n    └── svg\u002F\n",[20,18449,18447],{"__ignoreMap":91},[11,18451,18452,18453,18455,18456,73],{},"We have the files in place. Next, send ",[20,18454,13824],{}," when a user visits ",[20,18457,18289],{},[75,18459,18461,18462,10178,18464],{"id":18460},"sending-indexhtml-with-ressendfile","Sending ",[20,18463,13824],{},[20,18465,18466],{},"res.sendFile",[8894,18468],{"alt":18469,"caption":18470,"src":18471},"VS Code showing app.js with a GET route that calls res.sendFile for index.html","app.js after switching the `\u002F` handler to res.sendFile.","\u002Fimages\u002Fblog\u002Fsending-html-file-in-response-serving-static-files-using-express\u002Fapp-js-sendfile.png",[11,18473,18474,18475,18478],{},"All of the changes land in the ",[20,18476,18477],{},"app.get"," callback. Before, we were sending a string with:",[86,18480,18482],{"className":11329,"code":18481,"language":11331,"meta":91,"style":91},"res.end('Hi there, Server is working.')\n",[20,18483,18484],{"__ignoreMap":91},[95,18485,18486,18489,18491,18493,18495],{"class":97,"line":98},[95,18487,18488],{"class":101},"res.",[95,18490,18362],{"class":108},[95,18492,1216],{"class":101},[95,18494,18367],{"class":115},[95,18496,1222],{"class":101},[11,18498,18499,18501,18502,18505],{},[20,18500,18362],{}," is a method on the response object (the second parameter of the route callback). Express also gives us ",[20,18503,18504],{},"sendFile",", which sends a file from the server to the client.",[11,18507,18508,18510,18511,18514,18515,18518,18519,18521,18522,18524],{},[20,18509,18504],{}," takes the file path as its first argument. A template string like ",[20,18512,18513],{},"`${__dirname}\u002Findex.html`"," is the path to the HTML file. You might wonder what ",[20,18516,18517],{},"__dirname"," is—we never defined it. Every JS file in Node gets a few predefined variables, and ",[20,18520,18517],{}," is one of them. Digging into how Node compiles and runs modules could fill another article; for now, remember that ",[20,18523,18517],{}," is the absolute path of the directory containing the current module.",[11,18526,18527,18528,18188,18530,18533,18534,73],{},"There’s also a reason we use ",[20,18529,18513],{},[20,18531,18532],{},".\u002Findex.html",". That’s worth its own write-up; until then, this GeeksforGeeks piece covers the difference: ",[61,18535,18538,18539,11033,18541,18544],{"href":18536,"rel":18537},"https:\u002F\u002Fwww.geeksforgeeks.org\u002Fnode-js\u002Fdifference-between-__dirname-and-in-node-js\u002F",[65],"Difference between ",[20,18540,18517],{},[20,18542,18543],{},".\u002F"," in Node.js",[11,18546,18547,18548,18550],{},"Back to the route: the second argument to ",[20,18549,18504],{}," is an error callback that runs after Express tries to send the file. If something goes wrong, we log the error and send the client a message:",[86,18552,18554],{"className":11329,"code":18553,"language":11331,"meta":91,"style":91},"app.get('\u002F', (req, res) => {\n  res.sendFile(`${__dirname}\u002Findex.html`, (err) => {\n    if (err) {\n      console.log(err)\n      res.end(err.message)\n    }\n  })\n})\n",[20,18555,18556,18580,18607,18614,18624,18634,18638,18642],{"__ignoreMap":91},[95,18557,18558,18560,18562,18564,18566,18568,18570,18572,18574,18576,18578],{"class":97,"line":98},[95,18559,13861],{"class":101},[95,18561,11126],{"class":108},[95,18563,1216],{"class":101},[95,18565,18341],{"class":115},[95,18567,13872],{"class":101},[95,18569,13875],{"class":370},[95,18571,909],{"class":101},[95,18573,13880],{"class":370},[95,18575,373],{"class":101},[95,18577,350],{"class":201},[95,18579,353],{"class":101},[95,18581,18582,18584,18586,18588,18591,18593,18596,18598,18601,18603,18605],{"class":97,"line":105},[95,18583,13900],{"class":101},[95,18585,18504],{"class":108},[95,18587,1216],{"class":101},[95,18589,18590],{"class":115},"`${",[95,18592,18517],{"class":101},[95,18594,18595],{"class":115},"}\u002Findex.html`",[95,18597,13872],{"class":101},[95,18599,18600],{"class":370},"err",[95,18602,373],{"class":101},[95,18604,350],{"class":201},[95,18606,353],{"class":101},[95,18608,18609,18611],{"class":97,"line":122},[95,18610,2751],{"class":201},[95,18612,18613],{"class":101}," (err) {\n",[95,18615,18616,18619,18621],{"class":97,"line":134},[95,18617,18618],{"class":101},"      console.",[95,18620,2171],{"class":108},[95,18622,18623],{"class":101},"(err)\n",[95,18625,18626,18629,18631],{"class":97,"line":147},[95,18627,18628],{"class":101},"      res.",[95,18630,18362],{"class":108},[95,18632,18633],{"class":101},"(err.message)\n",[95,18635,18636],{"class":97,"line":160},[95,18637,2770],{"class":101},[95,18639,18640],{"class":97,"line":171},[95,18641,398],{"class":101},[95,18643,18644],{"class":97,"line":882},[95,18645,403],{"class":101},[11,18647,18648],{},"Run the app:",[86,18650,18652],{"className":14010,"code":18651,"language":14012,"meta":91,"style":91},"node app.js\n",[20,18653,18654],{"__ignoreMap":91},[95,18655,18656,18659],{"class":97,"line":98},[95,18657,18658],{"class":108},"node",[95,18660,18059],{"class":115},[11,18662,18663,18664,18667],{},"Then open ",[20,18665,18666],{},"127.0.0.1:9000"," in the browser.",[8894,18669],{"alt":18670,"caption":18671,"src":18672},"Browser at 127.0.0.1:9000 showing an unstyled portfolio with broken images","HTML arrives, but styles and images are missing.","\u002Fimages\u002Fblog\u002Fsending-html-file-in-response-serving-static-files-using-express\u002Fbrowser-html-without-assets.png",[11,18674,18675],{},"You must be thinking, what’s going on? CSS isn’t working, images aren’t working. Open the console and you’ll see errors like the ones below—",[8894,18677],{"alt":18678,"caption":18679,"src":18680},"Browser DevTools console listing 404s for CSS, jQuery, images, and other static files","Static asset requests failing with 404 before express.static is configured.","\u002Fimages\u002Fblog\u002Fsending-html-file-in-response-serving-static-files-using-express\u002Fconsole-static-404-errors.png",[11,18682,18683],{},"For every static file the client requests, the server responded with 404 Not Found. That happens because we still need to tell our Node\u002FExpress app where those static files live on disk.",[75,18685,18687,18688],{"id":18686},"serving-static-files-with-expressstatic","Serving static files with ",[20,18689,18690],{},"express.static",[11,18692,18693,18694,18697,18698,15571],{},"Add this line ",[13742,18695,18696],{},"before"," your route handlers (before ",[20,18699,18477],{},[86,18701,18703],{"className":11329,"code":18702,"language":11331,"meta":91,"style":91},"app.use(express.static(`${__dirname}\u002Fpublic`))\n",[20,18704,18705],{"__ignoreMap":91},[95,18706,18707,18709,18711,18713,18716,18718,18720,18722,18725],{"class":97,"line":98},[95,18708,13861],{"class":101},[95,18710,1693],{"class":108},[95,18712,14194],{"class":101},[95,18714,18715],{"class":108},"static",[95,18717,1216],{"class":101},[95,18719,18590],{"class":115},[95,18721,18517],{"class":101},[95,18723,18724],{"class":115},"}\u002Fpublic`",[95,18726,14666],{"class":101},[11,18728,18729,18730,18733],{},"Stop the server and start it again with ",[20,18731,18732],{},"node app.js",". That’s important: changing the code won’t show up on a browser refresh alone—you have to restart the process.",[11,18735,18736,18737,73],{},"Open the browser again at ",[20,18738,18666],{},[8894,18740],{"alt":18741,"caption":18742,"src":18743},"Styled portfolio homepage at 127.0.0.1:9000 with CSS, plane graphic, and social icons loaded","The same portfolio after static files are served correctly.","\u002Fimages\u002Fblog\u002Fsending-html-file-in-response-serving-static-files-using-express\u002Fbrowser-portfolio-with-assets.png",[11,18745,18746],{},"Now everything seems to be working—CSS loaded correctly, and images and SVGs are in place. So what magic suddenly happened after adding a single line of code?",[86,18748,18749],{"className":11329,"code":18702,"language":11331,"meta":91,"style":91},[20,18750,18751],{"__ignoreMap":91},[95,18752,18753,18755,18757,18759,18761,18763,18765,18767,18769],{"class":97,"line":98},[95,18754,13861],{"class":101},[95,18756,1693],{"class":108},[95,18758,14194],{"class":101},[95,18760,18715],{"class":108},[95,18762,1216],{"class":101},[95,18764,18590],{"class":115},[95,18766,18517],{"class":101},[95,18768,18724],{"class":115},[95,18770,14666],{"class":101},[11,18772,18773],{},"You can also write:",[86,18775,18777],{"className":11329,"code":18776,"language":11331,"meta":91,"style":91},"app.use(express.static('.\u002Fpublic'))\n",[20,18778,18779],{"__ignoreMap":91},[95,18780,18781,18783,18785,18787,18789,18791,18794],{"class":97,"line":98},[95,18782,13861],{"class":101},[95,18784,1693],{"class":108},[95,18786,14194],{"class":101},[95,18788,18715],{"class":108},[95,18790,1216],{"class":101},[95,18792,18793],{"class":115},"'.\u002Fpublic'",[95,18795,14666],{"class":101},[11,18797,18798,18799,18801,18802,18804],{},"Prefer ",[20,18800,18517],{}," over ",[20,18803,18543],{}," so the path stays correct regardless of the working directory you start Node from.",[11,18806,18807,18810,18811,18813],{},[20,18808,18809],{},"app.use"," registers middleware—functions that run as part of handling requests. ",[20,18812,18690],{}," takes the path to the directory that holds your static files. That’s how you tell Express where to look when the browser requests CSS, JS, images, and the rest.",[75,18815,18817,18818],{"id":18816},"the-finished-appjs","The finished ",[20,18819,13813],{},[86,18821,18823],{"className":11329,"code":18822,"language":11331,"meta":91,"style":91},"const express = require('express')\nconst app = express()\n\napp.use(express.static(`${__dirname}\u002Fpublic`))\n\napp.get('\u002F', (req, res) => {\n  res.sendFile(`${__dirname}\u002Findex.html`, (err) => {\n    if (err) {\n      console.log(err)\n      res.end(err.message)\n    }\n  })\n})\n\napp.listen(9000, '127.0.0.1', () => {\n  console.log('Server is running on port 9000')\n})\n",[20,18824,18825,18841,18853,18857,18877,18881,18905,18929,18935,18943,18951,18955,18959,18963,18967,18987,18999],{"__ignoreMap":91},[95,18826,18827,18829,18831,18833,18835,18837,18839],{"class":97,"line":98},[95,18828,335],{"class":201},[95,18830,18302],{"class":338},[95,18832,341],{"class":201},[95,18834,14050],{"class":108},[95,18836,1216],{"class":101},[95,18838,18311],{"class":115},[95,18840,1222],{"class":101},[95,18842,18843,18845,18847,18849,18851],{"class":97,"line":105},[95,18844,335],{"class":201},[95,18846,18320],{"class":338},[95,18848,341],{"class":201},[95,18850,18302],{"class":108},[95,18852,1726],{"class":101},[95,18854,18855],{"class":97,"line":122},[95,18856,1731],{"emptyLinePlaceholder":1410},[95,18858,18859,18861,18863,18865,18867,18869,18871,18873,18875],{"class":97,"line":134},[95,18860,13861],{"class":101},[95,18862,1693],{"class":108},[95,18864,14194],{"class":101},[95,18866,18715],{"class":108},[95,18868,1216],{"class":101},[95,18870,18590],{"class":115},[95,18872,18517],{"class":101},[95,18874,18724],{"class":115},[95,18876,14666],{"class":101},[95,18878,18879],{"class":97,"line":147},[95,18880,1731],{"emptyLinePlaceholder":1410},[95,18882,18883,18885,18887,18889,18891,18893,18895,18897,18899,18901,18903],{"class":97,"line":160},[95,18884,13861],{"class":101},[95,18886,11126],{"class":108},[95,18888,1216],{"class":101},[95,18890,18341],{"class":115},[95,18892,13872],{"class":101},[95,18894,13875],{"class":370},[95,18896,909],{"class":101},[95,18898,13880],{"class":370},[95,18900,373],{"class":101},[95,18902,350],{"class":201},[95,18904,353],{"class":101},[95,18906,18907,18909,18911,18913,18915,18917,18919,18921,18923,18925,18927],{"class":97,"line":171},[95,18908,13900],{"class":101},[95,18910,18504],{"class":108},[95,18912,1216],{"class":101},[95,18914,18590],{"class":115},[95,18916,18517],{"class":101},[95,18918,18595],{"class":115},[95,18920,13872],{"class":101},[95,18922,18600],{"class":370},[95,18924,373],{"class":101},[95,18926,350],{"class":201},[95,18928,353],{"class":101},[95,18930,18931,18933],{"class":97,"line":882},[95,18932,2751],{"class":201},[95,18934,18613],{"class":101},[95,18936,18937,18939,18941],{"class":97,"line":1788},[95,18938,18618],{"class":101},[95,18940,2171],{"class":108},[95,18942,18623],{"class":101},[95,18944,18945,18947,18949],{"class":97,"line":1793},[95,18946,18628],{"class":101},[95,18948,18362],{"class":108},[95,18950,18633],{"class":101},[95,18952,18953],{"class":97,"line":1801},[95,18954,2770],{"class":101},[95,18956,18957],{"class":97,"line":1820},[95,18958,398],{"class":101},[95,18960,18961],{"class":97,"line":1829},[95,18962,403],{"class":101},[95,18964,18965],{"class":97,"line":1851},[95,18966,1731],{"emptyLinePlaceholder":1410},[95,18968,18969,18971,18973,18975,18977,18979,18981,18983,18985],{"class":97,"line":1858},[95,18970,13861],{"class":101},[95,18972,17147],{"class":108},[95,18974,1216],{"class":101},[95,18976,18388],{"class":338},[95,18978,909],{"class":101},[95,18980,18393],{"class":115},[95,18982,2305],{"class":101},[95,18984,350],{"class":201},[95,18986,353],{"class":101},[95,18988,18989,18991,18993,18995,18997],{"class":97,"line":1863},[95,18990,2168],{"class":101},[95,18992,2171],{"class":108},[95,18994,1216],{"class":101},[95,18996,18410],{"class":115},[95,18998,1222],{"class":101},[95,19000,19001],{"class":97,"line":1868},[95,19002,403],{"class":101},[11,19004,19005],{},"That’s sendFile plus static middleware in a nutshell. Thanks for reading—see you next time.",[1383,19007,19008],{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":91,"searchDepth":105,"depth":105,"links":19010},[19011,19013,19015,19017],{"id":18419,"depth":105,"text":19012},"Serving a portfolio from \u002F",{"id":18460,"depth":105,"text":19014},"Sending index.html with res.sendFile",{"id":18686,"depth":105,"text":19016},"Serving static files with express.static",{"id":18816,"depth":105,"text":19018},"The finished app.js","2021-08-07","Continue the Node.js and Express series—send index.html with res.sendFile, then serve CSS, images, and other static assets with express.static.",{"src":19022,"alt":19023},"\u002Fimages\u002Fblog\u002Fsending-html-file-in-response-serving-static-files-using-express-cover.jpg","Getting started with Node.js cover showing Express code that serves HTML and static files",{},"\u002Fblog\u002Fsending-html-file-in-response-serving-static-files-using-express",{"title":18257,"description":19020},"blog\u002Fsending-html-file-in-response-serving-static-files-using-express",[15365,15366,15367,17652],"Wa-T4TZRfcfbzTuk9QjmuKSaxnpVm0CUWeMB0O_Sx_A",{"id":19031,"title":18270,"body":19032,"date":19527,"description":19528,"draft":1404,"extension":1405,"featured":1404,"image":19529,"meta":19532,"navigation":1410,"path":18269,"seo":19533,"stem":19534,"tags":19535,"updatedAt":1420,"__hash__":19536},"blog\u002Fblog\u002Fcreating-a-simple-node-js-server-using-express.md",{"type":8,"value":19033,"toc":19523},[19034,19037,19047,19050,19056,19060,19069,19079,19084,19090,19101,19104,19110,19113,19116,19132,19147,19150,19154,19169,19174,19177,19293,19296,19329,19360,19376,19391,19394,19401,19417,19423,19431,19472,19479,19482,19492,19497,19502,19508,19511,19518,19520],[11,19035,19036],{},"I know, that was a long title. Anyway—today I’ll show you how to get started with back-end development using Node.js and Express. The plan is to create a simple web server that can handle requests and send responses. This is the start of a series on getting started with Node.js.",[11,19038,59,19039,67,19043,73],{},[61,19040,66],{"href":19041,"rel":19042},"https:\u002F\u002Fmedium.com\u002Ffaun\u002Fcreating-a-simple-node-js-server-using-express-handling-incoming-requests-sending-responses-ba870ca68a0e",[65],[61,19044,16954],{"href":19045,"rel":19046},"https:\u002F\u002Ffaun.pub\u002Fcreating-a-simple-node-js-server-using-express-handling-incoming-requests-sending-responses-ba870ca68a0e",[65],[11,19048,19049],{},"I hope you know the terminology of the web and have a good idea of how it works—otherwise most of this article won’t land. If you need a refresher, read this first, then come back to build your first web server in JavaScript:",[43,19051,19052],{},[11,19053,16964,19054],{},[61,19055,17699],{"href":17698},[75,19057,19059],{"id":19058},"environment-setup","Environment setup",[11,19061,19062,19063,19068],{},"You don’t need anything fancy, but there are a few steps. First, install Node.js—the latest LTS—from the ",[61,19064,19067],{"href":19065,"rel":19066},"https:\u002F\u002Fnodejs.org\u002F",[65],"official Node.js site",". Search for “Node JS download,” open the first result, and grab the LTS build. During install, make sure “Add to PATH” is checked (it’s usually on by default); otherwise keep clicking Next and finish the install.",[11,19070,19071,19072,19075,19076,73],{},"Create a project folder—mine was ",[20,19073,19074],{},"A:\\projects\\first_node_server",". Open that folder in your editor. I’m using VS Code; you can open the terminal with ",[20,19077,19078],{},"Ctrl+`",[8894,19080],{"alt":19081,"caption":19082,"src":19083},"VS Code open on an empty first_node_server project with the terminal at A:\\\\Projects\\\\first_node_server","Empty project folder open in VS Code, ready for npm init.","\u002Fimages\u002Fblog\u002Fcreating-a-simple-node-js-server-using-express\u002Fvscode-empty-project.png",[11,19085,19086,19087,19089],{},"Now as you’ve installed Node, you also have ",[13742,19088,14019],{}," (Node Package Manager). To initialize a Node project, run:",[86,19091,19093],{"className":14010,"code":19092,"language":14012,"meta":91,"style":91},"npm init\n",[20,19094,19095],{"__ignoreMap":91},[95,19096,19097,19099],{"class":97,"line":98},[95,19098,14019],{"class":108},[95,19100,17983],{"class":115},[11,19102,19103],{},"The npm CLI will ask for project name, author, description, license, and so on. Change the values if you want, or keep hitting Enter—it doesn’t matter much at this stage.",[11,19105,19106,19107,19109],{},"That creates a ",[20,19108,17034],{}," in your project directory with name, author, description, scripts, and the list of installed packages (empty for now, since nothing is installed yet).",[11,19111,19112],{},"These packages are external libraries—other people’s code you want to use in your project.",[11,19114,19115],{},"Express is not a native Node module, so install it with:",[86,19117,19119],{"className":14010,"code":19118,"language":14012,"meta":91,"style":91},"npm install express --save\n",[20,19120,19121],{"__ignoreMap":91},[95,19122,19123,19125,19128,19130],{"class":97,"line":98},[95,19124,14019],{"class":108},[95,19126,19127],{"class":115}," install",[95,19129,18302],{"class":115},[95,19131,14028],{"class":338},[11,19133,19134,19135,19137,19138,19141,19142,15492,19144,19146],{},"npm installs Express, creates a ",[20,19136,18003],{}," folder for packages installed into the project, and generates ",[20,19139,19140],{},"package-lock.json",". That lockfile is created whenever npm changes ",[20,19143,18003],{},[20,19145,17034],{},". It describes the exact dependency tree so later installs can reproduce the same tree, even if intermediate dependencies change.",[11,19148,19149],{},"That’s a dense definition—if it doesn’t click yet, you can ignore it for now.",[75,19151,19153],{"id":19152},"initializing-express-and-starting-an-http-server","Initializing Express and starting an HTTP server",[11,19155,19156,19157,19159,19160,19162,19163,19166,19167,73],{},"Create a file named ",[20,19158,13813],{},". The name is a convention—you can call it something else, but conventions make it easier for others to follow your code. We usually split concerns into ",[20,19161,13813],{}," (initialize the app) and ",[20,19164,19165],{},"server.js"," (start and manage the server). For this small example, we’ll do both in ",[20,19168,13813],{},[8894,19170],{"alt":19171,"caption":19172,"src":19173},"VS Code showing app.js with Express require, a GET route, and listen on port 9000","The finished app.js in VS Code after installing Express.","\u002Fimages\u002Fblog\u002Fcreating-a-simple-node-js-server-using-express\u002Fapp-js-express-server.png",[11,19175,19176],{},"In the image above, you can see the code—I’ll explain it line by line. Here’s the same file for easy copying:",[86,19178,19179],{"className":11329,"code":18293,"language":11331,"meta":91,"style":91},[20,19180,19181,19197,19209,19213,19237,19249,19253,19257,19277,19289],{"__ignoreMap":91},[95,19182,19183,19185,19187,19189,19191,19193,19195],{"class":97,"line":98},[95,19184,335],{"class":201},[95,19186,18302],{"class":338},[95,19188,341],{"class":201},[95,19190,14050],{"class":108},[95,19192,1216],{"class":101},[95,19194,18311],{"class":115},[95,19196,1222],{"class":101},[95,19198,19199,19201,19203,19205,19207],{"class":97,"line":105},[95,19200,335],{"class":201},[95,19202,18320],{"class":338},[95,19204,341],{"class":201},[95,19206,18302],{"class":108},[95,19208,1726],{"class":101},[95,19210,19211],{"class":97,"line":122},[95,19212,1731],{"emptyLinePlaceholder":1410},[95,19214,19215,19217,19219,19221,19223,19225,19227,19229,19231,19233,19235],{"class":97,"line":134},[95,19216,13861],{"class":101},[95,19218,11126],{"class":108},[95,19220,1216],{"class":101},[95,19222,18341],{"class":115},[95,19224,13872],{"class":101},[95,19226,13875],{"class":370},[95,19228,909],{"class":101},[95,19230,13880],{"class":370},[95,19232,373],{"class":101},[95,19234,350],{"class":201},[95,19236,353],{"class":101},[95,19238,19239,19241,19243,19245,19247],{"class":97,"line":147},[95,19240,13900],{"class":101},[95,19242,18362],{"class":108},[95,19244,1216],{"class":101},[95,19246,18367],{"class":115},[95,19248,1222],{"class":101},[95,19250,19251],{"class":97,"line":160},[95,19252,403],{"class":101},[95,19254,19255],{"class":97,"line":171},[95,19256,1731],{"emptyLinePlaceholder":1410},[95,19258,19259,19261,19263,19265,19267,19269,19271,19273,19275],{"class":97,"line":882},[95,19260,13861],{"class":101},[95,19262,17147],{"class":108},[95,19264,1216],{"class":101},[95,19266,18388],{"class":338},[95,19268,909],{"class":101},[95,19270,18393],{"class":115},[95,19272,2305],{"class":101},[95,19274,350],{"class":201},[95,19276,353],{"class":101},[95,19278,19279,19281,19283,19285,19287],{"class":97,"line":1788},[95,19280,2168],{"class":101},[95,19282,2171],{"class":108},[95,19284,1216],{"class":101},[95,19286,18410],{"class":115},[95,19288,1222],{"class":101},[95,19290,19291],{"class":97,"line":1793},[95,19292,403],{"class":101},[11,19294,19295],{},"Line by line:",[10080,19297,19298,19316,19321],{},[642,19299,19300,19301,19303,19304,19307,19308,19310,19311,73],{},"The first line imports the Express library we just installed. You might wonder why not use an ES ",[20,19302,2987],{}," statement. You can try, but Node will throw ",[20,19305,19306],{},"Cannot use import statement outside a module"," because Node doesn’t enable ES modules by default. There’s experimental support—you need changes in ",[20,19309,17034],{}," to turn it on. More detail here: ",[61,19312,19315],{"href":19313,"rel":19314},"https:\u002F\u002Fwww.geeksforgeeks.org\u002Fhow-to-use-an-es6-import-in-node-js\u002F",[65],"How to use an ES6 import in Node.js? — GeeksforGeeks",[642,19317,19318,19319,73],{},"Next we call the Express module; it returns an app object we store in ",[20,19320,14084],{},[642,19322,19323,19325,19326,19328],{},[20,19324,18477],{}," handles GET requests to the path ",[20,19327,18289],{},". The first argument is the path; the second is a callback that runs when a matching request arrives:",[86,19330,19332],{"className":11329,"code":19331,"language":11331,"meta":91,"style":91},"app.[http_method]([path_of_the_incoming_request], (req, res) => {\n  \u002F\u002F Runs when a request hits the specified path\n})\n",[20,19333,19334,19351,19356],{"__ignoreMap":91},[95,19335,19336,19339,19341,19343,19345,19347,19349],{"class":97,"line":98},[95,19337,19338],{"class":101},"app.[http_method]([path_of_the_incoming_request], (",[95,19340,13875],{"class":370},[95,19342,909],{"class":101},[95,19344,13880],{"class":370},[95,19346,373],{"class":101},[95,19348,350],{"class":201},[95,19350,353],{"class":101},[95,19352,19353],{"class":97,"line":105},[95,19354,19355],{"class":382},"  \u002F\u002F Runs when a request hits the specified path\n",[95,19357,19358],{"class":97,"line":122},[95,19359,403],{"class":101},[11,19361,19362,19364,19365,19368,19369,19372,19373],{},[20,19363,18289],{}," is the domain root \u002F home page—e.g. ",[20,19366,19367],{},"www.google.com"," is the same as ",[20,19370,19371],{},"www.google.com\u002F",". Here we’re listening for GET requests on the base path and responding with the string ",[20,19374,19375],{},"Hi there, Server is working.",[11,19377,19378,19379,19382,19383,19385,19386,19382,19389,11762],{},"The callback’s first parameter is the ",[13742,19380,19381],{},"request"," object (",[20,19384,13875],{},"); the second is the ",[13742,19387,19388],{},"response",[20,19390,13880],{},[11,19392,19393],{},"The request object has details about the incoming request—protocol, hostname, body, params, and more.",[11,19395,19396,19397,19400],{},"We use the response object to send data back. ",[20,19398,19399],{},"res.end()"," ends the response process. You can pass what you want to send to the client:",[86,19402,19403],{"className":11329,"code":18481,"language":11331,"meta":91,"style":91},[20,19404,19405],{"__ignoreMap":91},[95,19406,19407,19409,19411,19413,19415],{"class":97,"line":98},[95,19408,18488],{"class":101},[95,19410,18362],{"class":108},[95,19412,1216],{"class":101},[95,19414,18367],{"class":115},[95,19416,1222],{"class":101},[11,19418,19419,19420],{},"Syntax: ",[20,19421,19422],{},"res.end([data] [, encoding])",[10080,19424,19425],{"start":134},[642,19426,19427,19428,19430],{},"Finally we start the server. Express provides ",[20,19429,17147],{}," on the app object:",[86,19432,19434],{"className":11329,"code":19433,"language":11331,"meta":91,"style":91},"app.listen(9000, '127.0.0.1', () => {\n  console.log('Server is running on port 9000')\n})\n",[20,19435,19436,19456,19468],{"__ignoreMap":91},[95,19437,19438,19440,19442,19444,19446,19448,19450,19452,19454],{"class":97,"line":98},[95,19439,13861],{"class":101},[95,19441,17147],{"class":108},[95,19443,1216],{"class":101},[95,19445,18388],{"class":338},[95,19447,909],{"class":101},[95,19449,18393],{"class":115},[95,19451,2305],{"class":101},[95,19453,350],{"class":201},[95,19455,353],{"class":101},[95,19457,19458,19460,19462,19464,19466],{"class":97,"line":105},[95,19459,2168],{"class":101},[95,19461,2171],{"class":108},[95,19463,1216],{"class":101},[95,19465,18410],{"class":115},[95,19467,1222],{"class":101},[95,19469,19470],{"class":97,"line":122},[95,19471,403],{"class":101},[11,19473,19474,19475,19478],{},"The first argument is the port (required). The second is the hostname (optional)—",[20,19476,19477],{},"127.0.0.1"," is localhost; if you omit it, localhost is used anyway. The last argument is a callback that runs once the server has started; we log that it’s running on port 9000.",[11,19480,19481],{},"Run the script with:",[86,19483,19484],{"className":14010,"code":18651,"language":14012,"meta":91,"style":91},[20,19485,19486],{"__ignoreMap":91},[95,19487,19488,19490],{"class":97,"line":98},[95,19489,18658],{"class":108},[95,19491,18059],{"class":115},[11,19493,19494,19495,73],{},"Then open your browser to ",[20,19496,18666],{},[8894,19498],{"alt":19499,"caption":19500,"src":19501},"Browser at 127.0.0.1:9000 showing the text Hi there, Server is working.","The browser receiving the response from your Express server.","\u002Fimages\u002Fblog\u002Fcreating-a-simple-node-js-server-using-express\u002Fbrowser-server-response.png",[11,19503,19504,19505,19507],{},"Remember to run ",[20,19506,17989],{}," and install Express before running this file, or you’ll get an error for obvious reasons.",[11,19509,19510],{},"That’s it—that’s how you initialize a simple web server with Node and Express. Next up: send an HTML file as the response and serve static assets—then later, reading and writing files with Node.js.",[43,19512,19513],{},[11,19514,19515,19516],{},"Next article: ",[61,19517,18257],{"href":19025},[11,19519,15337],{},[1383,19521,19522],{},"html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":91,"searchDepth":105,"depth":105,"links":19524},[19525,19526],{"id":19058,"depth":105,"text":19059},{"id":19152,"depth":105,"text":19153},"2021-08-04","Get started with Node.js and Express—set up a project with npm, handle GET requests, and send responses from a local HTTP server.",{"src":19530,"alt":19531},"\u002Fimages\u002Fblog\u002Fcreating-a-simple-node-js-server-using-express-cover.jpg","Getting started with Node.js cover showing Express code and a localhost server response",{},{"title":18270,"description":19528},"blog\u002Fcreating-a-simple-node-js-server-using-express",[15365,15366,15367,17652],"UclxTTIbprQoj1trX3_08fVFcTw09fBiK3EKykrLLqI",{"id":19538,"title":17699,"body":19539,"date":19694,"description":19695,"draft":1404,"extension":1405,"featured":1404,"image":19696,"meta":19699,"navigation":1410,"path":17698,"seo":19700,"stem":19701,"tags":19702,"updatedAt":1420,"__hash__":19705},"blog\u002Fblog\u002Fwhat-happens-when-you-visit-a-webpage.md",{"type":8,"value":19540,"toc":19686},[19541,19544,19554,19558,19563,19566,19574,19577,19585,19590,19593,19597,19608,19613,19617,19620,19624,19634,19638,19641,19646,19649,19652,19655,19659,19669,19671,19674,19683],[11,19542,19543],{},"Before you dive into web development—especially the back end—you should understand the web: how it works and what its main pieces are. This article covers those basics.",[11,19545,59,19546,67,19550,73],{},[61,19547,66],{"href":19548,"rel":19549},"https:\u002F\u002Fmedium.com\u002Ffaun\u002Fwhat-happens-when-you-visit-a-webpage-how-the-web-works-d42ce00e9651",[65],[61,19551,16954],{"href":19552,"rel":19553},"https:\u002F\u002Ffaun.pub\u002Fwhat-happens-when-you-visit-a-webpage-how-the-web-works-d42ce00e9651",[65],[75,19555,19557],{"id":19556},"components-of-the-web","Components of the web",[8894,19559],{"alt":19560,"caption":19561,"src":19562},"Diagram of client and server exchanging a request and a response","The request–response model, also called client–server architecture.","\u002Fimages\u002Fblog\u002Fwhat-happens-when-you-visit-a-webpage\u002Fweb-components.png",[11,19564,19565],{},"There are two things you must understand:",[10080,19567,19568,19571],{},[642,19569,19570],{},"Client (e.g. browser)",[642,19572,19573],{},"Server",[11,19575,19576],{},"The client (usually a browser) requests a website or some data from the server. A server is a remote system—a computer connected to the internet all the time—that listens for those requests and sends back a valid response. That whole cycle is the request–response cycle, or client–server architecture.",[11,19578,19579,19580,19584],{},"Here’s a concrete example. When I enter ",[61,19581,19367],{"href":19582,"rel":19583},"https:\u002F\u002Fwww.google.com",[65]," in my browser (not Internet Explorer—just kidding), my system and browser act as the client. Google has many servers waiting for such requests. They validate, compute if needed, maybe check a database, and send back a response—usually an HTML document—which gets parsed and rendered on my screen.",[8894,19586],{"alt":19587,"caption":19588,"src":19589},"Six-step flow from browser to server and database and back to display the page","A fuller view: browser, server, and database in the request cycle.","\u002Fimages\u002Fblog\u002Fwhat-happens-when-you-visit-a-webpage\u002Fclient-server-cycle.png",[11,19591,19592],{},"That’s the top-level view. Let’s go a level deeper.",[75,19594,19596],{"id":19595},"dns-lookup","DNS lookup",[11,19598,19599,19600,19603,19604,19607],{},"This is the first thing that happens after you enter a domain or URL and hit enter. For humans, remembering a domain like ",[61,19601,19367],{"href":19582,"rel":19602},[65]," is far easier than a public IP such as ",[20,19605,19606],{},"216.58.211.206:443",". The domain means nothing to the network by itself, so a DNS lookup runs first and resolves the public IP associated with that domain.",[8894,19609],{"alt":19610,"caption":19611,"src":19612},"Diagram showing DNS lookup, TCP\u002FIP connection, and the structure of an HTTP request","From DNS lookup through the TCP\u002FIP connection to the HTTP request.","\u002Fimages\u002Fblog\u002Fwhat-happens-when-you-visit-a-webpage\u002Fdns-http-request.png",[75,19614,19616],{"id":19615},"tcpip-socket-connection","TCP\u002FIP socket connection",[11,19618,19619],{},"Once the browser has the server’s IP from DNS, it opens a TCP\u002FIP socket connection between client and server. That connection is what makes communication between them possible.",[75,19621,19623],{"id":19622},"http-request","HTTP request",[11,19625,19626,19627,909,19629,13761,19631,19633],{},"With the socket in place, the client sends an HTTP request to the server. In the diagram above you can see what a request looks like: a start line with the HTTP method (what kind of request this is—more on methods later), the request target, and the HTTP version; headers with information about the request; and optionally a body when you send data (for example with ",[20,19628,13660],{},[20,19630,13760],{},[20,19632,13764],{},"). HTTP methods are covered at the end of this article.",[75,19635,19637],{"id":19636},"http-response","HTTP response",[11,19639,19640],{},"The server receives the request, runs any needed computation and validation, then sends back a response. The response object looks a lot like the request the browser sent.",[8894,19642],{"alt":19643,"caption":19644,"src":19645},"Diagram of HTTP response structure and browser rendering linked assets","HTTP response shape, then the browser loads HTML and linked assets.","\u002Fimages\u002Fblog\u002Fwhat-happens-when-you-visit-a-webpage\u002Fhttp-response.png",[11,19647,19648],{},"That response might be JSON, HTML, or something else. If it’s HTML, the browser parses it and requests linked files from the server—repeating a similar process for each asset—until the page is rendered.",[11,19650,19651],{},"When the request–response cycle is done, the TCP\u002FIP socket connection is closed.",[11,19653,19654],{},"That’s a brief look at how the web works. I hope the flow is clear.",[75,19656,19658],{"id":19657},"http-methods","HTTP methods",[11,19660,19661,19662,19665,19666,19668],{},"An HTTP request can be of different types. If you want data from a server, that’s typically a ",[20,19663,19664],{},"GET",". If you want to send data and create a new entry, that’s often a ",[20,19667,13660],{},". There are other methods for other use cases as well.",[8273,19670],{},[11,19672,19673],{},"Some of the diagrams in this article come from the theory lectures of a Udemy course on Node.js development. I purchased the course, which is why I have access to them—I don’t own them. The course is linked below if you want to check it out; it’s the one I used to start my back-end journey.",[43,19675,19676],{},[11,19677,19678],{},[61,19679,19682],{"href":19680,"rel":19681},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Fnodejs-express-mongodb-bootcamp\u002F",[65],"Node.js, Express, MongoDB & More: The Complete Bootcamp 2021",[11,19684,19685],{},"Thanks for reading—I hope this helps you become a better developer. See you next time.",{"title":91,"searchDepth":105,"depth":105,"links":19687},[19688,19689,19690,19691,19692,19693],{"id":19556,"depth":105,"text":19557},{"id":19595,"depth":105,"text":19596},{"id":19615,"depth":105,"text":19616},{"id":19622,"depth":105,"text":19623},{"id":19636,"depth":105,"text":19637},{"id":19657,"depth":105,"text":19658},"2021-08-01","How the web works at a high level—clients and servers, DNS lookup, TCP\u002FIP, HTTP requests and responses, and common HTTP methods.",{"src":19697,"alt":19698},"\u002Fimages\u002Fblog\u002Fwhat-happens-when-you-visit-a-webpage-cover.jpg","Infographic of the steps from entering a URL to seeing a rendered webpage",{},{"title":17699,"description":19695},"blog\u002Fwhat-happens-when-you-visit-a-webpage",[3681,15367,19703,19704],"HTTP","Networking","5zzMj4nvsz4RBcU-vi8SA920PXJTtgRWphRwj3MY1Zc",{"id":19707,"title":19708,"body":19709,"date":19918,"description":19919,"draft":1404,"extension":1405,"featured":1404,"image":19920,"meta":19923,"navigation":1410,"path":19924,"seo":19925,"stem":19926,"tags":19927,"updatedAt":1420,"__hash__":19930},"blog\u002Fblog\u002Fwhats-next-once-you-know-the-basics.md","What's Next Once You Know the Basics?",{"type":8,"value":19710,"toc":19911},[19711,19721,19727,19735,19739,19742,19752,19757,19766,19770,19773,19776,19779,19789,19799,19809,19819,19823,19826,19836,19839,19842,19845,19849,19854,19863,19869,19878,19884,19893,19899,19908],[11,19712,19713,19714,112,19717,19720],{},"This article continues my previous post, where we covered the minimum requirements for calling yourself a full-stack developer—but quoting ",[17467,19715,19716],{},"The Family Man",[13742,19718,19719],{},"“Don’t be a minimum guy.”"," Here I’ll talk about leveling up toward being a stronger full-stack developer rather than staying a newbie.",[11,19722,59,19723,73],{},[61,19724,66],{"href":19725,"rel":19726},"https:\u002F\u002Fmedium.com\u002F@akshattriv\u002Fwhats-next-once-you-know-the-basics-roadmap-to-full-stack-development-b69d022b96f8",[65],[43,19728,19729],{},[11,19730,16964,19731],{},[61,19732,19734],{"href":19733},"\u002Fblog\u002Fwhen-can-you-start-calling-yourself-a-full-stack-developer","When Can You Start Calling Yourself a Full-Stack Developer?",[75,19736,19738],{"id":19737},"real-time-websites-and-websockets","Real-time websites and WebSockets",[11,19740,19741],{},"Knowing WebSockets isn’t strictly required, though it can add a lot of weight to your resume. Once you understand what WebSockets are and how they work, you can build strong real-time projects—a chat app or a live stats dashboard are good portfolio pieces. Socket.IO is the most popular library for WebSockets. There’s also WebRTC, an open-source project that gives browsers and mobile apps real-time communication through simple APIs. With WebSockets and WebRTC you can build video calling, voice calling, and chat applications—again, excellent projects to have.",[43,19743,19744],{},[11,19745,19746,19747],{},"This is where I mastered Socket.IO — ",[61,19748,19751],{"href":19749,"rel":19750},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Fsocketio-with-websockets-the-details\u002F",[65],"Socket.IO (with websockets) — the details (socket.io v2)",[11,19753,19754],{},[13742,19755,19756],{},"WebRTC course —",[43,19758,19759],{},[11,19760,19761],{},[61,19762,19765],{"href":19763,"rel":19764},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Fthe-webrtc-bootcamp-2021\u002F",[65],"The WebRTC Bootcamp 2021 For Beginners",[75,19767,19769],{"id":19768},"front-end-javascript-frameworks","Front-end JavaScript frameworks",[11,19771,19772],{},"This has become a necessity—you’ll rarely find a company shipping vanilla JavaScript on the front end alone. Why reinvent the wheel when a framework gets you productive quickly?",[11,19774,19775],{},"React dominates the market, but I wouldn’t start there: the learning curve is steep and the ecosystem takes time. I’d suggest starting with Vue—it’s approachable, logical, and has a smaller learning curve than React. Once you’re comfortable with Vue and its ecosystem, React makes much more sense coming from Vue than from vanilla JavaScript.",[11,19777,19778],{},"And when I say learn Vue or React, I don’t mean the core library alone. For Vue that means Vue Router, Vuex, and Nuxt; for React that means React Router, Redux, and Next. You should also be aware of libraries like Axios (and, historically, jQuery).",[43,19780,19781],{},[11,19782,19783,19784],{},"This is where I learned Vue — ",[61,19785,19788],{"href":19786,"rel":19787},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Fvuejs-2-the-complete-guide\u002F",[65],"Vue — The Complete Guide (w\u002F Router, Vuex, Composition API)",[43,19790,19791],{},[11,19792,19793,19794],{},"Course for Nuxt — ",[61,19795,19798],{"href":19796,"rel":19797},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Fnuxtjs-vuejs-on-steroids\u002F",[65],"Nuxt.js — Vue.js on Steroids",[43,19800,19801],{},[11,19802,19803,19804],{},"Course for React — ",[61,19805,19808],{"href":19806,"rel":19807},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Freact-the-complete-guide-incl-redux\u002F",[65],"React — The Complete Guide (incl Hooks, React Router, Redux)",[43,19810,19811],{},[11,19812,19813,19814],{},"Course for Next.js — ",[61,19815,19818],{"href":19816,"rel":19817},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Fnextjs-react-the-complete-guide\u002F",[65],"Next.js & React — The Complete Guide (incl. Two Paths!)",[75,19820,19822],{"id":19821},"testing-front-end-and-back-end","Testing — front end and back end",[11,19824,19825],{},"Writing tests gives teammates confidence in your code and your work. There are many JS libraries for front-end and back-end testing. On the front end you’ll see Jasmine, Jest, PhantomJS, and more. On the back end, Mocha and Chai are common. Cypress and Selenium are also popular for testing web applications.",[43,19827,19828],{},[11,19829,19830,19831],{},"Cypress course — ",[61,19832,19835],{"href":19833,"rel":19834},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Fcypress-web-automation-testing-from-zero-to-hero\u002F",[65],"Cypress: Web Automation Testing from Zero to Hero",[75,19837,17651],{"id":19838},"deployment",[11,19840,19841],{},"Learn Linux—it’s rare to work on a remote OS that isn’t Linux. You should know installing packages, setting up databases like MySQL or PostgreSQL, navigating directories, creating and removing files and folders, and working with system services such as the firewall—all from the command line, without a GUI. After that, learn setting up and configuring Nginx and Apache, server blocks \u002F virtual hosting, reverse proxies, and adding SSL certificates to your apps.",[11,19843,19844],{},"You can go deeper with containers, Docker, and more advanced tools like Kubernetes or Jenkins. Deployment is a field of its own—load balancing, auto-scaling, and plenty more.",[75,19846,19848],{"id":19847},"some-more-important-things-to-know","Some more important things to know",[11,19850,19851,19853],{},[13742,19852,17650],{}," — You’ll need version control for managing projects, and GitHub or GitLab for saving and sharing code. Know what Git is, what branches and commits are, how to move through history—it’s an essential tool. This isn’t “nice to know”; you must know it.",[43,19855,19856],{},[11,19857,19858],{},[61,19859,19862],{"href":19860,"rel":19861},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Fgit-and-github-bootcamp\u002F",[65],"The Git & GitHub Bootcamp",[11,19864,19865,19868],{},[13742,19866,19867],{},"TypeScript"," — Learning TypeScript is optional, but you may need it later anyway as many companies adopt it. It’s popular because it’s safer than plain JavaScript. Don’t worry—it’s not a whole new language. TypeScript is a superset of JavaScript that primarily adds optional static typing, and it compiles down to JS. A big benefit is richer IDE feedback that spots common errors as you type.",[43,19870,19871],{},[11,19872,19873],{},[61,19874,19877],{"href":19875,"rel":19876},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Funderstanding-typescript\u002F",[65],"Understanding TypeScript — 2021 Edition",[11,19879,19880,19883],{},[13742,19881,19882],{},"Progressive Web Apps (PWAs)"," — Also optional, but worth learning if you have time. Google’s definition: “A progressive web application is a type of application software delivered through the web, built using common web technologies including HTML, CSS, and JavaScript. It is intended to work on any platform that uses a standards-compliant browser, including both desktop and mobile devices.” Simply put, it lets your site run more like a native app on a device.",[43,19885,19886],{},[11,19887,19888],{},[61,19889,19892],{"href":19890,"rel":19891},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Fprogressive-web-app-pwa-the-complete-guide\u002F",[65],"Progressive Web Apps (PWA) — The Complete Guide",[11,19894,19895,19898],{},[13742,19896,19897],{},"GraphQL"," — GraphQL is an open-source data query and manipulation language for APIs, plus a runtime for fulfilling queries against existing data. It was developed internally at Facebook in 2012 and released publicly in 2015. Learning it is optional—you can keep building with REST—but it’s worth knowing it exists and that it’s widely used in the community.",[43,19900,19901],{},[11,19902,19903],{},[61,19904,19907],{"href":19905,"rel":19906},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Fgraphql-bootcamp\u002F",[65],"The Modern GraphQL Bootcamp (Learn Node.js and Apollo)",[11,19909,19910],{},"So I think that’s it. There’s always more to learn—in this industry you never really stop. See you next time.",{"title":91,"searchDepth":105,"depth":105,"links":19912},[19913,19914,19915,19916,19917],{"id":19737,"depth":105,"text":19738},{"id":19768,"depth":105,"text":19769},{"id":19821,"depth":105,"text":19822},{"id":19838,"depth":105,"text":17651},{"id":19847,"depth":105,"text":19848},"2021-06-27","Beyond the full-stack foundation—WebSockets, front-end frameworks, testing, deployment, Git, TypeScript, PWAs, and GraphQL for leveling up.",{"src":19921,"alt":19922},"\u002Fimages\u002Fblog\u002Fwhats-next-once-you-know-the-basics-cover.jpg","Part 2 roadmap cover with milestones for WebSockets, frameworks, testing, Git, TypeScript, PWAs, and GraphQL",{},"\u002Fblog\u002Fwhats-next-once-you-know-the-basics",{"title":19708,"description":19919},"blog\u002Fwhats-next-once-you-know-the-basics",[19928,19929,1417,11453],"Full Stack","Career","7FLrQBVYSzaD-m6_p53OKrPTr6iQ9wRU1y5FegxvZ1M",{"id":19932,"title":19734,"body":19933,"date":20111,"description":20112,"draft":1404,"extension":1405,"featured":1404,"image":20113,"meta":20116,"navigation":1410,"path":19733,"seo":20117,"stem":20118,"tags":20119,"updatedAt":1420,"__hash__":20120},"blog\u002Fblog\u002Fwhen-can-you-start-calling-yourself-a-full-stack-developer.md",{"type":8,"value":19934,"toc":20106},[19935,19938,19944,19947,19958,19961,19964,19967,19975,19990,20000,20005,20015,20031,20041,20044,20047,20055,20071,20080,20088,20098,20100,20103],[11,19936,19937],{},"Becoming a full-stack developer is certainly not easy—though it gets simpler once you understand what the title means and what you need to know to earn it. This article is a roadmap to full-stack development so you can set goals and track progress on the way to becoming a stronger developer.",[11,19939,59,19940,73],{},[61,19941,66],{"href":19942,"rel":19943},"https:\u002F\u002Fmedium.com\u002F@akshattriv\u002Fwhen-can-you-start-calling-yourself-a-full-stack-developer-roadmap-to-full-stack-development-a16e8a6919d0",[65],[11,19945,19946],{},"There are three aspects of being a full-stack developer:",[639,19948,19949,19952,19955],{},[642,19950,19951],{},"Front end",[642,19953,19954],{},"Back end",[642,19956,19957],{},"Deployment (DevOps)",[75,19959,19951],{"id":19960},"front-end",[11,19962,19963],{},"Everything visual in an application—everything a user sees—comes under the front end. I used “application” instead of “website” on purpose: full-stack work is not limited to websites. A full-stack developer may build websites, progressive web apps, and native mobile applications too. That versatility is why the role is so valued in startup culture—you can contribute across parts of the team.",[11,19965,19966],{},"From a top view, the front end contains:",[639,19968,19969,19971,19973],{},[642,19970,17765],{},[642,19972,10813],{},[642,19974,1417],{},[11,19976,19977,19979,19980,909,19983,909,19986,19989],{},[13742,19978,17765],{}," — HTML is a markup language we use to define the structure of a page or an element in an application. You need to know how markup is written, how structure is defined, how tags differ from each other, and what their default style properties are. You should also know modern tags like ",[20,19981,19982],{},"video",[20,19984,19985],{},"audio",[20,19987,19988],{},"picture",", and similar.",[43,19991,19992],{},[11,19993,19994,19995],{},"This is the course where I learned HTML and basic CSS, if you want to check it out — ",[61,19996,19999],{"href":19997,"rel":19998},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Fdesign-and-develop-a-killer-website-with-html5-and-css3\u002F",[65],"Build Responsive Websites with HTML5 and CSS3",[11,20001,20002,20004],{},[13742,20003,10813],{}," — CSS is where we style elements and define how they look on the page. You should know font and color properties, the box model (border, margin, padding), transforms, transitions, animations, layouts (Flexbox, Grid), and media queries. There’s more to CSS than that, of course, but you should at least know this much. I’d also advise learning Sass, a CSS preprocessor.",[43,20006,20007],{},[11,20008,20009,20010],{},"This is the course I took for mastering CSS — ",[61,20011,20014],{"href":20012,"rel":20013},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Fadvanced-css-and-sass\u002F",[65],"Advanced CSS and Sass: Flexbox, Grid, Animations and More",[11,20016,20017,20019,20020,909,20022,909,20024,20026,20027,20030],{},[13742,20018,1417],{}," — JavaScript is what makes the web feel alive. The interactive behavior you see when you open a site is largely JS. You should learn about ",[20,20021,2392],{},[20,20023,335],{},[20,20025,11279],{},", functions deeply, the ",[20,20028,20029],{},"this"," keyword, DOM manipulation, event listeners, data types, objects, arrays, function constructors, prototypes, function currying, higher-order and first-class functions, the event loop, callbacks, asynchrony, promises, closures, ES6 classes, maps—and the list goes on. Check out the course below; I’m not telling you to buy it, but at least skim the curriculum to see how much there is to learn.",[43,20032,20033],{},[11,20034,20035,20036],{},"This is the course I enrolled in for JavaScript — ",[61,20037,20040],{"href":20038,"rel":20039},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Fthe-complete-javascript-course\u002F",[65],"The Complete JavaScript Course 2021: From Zero to Expert!",[75,20042,19954],{"id":20043},"back-end",[11,20045,20046],{},"From a top view, the back end contains:",[639,20048,20049,20052],{},[642,20050,20051],{},"Server-side language",[642,20053,20054],{},"Database",[11,20056,20057,20060,20061,909,20064,909,20067,20070],{},[13742,20058,20059],{},"Server-side language (Node.js)"," — I chose JavaScript on the server as well; you can pick another language if you prefer. You need to understand the difference between running JavaScript in the browser and in Node.js. Learn npm\u002Fyarn, splitting code into modules, requiring modules, native modules (",[20,20062,20063],{},"fs",[20,20065,20066],{},"os",[20,20068,20069],{},"http","), creating servers, REST APIs, and Express. Express is not part of Node—it’s a popular library for building servers—but it’s used so closely with Node that saying “I’m a Node.js developer” usually implies you’ve worked with Express.",[11,20072,20073,20075,20076,20079],{},[13742,20074,20054],{}," — I’d advise any full-stack developer to know at least one SQL and one NoSQL database. For SQL, MySQL or PostgreSQL; for NoSQL, MongoDB is the most common choice. A good full-stack developer should also know ",[17467,20077,20078],{},"when"," to use which—when SQL fits and when NoSQL does.",[43,20081,20082],{},[11,20083,20084,20085],{},"This is the course I took for back-end development — ",[61,20086,19682],{"href":19680,"rel":20087},[65],[43,20089,20090],{},[11,20091,20092,20093],{},"This is the course where I learned PostgreSQL — ",[61,20094,20097],{"href":20095,"rel":20096},"https:\u002F\u002Fwww.udemy.com\u002Fcourse\u002Fsql-and-postgresql\u002F",[65],"SQL and PostgreSQL: The Complete Developer’s Guide",[75,20099,17651],{"id":19838},[11,20101,20102],{},"I didn’t take a course for deployment—I learned by searching a lot. Search for how to deploy a Node application on an AWS EC2 instance or a GCP VM instance. AWS is Amazon Web Services; GCP is Google Cloud Platform. I may write a follow-up that goes deeper on deployment.",[11,20104,20105],{},"Anything short of this, and I’d hesitate to call yourself a full-stack developer yet. After covering what we discussed, you can start applying for internships. In a solid company you’ll quickly realize this still isn’t enough—there’s a lot more to learn. Let’s explore that in the next article.",{"title":91,"searchDepth":105,"depth":105,"links":20107},[20108,20109,20110],{"id":19960,"depth":105,"text":19951},{"id":20043,"depth":105,"text":19954},{"id":19838,"depth":105,"text":17651},"2021-06-12","A practical roadmap to full-stack development—front end, back end, and deployment—plus the skills and courses that helped me get there.",{"src":20114,"alt":20115},"\u002Fimages\u002Fblog\u002Fwhen-can-you-start-calling-yourself-a-full-stack-developer-cover.jpg","Infographic roadmap from front-end basics through databases, back end, and deploy on a winding mountain road",{},{"title":19734,"description":20112},"blog\u002Fwhen-can-you-start-calling-yourself-a-full-stack-developer",[19928,19929,1417,15365],"0sEB4PO5ibE5JHDuShXiC4TWYGsGXRsdnt3vXRw-Iiw",{"id":20122,"title":20123,"body":20124,"date":20593,"description":20594,"draft":1404,"extension":1405,"featured":1404,"image":20595,"meta":20598,"navigation":1410,"path":20599,"seo":20600,"stem":20601,"tags":20602,"updatedAt":1420,"__hash__":20604},"blog\u002Fblog\u002Fhow-to-limit-text-in-a-container.md","How to Limit Text in a Container Using CSS or JavaScript",{"type":8,"value":20125,"toc":20589},[20126,20129,20139,20142,20147,20152,20156,20163,20196,20271,20301,20304,20315,20318,20323,20327,20337,20527,20552,20563,20569,20576,20584,20586],[11,20127,20128],{},"A lot of times, while working with text in our designs, we need to limit copy to a certain number of characters or lines—and cut off anything after that gracefully. That technique is known as line clamping. There are a few ways to do it; in this article we’ll look at one approach with pure CSS and one with pure JavaScript.",[11,20130,59,20131,67,20135,73],{},[61,20132,66],{"href":20133,"rel":20134},"https:\u002F\u002Fmedium.com\u002Ffaun\u002Fhow-to-limit-text-in-a-container-line-clampin-truncating-multiple-line-text-ca34bb80ef1a",[65],[61,20136,16954],{"href":20137,"rel":20138},"https:\u002F\u002Ffaun.pub\u002Fhow-to-limit-text-in-a-container-line-clampin-truncating-multiple-line-text-ca34bb80ef1a",[65],[11,20140,20141],{},"If line clamping still feels fuzzy, the examples below should make it clear.",[8894,20143],{"alt":20144,"caption":20145,"src":20146},"List item headline wrapping across several lines without truncation","List item text without line clamping.","\u002Fimages\u002Fblog\u002Fhow-to-limit-text-in-a-container\u002Fwithout-line-clamp.png",[8894,20148],{"alt":20149,"caption":20150,"src":20151},"Same list item headline clamped to two lines with an ellipsis","List item text with line clamping.","\u002Fimages\u002Fblog\u002Fhow-to-limit-text-in-a-container\u002Fwith-line-clamp.png",[75,20153,20155],{"id":20154},"using-css","Using CSS",[11,20157,20158,20159,20162],{},"We can get the result with CSS alone. The biggest benefit: when the container width changes, the browser figures out how much text fits and adds the ",[20,20160,20161],{},"…"," ellipsis at the end of the last visible line.",[86,20164,20166],{"className":13688,"code":20165,"language":10300,"meta":91,"style":91},"\u003Cspan class=\"trendingListItemTextHeadLine\">\n  Twelve Start-ups from PIER71’s Smart Port Challenge Awarded S$600,000 to Bring Innovative Solutions to Maritime Market\n\u003C\u002Fspan>\n",[20,20167,20168,20183,20188],{"__ignoreMap":91},[95,20169,20170,20172,20174,20176,20178,20181],{"class":97,"line":98},[95,20171,192],{"class":101},[95,20173,95],{"class":195},[95,20175,13970],{"class":108},[95,20177,205],{"class":101},[95,20179,20180],{"class":115},"\"trendingListItemTextHeadLine\"",[95,20182,553],{"class":101},[95,20184,20185],{"class":97,"line":105},[95,20186,20187],{"class":101},"  Twelve Start-ups from PIER71’s Smart Port Challenge Awarded S$600,000 to Bring Innovative Solutions to Maritime Market\n",[95,20189,20190,20192,20194],{"class":97,"line":122},[95,20191,568],{"class":101},[95,20193,95],{"class":195},[95,20195,553],{"class":101},[86,20197,20199],{"className":10197,"code":20198,"language":10199,"meta":91,"style":91},".trendingListItemTextHeadLine {\n  display: -webkit-box;\n  -webkit-line-clamp: 2;\n  -webkit-box-orient: vertical;\n  overflow: hidden;\n  width: 100%;\n}\n",[20,20200,20201,20208,20220,20231,20243,20253,20267],{"__ignoreMap":91},[95,20202,20203,20206],{"class":97,"line":98},[95,20204,20205],{"class":108},".trendingListItemTextHeadLine",[95,20207,353],{"class":101},[95,20209,20210,20213,20215,20218],{"class":97,"line":105},[95,20211,20212],{"class":338},"  display",[95,20214,112],{"class":101},[95,20216,20217],{"class":338},"-webkit-box",[95,20219,10223],{"class":101},[95,20221,20222,20225,20227,20229],{"class":97,"line":122},[95,20223,20224],{"class":338},"  -webkit-line-clamp",[95,20226,112],{"class":101},[95,20228,465],{"class":338},[95,20230,10223],{"class":101},[95,20232,20233,20236,20238,20241],{"class":97,"line":134},[95,20234,20235],{"class":338},"  -webkit-box-orient",[95,20237,112],{"class":101},[95,20239,20240],{"class":338},"vertical",[95,20242,10223],{"class":101},[95,20244,20245,20247,20249,20251],{"class":97,"line":147},[95,20246,12542],{"class":338},[95,20248,112],{"class":101},[95,20250,12547],{"class":338},[95,20252,10223],{"class":101},[95,20254,20255,20258,20260,20263,20265],{"class":97,"line":160},[95,20256,20257],{"class":338},"  width",[95,20259,112],{"class":101},[95,20261,20262],{"class":338},"100",[95,20264,10314],{"class":201},[95,20266,10223],{"class":101},[95,20268,20269],{"class":97,"line":171},[95,20270,174],{"class":101},[639,20272,20273,20279,20295],{},[642,20274,20275,20278],{},[20,20276,20277],{},"-webkit-line-clamp"," sets how many lines the element may show; anything beyond that is clamped.",[642,20280,20281,20282,20284,20285,13801,20288,11033,20290,13801,20293,73],{},"For ",[20,20283,20277],{}," to work, ",[20,20286,20287],{},"display",[20,20289,20217],{},[20,20291,20292],{},"-webkit-box-orient",[20,20294,20240],{},[642,20296,20297,20298,73],{},"That combination adds the ellipsis after two lines and constrains the box height, but overflowing text can still paint unless you also set ",[20,20299,20300],{},"overflow: hidden",[11,20302,20303],{},"And that’s it—that’s all you need.",[11,20305,20306,20307,20309,20310,73],{},"To read more about ",[20,20308,20277],{},", see the ",[61,20311,20314],{"href":20312,"rel":20313},"https:\u002F\u002Fdeveloper.mozilla.org\u002Fen-US\u002Fdocs\u002FWeb\u002FCSS\u002F-webkit-line-clamp",[65],"MDN documentation",[11,20316,20317],{},"You can also check browser support below (as captured when the article was written):",[8894,20319],{"alt":20320,"caption":20321,"src":20322},"Browser compatibility table for the CSS line-clamp property","Browser support snapshot for -webkit-line-clamp.","\u002Fimages\u002Fblog\u002Fhow-to-limit-text-in-a-container\u002Fbrowser-compatibility.png",[75,20324,20326],{"id":20325},"using-javascript","Using JavaScript",[11,20328,20329,20330,20333,20334,73],{},"You can also shorten a sentence with JavaScript. It’s not as elegant as CSS ",[20,20331,20332],{},"line-clamp",", but it’s handy often enough—especially when you care about character count rather than line count. Here we use ",[20,20335,20336],{},"Array.prototype.reduce",[86,20338,20340],{"className":11329,"code":20339,"language":11331,"meta":91,"style":91},"function reduceText(itemText) {\n  const maxLengthOfLink = 120 \u002F\u002F maximum number of characters you want\n  const itemTextArray = itemText.split(' ')\n  let reducedFlag = -1\n\n  return itemTextArray.reduce((accumulator, currentVal) => {\n    if (accumulator.length + currentVal.length > maxLengthOfLink) {\n      reducedFlag++\n      return reducedFlag ? accumulator : `${accumulator}...`\n    }\n    else {\n      return `${accumulator} ${currentVal}`\n    }\n  })\n}\n",[20,20341,20342,20356,20371,20393,20408,20412,20438,20461,20469,20489,20493,20500,20515,20519,20523],{"__ignoreMap":91},[95,20343,20344,20346,20349,20351,20354],{"class":97,"line":98},[95,20345,823],{"class":201},[95,20347,20348],{"class":108}," reduceText",[95,20350,1216],{"class":101},[95,20352,20353],{"class":370},"itemText",[95,20355,2324],{"class":101},[95,20357,20358,20360,20363,20365,20368],{"class":97,"line":105},[95,20359,1715],{"class":201},[95,20361,20362],{"class":338}," maxLengthOfLink",[95,20364,341],{"class":201},[95,20366,20367],{"class":338}," 120",[95,20369,20370],{"class":382}," \u002F\u002F maximum number of characters you want\n",[95,20372,20373,20375,20378,20380,20383,20386,20388,20391],{"class":97,"line":122},[95,20374,1715],{"class":201},[95,20376,20377],{"class":338}," itemTextArray",[95,20379,341],{"class":201},[95,20381,20382],{"class":101}," itemText.",[95,20384,20385],{"class":108},"split",[95,20387,1216],{"class":101},[95,20389,20390],{"class":115},"' '",[95,20392,1222],{"class":101},[95,20394,20395,20397,20400,20402,20405],{"class":97,"line":134},[95,20396,3080],{"class":201},[95,20398,20399],{"class":101}," reducedFlag ",[95,20401,205],{"class":201},[95,20403,20404],{"class":201}," -",[95,20406,20407],{"class":338},"1\n",[95,20409,20410],{"class":97,"line":147},[95,20411,1731],{"emptyLinePlaceholder":1410},[95,20413,20414,20416,20419,20422,20424,20427,20429,20432,20434,20436],{"class":97,"line":160},[95,20415,358],{"class":201},[95,20417,20418],{"class":101}," itemTextArray.",[95,20420,20421],{"class":108},"reduce",[95,20423,367],{"class":101},[95,20425,20426],{"class":370},"accumulator",[95,20428,909],{"class":101},[95,20430,20431],{"class":370},"currentVal",[95,20433,373],{"class":101},[95,20435,350],{"class":201},[95,20437,353],{"class":101},[95,20439,20440,20442,20445,20448,20450,20453,20455,20458],{"class":97,"line":171},[95,20441,2751],{"class":201},[95,20443,20444],{"class":101}," (accumulator.",[95,20446,20447],{"class":338},"length",[95,20449,14718],{"class":201},[95,20451,20452],{"class":101}," currentVal.",[95,20454,20447],{"class":338},[95,20456,20457],{"class":201}," >",[95,20459,20460],{"class":101}," maxLengthOfLink) {\n",[95,20462,20463,20466],{"class":97,"line":882},[95,20464,20465],{"class":101},"      reducedFlag",[95,20467,20468],{"class":201},"++\n",[95,20470,20471,20473,20475,20477,20480,20482,20484,20486],{"class":97,"line":1788},[95,20472,4482],{"class":201},[95,20474,20399],{"class":101},[95,20476,15875],{"class":201},[95,20478,20479],{"class":101}," accumulator ",[95,20481,1651],{"class":201},[95,20483,5476],{"class":115},[95,20485,20426],{"class":101},[95,20487,20488],{"class":115},"}...`\n",[95,20490,20491],{"class":97,"line":1793},[95,20492,2770],{"class":101},[95,20494,20495,20498],{"class":97,"line":1801},[95,20496,20497],{"class":201},"    else",[95,20499,353],{"class":101},[95,20501,20502,20504,20506,20508,20511,20513],{"class":97,"line":1820},[95,20503,4482],{"class":201},[95,20505,5476],{"class":115},[95,20507,20426],{"class":101},[95,20509,20510],{"class":115},"} ${",[95,20512,20431],{"class":101},[95,20514,1848],{"class":115},[95,20516,20517],{"class":97,"line":1829},[95,20518,2770],{"class":101},[95,20520,20521],{"class":97,"line":1851},[95,20522,398],{"class":101},[95,20524,20525],{"class":97,"line":1858},[95,20526,174],{"class":101},[639,20528,20529,20532,20549],{},[642,20530,20531],{},"First, split the string into an array of words on spaces.",[642,20533,20534,20535],{},"The reducer receives four arguments:\n",[10080,20536,20537,20540,20543,20546],{},[642,20538,20539],{},"Accumulator",[642,20541,20542],{},"Current value",[642,20544,20545],{},"Current index",[642,20547,20548],{},"Source array",[642,20550,20551],{},"Whatever the reducer returns becomes the next accumulator, and after the last iteration that value is the final result.",[11,20553,20554,20555,20557,20558,73],{},"If ",[20,20556,20421],{}," still isn’t clear, read the docs on MDN: ",[61,20559,20562],{"href":20560,"rel":20561},"https:\u002F\u002Fdeveloper.mozilla.org\u002Fen-US\u002Fdocs\u002FWeb\u002FJavaScript\u002FReference\u002FGlobal_Objects\u002FArray\u002Freduce",[65],"Array.prototype.reduce()",[11,20564,20565,20566,20568],{},"On each iteration we return a string. If adding the next word would exceed the max character count, we flip a flag and append ",[20,20567,1219],{},"; otherwise we keep concatenating words.",[11,20570,20571,20572,20575],{},"This is useful when layout space is tight and the server returns long strings—you can run them through ",[20,20573,20574],{},"reduceText"," before rendering.",[11,20577,20578,20579,73],{},"That’s it for this article. These aren’t the only ways to line-clamp; CSS-Tricks has a roundup of more approaches: ",[61,20580,20583],{"href":20581,"rel":20582},"https:\u002F\u002Fcss-tricks.com\u002Fline-clampin\u002F",[65],"Line Clampin’ (Truncating Multiple Line Text)",[11,20585,15337],{},[1383,20587,20588],{},"html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s9eBZ, html code.shiki .s9eBZ{--shiki-default:#22863A;--shiki-dark:#85E89D}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":91,"searchDepth":105,"depth":105,"links":20590},[20591,20592],{"id":20154,"depth":105,"text":20155},{"id":20325,"depth":105,"text":20326},"2021-06-05","Line clamping explained—truncate multi-line text with -webkit-line-clamp in CSS, or trim long strings by character count with Array.reduce in JavaScript.",{"src":20596,"alt":20597},"\u002Fimages\u002Fblog\u002Fhow-to-limit-text-in-a-container-cover.jpg","Before and after cards showing full text versus line-clamped text with an ellipsis",{},"\u002Fblog\u002Fhow-to-limit-text-in-a-container",{"title":20123,"description":20594},"blog\u002Fhow-to-limit-text-in-a-container",[10813,1417,20603,3681],"Web Design","16zrGCmuJKQMlm1gXev9-WEv25TUBTNDLSsygElRjjo",{"id":20606,"title":20607,"body":20608,"date":20879,"description":20880,"draft":1404,"extension":1405,"featured":1404,"image":20881,"meta":20884,"navigation":1410,"path":20885,"seo":20886,"stem":20887,"tags":20888,"updatedAt":1420,"__hash__":20889},"blog\u002Fblog\u002Fthings-you-should-know-before-designing-your-first-web-page.md","Things You Should Know Before Designing Your First Web Page",{"type":8,"value":20609,"toc":20850},[20610,20613,20619,20622,20625,20630,20634,20637,20641,20644,20648,20651,20655,20658,20662,20665,20669,20672,20677,20681,20684,20688,20691,20695,20698,20702,20705,20711,20717,20723,20729,20735,20741,20747,20753,20757,20761,20764,20768,20771,20775,20778,20782,20796,20800,20807,20811,20814,20819,20823,20826,20830,20833,20837,20840,20844,20847],[11,20611,20612],{},"Presentation of a website is extremely important. It doesn’t matter how much work you’ve put into functionality or the back end—if the site looks cheap, you won’t get the views or traffic you deserve. This article covers the basics of web designing.",[11,20614,59,20615,73],{},[61,20616,66],{"href":20617,"rel":20618},"https:\u002F\u002Fmedium.com\u002F@akshattriv\u002Fthings-you-should-know-before-designing-your-first-web-page-basics-of-web-designing-ff4d0221c72f",[65],[75,20620,10739],{"id":20621},"typography",[11,20623,20624],{},"Your font reflects the look and feel of your website. The right font can make a rough-looking site feel classy. Be meticulous—these subtle choices are what make the experience memorable.",[8894,20626],{"alt":20627,"caption":20628,"src":20629},"Diagram labeling typography anatomy such as baseline, x-height, ascender, and serif","Knowing type anatomy helps you choose and set fonts with more intent.","\u002Fimages\u002Fblog\u002Fthings-you-should-know-before-designing-your-first-web-page\u002Ftypography-examples.jpeg",[3471,20631,20633],{"id":20632},"use-font-size-between-15px25px","Use font size between 15px–25px",[11,20635,20636],{},"Your text should be readable—not too small or too large. Keep body text between 15px and 25px. For example, the paragraph text on a typical well-designed page sits in that range.",[3471,20638,20640],{"id":20639},"use-big-fonts-for-headings","Use big fonts for headings",[11,20642,20643],{},"Go big on headings. They’re meant to pull focus to what you want people to notice first.",[3471,20645,20647],{"id":20646},"use-line-spacing","Use line spacing",[11,20649,20650],{},"Line spacing is the space between lines. It should sit around 120% to 150% of the paragraph font size. If the font size is 20px, spacing should land between about 24px and 30px.",[3471,20652,20654],{"id":20653},"_45-to-90-characters-per-line","45 to 90 characters per line",[11,20656,20657],{},"On a professional site, lines rarely run longer than about 90 characters. Longer lines force the eye to travel too far and get tiring after a few paragraphs. Don’t stretch text from one edge of the screen to the other.",[3471,20659,20661],{"id":20660},"use-good-fonts","Use good fonts",[11,20663,20664],{},"By good fonts I mean a solid font family. Lato and Open Sans are some of my favorites from Google Fonts. Prefer web fonts like those over default system stacks when you want smoother type—default faces often look less polished on the web.",[75,20666,20668],{"id":20667},"colors","Colors",[11,20670,20671],{},"Use flat UI colors. Search for flat UI color palettes and you’ll find plenty of sites with ready-to-use color codes. Flat colors work well on websites because they’re solid, uninterrupted, and consistent in stroke, depth, and shading.",[8894,20673],{"alt":20674,"caption":20675,"src":20676},"Grid of flat UI color swatches with hex codes","Flat UI palettes make it easier to pick a cohesive theme.","\u002Fimages\u002Fblog\u002Fthings-you-should-know-before-designing-your-first-web-page\u002Fflat-ui-color-palette.png",[3471,20678,20680],{"id":20679},"one-base-color","One base color",[11,20682,20683],{},"Choose one base color—your theme color—and carry it through the site. On many sites you’ll see blue used consistently as that base.",[3471,20685,20687],{"id":20686},"use-color-to-draw-attention","Use color to draw attention",[11,20689,20690],{},"Use color on buttons, links, or the background of a featured section to pull attention where it matters.",[3471,20692,20694],{"id":20693},"never-use-pure-black","Never use pure black",[11,20696,20697],{},"Prefer a dark grey or a deep blue over pure black. A pure black background kills light from the screen, so eyes work harder and open wider. White text can bleed into that black and look blurry.",[3471,20699,20701],{"id":20700},"choose-color-wisely","Choose color wisely",[11,20703,20704],{},"Different colors convey different messages. Match the palette to the type of site. Banking sites often use blue because it signals trust and integrity.",[11,20706,20707,20710],{},[13742,20708,20709],{},"Red"," works when you want power, passion, strength, or excitement. Brighter tones feel energetic; darker shades feel powerful and elegant.",[11,20712,20713,20716],{},[13742,20714,20715],{},"Orange"," draws attention without being as overpowering as red. It suggests cheerfulness and creativity, and can feel friendly, confident, and courageous.",[11,20718,20719,20722],{},[13742,20720,20721],{},"Yellow"," is energetic and suggests happiness and liveliness. It also associates with curiosity, intelligence, and brightness.",[11,20724,20725,20728],{},[13742,20726,20727],{},"Green"," is the color of harmony, nature, life, and health—and is often tied to money. In design it can feel balancing and calming.",[11,20730,20731,20734],{},[13742,20732,20733],{},"Blue"," suggests patience, peace, trustworthiness, and stability. It’s widely liked and associated with professionalism, trust, and honor—which is why so many large social networks use it.",[11,20736,20737,20740],{},[13742,20738,20739],{},"Purple"," has traditionally signaled power, nobility, and wealth. In design it can feel wise, royal, luxurious, or mysterious.",[11,20742,20743,20746],{},[13742,20744,20745],{},"Pink"," expresses romance, care, peace, and affection.",[11,20748,20749,20752],{},[13742,20750,20751],{},"Brown"," suggests relaxation and confidence—earthiness, durability, comfort, and reliability.",[75,20754,20756],{"id":20755},"working-with-images","Working with images",[3471,20758,20760],{"id":20759},"put-text-directly-on-the-image","Put text directly on the image",[11,20762,20763],{},"Overlay text on an image. Absolute positioning or a CSS background image both work.",[3471,20765,20767],{"id":20766},"put-your-text-in-a-box","Put your text in a box",[11,20769,20770],{},"Place the text in a transparent shape, then layer it over the image—simple and effective.",[3471,20772,20774],{"id":20773},"overlay-the-image","Overlay the image",[11,20776,20777],{},"Similar to text-on-image, but a transparent layer covers the whole image so the type stays readable.",[3471,20779,20781],{"id":20780},"blur-the-image","Blur the image",[11,20783,20784,20785,20787,20788,20791,20792,20795],{},"Use the CSS ",[20,20786,973],{}," property on an ",[20,20789,20790],{},"img"," element and set it to ",[20,20793,20794],{},"blur"," for a soft background effect.",[3471,20797,20799],{"id":20798},"the-floor-fade","The floor fade",[11,20801,20802,20803,20806],{},"One of my favorites: use ",[20,20804,20805],{},"linear-gradient"," to overlay a color that starts transparent and grows denser toward the bottom.",[75,20808,20810],{"id":20809},"working-with-icons","Working with icons",[11,20812,20813],{},"Use icons wherever they help. They add a premium feel—great for feature lists, actions, links, and buttons.",[8894,20815],{"alt":20816,"caption":20817,"src":20818},"IcoMoon icon set interface showing selectable SVG icons","Pick a style, export SVGs, and keep icons under your control.","\u002Fimages\u002Fblog\u002Fthings-you-should-know-before-designing-your-first-web-page\u002Ficomoon-icons.jpeg",[3471,20820,20822],{"id":20821},"icon-fonts-vs-svg","Icon fonts vs SVG",[11,20824,20825],{},"Learn SVG and how to use it in HTML. Icon fonts are less reliable—you depend on third-party hosts, they often fail to show up, and they can slow loading. Prefer SVG (Scalable Vector Graphics): they’re easier to control and usually render faster. Sites like IcoMoon let you pick a style, select icons, download them, and use them directly.",[75,20827,20829],{"id":20828},"spacing-and-layout","Spacing and layout",[11,20831,20832],{},"Always aim for a clean, simple layout.",[3471,20834,20836],{"id":20835},"using-whitespace","Using whitespace",[11,20838,20839],{},"Whitespace guides the flow of a page. Put space between elements, between groups of elements, and between sections. Build a rhythm that matches the message of the content.",[3471,20841,20843],{"id":20842},"keep-it-simple","Keep it simple",[11,20845,20846],{},"Keep things as simple and natural as possible. That’s the real secret—don’t go overboard with animations, gradients, and colors. Simplicity is what makes a site look good. That doesn’t mean boring; it means subtle.",[11,20848,20849],{},"That’s it for this article. Thanks for reading—see you next time.",{"title":91,"searchDepth":105,"depth":105,"links":20851},[20852,20859,20865,20872,20875],{"id":20621,"depth":105,"text":10739,"children":20853},[20854,20855,20856,20857,20858],{"id":20632,"depth":122,"text":20633},{"id":20639,"depth":122,"text":20640},{"id":20646,"depth":122,"text":20647},{"id":20653,"depth":122,"text":20654},{"id":20660,"depth":122,"text":20661},{"id":20667,"depth":105,"text":20668,"children":20860},[20861,20862,20863,20864],{"id":20679,"depth":122,"text":20680},{"id":20686,"depth":122,"text":20687},{"id":20693,"depth":122,"text":20694},{"id":20700,"depth":122,"text":20701},{"id":20755,"depth":105,"text":20756,"children":20866},[20867,20868,20869,20870,20871],{"id":20759,"depth":122,"text":20760},{"id":20766,"depth":122,"text":20767},{"id":20773,"depth":122,"text":20774},{"id":20780,"depth":122,"text":20781},{"id":20798,"depth":122,"text":20799},{"id":20809,"depth":105,"text":20810,"children":20873},[20874],{"id":20821,"depth":122,"text":20822},{"id":20828,"depth":105,"text":20829,"children":20876},[20877,20878],{"id":20835,"depth":122,"text":20836},{"id":20842,"depth":122,"text":20843},"2021-04-25","Basics of web design for beginners—typography, color, images, icons, spacing, and layout—so your first page looks intentional instead of unfinished.",{"src":20882,"alt":20883},"\u002Fimages\u002Fblog\u002Fthings-you-should-know-before-designing-your-first-web-page-cover.jpg","Basics of web designing cover with wireframe browser mockup, checklist, and layout pillars",{},"\u002Fblog\u002Fthings-you-should-know-before-designing-your-first-web-page",{"title":20607,"description":20880},"blog\u002Fthings-you-should-know-before-designing-your-first-web-page",[20603,3681,10813,10739],"U5G0OUfmFzwr_k-48bvfX0hYy-LsNPidoiWIfXFZExw",{"id":20891,"title":20892,"body":20893,"date":21453,"description":21454,"draft":1404,"extension":1405,"featured":1404,"image":21455,"meta":21458,"navigation":1410,"path":21459,"seo":21460,"stem":21461,"tags":21462,"updatedAt":1420,"__hash__":21464},"blog\u002Fblog\u002Fhow-i-got-my-first-internship-as-a-node-js-developer.md","How I Got My First Internship as a Node.js Developer",{"type":8,"value":20894,"toc":21439},[20895,20898,20904,20908,20911,20914,20919,20922,20926,20929,20932,20937,20940,20944,20947,20951,20954,21185,21189,21193,21196,21317,21321,21325,21328,21351,21355,21358,21362,21365,21378,21381,21407,21410,21414,21417,21420,21424,21427,21431,21434,21436],[11,20896,20897],{},"A few days ago, I approached a company for an internship program and after one coding round and three technical interviews, I received an offer and I took it. In this article, I’ll walk through how I applied, how I got shortlisted, what questions they asked, and some tips that can help you land an internship of your own.",[11,20899,59,20900,73],{},[61,20901,66],{"href":20902,"rel":20903},"https:\u002F\u002Fmedium.com\u002F@akshattriv\u002Fhow-i-got-my-first-internship-as-a-node-js-developer-ccb2617f0851",[65],[75,20905,20907],{"id":20906},"how-i-applied-for-the-internship","How I applied for the internship",[11,20909,20910],{},"My friend was an NLP intern at this company. He told me good things about the culture—they didn’t put too much pressure on interns—so I got interested. He said they were looking for web development interns, had posted an ad on Internshala, and that I should apply. I was skeptical, but he insisted I at least apply and not self-reject. So finally I applied.",[11,20912,20913],{},"They needed a developer with knowledge of Node and MySQL. I knew Node well, but I had never worked with MySQL before. I knew SQL, but I had never used it in a project—I was more of a MongoDB person. So I decided to build my next project with MySQL. It took seven days: I started with the front end, then moved to the back end with MySQL, and used WebSockets so things updated in real time.",[8894,20915],{"alt":20916,"src":20917,"caption":20918},"Node.js and MySQL logos side by side","\u002Fimages\u002Fblog\u002Fhow-i-got-my-first-internship\u002Fnode-mysql.png","The stack the role asked for—and the gap I closed before interviews.",[11,20920,20921],{},"Two weeks after I applied, they still hadn’t looked at my application. I assumed my resume and profile weren’t strong enough. So I rebuilt both, wrote a negative-persuasive introduction for interviews, and added the new project and skills to my resume.",[3471,20923,20925],{"id":20924},"reaching-out-on-linkedin","Reaching out on LinkedIn",[11,20927,20928],{},"I searched for the company on LinkedIn, found the CEO, and sent a polite message saying I had applied and hadn’t received a response. He replied with a recruiter’s email address.",[11,20930,20931],{},"I wrote a careful cover letter, attached my resume, and sent it. About two hours later, I got a call—not an interview, just a check on my skills, experience, and projects. She said we could move forward: coding round the next day at 11 AM. She would send the question paper at 11; I had to answer everything in JavaScript and upload my answers to a shared OneDrive folder.",[8894,20933],{"alt":20934,"src":20935,"caption":20936},"Screenshot of the cover letter email sent to the recruiter about a Node.js internship application","\u002Fimages\u002Fblog\u002Fhow-i-got-my-first-internship\u002Frecruiter-email.jpeg","The follow-up email that led to the screening call.",[11,20938,20939],{},"That’s how I got my chance. Write well. I think the call came because the cover letter was appealing and the resume was clear.",[75,20941,20943],{"id":20942},"coding-round","Coding round",[11,20945,20946],{},"I’ll share the questions from the paper, but not the solutions—you’ll learn more by working through them yourself.",[3471,20948,20950],{"id":20949},"question-on-strings","Question on strings",[11,20952,20953],{},"I had five stars in Problem Solving, JavaScript, and Python on HackerRank, so I had solved plenty of problems like this.",[43,20955,20956,20959,20967,20972],{},[11,20957,20958],{},"Write a function that takes a string as input and returns a count of each character in an object.",[11,20960,20961,23,20964],{},[13742,20962,20963],{},"Input:",[20,20965,20966],{},"\"Smart Operations & Edge Computing Platform\"",[11,20968,20969],{},[13742,20970,20971],{},"Output:",[86,20973,20975],{"className":11329,"code":20974,"language":11331,"meta":91,"style":91},"{\n  S: 1, m: 3, a: 3, r: 3, t: 4, ' ': 5, O: 1, p: 2,\n  e: 2, i: 2, o: 3, n: 2, s: 1, '&': 1, E: 1, d: 1,\n  g: 2, C: 1, u: 1, P: 1, l: 1, f: 1\n}\n",[20,20976,20977,20981,21054,21127,21181],{"__ignoreMap":91},[95,20978,20979],{"class":97,"line":98},[95,20980,102],{"class":101},[95,20982,20983,20986,20988,20990,20992,20995,20997,20999,21001,21003,21005,21007,21009,21012,21014,21016,21018,21021,21023,21026,21028,21030,21032,21035,21037,21040,21042,21044,21046,21048,21050,21052],{"class":97,"line":105},[95,20984,20985],{"class":108},"  S",[95,20987,112],{"class":101},[95,20989,444],{"class":338},[95,20991,909],{"class":101},[95,20993,20994],{"class":108},"m",[95,20996,112],{"class":101},[95,20998,484],{"class":338},[95,21000,909],{"class":101},[95,21002,61],{"class":108},[95,21004,112],{"class":101},[95,21006,484],{"class":338},[95,21008,909],{"class":101},[95,21010,21011],{"class":108},"r",[95,21013,112],{"class":101},[95,21015,484],{"class":338},[95,21017,909],{"class":101},[95,21019,21020],{"class":108},"t",[95,21022,112],{"class":101},[95,21024,21025],{"class":338},"4",[95,21027,909],{"class":101},[95,21029,20390],{"class":115},[95,21031,112],{"class":101},[95,21033,21034],{"class":338},"5",[95,21036,909],{"class":101},[95,21038,21039],{"class":108},"O",[95,21041,112],{"class":101},[95,21043,444],{"class":338},[95,21045,909],{"class":101},[95,21047,11],{"class":108},[95,21049,112],{"class":101},[95,21051,465],{"class":338},[95,21053,119],{"class":101},[95,21055,21056,21059,21061,21063,21065,21068,21070,21072,21074,21077,21079,21081,21083,21086,21088,21090,21092,21094,21096,21098,21100,21103,21105,21107,21109,21112,21114,21116,21118,21121,21123,21125],{"class":97,"line":122},[95,21057,21058],{"class":108},"  e",[95,21060,112],{"class":101},[95,21062,465],{"class":338},[95,21064,909],{"class":101},[95,21066,21067],{"class":108},"i",[95,21069,112],{"class":101},[95,21071,465],{"class":338},[95,21073,909],{"class":101},[95,21075,21076],{"class":108},"o",[95,21078,112],{"class":101},[95,21080,484],{"class":338},[95,21082,909],{"class":101},[95,21084,21085],{"class":108},"n",[95,21087,112],{"class":101},[95,21089,465],{"class":338},[95,21091,909],{"class":101},[95,21093,12506],{"class":108},[95,21095,112],{"class":101},[95,21097,444],{"class":338},[95,21099,909],{"class":101},[95,21101,21102],{"class":115},"'&'",[95,21104,112],{"class":101},[95,21106,444],{"class":338},[95,21108,909],{"class":101},[95,21110,21111],{"class":108},"E",[95,21113,112],{"class":101},[95,21115,444],{"class":338},[95,21117,909],{"class":101},[95,21119,21120],{"class":108},"d",[95,21122,112],{"class":101},[95,21124,444],{"class":338},[95,21126,119],{"class":101},[95,21128,21129,21132,21134,21136,21138,21141,21143,21145,21147,21150,21152,21154,21156,21159,21161,21163,21165,21168,21170,21172,21174,21177,21179],{"class":97,"line":134},[95,21130,21131],{"class":108},"  g",[95,21133,112],{"class":101},[95,21135,465],{"class":338},[95,21137,909],{"class":101},[95,21139,21140],{"class":108},"C",[95,21142,112],{"class":101},[95,21144,444],{"class":338},[95,21146,909],{"class":101},[95,21148,21149],{"class":108},"u",[95,21151,112],{"class":101},[95,21153,444],{"class":338},[95,21155,909],{"class":101},[95,21157,21158],{"class":108},"P",[95,21160,112],{"class":101},[95,21162,444],{"class":338},[95,21164,909],{"class":101},[95,21166,21167],{"class":108},"l",[95,21169,112],{"class":101},[95,21171,444],{"class":338},[95,21173,909],{"class":101},[95,21175,21176],{"class":108},"f",[95,21178,112],{"class":101},[95,21180,20407],{"class":338},[95,21182,21183],{"class":97,"line":147},[95,21184,174],{"class":101},[8894,21186],{"alt":21187,"src":21188},"Screenshot of the string character-count coding question from the internship assessment","\u002Fimages\u002Fblog\u002Fhow-i-got-my-first-internship\u002Fcoding-question-strings.png",[3471,21190,21192],{"id":21191},"question-on-object-traversal","Question on object traversal",[11,21194,21195],{},"Fairly easy—nothing unusual.",[43,21197,21198,21201,21303,21310],{},[11,21199,21200],{},"Write a function that takes a city name as input and returns the state name(s) from the following data structure:",[86,21202,21204],{"className":11329,"code":21203,"language":11331,"meta":91,"style":91},"const obj = {\n  India: {\n    Karnataka: ['Bangalore', 'Mysore'],\n    Maharashtra: ['Mumbai', 'Pune'],\n  },\n  USA: {\n    Texas: ['Dallas', 'Houston'],\n    IL: ['Chicago', 'Aurora', 'Pune'],\n  },\n}\n",[20,21205,21206,21217,21222,21237,21252,21256,21261,21276,21295,21299],{"__ignoreMap":91},[95,21207,21208,21210,21213,21215],{"class":97,"line":98},[95,21209,335],{"class":201},[95,21211,21212],{"class":338}," obj",[95,21214,341],{"class":201},[95,21216,353],{"class":101},[95,21218,21219],{"class":97,"line":105},[95,21220,21221],{"class":101},"  India: {\n",[95,21223,21224,21227,21230,21232,21235],{"class":97,"line":122},[95,21225,21226],{"class":101},"    Karnataka: [",[95,21228,21229],{"class":115},"'Bangalore'",[95,21231,909],{"class":101},[95,21233,21234],{"class":115},"'Mysore'",[95,21236,9535],{"class":101},[95,21238,21239,21242,21245,21247,21250],{"class":97,"line":134},[95,21240,21241],{"class":101},"    Maharashtra: [",[95,21243,21244],{"class":115},"'Mumbai'",[95,21246,909],{"class":101},[95,21248,21249],{"class":115},"'Pune'",[95,21251,9535],{"class":101},[95,21253,21254],{"class":97,"line":147},[95,21255,14671],{"class":101},[95,21257,21258],{"class":97,"line":160},[95,21259,21260],{"class":101},"  USA: {\n",[95,21262,21263,21266,21269,21271,21274],{"class":97,"line":171},[95,21264,21265],{"class":101},"    Texas: [",[95,21267,21268],{"class":115},"'Dallas'",[95,21270,909],{"class":101},[95,21272,21273],{"class":115},"'Houston'",[95,21275,9535],{"class":101},[95,21277,21278,21281,21284,21286,21289,21291,21293],{"class":97,"line":882},[95,21279,21280],{"class":101},"    IL: [",[95,21282,21283],{"class":115},"'Chicago'",[95,21285,909],{"class":101},[95,21287,21288],{"class":115},"'Aurora'",[95,21290,909],{"class":101},[95,21292,21249],{"class":115},[95,21294,9535],{"class":101},[95,21296,21297],{"class":97,"line":1788},[95,21298,14671],{"class":101},[95,21300,21301],{"class":97,"line":1793},[95,21302,174],{"class":101},[11,21304,21305,23,21307],{},[13742,21306,20963],{},[20,21308,21309],{},"\"Pune\"",[11,21311,21312,23,21314],{},[13742,21313,20971],{},[20,21315,21316],{},"[\"IL\", \"Maharashtra\"]",[8894,21318],{"alt":21319,"src":21320},"Screenshot of the nested object city-to-state traversal coding question","\u002Fimages\u002Fblog\u002Fhow-i-got-my-first-internship\u002Fcoding-question-object-traversal.png",[3471,21322,21324],{"id":21323},"making-a-get-request","Making a GET request",[11,21326,21327],{},"I wrote a solution that was only half correct—the retry logic was wrong.",[43,21329,21330,21333],{},[11,21331,21332],{},"For Node.js \u002F backend developers:",[10080,21334,21335,21342,21348],{},[642,21336,21337,21338,21341],{},"Make a GET request to ",[20,21339,21340],{},"https:\u002F\u002Frestcountries.eu\u002Frest\u002Fv2\u002Flang\u002Fes"," and store the response in a file.",[642,21343,21344,21345,21347],{},"Add retry logic if the request fails. Use the ",[20,21346,1698],{}," library or implement retries yourself.",[642,21349,21350],{},"For the GET request, choose any approach you prefer.",[8894,21352],{"alt":21353,"src":21354},"Screenshot of the GET request with retry logic coding question for Node.js candidates","\u002Fimages\u002Fblog\u002Fhow-i-got-my-first-internship\u002Fcoding-question-get-request.png",[11,21356,21357],{},"They gave me two hours. I finished in about thirty minutes and uploaded my answers. Three hours later, HR called: I had cleared the round, and a technical interview was scheduled for the next day at 1 PM.",[75,21359,21361],{"id":21360},"technical-interview-1","Technical interview 1",[11,21363,21364],{},"This was a one-hour technical interview—and an hour-long interview is no joke. The interviewer covered JavaScript from asynchrony to the event loop. We talked through the wrong retry code from the coding round, and he corrected it. There were two or three CSS questions; everything else was JavaScript.",[11,21366,21367,21368,21371,21372,11033,21375,73],{},"He also had me write code to find the maximum and second-maximum values in an array where numbers can repeat. For example, given ",[20,21369,21370],{},"[1, 2, 10, 19, 20, 20]",", the program should return ",[20,21373,21374],{},"20",[20,21376,21377],{},"19",[11,21379,21380],{},"Some more questions:",[639,21382,21383,21386,21389,21392,21395,21404],{},[642,21384,21385],{},"Explain the asynchronous nature of JavaScript.",[642,21387,21388],{},"What is the difference between vanilla JavaScript and Node.js?",[642,21390,21391],{},"Explain the event loop.",[642,21393,21394],{},"What is the difference between relatively and absolutely positioned elements?",[642,21396,21397,21398,11033,21401,15875],{},"What is the difference between ",[20,21399,21400],{},"display: inline",[20,21402,21403],{},"display: block",[642,21405,21406],{},"What is a closure?",[11,21408,21409],{},"There were more general JavaScript questions after that. I answered them, so I knew I had cleared it.",[75,21411,21413],{"id":21412},"technical-interview-2","Technical interview 2",[11,21415,21416],{},"This one was shorter—about twenty minutes—and more focused on the front end. The interviewer asked about my front-end projects and any work that used client-side rendering. I walked through a few and demoed one.",[11,21418,21419],{},"He asked if I knew any front-end frameworks. I said not yet, but that I would learn React soon (yes, React is a library—I still called it a framework in the moment). Then a few CSS questions, and he had me center a child inside a parent on JSFiddle. Overall it was short and straightforward.",[75,21421,21423],{"id":21422},"final-interview","Final interview",[11,21425,21426],{},"The CTO ran this round. Many questions overlapped with earlier interviews. He also asked why I should be hired and why I wanted the role—typical last-round questions. After a minute he said I got the job. We talked through stipend, joining date, working hours, and what I would work on.",[75,21428,21430],{"id":21429},"takeaways","Takeaways",[11,21432,21433],{},"Be polite. Be humble, but not needy. Have a strong resume and cover letter. Communicate clearly. Most important: know your subject well—without that, the rest doesn’t matter.",[11,21435,1378],{},[1383,21437,21438],{},"html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}",{"title":91,"searchDepth":105,"depth":105,"links":21440},[21441,21444,21449,21450,21451,21452],{"id":20906,"depth":105,"text":20907,"children":21442},[21443],{"id":20924,"depth":122,"text":20925},{"id":20942,"depth":105,"text":20943,"children":21445},[21446,21447,21448],{"id":20949,"depth":122,"text":20950},{"id":21191,"depth":122,"text":21192},{"id":21323,"depth":122,"text":21324},{"id":21360,"depth":105,"text":21361},{"id":21412,"depth":105,"text":21413},{"id":21422,"depth":105,"text":21423},{"id":21429,"depth":105,"text":21430},"2021-04-20","How I applied, got shortlisted, and cleared a coding round plus three technical interviews for my first Node.js internship—plus tips that helped me land the offer.",{"src":21456,"alt":21457},"\u002Fimages\u002Fblog\u002Fhow-i-got-my-first-internship-as-a-node-js-developer-cover.jpg","Laptop on a desk with sticky notes, glasses, and a notebook during a late-night coding session",{},"\u002Fblog\u002Fhow-i-got-my-first-internship-as-a-node-js-developer",{"title":20892,"description":21454},"blog\u002Fhow-i-got-my-first-internship-as-a-node-js-developer",[15365,21463,19929,1417],"Internships","spcJSjGs7Mp1EYCcGElYUewKHEjirloxxQYlz_c83U0",{"id":21466,"title":21467,"body":21468,"description":21721,"extension":1405,"meta":21722,"navigation":1410,"path":21723,"seo":21724,"stem":21725,"__hash__":21726},"pages\u002Fprivacy-policy.md","Privacy Policy",{"type":8,"value":21469,"toc":21707},[21470,21475,21478,21481,21485,21494,21497,21517,21520,21523,21526,21543,21547,21550,21553,21557,21560,21575,21579,21586,21595,21598,21605,21609,21612,21615,21619,21622,21625,21629,21632,21635,21638,21642,21649,21652,21656,21659,21662,21666,21669,21672,21675,21678,21682,21685,21689,21692],[11,21471,21472],{},[13742,21473,21474],{},"Last updated: December 5, 2026",[11,21476,21477],{},"I respect your privacy. This website is my personal portfolio and technical blog, operated by Akshat Trivedi in India. Its purpose is to share my work, technical articles, projects, and professional experience.",[11,21479,21480],{},"This Privacy Policy explains what information is processed when you visit this website, why it is used, and the choices available to you.",[75,21482,21484],{"id":21483},"_1-website-analytics","1. Website analytics",[11,21486,21487,21488,21493],{},"This website uses ",[61,21489,21492],{"href":21490,"rel":21491},"https:\u002F\u002Fumami.is\u002F",[65],"Umami",", a privacy-focused web analytics platform, to understand how visitors discover and interact with its content.",[11,21495,21496],{},"Depending on the analytics configuration, Umami may collect:",[639,21498,21499,21502,21505,21508,21511,21514],{},[642,21500,21501],{},"Pages visited and page titles.",[642,21503,21504],{},"Visit timestamps, session duration, and navigation patterns.",[642,21506,21507],{},"Referring websites and campaign information, such as UTM parameters.",[642,21509,21510],{},"Browser, operating system, device type, screen size, and language.",[642,21512,21513],{},"Approximate geographical information, such as country, region, or city.",[642,21515,21516],{},"Basic interaction events, such as clicks on links or buttons, where enabled.",[11,21518,21519],{},"I use this information to understand which articles are useful, measure website traffic, identify content and usability improvements, and evaluate the effectiveness of content shared on other platforms.",[11,21521,21522],{},"Umami's standard analytics tracker does not use tracking cookies. It may process your IP address and browser information to estimate location and generate a pseudonymous visitor or session identifier. Raw IP addresses are not stored as fields in standard Umami analytics records, although infrastructure providers may process or log them for operational and security purposes.",[11,21524,21525],{},"I do not use analytics to build advertising profiles, track you across unrelated websites, or intentionally collect your name, email address, or other directly identifying information. Google Analytics is no longer used on this website.",[11,21527,21528,21529,909,21534,11743,21539,73],{},"For further information, see ",[61,21530,21533],{"href":21531,"rel":21532},"https:\u002F\u002Fdocs.umami.is\u002Fdocs\u002Ffaq",[65],"Umami's documentation",[61,21535,21538],{"href":21536,"rel":21537},"https:\u002F\u002Fdocs.umami.is\u002Fdocs\u002Fmetric-definitions",[65],"metric definitions",[61,21540,21467],{"href":21541,"rel":21542},"https:\u002F\u002Fumami.is\u002Fprivacy",[65],[75,21544,21546],{"id":21545},"_2-cookies-and-local-storage","2. Cookies and local storage",[11,21548,21549],{},"This website does not use analytics cookies. Browser storage may be used to remember functional preferences, such as your selected light or dark theme.",[11,21551,21552],{},"These preferences are intended to improve your browsing experience, not to identify you or track your activity across websites. You can clear locally stored preferences through your browser settings.",[75,21554,21556],{"id":21555},"_3-website-hosting-and-security","3. Website hosting and security",[11,21558,21559],{},"This website is hosted on GitHub Pages. GitHub may collect technical information, including IP addresses and request logs, to deliver the website, maintain its infrastructure, and protect its services against abuse and security threats.",[11,21561,21562,21563,21568,21569,21574],{},"GitHub handles this information in accordance with its own ",[61,21564,21567],{"href":21565,"rel":21566},"https:\u002F\u002Fdocs.github.com\u002Fen\u002Fsite-policy\u002Fprivacy-policies\u002Fgithub-general-privacy-statement",[65],"Privacy Statement",". Its ",[61,21570,21573],{"href":21571,"rel":21572},"https:\u002F\u002Fdocs.github.com\u002Fen\u002Fpages\u002Fgetting-started-with-github-pages\u002Fwhat-is-github-pages",[65],"GitHub Pages documentation"," also explains its visitor IP logging practices.",[75,21576,21578],{"id":21577},"_4-community-chess-feature","4. Community chess feature",[11,21580,21581,21582,21585],{},"When the optional ",[13742,21583,21584],{},"Your Move"," section is enabled, your browser may request the current shared game and cumulative results, and may submit Black moves, through a Supabase-hosted API (Edge Function and Postgres database).",[11,21587,21588,21589,21594],{},"That service processes the technical information needed to operate the shared game, such as request metadata and move payloads (for example the selected squares and revision checks). The public site does not create a chess account for you. Infrastructure providers such as ",[61,21590,21593],{"href":21591,"rel":21592},"https:\u002F\u002Fsupabase.com\u002F",[65],"Supabase"," may also process IP addresses and request logs for delivery, security, and abuse prevention, according to their own policies. I do not intentionally store visitor IP addresses in the chess game database for profiling.",[11,21596,21597],{},"I do not use chess feature requests to build advertising profiles, track you across unrelated websites, or intentionally collect your name, email address, or other directly identifying information through this feature. No account is required to view the board or attempt a community move.",[11,21599,21528,21600,73],{},[61,21601,21604],{"href":21602,"rel":21603},"https:\u002F\u002Fsupabase.com\u002Fprivacy",[65],"Supabase's Privacy Policy",[75,21606,21608],{"id":21607},"_5-contact-and-communications","5. Contact and communications",[11,21610,21611],{},"If you voluntarily contact me by email or through a linked professional platform, I may receive information you choose to share, such as your name, email address, and message.",[11,21613,21614],{},"I use that information to respond to your enquiry and, where relevant, continue the conversation. I do not intentionally add your contact information to marketing lists without your permission.",[75,21616,21618],{"id":21617},"_6-legal-bases-for-processing","6. Legal bases for processing",[11,21620,21621],{},"Where applicable data protection law requires a legal basis, I rely on legitimate interests for necessary website operation, security, privacy-conscious audience measurement, and operating the optional shared chess feature, where permitted and where those interests are not overridden by your rights. I process correspondence to respond to your requests and may process information to comply with legal obligations.",[11,21623,21624],{},"Where consent is required by applicable law, processing that requires consent will take place only after it has been obtained. You may withdraw that consent at any time.",[75,21626,21628],{"id":21627},"_7-sharing-and-international-processing","7. Sharing and international processing",[11,21630,21631],{},"I do not sell or rent visitor information, or share it for targeted advertising.",[11,21633,21634],{},"Information may be processed by hosting, analytics, and chess-feature service providers for the purposes described in this policy, or disclosed where necessary to comply with a legal obligation or protect the security of the website.",[11,21636,21637],{},"The website and its service providers may process information outside your country of residence. Where applicable, international transfers of personal information are subject to the safeguards required by relevant data protection law.",[75,21639,21641],{"id":21640},"_8-data-retention","8. Data retention",[11,21643,21644,21645,21648],{},"Analytics records are retained for ",[13742,21646,21647],{},"6 months",", subject to the configured retention settings and any applicable backup or legal retention requirements.",[11,21650,21651],{},"Information received through correspondence is retained for as long as reasonably necessary to address the enquiry, maintain relevant records, or comply with legal obligations. Infrastructure logs and chess-feature operational data are retained according to the relevant provider's policies and the technical needs of the shared game.",[75,21653,21655],{"id":21654},"_9-external-links","9. External links",[11,21657,21658],{},"This website may contain links to external websites, including GitHub, LinkedIn, Medium, X (formerly Twitter), Chess.com, and technical documentation.",[11,21660,21661],{},"Once you follow an external link, the destination website's own privacy practices apply. I do not control the information collected by those websites and encourage you to review their respective privacy policies.",[75,21663,21665],{"id":21664},"_10-your-privacy-choices-and-rights","10. Your privacy choices and rights",[11,21667,21668],{},"You can use browser privacy settings or content-blocking extensions to prevent analytics requests and clear locally stored website preferences. If you do not want the chess feature to send requests from your browser, leave the section unused or avoid enabling a live chess build.",[11,21670,21671],{},"Depending on the laws applicable to you, you may also have the right to request access to, correction of, or deletion of personal information; restrict or object to certain processing; withdraw consent; request portability of eligible data; or raise a complaint with the relevant data protection authority.",[11,21673,21674],{},"Because standard website analytics are not linked to your name or email address, I may not be able to identify individual analytics records associated with you. I will not collect additional identifying information solely to identify an otherwise unidentifiable analytics record unless required by law.",[11,21676,21677],{},"To exercise your applicable rights or raise a privacy concern, please use the contact details below.",[75,21679,21681],{"id":21680},"_11-changes-to-this-policy","11. Changes to this policy",[11,21683,21684],{},"I may update this Privacy Policy as the website, its features, or applicable privacy requirements change. The latest version will be available on this page, with the date of the most recent update shown at the top.",[75,21686,21688],{"id":21687},"_12-contact","12. Contact",[11,21690,21691],{},"If you have questions about this Privacy Policy, how information is handled, or your privacy rights, please contact:",[11,21693,21694,21697,21700,21701],{},[13742,21695,21696],{},"Akshat Trivedi",[21698,21699],"br",{},"\nEmail: ",[13742,21702,21703],{},[61,21704,21706],{"href":21705},"mailto:akshattriv.iit@gmail.com","akshattriv.iit@gmail.com",{"title":91,"searchDepth":105,"depth":105,"links":21708},[21709,21710,21711,21712,21713,21714,21715,21716,21717,21718,21719,21720],{"id":21483,"depth":105,"text":21484},{"id":21545,"depth":105,"text":21546},{"id":21555,"depth":105,"text":21556},{"id":21577,"depth":105,"text":21578},{"id":21607,"depth":105,"text":21608},{"id":21617,"depth":105,"text":21618},{"id":21627,"depth":105,"text":21628},{"id":21640,"depth":105,"text":21641},{"id":21654,"depth":105,"text":21655},{"id":21664,"depth":105,"text":21665},{"id":21680,"depth":105,"text":21681},{"id":21687,"depth":105,"text":21688},"Learn how this website handles visitor information and uses privacy-focused analytics.",{},"\u002Fprivacy-policy",{"title":21467,"description":21721},"privacy-policy","hbDoZ5oCU6k6QGr5BOrwcj0b6QxC79DpJI1U1j11rOM",1796414207480]