link1096 link1097 link1098 link1099 link1100 link1101 link1102 link1103 link1104 link1105 link1106 link1107 link1108 link1109 link1110 link1111 link1112 link1113 link1114 link1115 link1116 link1117 link1118 link1119 link1120 link1121 link1122 link1123 link1124 link1125 link1126 link1127 link1128 link1129 link1130 link1131 link1132 link1133 link1134 link1135 link1136 link1137 link1138 link1139 link1140 link1141 link1142 link1143 link1144 link1145 link1146 link1147 link1148 link1149 link1150 link1151 link1152 link1153 link1154 link1155 link1156 link1157 link1158 link1159 link1160 link1161 link1162 link1163 link1164 link1165 link1166 link1167 link1168 link1169 link1170 link1171 link1172 link1173 link1174 link1175 link1176 link1177 link1178 link1179 link1180 link1181 link1182 link1183 link1184 link1185 link1186 link1187 link1188 link1189 link1190 link1191 link1192 link1193 link1194 link1195 link1196 link1197 link1198 link1199 link1200 link1201 link1202 link1203 link1204 link1205 link1206 link1207 link1208 link1209 link1210 link1211 link1212 link1213 link1214 link1215 link1216 link1217 link1218 link1219 link1220 link1221 link1222 link1223 link1224 link1225 link1226 link1227 link1228 link1229 link1230 link1231 link1232

[Vue.js] created hook for vuex / nuxtClientInit?

I was wondering whats the best way to do something like nuxtClientInit. I’m trying to load the Auth State as early as possible on the client and save it in my vuex store but not on the server side. It would be great if vuex had something like the created hook for components but that doesn’t exist to my knowledge.

How could I achieve this behavior? One way could be calling an action from a layout but is that the best way?

Solution :

I understand the nuxt team are working on a nuxtClientInit feature but before they release that you could just make the own. To understand the workflow that nuxt undertakes when there is a request you can look at the lifecycle here. This shows that nuxtServerInit is called first then middleware. During this middleware call nuxt.config.js is served and this contains the custom configuration. Part of this is ‘plugins’ which as the docs say,

This option lets you define JavaScript plugins that should be run
before instantiating the root Vue.js application.

So if you write a plugin to call a store action you can then get and set the local storage from there. So, start with a nuxt-client-init plugin:

//nuxt-client-init.client.js
export default async context => {
await context.store.dispatch(‘nuxtClientInit’, context)
}

then add the plugin to nuxt.config.js:

//nuxt.config.js
plugins: [
‘~/plugins/nuxt-client-init.client.js’
],

If you notice the naming convention here, the .client.js part of the plugin tells nuxt this is a client only plugin and is shorthand for ‘{ src: ‘~/plugins/nuxt-client-init.js’, mode: ‘client’ }’ which since nuxt 2.4 is the way to define the old ‘{ src: ‘~/plugins/nuxt-client-init.js’, ssr: false }’. Anyway, you now have a call to the store so you can have an action to call from local storage and set a state item.

//store/index.js
export const actions = {
nuxtClientInit({ commit }, { req }) {
const autho = localStorage.getItem(‘auth._token.local’) //or whatever yours is called
commit(‘SET_AUTHO’, autho)
console.log(‘From nuxtClientInit - ‘+autho)
}
}

You probably need to restart the app for that to all take effect but you are then getting and using the Auth State without any of that pesky nuxtServerInit business.