@shopbite-de/storefront 2.2.0 → 2.3.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/CLAUDE.md +11 -6
- package/app/app.vue +9 -6
- package/app/components/Category/Breadcrumb.vue +9 -10
- package/app/components/Category/Listing.vue +0 -1
- package/app/components/Checkout/Summary.vue +27 -2
- package/app/components/Header.vue +2 -2
- package/app/components/Menu/BonCard.vue +0 -13
- package/app/components/Product/FoodLabels.vue +47 -20
- package/app/components/Product/QuickView.vue +1 -1
- package/app/components/Sb/Icon.vue +3 -1
- package/app/composables/useBusinessHours.ts +35 -14
- package/app/composables/useHolidays.ts +31 -11
- package/app/composables/useHomeMenuCategories.ts +6 -34
- package/app/composables/useLiveShopData.ts +17 -0
- package/app/composables/useNavigation.ts +14 -66
- package/app/composables/useNavigationSearch.ts +8 -31
- package/app/composables/useRestaurantSchema.ts +2 -13
- package/app/composables/useSeoUrlRoute.ts +21 -0
- package/app/composables/useShopBiteConfig.ts +50 -6
- package/app/composables/useTopSellers.ts +2 -43
- package/app/error.vue +4 -0
- package/app/utils/product.ts +1 -62
- package/docs/notes/2026-09-10-issue-275-accessibility.md +2 -2
- package/docs/notes/2026-09-12-issue-327-home-hero.md +1 -1
- package/docs/notes/2026-09-13-issue-332-home-info-cards.md +1 -3
- package/docs/notes/2026-09-21-issue-273-cache-and-demo-video.md +1 -1
- package/docs/notes/2026-09-21-issue-363-non-fatal-404.md +1 -1
- package/docs/notes/2026-09-23-issue-388-home-sections.md +2 -2
- package/docs/notes/2026-10-05-issue-401-restaurant-schema-ordering.md +1 -1
- package/modules/theme.ts +1 -1
- package/nuxt.config.ts +13 -0
- package/package.json +5 -5
- package/server/api/_cache/purge.post.ts +6 -3
- package/server/api/breadcrumb/[categoryId].get.ts +31 -0
- package/server/api/navigation/[type].get.ts +97 -0
- package/server/api/payment-methods.get.ts +29 -0
- package/server/api/seo-url/index.get.ts +17 -0
- package/server/api/seo-url/known.get.ts +33 -0
- package/server/api/shopbite/business-hours.get.ts +22 -0
- package/server/api/shopbite/config.get.ts +6 -0
- package/server/api/shopbite/holidays.get.ts +20 -0
- package/server/api/top-sellers.get.ts +59 -0
- package/server/utils/backoff.ts +67 -0
- package/server/utils/cachePurge.ts +45 -12
- package/server/utils/menuRoot.ts +24 -18
- package/server/utils/seoPaths.ts +130 -0
- package/server/utils/seoUrlCache.ts +98 -0
- package/server/utils/shopBiteConfig.ts +30 -0
- package/server/utils/storeApi.ts +22 -0
- package/test/layer/nuxt.config.ts +1 -1
- package/test/nuxt/CheckoutSummary.test.ts +38 -2
- package/test/nuxt/ProductFoodLabels.test.ts +70 -0
- package/test/nuxt/liveShopData.test.ts +114 -0
- package/test/nuxt/useShopBiteConfig.test.ts +22 -0
- package/test/nuxt/useTopSellers.test.ts +10 -27
- package/test/unit/cachePurge.spec.ts +41 -0
- package/test/unit/layoutCacheRoutes.spec.ts +566 -0
- package/test/unit/nitroStubs.ts +99 -0
- package/test/unit/product.spec.ts +1 -23
- package/test/unit/seoPaths.spec.ts +117 -0
- package/app/components/Menu/FoodLabelLegend.vue +0 -28
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { components } from "~~/api-types/storeApiTypes";
|
|
2
|
+
import { createResilientLoader } from "../../utils/backoff";
|
|
3
|
+
type Schemas = components["schemas"];
|
|
4
|
+
|
|
5
|
+
// What the header, footer and menu navigation read (useNavigation).
|
|
6
|
+
const NAVIGATION_CRITERIA = {
|
|
7
|
+
includes: {
|
|
8
|
+
category: [
|
|
9
|
+
"id",
|
|
10
|
+
"name",
|
|
11
|
+
"translated",
|
|
12
|
+
"seoUrl",
|
|
13
|
+
"customFields",
|
|
14
|
+
"children",
|
|
15
|
+
"linkNewTab",
|
|
16
|
+
],
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// The menu sections of the home page with their photo (useHomeMenuCategories).
|
|
21
|
+
const HOME_CRITERIA = {
|
|
22
|
+
depth: 1,
|
|
23
|
+
includes: {
|
|
24
|
+
category: ["id", "name", "translated", "seoUrl", "customFields", "media"],
|
|
25
|
+
media: ["url", "thumbnails", "metaData"],
|
|
26
|
+
media_thumbnail: ["url", "width"],
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const NAVIGATIONS = {
|
|
31
|
+
main: { activeId: "main-navigation", root: "main-navigation" },
|
|
32
|
+
footer: { activeId: "footer-navigation", root: "footer-navigation" },
|
|
33
|
+
menu: { activeId: "main-navigation", root: "menu" },
|
|
34
|
+
home: { activeId: "main-navigation", root: "menu" },
|
|
35
|
+
} as const;
|
|
36
|
+
|
|
37
|
+
type NavigationType = keyof typeof NAVIGATIONS;
|
|
38
|
+
|
|
39
|
+
const load = createResilientLoader<Schemas["Category"][]>();
|
|
40
|
+
|
|
41
|
+
function navigationOf(type: string | undefined) {
|
|
42
|
+
if (!type || !Object.hasOwn(NAVIGATIONS, type)) return null;
|
|
43
|
+
return NAVIGATIONS[type as NavigationType];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type RootContext = { context?: Record<string, unknown> };
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The root of a navigation, resolved once per request (key and request
|
|
50
|
+
* share `event.context`): the plugin config may expire in between, and a
|
|
51
|
+
* second answer would cache one root's sections under the other's key.
|
|
52
|
+
*/
|
|
53
|
+
async function rootIdOf(
|
|
54
|
+
event: RootContext,
|
|
55
|
+
type: string | undefined,
|
|
56
|
+
): Promise<string> {
|
|
57
|
+
const context = (event.context ??= {});
|
|
58
|
+
if (typeof context.navigationRoot === "string") return context.navigationRoot;
|
|
59
|
+
const navigation = navigationOf(type);
|
|
60
|
+
if (!navigation) return "";
|
|
61
|
+
const root =
|
|
62
|
+
navigation.root === "menu" ? await menuRootIdStrict() : navigation.root;
|
|
63
|
+
context.navigationRoot = root;
|
|
64
|
+
return root;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The navigations every page renders: `main` (header), `footer`, `menu` (the
|
|
69
|
+
* menu sections below the menu root, #486) and `home` (the sections with
|
|
70
|
+
* their photo). Shared by all visitors, so the server keeps them instead of
|
|
71
|
+
* asking Shopware on each request; the key carries the menu root, which the
|
|
72
|
+
* plugin configuration may change. Without the plugin configuration the
|
|
73
|
+
* request fails rather than caching a menu of the fallback root.
|
|
74
|
+
*/
|
|
75
|
+
export default defineCachedEventHandler(
|
|
76
|
+
async (event): Promise<Schemas["Category"][]> => {
|
|
77
|
+
const type = getRouterParam(event, "type");
|
|
78
|
+
const navigation = navigationOf(type);
|
|
79
|
+
if (!navigation) throw createError({ statusCode: 404 });
|
|
80
|
+
|
|
81
|
+
const root = await rootIdOf(event, type);
|
|
82
|
+
return await load(`${type}-${root}`, () =>
|
|
83
|
+
storeApiPost(
|
|
84
|
+
`/navigation/${navigation.activeId}/${root}`,
|
|
85
|
+
type === "home" ? HOME_CRITERIA : NAVIGATION_CRITERIA,
|
|
86
|
+
),
|
|
87
|
+
);
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
maxAge: useRuntimeConfig().public.shopBite.cacheTtl.navigation,
|
|
91
|
+
name: "navigation",
|
|
92
|
+
getKey: async (event) => {
|
|
93
|
+
const type = getRouterParam(event, "type");
|
|
94
|
+
return `navigation-${type}-${await rootIdOf(event, type)}`;
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { components } from "~~/api-types/storeApiTypes";
|
|
2
|
+
import { createResilientLoader } from "../utils/backoff";
|
|
3
|
+
|
|
4
|
+
const load = createResilientLoader<string[]>();
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Names of the payment methods for the Restaurant schema of the home page
|
|
8
|
+
* (useRestaurantSchema). Asked without a visitor's context, so it lists what
|
|
9
|
+
* a new guest can pay with.
|
|
10
|
+
*/
|
|
11
|
+
export default defineCachedEventHandler(
|
|
12
|
+
(): Promise<string[]> =>
|
|
13
|
+
load("payment-methods", async () => {
|
|
14
|
+
const result = await storeApiPost<{
|
|
15
|
+
elements?: components["schemas"]["PaymentMethod"][];
|
|
16
|
+
}>("/payment-method", {
|
|
17
|
+
onlyAvailable: true,
|
|
18
|
+
includes: { payment_method: ["name", "translated"] },
|
|
19
|
+
});
|
|
20
|
+
return (result.elements ?? []).map(
|
|
21
|
+
(method) => method.translated?.name ?? method.name,
|
|
22
|
+
);
|
|
23
|
+
}),
|
|
24
|
+
{
|
|
25
|
+
maxAge: useRuntimeConfig().public.shopBite.cacheTtl.paymentMethods,
|
|
26
|
+
name: "payment-methods",
|
|
27
|
+
getKey: () => "payment-methods",
|
|
28
|
+
},
|
|
29
|
+
);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { isHostilePath } from "../../utils/seoPaths";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The canonical SEO URL of a path (useNavigationSearch); matches are cached
|
|
5
|
+
* (findSeoUrl). `{ seoUrl: null }` for an unknown path: not an error (see
|
|
6
|
+
* content/page).
|
|
7
|
+
*/
|
|
8
|
+
export default defineEventHandler(async (event) => {
|
|
9
|
+
const { path } = getQuery(event);
|
|
10
|
+
if (typeof path !== "string" || !path.startsWith("/")) {
|
|
11
|
+
throw createError({ statusCode: 400, statusMessage: "Invalid path" });
|
|
12
|
+
}
|
|
13
|
+
// longer than Shopware stores a SEO path, or a probe no SEO URL can match
|
|
14
|
+
if (path.length > 750 || isHostilePath(path)) return { seoUrl: null };
|
|
15
|
+
|
|
16
|
+
return { seoUrl: await findSeoUrl(path) };
|
|
17
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { createBackoff } from "../../utils/backoff";
|
|
2
|
+
import { isHostilePath, mayBeSeoPath } from "../../utils/seoPaths";
|
|
3
|
+
|
|
4
|
+
/** After a failed load, let every path through for the backoff. */
|
|
5
|
+
const backoff = createBackoff();
|
|
6
|
+
let memo: { loadedAt: number; known: Set<string> } | null = null;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Whether a path may be a category SEO URL (useSeoUrlRoute asks before its
|
|
10
|
+
* lookups on the server): `false` for a hostile path (isHostilePath) and
|
|
11
|
+
* for one no category ever had. When the list is too long or Shopware
|
|
12
|
+
* fails, every other path may.
|
|
13
|
+
*/
|
|
14
|
+
export default defineEventHandler(async (event) => {
|
|
15
|
+
const { path } = getQuery(event);
|
|
16
|
+
if (typeof path !== "string" || !path.startsWith("/")) {
|
|
17
|
+
throw createError({ statusCode: 400, statusMessage: "Invalid path" });
|
|
18
|
+
}
|
|
19
|
+
// before the branches that let every path through
|
|
20
|
+
if (isHostilePath(path)) return { known: false };
|
|
21
|
+
if (backoff.active()) return { known: true };
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const { paths, loadedAt } = await knownSeoPaths();
|
|
25
|
+
if (!paths) return { known: true };
|
|
26
|
+
if (memo?.loadedAt !== loadedAt) memo = { loadedAt, known: new Set(paths) };
|
|
27
|
+
return { known: mayBeSeoPath(path, memo.known) };
|
|
28
|
+
} catch (error) {
|
|
29
|
+
backoff.trip();
|
|
30
|
+
console.error("Failed to load the SEO paths", error);
|
|
31
|
+
return { known: true };
|
|
32
|
+
}
|
|
33
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { components } from "~~/api-types/storeApiTypes";
|
|
2
|
+
import { createResilientLoader } from "../../utils/backoff";
|
|
3
|
+
|
|
4
|
+
const load =
|
|
5
|
+
createResilientLoader<components["schemas"]["BusinessHourStruct"]>();
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Weekly opening hours for server-side rendering (footer, home page,
|
|
9
|
+
* Restaurant schema); the browser loads them from Shopware again after
|
|
10
|
+
* hydration (useLiveShopData).
|
|
11
|
+
*/
|
|
12
|
+
export default defineCachedEventHandler(
|
|
13
|
+
(): Promise<components["schemas"]["BusinessHourStruct"]> =>
|
|
14
|
+
load("business-hours", () => storeApiGet("/shopbite/business-hour")),
|
|
15
|
+
{
|
|
16
|
+
maxAge: useRuntimeConfig().public.shopBite.cacheTtl.businessHours,
|
|
17
|
+
// never older than the TTL (default swr would serve a stale answer once)
|
|
18
|
+
swr: false,
|
|
19
|
+
name: "business-hours",
|
|
20
|
+
getKey: () => "business-hours",
|
|
21
|
+
},
|
|
22
|
+
);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ShopBite plugin configuration (delivery time, checkout switch, menu root)
|
|
3
|
+
* for server-side rendering, cached `cacheTtl.config` (shopBiteConfig). The
|
|
4
|
+
* browser loads it from Shopware again after hydration (useLiveShopData).
|
|
5
|
+
*/
|
|
6
|
+
export default defineEventHandler(() => shopBiteConfig());
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { components } from "~~/api-types/storeApiTypes";
|
|
2
|
+
import { createResilientLoader } from "../../utils/backoff";
|
|
3
|
+
|
|
4
|
+
const load = createResilientLoader<components["schemas"]["HolidayStruct"]>();
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Closing days for server-side rendering (home page, Restaurant schema); the
|
|
8
|
+
* browser loads them from Shopware again after hydration (useLiveShopData).
|
|
9
|
+
*/
|
|
10
|
+
export default defineCachedEventHandler(
|
|
11
|
+
(): Promise<components["schemas"]["HolidayStruct"]> =>
|
|
12
|
+
load("holidays", () => storeApiGet("/shopbite/holiday")),
|
|
13
|
+
{
|
|
14
|
+
maxAge: useRuntimeConfig().public.shopBite.cacheTtl.holidays,
|
|
15
|
+
// never older than the TTL (default swr would serve a stale answer once)
|
|
16
|
+
swr: false,
|
|
17
|
+
name: "holidays",
|
|
18
|
+
getKey: () => "holidays",
|
|
19
|
+
},
|
|
20
|
+
);
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { components } from "~~/api-types/storeApiTypes";
|
|
2
|
+
import { createResilientLoader } from "../utils/backoff";
|
|
3
|
+
|
|
4
|
+
const TOP_SELLERS_LIMIT = 8;
|
|
5
|
+
|
|
6
|
+
const criteria = {
|
|
7
|
+
filter: [{ type: "equals", field: "markAsTopseller", value: true }],
|
|
8
|
+
limit: TOP_SELLERS_LIMIT,
|
|
9
|
+
// What the compact card and the quick view need (#327).
|
|
10
|
+
includes: {
|
|
11
|
+
product: [
|
|
12
|
+
"id",
|
|
13
|
+
"productNumber",
|
|
14
|
+
"name",
|
|
15
|
+
"translated",
|
|
16
|
+
"description",
|
|
17
|
+
"calculatedPrice",
|
|
18
|
+
"available",
|
|
19
|
+
"cover",
|
|
20
|
+
"sortedProperties",
|
|
21
|
+
// Allergens checked (#485); includes filter `translated` as well,
|
|
22
|
+
// so custom fields only come back when listed here.
|
|
23
|
+
"customFields",
|
|
24
|
+
// Card link to the quick view deep link (#289).
|
|
25
|
+
"seoUrls",
|
|
26
|
+
],
|
|
27
|
+
seo_url: ["seoPathInfo", "isCanonical"],
|
|
28
|
+
product_media: ["media"],
|
|
29
|
+
media: ["url", "thumbnails"],
|
|
30
|
+
media_thumbnail: ["width", "url"],
|
|
31
|
+
property_group: ["id", "name", "translated", "options"],
|
|
32
|
+
property_group_option: ["id", "name", "translated", "customFields"],
|
|
33
|
+
},
|
|
34
|
+
associations: {
|
|
35
|
+
cover: { associations: { media: {} } },
|
|
36
|
+
properties: { associations: { group: {} } },
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const load = createResilientLoader<components["schemas"]["Product"][]>();
|
|
41
|
+
|
|
42
|
+
/** The dishes marked as top sellers, for the home page (useTopSellers). */
|
|
43
|
+
export default defineCachedEventHandler(
|
|
44
|
+
(): Promise<components["schemas"]["Product"][]> =>
|
|
45
|
+
load("top-sellers", async () => {
|
|
46
|
+
const result = await storeApiPost<{
|
|
47
|
+
elements?: components["schemas"]["Product"][];
|
|
48
|
+
}>("/product", criteria, {
|
|
49
|
+
// Variants marked as top sellers inherit name, cover and properties.
|
|
50
|
+
headers: { "sw-inheritance": "true", "sw-include-seo-urls": "true" },
|
|
51
|
+
});
|
|
52
|
+
return result.elements ?? [];
|
|
53
|
+
}),
|
|
54
|
+
{
|
|
55
|
+
maxAge: useRuntimeConfig().public.shopBite.cacheTtl.listing,
|
|
56
|
+
name: "top-sellers",
|
|
57
|
+
getKey: () => "top-sellers",
|
|
58
|
+
},
|
|
59
|
+
);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/** How long a failed Shopware request pauses the next ones. */
|
|
2
|
+
export const FAILURE_BACKOFF_MS = 30_000;
|
|
3
|
+
|
|
4
|
+
/** Remembers a failure for `ms`, so an outage is not asked on every request. */
|
|
5
|
+
export function createBackoff(
|
|
6
|
+
ms: number = FAILURE_BACKOFF_MS,
|
|
7
|
+
now: () => number = () => Date.now(),
|
|
8
|
+
) {
|
|
9
|
+
let until = 0;
|
|
10
|
+
return {
|
|
11
|
+
active: () => now() < until,
|
|
12
|
+
trip: () => {
|
|
13
|
+
until = now() + ms;
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function statusOf(error: unknown): number | undefined {
|
|
19
|
+
const { statusCode, status } = (error ?? {}) as {
|
|
20
|
+
statusCode?: number;
|
|
21
|
+
status?: number;
|
|
22
|
+
};
|
|
23
|
+
return statusCode ?? status;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A 5xx, or a fetch error without a response (network, timeout). A 4xx is
|
|
28
|
+
* an answer, and a programming error (TypeError) is no outage either.
|
|
29
|
+
*/
|
|
30
|
+
export function isOutage(error: unknown): boolean {
|
|
31
|
+
const status = statusOf(error);
|
|
32
|
+
if (status !== undefined) return status >= 500;
|
|
33
|
+
return (error as { name?: string } | null)?.name === "FetchError";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Shields Shopware from a cache's misses during an outage (one loader per
|
|
38
|
+
* cached handler): the last good answer per key is served when a request
|
|
39
|
+
* fails, and after a failure Shopware is asked again only after the
|
|
40
|
+
* backoff; without a last good answer the request fails fast with 503.
|
|
41
|
+
* `keep` decides which answers are remembered (keeps the memory bounded).
|
|
42
|
+
*/
|
|
43
|
+
export function createResilientLoader<T>(
|
|
44
|
+
keep: (value: T) => boolean = () => true,
|
|
45
|
+
backoff = createBackoff(),
|
|
46
|
+
) {
|
|
47
|
+
const lastGood = new Map<string, T>();
|
|
48
|
+
const unavailable = () =>
|
|
49
|
+
Object.assign(new Error("Shopware unavailable"), { statusCode: 503 });
|
|
50
|
+
|
|
51
|
+
return async (key: string, load: () => Promise<T>): Promise<T> => {
|
|
52
|
+
if (backoff.active()) {
|
|
53
|
+
if (lastGood.has(key)) return lastGood.get(key)!;
|
|
54
|
+
throw unavailable();
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
const value = await load();
|
|
58
|
+
if (keep(value)) lastGood.set(key, value);
|
|
59
|
+
return value;
|
|
60
|
+
} catch (error) {
|
|
61
|
+
if (!isOutage(error)) throw error;
|
|
62
|
+
backoff.trip();
|
|
63
|
+
if (lastGood.has(key)) return lastGood.get(key)!;
|
|
64
|
+
throw Object.assign(unavailable(), { cause: error });
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -2,33 +2,60 @@ import { timingSafeEqual } from "node:crypto";
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Cache purge for the backoffice (#484): the cached Store API handlers keep
|
|
5
|
-
* products, listings and
|
|
6
|
-
* handler response under
|
|
7
|
-
*
|
|
5
|
+
* products, listings, categories and the layout data of every page for up
|
|
6
|
+
* to a day (`cacheTtl`). Nitro stores a handler response under
|
|
7
|
+
* `cache:nitro:handlers:<name>:<escaped key>.json` (cached functions under
|
|
8
|
+
* `cache:nitro:functions:`); the escaped key keeps the hex ids, so an id
|
|
9
|
+
* finds its entries.
|
|
8
10
|
*/
|
|
9
11
|
|
|
10
12
|
/** Entries of one product: the product, its variants and its extras. */
|
|
11
13
|
const PER_PRODUCT = ["product", "variant", "cross-selling"];
|
|
12
14
|
/** Lists that may show any product or category. */
|
|
13
|
-
const LISTS = ["listing", "search"];
|
|
14
|
-
|
|
15
|
+
const LISTS = ["listing", "search", "top-sellers"];
|
|
16
|
+
/**
|
|
17
|
+
* Layout data of every page: navigations, SEO URL matches and breadcrumbs
|
|
18
|
+
* (a renamed or moved section changes them), plugin config, hours, closing
|
|
19
|
+
* days and payment methods. Small, so every purge takes all of it.
|
|
20
|
+
*/
|
|
21
|
+
export const LAYOUT = [
|
|
22
|
+
"navigation",
|
|
23
|
+
"breadcrumb",
|
|
24
|
+
"business-hours",
|
|
25
|
+
"holidays",
|
|
26
|
+
"payment-methods",
|
|
27
|
+
];
|
|
28
|
+
/** Always purged: the lists and the layout data. */
|
|
29
|
+
const SHARED = [...LISTS, ...LAYOUT];
|
|
30
|
+
const ALL = [...PER_PRODUCT, "category", ...SHARED];
|
|
15
31
|
const HANDLERS = "cache:nitro:handlers:";
|
|
32
|
+
/** Cached functions: SEO URL matches, known SEO paths, plugin config. */
|
|
33
|
+
const FUNCTIONS = "cache:nitro:functions:";
|
|
34
|
+
const LAYOUT_FUNCTIONS = ["seo-url", "seo-paths", "shopbite-config"];
|
|
16
35
|
/** Route rules with `swr` (llms.txt lists the menu). */
|
|
17
36
|
const ROUTES = "cache:nitro:routes:";
|
|
18
37
|
|
|
19
38
|
export interface PurgeTarget {
|
|
20
39
|
products?: string[];
|
|
21
40
|
categories?: string[];
|
|
41
|
+
/**
|
|
42
|
+
* Only the layout data, e.g. after the opening hours, closing days or the
|
|
43
|
+
* checkout switch changed. Combined with products or categories, their
|
|
44
|
+
* entries go as well.
|
|
45
|
+
*/
|
|
46
|
+
layout?: boolean;
|
|
22
47
|
}
|
|
23
48
|
|
|
24
|
-
const
|
|
25
|
-
key.slice(
|
|
49
|
+
const nameOf = (key: string, prefix: string) =>
|
|
50
|
+
key.slice(prefix.length).split(":")[0] ?? "";
|
|
26
51
|
const compact = (id: string) => id.toLowerCase().replace(/[^0-9a-f]/g, "");
|
|
27
52
|
|
|
28
53
|
/**
|
|
29
54
|
* The cache keys to remove: without a target everything; with a target the
|
|
30
|
-
* entries of the named products and categories plus every list
|
|
31
|
-
* group change reaches many dishes, so
|
|
55
|
+
* entries of the named products and categories plus every list and the
|
|
56
|
+
* layout data. An extras group change reaches many dishes, so
|
|
57
|
+
* cross-sellings always go completely. `layout` alone takes only the
|
|
58
|
+
* layout data.
|
|
32
59
|
*/
|
|
33
60
|
export function keysToPurge(
|
|
34
61
|
keys: string[],
|
|
@@ -36,13 +63,19 @@ export function keysToPurge(
|
|
|
36
63
|
): string[] {
|
|
37
64
|
const products = (target.products ?? []).map(compact).filter(Boolean);
|
|
38
65
|
const categories = (target.categories ?? []).map(compact).filter(Boolean);
|
|
39
|
-
const
|
|
66
|
+
const menu = products.length > 0 || categories.length > 0;
|
|
67
|
+
const layoutOnly = Boolean(target.layout) && !menu;
|
|
68
|
+
const everything = !menu && !layoutOnly;
|
|
40
69
|
return keys.filter((key) => {
|
|
70
|
+
if (key.startsWith(FUNCTIONS))
|
|
71
|
+
return LAYOUT_FUNCTIONS.includes(nameOf(key, FUNCTIONS));
|
|
41
72
|
if (key.startsWith(ROUTES)) return true;
|
|
42
73
|
if (!key.startsWith(HANDLERS)) return false;
|
|
43
|
-
const handler =
|
|
74
|
+
const handler = nameOf(key, HANDLERS);
|
|
44
75
|
if (!ALL.includes(handler)) return false;
|
|
45
|
-
if (
|
|
76
|
+
if (LAYOUT.includes(handler)) return true;
|
|
77
|
+
if (layoutOnly) return false;
|
|
78
|
+
if (everything || SHARED.includes(handler) || handler === "cross-selling")
|
|
46
79
|
return true;
|
|
47
80
|
if (handler === "category")
|
|
48
81
|
return categories.some((id) => key.includes(id));
|
package/server/utils/menuRoot.ts
CHANGED
|
@@ -1,25 +1,31 @@
|
|
|
1
1
|
import { pickMenuRoot } from "../../app/utils/menuRoot";
|
|
2
|
+
import { shopBiteConfig } from "./shopBiteConfig";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
|
-
* Server side of the menu root (#486, see app/utils/menuRoot.ts)
|
|
5
|
-
*
|
|
5
|
+
* Server side of the menu root (#486, see app/utils/menuRoot.ts). The plugin
|
|
6
|
+
* value comes from the cached plugin configuration (shopBiteConfig).
|
|
6
7
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
).catch(() => null);
|
|
14
|
-
return config?.menuCategoryId ?? null;
|
|
15
|
-
},
|
|
16
|
-
{ maxAge: 300, name: "menu-root", getKey: () => "menu-root" },
|
|
17
|
-
);
|
|
8
|
+
function configuredRoot(): string | undefined {
|
|
9
|
+
return (
|
|
10
|
+
(useRuntimeConfig().public.shopBite.menuCategoryId as string | undefined) ||
|
|
11
|
+
undefined
|
|
12
|
+
);
|
|
13
|
+
}
|
|
18
14
|
|
|
15
|
+
/** For the sitemap and llms.txt: falls back to the navigation root. */
|
|
19
16
|
export async function menuRootId(): Promise<string> {
|
|
20
|
-
const configured =
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
const configured = configuredRoot();
|
|
18
|
+
if (configured) return configured;
|
|
19
|
+
const config = await shopBiteConfig().catch(() => null);
|
|
20
|
+
return pickMenuRoot(undefined, config?.menuCategoryId);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* For cached navigations: rejects when the plugin configuration cannot be
|
|
25
|
+
* loaded, so a menu built on the fallback root is never cached.
|
|
26
|
+
*/
|
|
27
|
+
export async function menuRootIdStrict(): Promise<string> {
|
|
28
|
+
const configured = configuredRoot();
|
|
29
|
+
if (configured) return configured;
|
|
30
|
+
return pickMenuRoot(undefined, (await shopBiteConfig()).menuCategoryId);
|
|
25
31
|
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Known SEO paths of the menu sections (server/utils/seoUrlCache.ts,
|
|
3
|
+
* server/api/seo-url/known.get.ts): lets the server answer a path that no
|
|
4
|
+
* category ever had (`/wp-admin/`, `/.git/HEAD`) with a 404 without asking
|
|
5
|
+
* Shopware four times (canonical and old SEO URL, with and without the
|
|
6
|
+
* trailing slash). Wherever the comparison could differ from Shopware's
|
|
7
|
+
* database lookup, the path is let through.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Technical URLs (`/navigation/<id>`) are looked up by `pathInfo`. */
|
|
11
|
+
export function isTechnicalPath(path: string): boolean {
|
|
12
|
+
return /^\/(navigation|detail|landingPage)\//.test(path);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The only shape of a technical URL: prefix, entity id, optional slash. */
|
|
16
|
+
const TECHNICAL_URL = /^\/(navigation|detail|landingPage)\/[0-9a-f]{32}\/?$/i;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Paths no SEO URL can have, answered with a 404 without any lookup:
|
|
20
|
+
* undecodable or invalid UTF-8 (`/%ff`, `/%c0%ae`), double encoding
|
|
21
|
+
* (`%25`), `..` (path traversal probes) and technical prefixes without an
|
|
22
|
+
* entity id (`/navigation/zzz`).
|
|
23
|
+
*/
|
|
24
|
+
export function isHostilePath(path: string): boolean {
|
|
25
|
+
if (/%25/i.test(path) || path.includes("..")) return true;
|
|
26
|
+
let decoded: string;
|
|
27
|
+
try {
|
|
28
|
+
decoded = decodeURIComponent(path);
|
|
29
|
+
} catch {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
if (decoded.includes("..")) return true;
|
|
33
|
+
return isTechnicalPath(path) && !TECHNICAL_URL.test(path);
|
|
34
|
+
}
|
|
35
|
+
const CATEGORY_ROUTE = "frontend.navigation.page";
|
|
36
|
+
/** Shopware's default MAX_LIMIT of the Store API. */
|
|
37
|
+
export const SEO_PATH_PAGE_SIZE = 100;
|
|
38
|
+
const MAX_PAGES = 10;
|
|
39
|
+
|
|
40
|
+
function decode(path: string): string {
|
|
41
|
+
try {
|
|
42
|
+
return decodeURIComponent(path);
|
|
43
|
+
} catch {
|
|
44
|
+
return path;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The form two paths are compared in, close to MySQL's case- and
|
|
50
|
+
* accent-insensitive collation: decoded, lower case, without accents, `ß`,
|
|
51
|
+
* `æ`, `œ` written out, without leading and trailing slashes (the lookup
|
|
52
|
+
* tries both).
|
|
53
|
+
*/
|
|
54
|
+
export function seoPathKey(path: string): string {
|
|
55
|
+
return decode(path)
|
|
56
|
+
.toLowerCase()
|
|
57
|
+
.normalize("NFD")
|
|
58
|
+
.replace(/\p{Diacritic}/gu, "")
|
|
59
|
+
.replace(/ß/g, "ss")
|
|
60
|
+
.replace(/æ/g, "ae")
|
|
61
|
+
.replace(/œ/g, "oe")
|
|
62
|
+
.replace(/^\/+|\/+$/g, "");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Whether a path may resolve to a SEO URL. `known` is null when the list is
|
|
67
|
+
* not available or too long: then every path may, and the lookup decides.
|
|
68
|
+
* A key with characters outside printable ASCII (or an undecodable `%`) is
|
|
69
|
+
* let through, since the collation may match it in ways not mapped here.
|
|
70
|
+
*/
|
|
71
|
+
export function mayBeSeoPath(path: string, known: Set<string> | null): boolean {
|
|
72
|
+
if (isHostilePath(path)) return false;
|
|
73
|
+
if (!known || isTechnicalPath(path)) return true;
|
|
74
|
+
const key = seoPathKey(path);
|
|
75
|
+
if (/[^\x20-\x7e]/.test(key) || key.includes("%")) return true;
|
|
76
|
+
return known.has(key);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Store API criteria for one page of category SEO URLs. */
|
|
80
|
+
export function seoPathCriteria(isCanonical: true | null, page: number) {
|
|
81
|
+
return {
|
|
82
|
+
limit: SEO_PATH_PAGE_SIZE,
|
|
83
|
+
// `/seo-url` pages with `page`; `p` is ignored there
|
|
84
|
+
page,
|
|
85
|
+
// a stable order, so pages neither skip nor repeat rows
|
|
86
|
+
sort: [{ field: "id", order: "ASC" }],
|
|
87
|
+
filter: [
|
|
88
|
+
{ type: "equals", field: "routeName", value: CATEGORY_ROUTE },
|
|
89
|
+
// the Store API returns old SEO URLs only when the criteria filter on
|
|
90
|
+
// `isCanonical`
|
|
91
|
+
{ type: "equals", field: "isCanonical", value: isCanonical },
|
|
92
|
+
],
|
|
93
|
+
includes: { seo_url: ["seoPathInfo"] },
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
type FetchPage = (
|
|
98
|
+
criteria: ReturnType<typeof seoPathCriteria>,
|
|
99
|
+
) => Promise<{ seoPathInfo?: string }[]>;
|
|
100
|
+
|
|
101
|
+
async function pathsOf(
|
|
102
|
+
isCanonical: true | null,
|
|
103
|
+
fetchPage: FetchPage,
|
|
104
|
+
): Promise<string[] | null> {
|
|
105
|
+
const paths: string[] = [];
|
|
106
|
+
for (let page = 1; page <= MAX_PAGES; page++) {
|
|
107
|
+
const elements = await fetchPage(seoPathCriteria(isCanonical, page));
|
|
108
|
+
paths.push(
|
|
109
|
+
...elements.map((seoUrl) => seoPathKey(seoUrl.seoPathInfo ?? "")),
|
|
110
|
+
);
|
|
111
|
+
if (elements.length < SEO_PATH_PAGE_SIZE) return paths;
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* All current and old category SEO paths as `seoPathKey`s; null if there
|
|
118
|
+
* are more than MAX_PAGES pages of one kind (then nothing is filtered).
|
|
119
|
+
* Throws when Shopware fails.
|
|
120
|
+
*/
|
|
121
|
+
export async function loadSeoPaths(
|
|
122
|
+
fetchPage: FetchPage,
|
|
123
|
+
): Promise<string[] | null> {
|
|
124
|
+
const lists = await Promise.all([
|
|
125
|
+
pathsOf(true, fetchPage),
|
|
126
|
+
pathsOf(null, fetchPage),
|
|
127
|
+
]);
|
|
128
|
+
if (lists.some((list) => list === null)) return null;
|
|
129
|
+
return lists.flatMap((list) => list ?? []);
|
|
130
|
+
}
|