nuxt-multi-cache
Advanced tools
Comparing version 3.3.1 to 3.3.2
@@ -1,3 +0,4 @@ | ||
import type { NuxtApp, AsyncDataOptions } from 'nuxt/app'; | ||
import type { DefaultAsyncDataValue } from '#app/defaults'; | ||
import type { NuxtApp, AsyncDataOptions, AsyncData, NuxtError } from 'nuxt/app'; | ||
import type { DefaultAsyncDataErrorValue, DefaultAsyncDataValue } from '#app/defaults'; | ||
import type { PickFrom } from '#app/composables/asyncData'; | ||
type KeysOf<T> = Array<T extends T ? (keyof T extends string ? keyof T : never) : never>; | ||
@@ -46,3 +47,3 @@ type ValueOrMethod<T extends number | string[] | undefined, ResT> = ((v: ResT) => T) | T; | ||
*/ | ||
export declare function useCachedAsyncData<ResT, DataT = ResT, PickKeys extends KeysOf<DataT> = KeysOf<DataT>, DefaultT = null>(key: string, handler: (app?: NuxtApp) => Promise<ResT>, providedOptions?: CachedAsyncDataOptions<ResT, DataT, PickKeys, DefaultT>): any; | ||
export declare function useCachedAsyncData<ResT, NuxtErrorDataT = unknown, DataT = ResT, PickKeys extends KeysOf<DataT> = KeysOf<DataT>, DefaultT = null>(key: string, handler: (app?: NuxtApp) => Promise<ResT>, providedOptions?: CachedAsyncDataOptions<ResT, DataT, PickKeys, DefaultT>): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>) | DefaultAsyncDataErrorValue>; | ||
export {}; |
import { type H3Event } from 'h3'; | ||
import { RouteCacheItem } from '../types.js'; | ||
export declare function serveCachedRoute(event: H3Event, decoded: RouteCacheItem): Promise<void>; | ||
export declare function setCachedResponse(event: H3Event, decoded: RouteCacheItem): void; |
import { setResponseHeaders, setResponseStatus } from "h3"; | ||
export async function serveCachedRoute(event, decoded) { | ||
export function setCachedResponse(event, decoded) { | ||
if (decoded.headers) { | ||
@@ -9,7 +9,3 @@ setResponseHeaders(event, decoded.headers); | ||
} | ||
const response = new Response(decoded.data); | ||
Object.entries(decoded.headers).forEach(([name, value]) => { | ||
response.headers.set(name, value); | ||
}); | ||
await event.respondWith(response); | ||
event.__MULTI_CACHE_SERVED_FROM_CACHE = true; | ||
} |
@@ -31,2 +31,5 @@ import { getResponseHeaders, getResponseStatus } from "h3"; | ||
export async function onAfterResponse(event, response) { | ||
if (event.__MULTI_CACHE_SERVED_FROM_CACHE) { | ||
return; | ||
} | ||
if (!response?.body) { | ||
@@ -53,2 +56,3 @@ return; | ||
let responseHeaders = getResponseHeaders(event); | ||
responseHeaders["content-encoding"] = void 0; | ||
if (serverOptions.route?.alterCachedHeaders) { | ||
@@ -55,0 +59,0 @@ responseHeaders = serverOptions.route.alterCachedHeaders(responseHeaders); |
@@ -7,2 +7,4 @@ import { type H3Event } from 'h3'; | ||
*/ | ||
export declare function onBeforeResponse(event: H3Event): void; | ||
export declare function onBeforeResponse(event: H3Event, response: { | ||
body?: unknown; | ||
}): void; |
@@ -23,5 +23,5 @@ import { format } from "@tusbar/cache-control"; | ||
} | ||
export function onBeforeResponse(event) { | ||
export function onBeforeResponse(event, response) { | ||
const app = useMultiCacheApp(); | ||
handleCDN(app, event); | ||
} |
@@ -1,7 +0,7 @@ | ||
import type { CapturedErrorContext } from 'nitropack/types'; | ||
import type { CapturedErrorContext } from 'nitropack'; | ||
/** | ||
* Callback for the 'beforeResponse' nitro hook. | ||
* Callback for the 'error' nitro hook. | ||
* | ||
* This is called after a valid response was built, but before it is sent. | ||
* This is called during any error that happens in an event handler. | ||
*/ | ||
export declare function onError(_error: Error, ctx: CapturedErrorContext): void; | ||
export declare function onError(_error: Error, ctx: CapturedErrorContext): Promise<void> | undefined; |
@@ -1,2 +0,3 @@ | ||
import { serveCachedRoute } from "../../helpers/routeCache.js"; | ||
import { setCachedResponse } from "../../helpers/routeCache.js"; | ||
import { useMultiCacheApp } from "../utils/useMultiCacheApp.js"; | ||
export function onError(_error, ctx) { | ||
@@ -7,2 +8,6 @@ try { | ||
} | ||
const { state } = useMultiCacheApp(); | ||
if (ctx.event.__MULTI_CACHE_REVALIDATION_KEY) { | ||
state.removeKeyBeingRevalidated(ctx.event.__MULTI_CACHE_REVALIDATION_KEY); | ||
} | ||
const decoded = ctx.event.__MULTI_CACHE_DECODED_CACHED_ROUTE; | ||
@@ -19,5 +24,9 @@ if (!decoded) { | ||
} | ||
serveCachedRoute(ctx.event, decoded); | ||
setCachedResponse(ctx.event, decoded); | ||
const response = new Response(decoded.data, { | ||
headers: decoded.headers | ||
}); | ||
return ctx.event.respondWith(response); | ||
} catch (_e) { | ||
} | ||
} |
@@ -5,16 +5,7 @@ import { | ||
MULTI_CACHE_PREFIX_KEY, | ||
MULTI_CACHE_ROUTE_CONTEXT_KEY, | ||
encodeRouteCacheKey, | ||
getCacheKeyWithPrefix | ||
MULTI_CACHE_ROUTE_CONTEXT_KEY | ||
} from "../../helpers/server.js"; | ||
import { NuxtMultiCacheRouteCacheHelper } from "../../helpers/RouteCacheHelper.js"; | ||
import { | ||
decodeRouteCacheItem, | ||
handleRawCacheData | ||
} from "../../helpers/cacheItem.js"; | ||
import { logger } from "../../helpers/logger.js"; | ||
import { useMultiCacheApp } from "../utils/useMultiCacheApp.js"; | ||
import { NuxtMultiCacheCDNHelper } from "../../helpers/CDNHelper.js"; | ||
import { serveCachedRoute } from "../../helpers/routeCache.js"; | ||
import { useRuntimeConfig } from "#imports"; | ||
async function addCacheContext(event) { | ||
@@ -56,13 +47,2 @@ const { cache, serverOptions, config } = useMultiCacheApp(); | ||
} | ||
function canBeServedFromCache(key, decoded, state) { | ||
const now = Date.now() / 1e3; | ||
const isExpired = decoded.expires ? now >= decoded.expires : false; | ||
if (!isExpired) { | ||
return true; | ||
} | ||
if (decoded.staleWhileRevalidate && state.isBeingRevalidated(key)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
export async function onRequest(event) { | ||
@@ -79,41 +59,3 @@ if (!event.path) { | ||
} | ||
const multiCache = await addCacheContext(event); | ||
if (!multiCache?.route) { | ||
return; | ||
} | ||
try { | ||
const { serverOptions, state } = useMultiCacheApp(); | ||
const fullKey = serverOptions?.route?.buildCacheKey ? serverOptions.route.buildCacheKey(event) : getCacheKeyWithPrefix(encodeRouteCacheKey(event.path), event); | ||
const cachedRaw = handleRawCacheData( | ||
await multiCache.route.getItemRaw(fullKey) | ||
); | ||
if (!cachedRaw) { | ||
return; | ||
} | ||
const decoded = decodeRouteCacheItem(cachedRaw); | ||
if (!decoded) { | ||
return; | ||
} | ||
if (!canBeServedFromCache(fullKey, decoded, state)) { | ||
if (decoded.staleWhileRevalidate) { | ||
state.addKeyBeingRevalidated(fullKey); | ||
event.__MULTI_CACHE_REVALIDATION_KEY = fullKey; | ||
} | ||
if (decoded.staleIfErrorExpires) { | ||
event.__MULTI_CACHE_DECODED_CACHED_ROUTE = decoded; | ||
} | ||
return; | ||
} | ||
const debugEnabled = useRuntimeConfig().multiCache.debug; | ||
if (debugEnabled) { | ||
logger.info("Serving cached route for path: " + event.path, { | ||
fullKey | ||
}); | ||
} | ||
await serveCachedRoute(event, decoded); | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
console.debug(e.message); | ||
} | ||
} | ||
await addCacheContext(event); | ||
} |
@@ -8,4 +8,5 @@ import { defineNitroPlugin } from "nitropack/runtime"; | ||
import { MultiCacheState } from "../../helpers/MultiCacheState.js"; | ||
import { serveCachedHandler } from "../handler/serveCachedRoute.js"; | ||
import { serverOptions } from "#multi-cache-server-options"; | ||
import { useRuntimeConfig } from "#imports"; | ||
import { serverOptions } from "#multi-cache-server-options"; | ||
function createMultiCacheApp() { | ||
@@ -38,2 +39,6 @@ const runtimeConfig = useRuntimeConfig(); | ||
if (multiCache.config.route) { | ||
nitroApp.h3App.stack.unshift({ | ||
route: "/", | ||
handler: serveCachedHandler | ||
}); | ||
nitroApp.hooks.hook("afterResponse", onAfterResponse); | ||
@@ -40,0 +45,0 @@ nitroApp.hooks.hook("error", onError); |
@@ -297,4 +297,8 @@ /// <reference types="node" /> | ||
__MULTI_CACHE_REVALIDATION_KEY?: string; | ||
/** | ||
* Whether the current request has already been served from cache. | ||
*/ | ||
__MULTI_CACHE_SERVED_FROM_CACHE?: boolean; | ||
} | ||
} | ||
export {}; |
{ | ||
"name": "nuxt-multi-cache", | ||
"version": "3.3.1", | ||
"version": "3.3.2", | ||
"description": "SSR route, component and data cache for Nuxt.js", | ||
@@ -46,2 +46,3 @@ "type": "module", | ||
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground && nuxi prepare playground-disk", | ||
"dev:inspect": "nuxi dev playground --inspect", | ||
"typecheck": "nuxi typecheck", | ||
@@ -84,2 +85,3 @@ "docs:dev": "vitepress dev docs --port 5000", | ||
"eslint-plugin-vue": "^9.26.0", | ||
"h3-compression": "^0.3.2", | ||
"happy-dom": "^14.12.0", | ||
@@ -86,0 +88,0 @@ "nuxt": "^3.12.2", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
89681
72
2486
19
1