Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICO-245 Fix homepage loading #118

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/Account/AccountLogin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const handleLogin = async (fields: LoginForm) => {
type="form"
:submit-label="$t('account.login.submitLabel')"
:classes="{
form: 'w-full flex flex-wrap flex-col gap-4',
form: 'w-full flex flex-col gap-4',
actions: 'w-full',
}"
@submit="handleLogin"
Expand Down
13 changes: 12 additions & 1 deletion components/LanguageSwitch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@ const { languages, changeLanguage, getLanguageCodeFromId, getLanguageIdFromCode
const { refreshSessionContext } = useSessionContext();
const customerStore = useCustomerStore();
const { loading } = storeToRefs(customerStore);
const { getSeoUrlByNavigationId, isCustomRoute } = useSeoUrl();
const { getLocalePathPrefix } = useLocale();
const router = useRouter();

const selectedLanguageId = computed(() => getLanguageIdFromCode(locale.value));

const onLanguageChange = async (option: Event) => {
const selectedOptionId = (option.target as HTMLSelectElement).value;
loading.value = true;
await changeLanguage(selectedOptionId);
setLocale(getLanguageCodeFromId(selectedOptionId));
const languageCode = getLanguageCodeFromId(selectedOptionId);
if(isCustomRoute.value) {
const seoUrl = await getSeoUrlByNavigationId();
await router.replace(
`${getLocalePathPrefix(languageCode)}/${seoUrl}`,
);
} else {
await setLocale(languageCode);
}
await refreshSessionContext();
loading.value = false;
};
Expand Down
16 changes: 16 additions & 0 deletions composables/useLocale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const useLocale = () => {
const { locale, defaultLocale } = useI18n();

const getLocalePathPrefix = (givenLocale?: string) => {
const workingLocale = givenLocale ?? locale.value;
if (workingLocale === defaultLocale) {
return '';
}
return `/${workingLocale}`;

};

return {
getLocalePathPrefix,
};
};
75 changes: 54 additions & 21 deletions composables/useSeoUrl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export function useSeoUrl() {

const { apiClient } = useShopwareContext();
const seoUrlStore = useSeoUrlStore();
const { navigationId } = storeToRefs(seoUrlStore);
const route = useRoute();

const getUrlByProductId = async (id: string) => ({
path: await getSeoUrlByProductId(id),
state: {
Expand All @@ -7,31 +13,58 @@ export function useSeoUrl() {
},
});

return { getUrlByProductId };
}
const getSeoUrlByNavigationId = async () => {
const params = new URLSearchParams(
route.query as Record<string, string>,
);
const seoPath = await getSeoUrlForForeignKey(navigationId.value);
return `${seoPath}?${params.toString()}`;
};

const getSeoUrlByProductId = async (id: string) => {
if (!id) return '/';
const isCustomRoute = computed(() => navigationId.value !== '');

const { apiClient } = useShopwareContext();
const getSeoUrlForForeignKey = async (id: string): Promise<string> => {
if (!id) return '/';

const { data: seoUrls } = await apiClient.invoke('readSeoUrl post /seo-url', {
body: {
filter: [
{
type: 'equals',
field: 'foreignKey',
value: id,
},
],
},
});

const { data: seoUrls } = await apiClient.invoke('readSeoUrl post /seo-url', {
body: {
filter: [
{
type: 'equals',
field: 'pathInfo',
value: `/detail/${ id}`,
return seoUrls.elements?.[0]?.seoPathInfo ?? '';
};

const getSeoUrlByProductId = async (id: string) => {
if (!id) return '/';

const { data: seoUrls } = await apiClient.invoke('readSeoUrl post /seo-url', {
body: {
filter: [
{
type: 'equals',
field: 'pathInfo',
value: `/detail/${id}`,
},
],
limit: 1,
includes: {
seo_url: ['seoPathInfo'],
},
],
limit: 1,
includes: {
seo_url: ['seoPathInfo'],
},
},
});
});

const seoUrl = seoUrls?.elements[0]?.seoPathInfo;
const seoUrl = seoUrls?.elements[0]?.seoPathInfo;

return seoUrl ? `/${seoUrl}` : `/detail/${id}`;
};
return seoUrl ? `/${seoUrl}` : `/detail/${id}`;
};


return { getUrlByProductId, getSeoUrlByNavigationId, isCustomRoute };
}
35 changes: 23 additions & 12 deletions pages/[...all].vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ await refreshSessionContext();
const { resolvePath } = useNavigationSearch();
const route = useRoute();
const { t } = useI18n();
const { setNavigationId, clearNavigationId } = useSeoUrlStore();

/**
* Replace the locale value from the route path
Expand All @@ -18,24 +19,31 @@ const { t } = useI18n();
const { $i18n } = useNuxtApp();
const { locale } = useI18n();
const defaultLocale = $i18n.defaultLocale;
const routePath =
let routePath =
locale.value !== defaultLocale
? route.path.replace(/^\/[^/]+/, '')
: route.path;

const { data: seoResult } = await useAsyncData(`seoPath${routePath}`, async () => {
// For client links if the history state contains seo url information we can omit the api call
if (import.meta.client) {
if (history.state?.routeName) {
return {
routeName: history.state?.routeName,
foreignKey: history.state?.foreignKey,
};
if (routePath === '') {
routePath = '/';
}

const { data: seoResult } = await useAsyncData(
`seoPath/${locale}/${routePath}`,
async () => {
// For client links if the history state contains seo url information we can omit the api call
if (import.meta.client) {
if (history.state?.routeName) {
return {
routeName: history.state?.routeName,
foreignKey: history.state?.foreignKey,
};
}
}
}

return await resolvePath(routePath);
});
return await resolvePath(routePath);
},
);

const { routeName, foreignKey } = useNavigationContext(seoResult);
const { componentExists } = useCmsUtils();
Expand All @@ -44,8 +52,11 @@ if (!routeName.value) {
throw createError({ statusCode: 404, message: t('error.404.detail') });
}

setNavigationId(foreignKey.value);

onBeforeRouteLeave(() => {
clearBreadcrumbs();
clearNavigationId();
});
</script>

Expand Down
13 changes: 13 additions & 0 deletions stores/SeoUrlStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const useSeoUrlStore = defineStore('seoUrl', {
state: () => ({
navigationId: '',
}),
actions: {
setNavigationId(navigation: string) {
this.navigationId = navigation;
},
clearNavigationId() {
this.navigationId = '';
},
},
});