<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rio Chandra</title>
    <description>The latest articles on DEV Community by Rio Chandra (@riochndr).</description>
    <link>https://dev.to/riochndr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F626328%2F838a1cdd-19de-46d7-896c-a21f15b52b50.jpg</url>
      <title>DEV Community: Rio Chandra</title>
      <link>https://dev.to/riochndr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/riochndr"/>
    <language>en</language>
    <item>
      <title>DX: bind mounting node_modules are not work properly</title>
      <dc:creator>Rio Chandra</dc:creator>
      <pubDate>Tue, 13 Feb 2024 08:11:17 +0000</pubDate>
      <link>https://dev.to/riochndr/dx-bind-mounting-nodemodules-are-not-work-properly-4an7</link>
      <guid>https://dev.to/riochndr/dx-bind-mounting-nodemodules-are-not-work-properly-4an7</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Nodejs
&lt;/h3&gt;

&lt;p&gt;Nodejs have couple of file to look at : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;package.json&lt;/li&gt;
&lt;li&gt;node_modules&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These file are the location of package that used at nodejs application&lt;/p&gt;

&lt;h3&gt;
  
  
  Docker
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Docker is a platform designed to help developers build, share, and run modern applications&lt;/strong&gt; &lt;a href="https://hub.docker.com"&gt;https://hub.docker.com&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I will not explain more here.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Development using docker
&lt;/h3&gt;

&lt;p&gt;Usually we develop nodejs app using docker and use &lt;a href="https://docs.docker.com/storage/volumes/"&gt;Docker Volume&lt;/a&gt; to able bind directory host to container directly, so we able to update and restart the app whenever we do development. But there is some problem, We actually can't do that because node_modules [at host] are not sync with node_modules [at container].&lt;/p&gt;

&lt;p&gt;This is problem since many years, I as developer always face this problem every year. So I will wrap all resource here to make you easy to track all discussion.&lt;/p&gt;

&lt;h3&gt;
  
  
  Report
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Is there a way that is not too impractical to Dockerize a Node.js app without mounting the node_modules folder of your host machine to the container when developing ?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/q/59556554/11218747"&gt;stackoverflow question by Ewaren&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ewaren (the person who ask that) explain what we expect when using nodejs + docker, this is what he (also we as developer) expected : &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;able to setup dev environtment on new machine without do manually install&lt;/li&gt;
&lt;li&gt;The app easy to run only use &lt;code&gt;docker compose up&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The runtime environtment is consistent across all machines.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thanks to him, he explain so much and give some "good solution" to implement, but each solution have plus and minus. You can follow up some solution at &lt;a href="https://stackoverflow.com/q/59556554/11218747"&gt;here&lt;/a&gt;. Here is list of his solution.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;mount all files to host folder and run &lt;code&gt;npm install&lt;/code&gt; first before running the docker compose. This is classic solution, but we do not want this because not plassform-spesific.&lt;/li&gt;
&lt;li&gt;mounta all files to host folder and mount &lt;code&gt;node_modules&lt;/code&gt; container to anonymous volume. This will prevent node_modules collide with host folder. But we have to re-build the container everytime package added.&lt;/li&gt;
&lt;li&gt;mount all files to named volumes. This is clean, but we can't do development at host, but use &lt;a href="https://code.visualstudio.com/docs/devcontainers/containers"&gt;vscode container instead&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Node docker repository
&lt;/h3&gt;

&lt;p&gt;Finally !!, someone make template to easy to start !. &lt;a href="https://github.com/BretFisher/node-docker-good-defaults"&gt;Node.js + Docker for Showing Good Defaults in Using Node.js with Docker&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This repository have a lot feature !, &lt;strong&gt;dev as close to prod as you can&lt;/strong&gt;, &lt;strong&gt;edit locally while code runs in container&lt;/strong&gt;, &lt;strong&gt;production-minded features&lt;/strong&gt;, and more.&lt;/p&gt;

&lt;p&gt;They using &lt;em&gt;dirty solution&lt;/em&gt;, but valid. They do different folder node_modules. node_modules container will saved in container at &lt;code&gt;/opt/node_app&lt;/code&gt; and mount project folder host to &lt;code&gt;/opt/node_app/app&lt;/code&gt; (sub folder). This make 2 different node_modules, they claim we are still able to development and restart app like usual.&lt;/p&gt;

&lt;p&gt;But, when I run it only using &lt;code&gt;docker compose up&lt;/code&gt; (of course use &lt;code&gt;sudo&lt;/code&gt; for linux). folder &lt;code&gt;node_modules&lt;/code&gt; are empty !. I don't know what happen.&lt;/p&gt;

&lt;p&gt;Someone have similar problem, &lt;a href="https://github.com/BretFisher/node-docker-good-defaults/issues/66"&gt; Bind mountings not working as expected #66&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/BretFisher/node-docker-good-defaults/issues/28#issuecomment-584895934"&gt;Bread Fisher have 2 solution&lt;/a&gt;. He also mention this from his &lt;a href="https://www.bretfisher.com/docker-mastery-for-nodejs/"&gt;docker for node.js course&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Solution 1:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don't move node_modules up a directory. Leave everything in the standard single dir and do a single bind-mount in the compose file.&lt;/li&gt;
&lt;li&gt;Before doing a docker-compose up the first time, we'll need to do a docker-compose run  npm install to see the node_modules that are bind-mounted to the host. This is the main disadvantage of this Solution.&lt;/li&gt;
&lt;li&gt;This also means on macOS/Windows that you can't "dual develop" by sometimes using native host Node.js and sometimes using Docker-based Node.js because the binaries in node_modules (if you have them) would be incompatible. This is a disadvantage if you don't always use docker to run this code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Solution 2:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Similar to this repo's setup, you would move node_modules and package*.json up a level in the file path, so they can be separated from host node_modules. This adds complexity to the setup but also provides the flexibility of developing with a different node_modules on the host than what is in the container.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ok, there is no different with early solution that we found. Similar solution, only this solution that we can rely on.&lt;/p&gt;

&lt;p&gt;So, basically you have to &lt;strong&gt;Install the package on host manually&lt;/strong&gt;, to make package are sync with &lt;strong&gt;container system&lt;/strong&gt;, we have to use &lt;code&gt;docker compose exec&lt;/code&gt; or &lt;code&gt;docker compose run&lt;/code&gt; to run the npm install.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;## use exec when container already run&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;docker compose &lt;span class="nb"&gt;exec &lt;/span&gt;node npm i

&lt;span class="c"&gt;## use run before run container&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;docker compose run node npm i 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;You see &lt;code&gt;node&lt;/code&gt; at that script, that refer to name of service in &lt;code&gt;docker-compose.yml&lt;/code&gt; file.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;He also mention at &lt;a href="https://github.com/BretFisher/node-docker-good-defaults/issues/91#issuecomment-1251597538"&gt;other issue&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-w&lt;/span&gt; /opt/node_app node npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Not good ending.&lt;/p&gt;

&lt;p&gt;There is no solid solution for node.js environtment to fit with our expectation, you have to choose some method and face the fate. You have to install the package first !.&lt;/p&gt;

&lt;p&gt;Thanks.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>docker</category>
    </item>
    <item>
      <title>Store Persist State to URL Nuxt Js</title>
      <dc:creator>Rio Chandra</dc:creator>
      <pubDate>Sun, 10 Oct 2021 11:23:47 +0000</pubDate>
      <link>https://dev.to/riochndr/store-persist-state-to-url-nuxt-js-3kla</link>
      <guid>https://dev.to/riochndr/store-persist-state-to-url-nuxt-js-3kla</guid>
      <description>&lt;p&gt;Multi-tenancy become more complex problem for authentication and authorization. Nuxt js as front-end app need to adapte to this problem&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P-g-7mMe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mixvkzb526hjgvvh8f5s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P-g-7mMe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mixvkzb526hjgvvh8f5s.png" alt="Multiapp for one user" width="571" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The scenario :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nuxt Js is a front-end App,&lt;/li&gt;
&lt;li&gt;a user can login as company 1 or company 2. each company have an id.&lt;/li&gt;
&lt;li&gt;when hit an API, front-end must send company id&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The problem is, how to make Front-end store company id and can be used when Request API. I have some approach to store company id but only 1 way to solve this problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  Token
&lt;/h3&gt;

&lt;p&gt;You can store company id on token, this is good from API perspective immediately know user information, but you need to recall API to re-create token if user change company id, which is this is not ideal when user switch to other company id periodically&lt;/p&gt;

&lt;h3&gt;
  
  
  Localstorage
&lt;/h3&gt;

&lt;p&gt;Localstorage is deadly simple to store information in browser client. we can store it to localstorage and use it when it needed. But, the problem is when we want to use on Server Side. Since Nuxt Js Support Server Side Rendering, nuxt cannot read localstorage because localstorage only accessable on client side.&lt;/p&gt;

&lt;h3&gt;
  
  
  State Management (Vuex)
&lt;/h3&gt;

&lt;p&gt;State management solve problem localstorage to access from Server Side and accessable from any page on website. But, the problem is state management will be reset when user refresh the page.&lt;/p&gt;

&lt;h3&gt;
  
  
  Param URL
&lt;/h3&gt;

&lt;p&gt;We can store some variable on Parameter URL and use the param when it needed, it persistence and user know 'where are they' only from URL.&lt;/p&gt;

&lt;p&gt;I see this is the absolute solution to store company id or other variable. Google do this for a long time, when you logged in to google with different account, google &lt;br&gt;
store state current user on query URL&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sekV9G3j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ru5givi8jbw5wrpa90xw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sekV9G3j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ru5givi8jbw5wrpa90xw.png" alt="How to Google store user logged in on URL" width="800" height="177"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kUQwEf16--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ppkluh0sl2wgdq10k51.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kUQwEf16--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ppkluh0sl2wgdq10k51.png" alt="How to Google store user logged in on URL(Gmail.com)" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How to Google store user logged in on URLHow to Google store user logged in on URL (Gmail.com)Quite a nice idea. But we found new problem, we should add query or param to every &lt;code&gt;&amp;lt;a href /&amp;gt;&lt;/code&gt; tags. Every programmer never do it manually. Lets create Middleware to solve this problem&lt;/p&gt;

&lt;p&gt;Automatically Query Url Update Middleware Nuxt (AQUUMN)&lt;br&gt;
I have no idea about this technique name&lt;br&gt;
We need something to update query URL every change route, we can do this in Middleware that provide by Nuxt Js (because we use Nuxt Js). We will store the company id in State Management and URL, you got the point right ?. We will create middleware with flow like this :&lt;br&gt;
Whenever User change Route, get the Company id from state management&lt;br&gt;
Redirect user to new URL that Query company id included&lt;br&gt;
Whenever browser refresh the page, state management will be reset and we need to restore from URL query company id&lt;/p&gt;

&lt;p&gt;Now we have persist parameter user's company id on URL, no matter what URL requested because middleware will put Query company id to URL.&lt;br&gt;
Middleware Code&lt;br&gt;
First of all, we need to create state management for company id. In this case I use Vuex. This is companyStore.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;companyId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mutations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setCompanyId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;companyId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;companyId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;labId&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;Create&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;middleware&lt;/span&gt; &lt;span class="nx"&gt;called&lt;/span&gt; &lt;span class="nx"&gt;authApp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ts&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;folder&lt;/span&gt; &lt;span class="nf"&gt;middleware &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;there&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;no&lt;/span&gt; &lt;span class="nx"&gt;folder&lt;/span&gt; &lt;span class="nx"&gt;middleware&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt; &lt;span class="nx"&gt;one&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Context&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@nuxt/types&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;companyId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;companyId&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="cm"&gt;/**
* this middleware make apps to require query `companyId`
* ex: /home/?companyId=123-24123-4123
*/&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;excludeRoute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/login&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/login/app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;companyIdQuery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;companyId&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;companyIdStore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;companyStore&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;companyId&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;companyIdQuery&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;companyIdStore&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;?companyId=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;     &lt;span class="nx"&gt;companyIdStore&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;excludeRoute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/login/app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;companyIdStore&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;companyStore/setcompanyId&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;companyIdQuery&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;companyIdStore&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;companyIdQuery&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;companyStore/setcompanyId&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;companyIdQuery&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I add array excludeRoute as a route redirect if company id is null, user require to select their current company that selected on the app.&lt;br&gt;
lastly, register this middleware to &lt;code&gt;nuxt.config.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;middleware&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;authApp&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...]&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can get the company id from $route or $store&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;companyIdStore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;companyStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;companyId&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;companyIdRoute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;companyId&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finish&lt;br&gt;
That's it how I solve this problem, I didn't found another article write about Nuxt to create persistence state like this, so I create one. Hope this help you.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>nuxt</category>
      <category>state</category>
    </item>
  </channel>
</rss>
