I'm having an authorization (JWT) related caching issue when using Nuxt.
This is the nuxtServerInit
action, where I set the access token:
// store/index.js
import cookie from 'cookie';
export const state = () => ({
authCookie: 'MyAuthCookie',
});
export const actions = {
async nuxtServerInit({ dispatch, commit, state }, { req }) {
// Check for access token
const accessToken = req.headers.cookie &&
cookie.parse(req.headers.cookie)[state.authCookie];
// Set the access token, if there is one
if (accessToken) {
commit('auth/setAccessToken', accessToken);
}
},
};
The accessToken
state is later used to set the Authorization
header for all future requests in this plugin:
// plugins/axios.js
export default function ({ app, store }) {
app.$axios.onRequest((config) => {
// Set the `Authorization` header for future requests if we're logged in
if (store.getters['auth/isLoggedIn']) {
config.headers.common.Authorization = `Bearer ${store.state.auth.accessToken}`;
}
});
};
Nuxt stores data shared between client and server in a global window.__NUXT__
variable in an inlined <script>
tag, and because I am aggressively caching the page in question (using Akamai), the access token will never be updated.
Is there a better way of handling this, and to prevent the access token from being cached? Or in other words how do I prevent the accessToken
state to be written to the global __NUXT__
variable?