Skip to content

Commit

Permalink
Fix loading user and cart information (#6265)
Browse files Browse the repository at this point in the history
* Fix loading user and cart information

* Add changelogs
  • Loading branch information
filipsobol committed Sep 3, 2021
1 parent 8eef208 commit f20d9f9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
Expand Up @@ -15,6 +15,7 @@ const load = async (context: Context, {customQuery}) => {
}

const profile = await context.$ct.api.getMe({ customer: true }, customQuery);
context.setCart(profile.data.me.activeCart);
return profile.data.me.customer;
};

Expand Down
8 changes: 6 additions & 2 deletions packages/commercetools/theme/components/AppHeader.vue
Expand Up @@ -97,6 +97,7 @@
<script>
import { SfHeader, SfImage, SfIcon, SfButton, SfBadge, SfSearchBar, SfOverlay, SfMenuItem, SfLink } from '@storefront-ui/vue';
import { useUiState } from '~/composables';
import { onSSR } from '@vue-storefront/core';
import { useCart, useUser, cartGetters } from '@vue-storefront/commercetools';
import { computed, ref, onBeforeUnmount, watch } from '@vue/composition-api';
import { useUiHelpers } from '~/composables';
Expand Down Expand Up @@ -131,7 +132,7 @@ export default {
const { toggleCartSidebar, toggleWishlistSidebar, toggleLoginModal, isMobileMenuOpen } = useUiState();
const { setTermForUrl, getFacetsFromURL } = useUiHelpers();
const { isAuthenticated, load: loadUser } = useUser();
const { cart } = useCart();
const { cart, load: loadCart } = useCart();
const term = ref(getFacetsFromURL().phrase);
const isSearchOpen = ref(false);
const searchBarRef = ref(null);
Expand All @@ -145,7 +146,10 @@ export default {
const accountIcon = computed(() => isAuthenticated.value ? 'profile_fill' : 'profile');
loadUser();
onSSR(async () => {
await loadUser();
await loadCart();
});
// TODO: https://github.com/DivanteLtd/vue-storefront/issues/4927
const handleAccountClick = async () => {
Expand Down
15 changes: 15 additions & 0 deletions packages/core/docs/changelog/6265.js
@@ -0,0 +1,15 @@
module.exports = {
description: 'Fix loading user and cart information',
link: 'https://github.com/vuestorefront/vue-storefront/pull/6265/',
isBreaking: true,
breakingChanges: [
{
module: 'Base theme',
before: '`loadCart` was called directly inside `setup` method in `CartSidebar.vue` component',
after: '`loadCart` is called inside `onSSR` callback in `CartSidebar.vue` component',
comment: 'Calling `loadCart` directly inside `setup` method caused hydration issues, since cart information was not properly loaded during SSR'
}
],
author: 'Filip Sobol',
linkToGitHubAccount: 'https://github.com/filipsobol'
};
15 changes: 15 additions & 0 deletions packages/core/docs/commercetools/changelog/6265.js
@@ -0,0 +1,15 @@
module.exports = {
description: 'Fix loading user and cart information',
link: 'https://github.com/vuestorefront/vue-storefront/pull/6265/',
isBreaking: true,
breakingChanges: [
{
module: 'commercetools theme',
before: '`loadUser` was called directly inside `setup` method in `CartSidebar.vue` component',
after: '`loadUser` is called inside `onSSR` callback in `CartSidebar.vue` component',
comment: 'Calling `loadUser` directly inside `setup` method caused hydration issues, since cart information was not properly loaded during SSR. Additionally cart will now be automatically updated after calling `load` from the `useUser` composable, the same way as it happens when calling `logIn`, `logOut` and `register`.'
}
],
author: 'Filip Sobol',
linkToGitHubAccount: 'https://github.com/filipsobol'
};
Expand Up @@ -121,6 +121,7 @@ import {
SfQuantitySelector
} from '@storefront-ui/vue';
import { computed } from '@vue/composition-api';
import { onSSR } from '@vue-storefront/core';
import { useCart, useUser, cartGetters } from '<%= options.generate.replace.composables %>';
import { useUiState } from '~/composables';
Expand All @@ -144,7 +145,10 @@ export default {
const products = computed(() => cartGetters.getItems(cart.value));
const totals = computed(() => cartGetters.getTotals(cart.value));
const totalItems = computed(() => cartGetters.getTotalItems(cart.value));
loadCart();
onSSR(async () => {
await loadCart();
});
return {
loading,
Expand Down

0 comments on commit f20d9f9

Please sign in to comment.