@plentymarkets/shop-core 1.21.14 → 1.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module.json +1 -1
- package/dist/module.mjs +1 -0
- package/dist/runtime/plugins/cache-control.server.d.ts +5 -0
- package/dist/runtime/plugins/cache-control.server.js +24 -0
- package/dist/runtime/plugins/sdk.js +3 -2
- package/dist/runtime/templates/types.template +6 -2
- package/dist/runtime/utils/cacheHelper.d.ts +1 -0
- package/dist/runtime/utils/cacheHelper.js +7 -0
- package/package.json +1 -1
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -113,6 +113,7 @@ export type ShopCoreConfig = ${genInlineTypeImport(projectRootResolver.resolve("
|
|
|
113
113
|
filePath: buildDirectoryResolver.resolve("ModuleComponentRendering.vue")
|
|
114
114
|
});
|
|
115
115
|
addPlugin(resolver.resolve("./runtime/plugins/sdk"));
|
|
116
|
+
addPlugin(resolver.resolve("./runtime/plugins/cache-control.server.ts"));
|
|
116
117
|
addImports({
|
|
117
118
|
name: "useSdk",
|
|
118
119
|
as: "useSdk",
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { defineNuxtPlugin, useCookie, useRequestEvent, useRuntimeConfig } from "#imports";
|
|
2
|
+
import { setResponseHeader } from "h3";
|
|
3
|
+
import { getCacheControlMeta } from "../utils/cacheHelper.js";
|
|
4
|
+
import { isServer } from "../utils/runtime.js";
|
|
5
|
+
export default defineNuxtPlugin({
|
|
6
|
+
name: "shop-core:cache-control",
|
|
7
|
+
enforce: "post",
|
|
8
|
+
setup(nuxtApp) {
|
|
9
|
+
if (!isServer()) return;
|
|
10
|
+
const event = useRequestEvent();
|
|
11
|
+
if (!event) return;
|
|
12
|
+
const runtimeConfig = useRuntimeConfig();
|
|
13
|
+
const pwaCookie = useCookie("pwa");
|
|
14
|
+
const cacheControlValue = getCacheControlMeta(nuxtApp.$router);
|
|
15
|
+
const isPreview = !!pwaCookie.value || !!runtimeConfig.public.isPreview;
|
|
16
|
+
if (cacheControlValue && !isPreview) {
|
|
17
|
+
setResponseHeader(event, "Cache-Control", cacheControlValue);
|
|
18
|
+
} else {
|
|
19
|
+
setResponseHeader(event, "Cache-Control", "no-cache, no-store, must-revalidate");
|
|
20
|
+
setResponseHeader(event, "Pragma", "no-cache");
|
|
21
|
+
setResponseHeader(event, "Expires", "0");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
@@ -6,6 +6,7 @@ import { createSdkLogger } from "../utils/sdk/sdk.logger.js";
|
|
|
6
6
|
import { createSdkRequestHeaders, getI18nLocale } from "../utils/sdk/sdk.request.headers.js";
|
|
7
7
|
import { handleSdkResponse } from "../utils/sdk/sdk.response.headers.js";
|
|
8
8
|
import { setSSRMetrics } from "../utils/ssrMetricsHelper.js";
|
|
9
|
+
import { getCacheControlMeta } from "../utils/cacheHelper.js";
|
|
9
10
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
10
11
|
const runtimeConfig = useRuntimeConfig();
|
|
11
12
|
const moduleConfig = runtimeConfig.public.shopCore;
|
|
@@ -17,7 +18,7 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|
|
17
18
|
);
|
|
18
19
|
const noCache = runtimeConfig.public.noCache || nuxtApp.$router.currentRoute.value.query?.noCache?.toString() || "";
|
|
19
20
|
const referrerId = nuxtApp.$router.currentRoute.value.query?.ReferrerID?.toString() ?? "";
|
|
20
|
-
const
|
|
21
|
+
const isCacheable = getCacheControlMeta(nuxtApp.$router);
|
|
21
22
|
const { token: csrfToken } = useCsrfToken();
|
|
22
23
|
const ssrCookie = ref([]);
|
|
23
24
|
logger.log("\u{1F680} initializing SDK plugin");
|
|
@@ -35,7 +36,7 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|
|
35
36
|
noCache,
|
|
36
37
|
requestHeaders,
|
|
37
38
|
ssrCookie: ssrCookie.value,
|
|
38
|
-
shouldSkipCookiesSSR
|
|
39
|
+
shouldSkipCookiesSSR: !!isCacheable
|
|
39
40
|
});
|
|
40
41
|
logger.verbose(`\u{1F4EC} prepared headers: `, JSON.stringify(headers));
|
|
41
42
|
return {
|
|
@@ -7,8 +7,12 @@ declare module '#app' {
|
|
|
7
7
|
};
|
|
8
8
|
}
|
|
9
9
|
interface PageMeta {
|
|
10
|
-
/**
|
|
11
|
-
|
|
10
|
+
/** Cache-Control header value to set for this page, e.g. { type: 'public', maxAge: 30, staleWhileRevalidate: 900 } */
|
|
11
|
+
cacheControl?: {
|
|
12
|
+
maxAge: number;
|
|
13
|
+
staleWhileRevalidate: number;
|
|
14
|
+
type: 'public' | 'private'
|
|
15
|
+
};
|
|
12
16
|
}
|
|
13
17
|
}
|
|
14
18
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getCacheControlMeta: (route: any) => string | false;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const getCacheControlMeta = (route) => {
|
|
2
|
+
const cacheControl = route?.currentRoute?.value?.meta?.cacheControl;
|
|
3
|
+
if (!cacheControl || typeof cacheControl !== "object") return false;
|
|
4
|
+
const { type, maxAge, staleWhileRevalidate } = cacheControl;
|
|
5
|
+
if (typeof type !== "string" || typeof maxAge !== "number" || typeof staleWhileRevalidate !== "number") return false;
|
|
6
|
+
return `${type}, max-age=${maxAge}, stale-while-revalidate=${staleWhileRevalidate}`;
|
|
7
|
+
};
|