Skip to content

Commit c22b55a

Browse files
committed
don't render on the server
1 parent a91d977 commit c22b55a

File tree

1 file changed

+21
-66
lines changed

1 file changed

+21
-66
lines changed

next/components/Advert.tsx

Lines changed: 21 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -60,51 +60,12 @@ export default function Advert({
6060
}) {
6161
if (process.env.NEXT_PUBLIC_REMOVE_ADVERTS === "1") return null;
6262

63-
// Handle a server-friendly execution path. We will handle the client-side
64-
// logic in effects.
65-
const [cta, setCta] = useState(() => {
66-
// Handle the selected promotion.
67-
const selectedPromotion = marketingData.promotions[instanceGroup];
68-
for (const promotion of selectedPromotion || []) {
69-
if (promotion.if) {
70-
if (promotion.if.gpu && gpu) {
71-
return marketingData.ctas[promotion.cta];
72-
}
73-
} else {
74-
return marketingData.ctas[promotion.cta];
75-
}
76-
}
77-
78-
// Try with the generic promotion.
79-
const genericPromotion = marketingData.promotions.generic;
80-
for (const promotion of genericPromotion || []) {
81-
if (promotion.if) {
82-
if (promotion.if.ab) continue;
83-
if (promotion.if.gpu && gpu) {
84-
return marketingData.ctas[promotion.cta];
85-
}
86-
} else {
87-
return marketingData.ctas[promotion.cta];
88-
}
89-
}
90-
91-
// If no promotion was found, handle it differently depending on if this
92-
// is the server (if this is the server, scream!).
93-
if (typeof window === "undefined") {
94-
throw new Error("No promotion found");
95-
} else {
96-
setTimeout(() => {
97-
throw new Error("No promotion found");
98-
}, 0);
99-
}
100-
101-
// Return a generic promotion.
102-
const cta = Object.keys(marketingData.ctas)[0];
103-
if (cta) {
104-
return marketingData.ctas[cta];
105-
}
106-
return null;
107-
});
63+
// Get the cta.
64+
const [cta, setCta] = useState<{
65+
title: string;
66+
cta_text: string;
67+
cta_url: string;
68+
} | null>(null);
10869

10970
// Make an ab group.
11071
const [abGroup, setAbGroup] = useState(false);
@@ -191,29 +152,23 @@ export default function Advert({
191152
}
192153

193154
// Throw if no promotion was found.
194-
throw new Error("No promotion found");
155+
setTimeout(() => {
156+
throw new Error("No promotion found");
157+
}, 0);
195158
}, [gpu, abGroup, loadedMarketingData]);
196159

197-
const handledCta =
198-
cta ||
199-
(() => {
200-
if (typeof window === "undefined") {
201-
throw new Error("No CTA found");
202-
} else {
203-
setTimeout(() => {
204-
throw new Error("No CTA found");
205-
}, 0);
206-
}
207-
return {
208-
title: "Get a demo",
209-
cta_text: "Get a demo of Vantage from a FinOps expert.",
210-
cta_url:
211-
"https://vantage.sh/lp/aws-instances-demo?utm_campaign=Instances%20Blog%20Clicks&utm_source=aws-banner",
212-
};
213-
})();
160+
// By default, show the banner with no context.
161+
if (!cta) {
162+
return (
163+
<div className="h-[2.5em]" style={style}>
164+
<div className="flex items-center justify-center h-full"></div>
165+
</div>
166+
);
167+
}
214168

169+
// Return when the cta is found.
215170
return (
216-
<a href={handledCta.cta_url} target="_blank">
171+
<a href={cta.cta_url} target="_blank">
217172
<div className="h-[2.5em]" style={style}>
218173
<div className="flex items-center justify-center h-full">
219174
<img
@@ -222,8 +177,8 @@ export default function Advert({
222177
className="h-4 mr-1.5"
223178
/>
224179
<p>
225-
{handledCta.title}{" "}
226-
<span className="font-bold">{handledCta.cta_text}</span>
180+
{cta.title}{" "}
181+
<span className="font-bold">{cta.cta_text}</span>
227182
</p>
228183
</div>
229184
</div>

0 commit comments

Comments
 (0)