baked-recipe-admin
Version:
Baked is an opinionated framework for .NET in backend and Nuxt in frontend. This is a recipe package that brings together all the components one needs for an Admin UI.
36 lines (31 loc) • 926 B
JavaScript
import { defineNuxtPlugin, useRuntimeConfig } from "#app";
import { useCache, useToken } from "#imports";
export default defineNuxtPlugin({
name: "cache-user",
enforce: "pre",
setup(nuxtApp) {
const { public: { cacheUser } } = useRuntimeConfig();
const cache = useCache("cache:user", cacheUser.expirationInMinutes);
const { $fetchInterceptors } = nuxtApp;
$fetchInterceptors.register(
"cache-user",
async({ request, options }, next) => {
if(options.attributes["client-cache"] !== "user") {
return await next();
}
return await cache.getOrCreate(cache.buildKey(request, options.query), next);
},
// should run before other interceptors
-10
);
},
hooks: {
"app:mounted"() {
const cache = useCache("cache:user");
const token = useToken();
token.onChanged(() => {
cache.clear();
});
}
}
});