My shopify app proxy information:
Subpath prefix: apps
Subpath: rma
Host: https://www.example.com/shopdevc/shopifyAPI/
If I query the host directly using https://www.example.com/shopdevc/shopifyAPI/apps/rma, it works great.
But, in my React function, querying "/apps/rma" returns
<!DOCTYPE html> <html lang="en"> <head> <script type="module" src="/@vite/client"></script> <script type="module"> import RefreshRuntime from "/@react-refresh" RefreshRuntime.injectIntoGlobalHook(window) window.$RefreshReg$ = () => {} window.$RefreshSig$ = () => (type) => type window.__vite_plugin_react_preamble_installed__ = true </script> <meta charset="UTF-8" /> </head> <body> <div id="app"><!--app-html--></div> <script type="module" src="/src/entry-client.jsx"></script> </body> </html>
Is there something wrong with my proxy url or path? My folder structure is
"c:wwwrootshopDevCshopifyAPIappsrmaindex.aspx"
where index.aspx is the default document.
My code:
function delayed_render(async_fun, deps=[]) {
const [output, setOutput] = useState();
useEffect(async () => setOutput(await async_fun()), deps);
return (output === undefined) ? null : output;
}
function TestAPI() {
return delayed_render(async () => {
// const resp = await fetch(`https://www.example.com/shopdevc/shopifyAPI/apps/rma`); // this works fine
const resp = await fetch(`/apps/rma`); //this does not work even though our proxy is set to https://www.example.com/shopdevc/shopifyAPI
const data = await resp.text();
return <div>
<h1> Fetch data from an api in react: {data} </h1>
</div>;
2
Answers
I simply bypassed using the shopify proxy and rolled my own in index.js:
I too have been working on a similar problem today. Almost my exact search phrase
Shopify proxy url not working but unproxied is working
. This is not an answer to your problem but may help you and perhaps others may find this useful. It’s the url resolution I think, not your react code.My skeleton was built with the
shopify-cli
:shopify create app node
.What I saw in my browser developer window when loading
https://{shop}.myshopify.com/{proxy_prefix}/{proxy_path}/{rest-of-path}
was that assets were requested to the root.src="/src/entry-client.jsx"
etc. Which was trying to loadhttps://{shop}.myshopify.com/src/entry-client
. My solution was to replace the root with the full url of my host in the server fileserver/index.js
, . The following works for the compiled files in dist (isProd === true
):For the development server (the
!isProd
section) more substitutions are required:And then to allow the string substitutions the
vite.createServer
instance needs to havemiddlewareMode: "ssr"
(Server-Side Rendering) which meant that thereact-refresh
code was not injected. So I included this:On a final note – I’m still trying to figure out how to make the socket
__vite_ping
go to myHOST
.Good luck. Ngā mihi nui.