Liquid, JavaScript, themes, sales channels
Hello all, I am working on a Shopify app, and with it a theme extension to pull data from my database and display it for users. I've gone through all the posts I could find about this topic and tried all the suggestions I found, however I am still getting either a CORS error or a 404 not found for my api endpoint.
I have set up an app proxy in my partner's dashboard and I updated the proxy URL every time it's updated. I am using the default cloudflare tunnel setup.
I am using the Remix template, here is my folder structure
Project{
app{
routes{
api{
data.server.js //MY ENDPOINT
}
}
}
EXTENSIONS{
My_Extension{
blocks{
App_block.liquid //MY APP BLOCK
}
}
}
}
In my App Block I am trying to fetch data from my data.server.js using an App proxy. This returns a 404 error. I added fetch options and headers to try and solve the problem with no luck. If I use the cloudflare tunnel URL in my fetch, I will get the cors policy, that's why I have 'Access-Control-Allow-Origin': 'https://{STORE-NAME}.myshopify.com' as an option.
async function fetchData() {
try {
const response = await fetch('/apps/api', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': 'https://{STORE-NAME}.myshopify.com',
}
});
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
const data = await response.json();
const container = document.getElementById('app-block-container');
container.innerText = JSON.stringify(data, null, 2);
} catch (error) {
console.error('There has been a problem with your fetch operation:', error);
}
}
fetchData();
And then my data.server.js
import { authenticate } from "../../shopify.server";
import prisma from '../../db.server';
export let data = async ({ request }) => {
try {
const shopy = await authenticate.admin(request);
const allModels = await prisma.MY_DATA.findMany({
where: {
shop: { equals: shopy.session.shop}
},
});
return {
status: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': 'https://{STORE-NAME}.myshopify.com',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
},
data: JSON.stringify(allModels),
};
} catch (error) {
console.error(error);
return {
status: 500,
data: 'An error occurred while fetching data.',
};
} finally {
await prisma.$disconnect();
}
};
I'm not sure where I'm going wrong. Like I said, I've looked through a bunch of posts, it seems like a relatively common problem, but without any luck on solving it.
Any help would be appreciated! Thanks!
Solved! Go to the solution
This is an accepted solution.
I fixed my issue.
My proxy URL changed to .....cloudflare.com/MY_ENDPOINT
This is an accepted solution.
I fixed my issue.
My proxy URL changed to .....cloudflare.com/MY_ENDPOINT
Hi, @DevBijan
Thanks a lot for sharing your experience. Currently, I am in the same problem: I have to fetch an application route from a block.
With the example you give above, with the subpath 'api' and proxy url '....cloudflare.com/my_endpoint'. What url do you fetch?
Hey, I am fetching '/prefix/subpath' so for example: '/apps/api'
Thank you so much!
And if that's not too many questions... What URL proxy do you have in the configuration and what route do you use in the app?
Is it necessary to reinstall the app?
You can name the route inside your app anything you want. For me my route is 'modelData.jsx' so my proxy url is '...cloudflare.com/modelData'
This will result in fetch('/apps/api') to fetch from '...cloudflare.com/modelData'
If everything is set up correctly but not working during testing, try reinstalling the app to your store.
Exact! After reinstalling it worked.
Thank you so much!
Hello DevBijan,
I am stuck a same as you.
1) I add file to main folder getData.jsx as per your solution
2) Reinstalled app
My proxy URL apps/api
ENDPOINT getData
I am using ngrok for tunnel
On My console network api error show 302 and auth login 404 not found.
Hello,
make sure to double check that the app-proxy url is using the same ngrok tunnel url. It doesn't update automatically so you will need to adjust your app setup each time.
If your path is /apps/api and you have a tunnel url that looks like: ....ngrok.com/getData
you can do a fetch statement like this via an app block.
Hi Dev,
Thanks for reply i figure out the issue, I add admin auth that's why not fetch api, when i change to public auth its work.
Thanks Dev.
User | RANK |
---|---|
37 | |
28 | |
14 | |
13 | |
9 |
Make the shift from discounts to donations, and witness your business not only thrive fina...
By Holly Dec 4, 2023On our Shopify Expert Marketplace, you can find many trusted third party developers and fr...
By Arno Nov 27, 2023You've downloaded the Search & Discovery app from the Shopify App store, and as you're ...
By Skye Nov 8, 2023