Hi Developer,
I would really appreciate it if I could get your help regarding my concern.
I’m encountering a CORS error when making a fetch request from my Checkout UI Extension to my app proxy endpoint (/apps/pobox).
Below is my extension toml configuration:
[extensions.capabilities]
block_progress = true
api_access = true
network_access = true
Below is my app.proxy.tsx:
import { json, LoaderFunctionArgs } from "@remix-run/node";
import { authenticate } from "../shopify.server";
import prisma from "../db.server"; // Import Prisma client
export const loader = async ({ request }: LoaderFunctionArgs) => {
// Authenticate the request as a public app proxy
await authenticate.public.appProxy(request);
// Fetch the first record from the `poboxToggle` table
const poboxToggle = await prisma.poboxToggle.findFirst();
console.log("Loader function: Toggle status fetched"); // Less verbose log
// Return the fetched record or a default object if no record is found
return json(poboxToggle || { active: false });
};
Below is the output for this proxy, proxy has been setup correctly, store url is https://{shopify-store}.myshopify.com/apps/pobox and below is the output:
{
“id”: “default-toggle”,
“active”: true,
“createdAt”: “2024-12-16T04:02:30.300Z”
}
However, whenever I try to fetch this proxy data in Checkout extension, i get CORS error, below is my code in Checkout.jsx:
const { address1 } = useShippingAddress() || {};
const [isPOBoxBlockingActive, setIsPOBoxBlockingActive] = useState(false);
const [fetchError, setFetchError] = useState(null);
const extensionApi = useExtensionApi(); // Get the full object
const shop = extensionApi.shop.myshopifyDomain; // Use the `myshopifyDomain` property
useEffect(() => {
const fetchToggleState = async () => {
try {
const apiUrl = `https://${shop}/apps/pobox`; // Use the shop domain dynamically
console.log("Fetching from:", apiUrl); // Log the fetch URL
const response = await fetch(apiUrl, {
headers: { Accept: "application/json" },
});
console.log("Fetch response:", response); // Log the response object
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
const data = await response.json();
console.log("Toggle state fetched successfully:", data);
setIsPOBoxBlockingActive(data.active);
setFetchError(null);
} catch (error) {
console.error("Failed to fetch toggle status:", error);
setFetchError("Failed to load PO Box settings. Please try again later.");
}
};
fetchToggleState();
}, [shop]); // Add shop to the dependency array
However, I am still getting CORS error message in console log as per attached:
Thank you for your time, I would really appreciate your help and information.
Many thanks