Skip to content

Commit

Permalink
fix: ssr context from cookies (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
patzick authored Apr 25, 2023
1 parent 8d056bb commit 4b323a1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changeset/five-icons-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"vue-demo-store": patch
"@shopware-pwa/nuxt3-module": patch
---

Proper SSR context for requests. Logged in client have hydrated data on reload.
5 changes: 0 additions & 5 deletions packages/composables/src/useAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ export function useAddress(): UseAddressReturn {
);
provide("swCustomerAddresses", _storeCustomerAddresses);

watch(isLoggedIn, () => {
_storeCustomerAddresses.value = [];
loadCustomerAddresses();
});

/**
* Get customer address list
*/
Expand Down
18 changes: 16 additions & 2 deletions packages/nuxt3-module/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ const ShopwarePlugin = {
install(app, options) {
const runtimeConfig = useRuntimeConfig();

const contextToken = useCookie("sw-context-token", {
maxAge: 60 * 60 * 24 * 365,
sameSite: "Lax",
path: "/",
});
const languageId = useCookie("sw-language-id", {
maxAge: 60 * 60 * 24 * 365,
sameSite: "Lax",
path: "/",
});

// workaround for SSG case, where cookies contains additional dot in name, related: https://github.com/shopware/frontends/commit/ee5b8a71e1e016c973a7852efa3b85a136e6ea14
const cookieContextToken = Cookies.get("sw-context-token");
const cookieLanguageId = Cookies.get("sw-language-id");

Expand All @@ -35,14 +47,16 @@ const ShopwarePlugin = {
password:
"<%= options.shopwareApiClient.auth ? options.shopwareApiClient.auth.password : undefined %>",
},
contextToken: cookieContextToken,
languageId: cookieLanguageId,
contextToken: contextToken.value || cookieContextToken,
languageId: languageId.value || cookieLanguageId,
});
/**
* Save current contextToken when its change
*/
instance.onConfigChange(({ config }) => {
try {
contextToken.value = config.contextToken;
languageId.value = config.languageId;
Cookies.set("sw-context-token", config.contextToken || "", {
expires: 365, //days
sameSite: "Lax",
Expand Down
6 changes: 0 additions & 6 deletions templates/vue-demo-store/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ useHead({
},
});
const { refreshSessionContext } = useSessionContext();
onBeforeMount(async () => {
await refreshSessionContext();
getWishlistProducts();
});
const { apiInstance } = useShopwareContext();
const { data: sessionContextData } = await useAsyncData(
"sessionContext",
Expand Down
27 changes: 25 additions & 2 deletions templates/vue-demo-store/pages/wishlist.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ export default {
<script setup lang="ts">
import { getProducts } from "@shopware-pwa/api-client";
import { ClientApiError, Product } from "@shopware-pwa/types";
import { removeWishlistProduct } from "@shopware-pwa/api-client";
const { items } = useWishlist();
const { items, getWishlistProducts } = useWishlist();
const { apiInstance } = useShopwareContext();
const products = ref<Product[]>([]);
const isLoading = ref(false);
Expand Down Expand Up @@ -59,6 +60,13 @@ watch(
immediate: true,
}
);
async function clearWishlist() {
await Promise.all(
items.value.map((id) => removeWishlistProduct(id, apiInstance))
);
getWishlistProducts();
}
</script>

<template>
Expand All @@ -67,8 +75,23 @@ watch(
class="max-w-2xl mx-auto py-4 px-4 sm:py-4 sm:px-6 lg:max-w-7xl lg:px-8"
>
<!-- Wishlist is completed -->
<div v-if="products.length">
<div
v-if="isLoading"
class="absolute inset-0 flex items-center justify-center z-10 bg-white/50"
>
<div
class="h-15 w-15 i-carbon-progress-bar-round animate-spin c-gray-500"
/>
</div>
<div v-else-if="products.length">
<h1 class="my-3 text-3xl font-extrabold">Wishlist</h1>
<button
class="mb-4 justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-black hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500"
data-testid="clear-wishlist-button"
@click="clearWishlist"
>
Clear wishlist
</button>
<div
class="grid grid-cols-1 gap-y-10 gap-x-6 sm:grid-cols-2 lg:grid-cols-4 xl:gap-x-8"
>
Expand Down

2 comments on commit 4b323a1

@vercel
Copy link

@vercel vercel bot commented on 4b323a1 Apr 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 4b323a1 Apr 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

frontends-demo – ./templates/vue-demo-store

frontends-demo-shopware-frontends.vercel.app
frontends-demo.vercel.app
frontends-demo-git-main-shopware-frontends.vercel.app

Please sign in to comment.