-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #746 from deco-cx/fix-magento-extension-cache
Fix magento extension cache
- Loading branch information
Showing
23 changed files
with
379 additions
and
477 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,4 +96,4 @@ const loader = async ( | |
return cartWithImages; | ||
}; | ||
|
||
export default loader; | ||
export default loader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 18 additions & 14 deletions
32
.../product/extensions/amasty/detailsPage.ts → ...sions/product/detailsPage/liveloPoints.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,36 @@ | ||
import { AppContext } from "../../../../mod.ts"; | ||
import extend, { Props } from "../extend.ts"; | ||
import { ProductDetailsPage } from "../../../../../commerce/types.ts"; | ||
import { ExtensionOf } from "../../../../../website/loaders/extension.ts"; | ||
import { AppContext } from "../../../../mod.ts"; | ||
import { ExtensionProps } from "../../../../utils/client/types.ts"; | ||
|
||
/** | ||
* @title Magento ExtensionOf - Details Page | ||
* @title Magento ExtensionOf Details Page - Livelo Points | ||
* @description Add extra data to your loader. This may harm performance | ||
*/ | ||
const loader = ( | ||
props: Omit<Props, "products">, | ||
req: Request, | ||
props: ExtensionProps, | ||
_req: Request, | ||
ctx: AppContext, | ||
): ExtensionOf<ProductDetailsPage | null> => | ||
async (page: ProductDetailsPage | null) => { | ||
if (!page) { | ||
return page; | ||
} | ||
|
||
const products = await extend( | ||
{ products: [page.product], ...props }, | ||
req, | ||
ctx, | ||
); | ||
if (props.active) { | ||
const product = await ctx.invoke.magento.loaders.extensions.product | ||
.liveloPoints({ | ||
products: [page.product], | ||
path: props.path, | ||
from: "PDP", | ||
}); | ||
|
||
return { | ||
...page, | ||
product: products[0], | ||
}; | ||
return { | ||
...page, | ||
product: product[0], | ||
}; | ||
} | ||
return page; | ||
}; | ||
|
||
export default loader; |
36 changes: 36 additions & 0 deletions
36
magento/loaders/extensions/product/detailsPage/reviewsAmasty.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { ProductDetailsPage } from "../../../../../commerce/types.ts"; | ||
import { ExtensionOf } from "../../../../../website/loaders/extension.ts"; | ||
import { AppContext } from "../../../../mod.ts"; | ||
import { ExtensionProps } from "../../../../utils/client/types.ts"; | ||
|
||
/** | ||
* @title Magento ExtensionOf Details Page - Amasty Reviews | ||
* @description Add extra data to your loader. This may harm performance | ||
*/ | ||
const loader = ( | ||
props: ExtensionProps, | ||
_req: Request, | ||
ctx: AppContext, | ||
): ExtensionOf<ProductDetailsPage | null> => | ||
async (page: ProductDetailsPage | null) => { | ||
if (!page) { | ||
return page; | ||
} | ||
|
||
if (props.active) { | ||
const product = await ctx.invoke.magento.loaders.extensions.product | ||
.reviewsAmasty({ | ||
products: [page.product], | ||
path: props.path, | ||
from: "PDP", | ||
}); | ||
|
||
return { | ||
...page, | ||
product: product[0], | ||
}; | ||
} | ||
return page; | ||
}; | ||
|
||
export default loader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Product } from "../../../../../commerce/types.ts"; | ||
import { ExtensionOf } from "../../../../../website/loaders/extension.ts"; | ||
import { AppContext } from "../../../../mod.ts"; | ||
import { ExtensionProps } from "../../../../utils/client/types.ts"; | ||
|
||
/** | ||
* @title Magento ExtensionOf Shelves - Livelo Points | ||
* @description Add extra data to your loader. This may harm performance | ||
*/ | ||
const loader = ( | ||
props: ExtensionProps, | ||
_req: Request, | ||
ctx: AppContext, | ||
): ExtensionOf<Product[] | null> => | ||
async (products: Product[] | null) => { | ||
if (!products || products.length === 0) { | ||
return products; | ||
} | ||
|
||
if (props.active) { | ||
const extendedProducts = await ctx.invoke.magento.loaders.extensions.product | ||
.liveloPoints({ products, path: props.path, from: "SHELF" }); | ||
|
||
return extendedProducts; | ||
} | ||
return products; | ||
}; | ||
|
||
export default loader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Product } from "../../../../../commerce/types.ts"; | ||
import { ExtensionOf } from "../../../../../website/loaders/extension.ts"; | ||
import { AppContext } from "../../../../mod.ts"; | ||
import { ExtensionProps } from "../../../../utils/client/types.ts"; | ||
|
||
/** | ||
* @title Magento ExtensionOf Details Page - Amasty Reviews | ||
* @description Add extra data to your loader. This may harm performance | ||
*/ | ||
const loader = ( | ||
props: ExtensionProps, | ||
_req: Request, | ||
ctx: AppContext, | ||
): ExtensionOf<Product[] | null> => | ||
async (products: Product[] | null) => { | ||
if (!products || products.length === 0) { | ||
return products; | ||
} | ||
|
||
if (props.active) { | ||
const extendedProducts = await ctx.invoke.magento.loaders.extensions.product | ||
.reviewsAmasty({ products, path: props.path, from: "SHELF" }); | ||
|
||
return extendedProducts; | ||
} | ||
return products; | ||
}; | ||
|
||
export default loader; |
28 changes: 14 additions & 14 deletions
28
.../product/extensions/amasty/listingPage.ts → ...sions/product/listingPage/liveloPoints.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
import { AppContext } from "../../../../mod.ts"; | ||
import extend, { Props } from "../extend.ts"; | ||
import { ProductListingPage } from "../../../../../commerce/types.ts"; | ||
import { ExtensionOf } from "../../../../../website/loaders/extension.ts"; | ||
import { AppContext } from "../../../../mod.ts"; | ||
import { ExtensionProps } from "../../../../utils/client/types.ts"; | ||
|
||
/** | ||
* @title Magento ExtensionOf - Listing Page | ||
* @title Magento ExtensionOf Listing Page - Livelo Points | ||
* @description Add extra data to your loader. This may harm performance | ||
*/ | ||
const loader = ( | ||
props: Omit<Props, "products">, | ||
req: Request, | ||
props: ExtensionProps, | ||
_req: Request, | ||
ctx: AppContext, | ||
): ExtensionOf<ProductListingPage | null> => | ||
async (page: ProductListingPage | null) => { | ||
if (!page) { | ||
return page; | ||
} | ||
|
||
const products = await extend( | ||
{ products: page.products, ...props }, | ||
req, | ||
ctx, | ||
); | ||
if (props.active) { | ||
const product = await ctx.invoke.magento.loaders.extensions.product | ||
.liveloPoints({ products: page.products, path: props.path, from: "PLP" }); | ||
|
||
return { | ||
...page, | ||
products, | ||
}; | ||
return { | ||
...page, | ||
product: product[0], | ||
}; | ||
} | ||
return page; | ||
}; | ||
|
||
export default loader; |
36 changes: 36 additions & 0 deletions
36
magento/loaders/extensions/product/listingPage/reviewsAmasty.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { ProductListingPage } from "../../../../../commerce/types.ts"; | ||
import { ExtensionOf } from "../../../../../website/loaders/extension.ts"; | ||
import { AppContext } from "../../../../mod.ts"; | ||
import { ExtensionProps } from "../../../../utils/client/types.ts"; | ||
|
||
/** | ||
* @title Magento ExtensionOf Listing Page - Amasty Reviews | ||
* @description Add extra data to your loader. This may harm performance | ||
*/ | ||
const loader = ( | ||
props: ExtensionProps, | ||
_req: Request, | ||
ctx: AppContext, | ||
): ExtensionOf<ProductListingPage | null> => | ||
async (page: ProductListingPage | null) => { | ||
if (!page) { | ||
return page; | ||
} | ||
|
||
if (props.active) { | ||
const product = await ctx.invoke.magento.loaders.extensions.product | ||
.reviewsAmasty({ | ||
products: page.products, | ||
path: props.path, | ||
from: "PLP", | ||
}); | ||
|
||
return { | ||
...page, | ||
product: product[0], | ||
}; | ||
} | ||
return page; | ||
}; | ||
|
||
export default loader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Product } from "../../../../commerce/types.ts"; | ||
import { STALE as DecoStale } from "../../../../utils/fetch.ts"; | ||
import { AppContext } from "../../../mod.ts"; | ||
import { ExtensionLoaderProps } from "../../../utils/client/types.ts"; | ||
import { toLiveloPoints } from "../../../utils/transform.ts"; | ||
import { sanitizePath } from "../../../utils/utils.ts"; | ||
|
||
/** | ||
* @title Magento Product Extension Loader - Livelo Points | ||
* @description Only invokable | ||
*/ | ||
async function loader( | ||
{ products, path }: ExtensionLoaderProps, | ||
_req: Request, | ||
ctx: AppContext, | ||
): Promise<Product[]> { | ||
const STALE = ctx.enableCache ? DecoStale : undefined; | ||
|
||
const liveloPoints = await Promise.all( | ||
products.map( | ||
async (product) => | ||
await ctx.clientAdmin["GET /rest/:liveloUrl/:productId"]( | ||
{ | ||
liveloUrl: sanitizePath(path), | ||
productId: product!.productID, | ||
}, | ||
STALE, | ||
).then((points) => points.json()), | ||
), | ||
); | ||
|
||
return toLiveloPoints(products, liveloPoints); | ||
} | ||
|
||
export const cache = "stale-while-revalidate"; | ||
|
||
export const cacheKey = ( | ||
{ products, path }: ExtensionLoaderProps, | ||
_req: Request, | ||
_ctx: AppContext, | ||
) => { | ||
const skus = products?.reduce((acc, p) => `${acc}|${p.sku}`, ""); | ||
return `${skus}-liveloPointsExt-${path}`; | ||
}; | ||
|
||
export default loader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Product } from "../../../../commerce/types.ts"; | ||
import { STALE as DecoStale } from "../../../../utils/fetch.ts"; | ||
import { AppContext } from "../../../mod.ts"; | ||
import { ExtensionLoaderProps } from "../../../utils/client/types.ts"; | ||
import { toReviewAmasty } from "../../../utils/transform.ts"; | ||
import { sanitizePath } from "../../../utils/utils.ts"; | ||
|
||
/** | ||
* @title Magento Product Extension Loader - Reviews Amasty | ||
* @description Only invokable | ||
*/ | ||
async function loader( | ||
{ products, path }: ExtensionLoaderProps, | ||
_req: Request, | ||
ctx: AppContext, | ||
): Promise<Product[]> { | ||
const STALE = ctx.enableCache ? DecoStale : undefined; | ||
|
||
const reviews = await Promise.all( | ||
products.map( | ||
async (product) => | ||
await ctx.clientAdmin["GET /rest/:reviewUrl/:productId"]( | ||
{ | ||
reviewUrl: sanitizePath(path), | ||
productId: product!.productID, | ||
}, | ||
STALE, | ||
).then((review) => review.json()), | ||
), | ||
); | ||
|
||
return toReviewAmasty(products, reviews); | ||
} | ||
|
||
export const cache = "stale-while-revalidate"; | ||
|
||
export const cacheKey = ( | ||
{ products, path, from }: ExtensionLoaderProps, | ||
_req: Request, | ||
_ctx: AppContext, | ||
) => { | ||
const skus = products?.reduce((acc, p) => `${acc}|${p.sku}`, ""); | ||
return `${skus}-amastyReviewsExt-${path}-${from}`; | ||
}; | ||
|
||
export default loader; |
Oops, something went wrong.