Skip to content

Commit eac3abb

Browse files
Merge pull request #27 from LelabsTeam/fix-canonical-tavano/myvtex
Fix canonical tavano/myvtex
2 parents b0549da + 75b0a60 commit eac3abb

File tree

5 files changed

+39
-26
lines changed

5 files changed

+39
-26
lines changed

utils/fetch.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ export const fetchSafe = async (
4949
return response;
5050
}
5151

52-
logger.error(`error sending request for url: ${input}, body error is: ${await response.text()}`)
53-
throw new HttpError(response.status, `${await response.text()}`);
52+
const responseText = await response.text();
53+
logger.error(
54+
`error sending request for url: ${input}, body error is: ${responseText}`,
55+
);
56+
throw new HttpError(response.status, `${responseText}`);
5457
};
5558
export const fetchAPI = async <T>(
5659
input: string | Request | URL,

utils/http.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpError as DecoHttpError, type RequestInit } from "@deco/deco";
1+
import { type RequestInit } from "@deco/deco";
22
import { fetchSafe } from "./fetch.ts";
33

44
// Check if DEBUG_HTTP env var is set
@@ -17,17 +17,9 @@ const HTTP_VERBS = new Set(
1717
"HEAD",
1818
] as const,
1919
);
20-
export class HttpError extends DecoHttpError {
21-
constructor(status: number, message?: string) {
22-
super(
23-
new Response(JSON.stringify({ message }), {
24-
status,
25-
headers: {
26-
"Content-Type": "application/json",
27-
},
28-
}),
29-
);
30-
this.message = message ?? `http error ${status}`;
20+
export class HttpError extends Error {
21+
constructor(public status: number, message?: string, options?: ErrorOptions) {
22+
super(message, options);
3123
this.name = `HttpError ${status}`;
3224
}
3325
}

vtex/loaders/legacy/relatedProductsLoader.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AppContext } from "../../mod.ts";
55
import { batch } from "../../utils/batch.ts";
66
import { isFilterParam, toSegmentParams } from "../../utils/legacy.ts";
77
import { getSegmentFromBag, withSegmentCookie } from "../../utils/segment.ts";
8-
import { pickSku } from "../../utils/transform.ts";
8+
import { pickSku, toProduct } from "../../utils/transform.ts";
99
import type { CrossSellingType } from "../../utils/types.ts";
1010
import productList from "./productList.ts";
1111

@@ -95,6 +95,24 @@ async function loader(
9595
);
9696
}
9797

98+
const inStock = (p: Product) =>
99+
p.offers?.offers.find((o) =>
100+
o.availability === "https://schema.org/InStock"
101+
);
102+
103+
if(ctx.advancedConfigs?.doNotFetchVariantsForRelatedProducts) {
104+
const toProducts = products.slice(0, count).map((p) =>
105+
toProduct(p, p.items[0], 0, {
106+
baseUrl: req.url,
107+
priceCurrency: segment?.payload?.currencyCode ?? "BRL",
108+
})
109+
);
110+
if(hideUnavailableItems){
111+
return toProducts.filter(inStock)
112+
}
113+
return toProducts;
114+
}
115+
98116
// unique Ids
99117
const relatedIds = [...new Set(
100118
products.slice(0, count).map((p) => pickSku(p).itemId),
@@ -127,10 +145,6 @@ async function loader(
127145
});
128146

129147
if (hideUnavailableItems && relatedProducts) {
130-
const inStock = (p: Product) =>
131-
p.offers?.offers.find((o) =>
132-
o.availability === "https://schema.org/InStock"
133-
);
134148

135149
return relatedProducts.filter(inStock);
136150
}

vtex/mod.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ export interface Props {
8989
* @title Skip Simulation Behavior
9090
*/
9191
skipSimulationBehavior?: Matcher;
92+
93+
advancedConfigs?: {
94+
doNotFetchVariantsForRelatedProducts?: boolean;
95+
}
9296
}
9397
export const color = 0xf71963;
9498
/**
@@ -123,18 +127,18 @@ export default function VTEX(
123127
fetcher: fetchSafe,
124128
});
125129
const vcsDeprecated = createHttpClient<VTEXCommerceStable>({
126-
base: `https://${account}.vtexcommercestable.com.br`,
130+
base: `https://${account}.myvtex.com/`,
127131
processHeaders: removeDirtyCookies,
128132
fetcher: fetchSafe,
129133
});
130134
const io = createGraphqlClient({
131135
endpoint:
132-
`https://${account}.vtexcommercestable.com.br/api/io/_v/private/graphql/v1`,
136+
`https://${account}.myvtex.com/api/io/_v/private/graphql/v1`,
133137
processHeaders: removeDirtyCookies,
134138
fetcher: fetchSafe,
135139
});
136140
const vcs = createHttpClient<VCS>({
137-
base: `https://${account}.vtexcommercestable.com.br`,
141+
base: `https://${account}.myvtex.com/`,
138142
fetcher: fetchSafe,
139143
processHeaders: removeDirtyCookies,
140144
headers: headers,

vtex/utils/legacy.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ export const pageTypesToSeo = (
113113
const current = pages.at(-1);
114114
const url = new URL(baseUrl);
115115
const fullTextSearch = url.searchParams.get("q");
116-
117116
const canonical = `https://${url.host + url.pathname}`;
118117

119118
if (
@@ -132,6 +131,8 @@ export const pageTypesToSeo = (
132131
return null;
133132
}
134133

134+
const regexBadDomain = /casaevideonewio\.myvtex\.com\/|lebiscuit\.myvtex\.com\/|.+\.vtexcommercestable\.com\.br\//;
135+
135136
return {
136137
title: current.title || current.name || "",
137138
description: current.metaTagDescription!,
@@ -140,7 +141,7 @@ export const pageTypesToSeo = (
140141
new URL(
141142
(current.url && current.pageType !== "Collection" &&
142143
current.pageType !== "Brand")
143-
? current.url.replace(/.+\.vtexcommercestable\.com\.br/, "")
144+
? current.url.replace(regexBadDomain, "")
144145
.toLowerCase()
145146
: url,
146147
url,
@@ -155,8 +156,7 @@ function toCanonical(url: URL, page?: number) {
155156
if (typeof page === "number") {
156157
url.searchParams.set("page", `${page}`);
157158
}
158-
159-
return url.href;
159+
return url.href
160160
}
161161

162162
/**

0 commit comments

Comments
 (0)