change: use sitemap for nav
This commit is contained in:
parent
e718f3fe35
commit
28337cd80f
5 changed files with 139 additions and 34 deletions
71
composables/calculateSitemap.ts
Normal file
71
composables/calculateSitemap.ts
Normal file
|
@ -0,0 +1,71 @@
|
|||
import type Article from "../types/collection/article";
|
||||
import type Page from "../types/collection/page";
|
||||
import type { ComponentTypes } from "../types/component/baseComponent";
|
||||
import type SharedList from "../types/component/shared/list";
|
||||
import type Global from "../types/single/global";
|
||||
|
||||
export default async function () {
|
||||
const { find } = useStrapi();
|
||||
|
||||
const nuxtApp = useNuxtApp();
|
||||
const { navbar, footer } = await nuxtApp.runWithContext(() => provideGlobal());
|
||||
const { data: page_res } = await nuxtApp.runWithContext(() =>
|
||||
useAsyncData("sitemap_pages", () =>
|
||||
find<Page>("pages", {
|
||||
filters: {
|
||||
ref_only_access: false,
|
||||
},
|
||||
})
|
||||
)
|
||||
);
|
||||
const pages = page_res.value?.data ?? [];
|
||||
|
||||
const accessableURLs: Array<{ path: string; origin: string; document?: string; hasCollection?: boolean }> = [];
|
||||
|
||||
for (const element of navbar?.navbar_items ?? []) {
|
||||
if (!element.default_active_child) {
|
||||
accessableURLs.push({
|
||||
path: element.URL.startsWith("/") ? element.URL : `/${element.URL}`,
|
||||
origin: "navbar",
|
||||
document: element?.page?.documentId,
|
||||
hasCollection: element.page.content.filter((c: ComponentTypes) => c.__component == "shared.list").length != 0,
|
||||
});
|
||||
}
|
||||
for (const subelement of element.navbar_sub_items) {
|
||||
let url = `${element.URL}/${subelement.URL}`;
|
||||
accessableURLs.push({
|
||||
path: url.startsWith("/") ? url : `/${url}`,
|
||||
origin: "navbar",
|
||||
document: subelement?.page?.documentId,
|
||||
hasCollection:
|
||||
subelement?.page?.content.filter((c: ComponentTypes) => c.__component == "shared.list").length != 0,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const element of pages) {
|
||||
let url = element.slug.replaceAll("~", "/");
|
||||
if (!accessableURLs.find((a) => a.path == url)) {
|
||||
accessableURLs.push({
|
||||
path: url.startsWith("/") ? url : `/${url}`,
|
||||
origin: "page",
|
||||
document: element.documentId,
|
||||
hasCollection: element.content.filter((c: ComponentTypes) => c.__component == "shared.list").length != 0,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const element of footer?.links ?? []) {
|
||||
let url = element.URL.startsWith("/") ? element.URL : `/${element.URL}`;
|
||||
if (!accessableURLs.find((a) => a.path == url) && !element.URL.startsWith("http")) {
|
||||
accessableURLs.push({
|
||||
path: url,
|
||||
origin: "footer",
|
||||
document: undefined,
|
||||
hasCollection: undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return accessableURLs;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue