Bogdan,
I did solve this last spring; with a bit of trial and error:
Note the proxy set in the vite config file, located in your front-end directory.
vite.config.js
import { defineConfig } from "vite";
import { dirname } from "path";
import { fileURLToPath } from "url";
//import https from "https";
import react from "@vitejs/plugin-react";
import { nodePolyfills } from 'vite-plugin-node-polyfills'
import dotenv from 'dotenv';
//import basicSsl from '@vitejs/plugin-basic-ssl'
dotenv.config(); // load env vars from .env
if (
process.env.npm_lifecycle_event === "build" &&
!process.env.CI &&
!process.env.SHOPIFY_API_KEY
) {
console.warn(
"\nBuilding the frontend app without an API key. The frontend build will not run without an API key. Set the SHOPIFY_API_KEY environment variable when running the build command.\n"
);
}
// Note for below: Change *[http://ITS-D25Q310QF8J](http://ITS-D25Q310QF8J) to resolve to your host*
const proxyOptions = {
target: `*http://ITS-D25Q310QF8J*:${process.env.BACKEND_PORT}`,
changeOrigin: true,
secure: false, // changed from true 4-3-23
ws: false,
};
const host = process.env.HOST
? process.env.HOST.replace(/https?:\/\//, "")
: "localhost";
let hmrConfig;
if (host === "localhost") {
hmrConfig = {
protocol: "ws",
host: "localhost",
port: 64999,
clientPort: 64999,
};
} else {
hmrConfig = {
protocol: "wss",
host: host,
port: process.env.FRONTEND_PORT,
clientPort: 443,
};
}
console.log("48 hmrConfig: ", hmrConfig);
export default defineConfig({
root: dirname(fileURLToPath(import.meta.url)),
plugins: [
react(),
nodePolyfills({
// Whether to polyfill `node:` protocol imports.
protocolImports: true,
}),
//basicSsl(),
],
define: {
"process.env.SHOPIFY_API_KEY": JSON.stringify(process.env.SHOPIFY_API_KEY),
'process.env.NODE_DEBUG' : "'development'",
'global' : {
"Uint8Array": Uint8Array
},
__BACKEND_PORT__: `"${process.env.BACKEND_PORT}"`,
},
resolve: {
preserveSymlinks: true,
alias: {
//'node:stream/web': path.resolve(__dirname, 'node_modules/stream-browserify/index.js'),
//stream: 'stream-browserify',
'node:stream/web':'stream-browserify/index.js',
'node:fs':'node/lib/fs.js',
},
},
server: {
host: "its-d25q310qf8j", // Resolve to your host server
port: process.env.FRONTEND_PORT,
hmr: hmrConfig,
cors: true,
proxy: {
"^/(\\?.*)?$": proxyOptions,
"^/api(/|(\\?.*)?$)": proxyOptions,
"^/qrcodes/[0-9]+/image(\\?.*)?$": proxyOptions,
"^/qrcodes/[0-9]+/scan(\\?.*)?$": proxyOptions,
"^/smp(/|(\\?.*)?$)": proxyOptions,
},
},
});
With the vite proxy set for your host machine, the rest of the Shopify CLIv3 config is straight-forward:
App-level shopify.app.toml
# This file stores configurations for your Shopify app.
scopes = "write_products,read_discounts,write_customers ....
Back-end shopify.app.toml
==========================
type="backend"
[commands]
dev = "npm run dev"
PORT=8081
FRONTEND_PORT=8081
BACKEND_PORT=8081
Front-end shopify.app.toml
type="frontend"
[commands]
dev = "npm run dev"
build = "npm run build"
PORT=8081
FRONTEND_PORT=8081
BACKEND_PORT=8081
===
Note:
Shopify should publish a pattern with best practices for the CLIv3 framework,
including its move from webpack to vite. Deeper documentation of FRONTEND_PORT / BACKEND_PORT and how this all of this works with VITE would also be useful.
I hope this helps you.
Regards,
Tom