Add typeof window checks for sessionStorage access in checkout
Prevents ReferenceError when sessionStorage is accessed during SSR or server-side rendering contexts. Wraps all sessionStorage calls in typeof window !== 'undefined' checks. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 4.5
parent
570be6430c
commit
6c1053e985
@@ -56,7 +56,9 @@ export default function CheckoutPageClient({ isLoggedIn, customer }: CheckoutPag
|
|||||||
setCheckoutError(null);
|
setCheckoutError(null);
|
||||||
try {
|
try {
|
||||||
// Mark that we're checking out so LogoutOnUnload doesn't clear cart
|
// Mark that we're checking out so LogoutOnUnload doesn't clear cart
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
sessionStorage.setItem('isCheckingOut', 'true');
|
sessionStorage.setItem('isCheckingOut', 'true');
|
||||||
|
}
|
||||||
|
|
||||||
const res = await fetch('/api/checkout', {
|
const res = await fetch('/api/checkout', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -73,7 +75,9 @@ export default function CheckoutPageClient({ isLoggedIn, customer }: CheckoutPag
|
|||||||
});
|
});
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
if (!res.ok || !data.url) {
|
if (!res.ok || !data.url) {
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
sessionStorage.removeItem('isCheckingOut');
|
sessionStorage.removeItem('isCheckingOut');
|
||||||
|
}
|
||||||
throw new Error(data.error ?? 'Something went wrong starting checkout.');
|
throw new Error(data.error ?? 'Something went wrong starting checkout.');
|
||||||
}
|
}
|
||||||
window.location.href = data.url;
|
window.location.href = data.url;
|
||||||
@@ -122,7 +126,9 @@ export default function CheckoutPageClient({ isLoggedIn, customer }: CheckoutPag
|
|||||||
setCheckoutError(null);
|
setCheckoutError(null);
|
||||||
try {
|
try {
|
||||||
// Mark that we're checking out so LogoutOnUnload doesn't clear cart
|
// Mark that we're checking out so LogoutOnUnload doesn't clear cart
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
sessionStorage.setItem('isCheckingOut', 'true');
|
sessionStorage.setItem('isCheckingOut', 'true');
|
||||||
|
}
|
||||||
|
|
||||||
const res = await fetch('/api/checkout', {
|
const res = await fetch('/api/checkout', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -132,7 +138,9 @@ export default function CheckoutPageClient({ isLoggedIn, customer }: CheckoutPag
|
|||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
console.log('Checkout response:', { status: res.status, ok: res.ok, data });
|
console.log('Checkout response:', { status: res.status, ok: res.ok, data });
|
||||||
if (!res.ok || !data.url) {
|
if (!res.ok || !data.url) {
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
sessionStorage.removeItem('isCheckingOut');
|
sessionStorage.removeItem('isCheckingOut');
|
||||||
|
}
|
||||||
throw new Error(data.error ?? 'Something went wrong starting checkout.');
|
throw new Error(data.error ?? 'Something went wrong starting checkout.');
|
||||||
}
|
}
|
||||||
window.location.href = data.url;
|
window.location.href = data.url;
|
||||||
|
|||||||
Reference in New Issue
Block a user