Skip to content

Commit d5429f0

Browse files
committed
fix(dev): add assets proxy route for localhost browser dev builds
- Add /proxy/assets/* route to serve-proxy.js proxying to assets.phcode.dev - Tag proxy requests with _proxyTarget so Host header matches the target - Declare ASSETS_SERVER constant for the assets base URL
1 parent 5fbccef commit d5429f0

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

serve-proxy.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const httpProxy = require('http-proxy');
1010
const ACCOUNT_PROD = 'https://account.phcode.dev';
1111
const ACCOUNT_STAGING = 'https://account-stage.phcode.dev';
1212
const ACCOUNT_DEV = 'http://localhost:5000';
13+
const ASSETS_SERVER = 'https://assets.phcode.dev';
1314

1415
// Account server configuration - switch between local and production
1516
let accountServer = ACCOUNT_PROD; // Production
@@ -91,9 +92,11 @@ proxy.on('proxyReq', (proxyReq, req) => {
9192
const originalReferer = req.headers.referer;
9293
const originalOrigin = req.headers.origin;
9394

94-
// Set target host
95-
const accountHost = new URL(accountServer).hostname;
96-
proxyReq.setHeader('Host', accountHost);
95+
// Set target host based on which proxy route is being used
96+
const targetHost = req._proxyTarget
97+
? new URL(req._proxyTarget).hostname
98+
: new URL(accountServer).hostname;
99+
proxyReq.setHeader('Host', targetHost);
97100

98101
// Transform referer from localhost:8000 to phcode.dev
99102
if (originalReferer && originalReferer.includes('localhost:8000')) {
@@ -328,6 +331,7 @@ const server = http.createServer((req, res) => {
328331

329332
// Modify the request URL for the proxy
330333
req.url = targetPath + (parsedUrl.search || '');
334+
req._proxyTarget = accountServer;
331335

332336
if (!config.silent) {
333337
console.log(`[PROXY] ${req.method} ${originalUrl} -> ${accountServer}${req.url}`);
@@ -342,6 +346,24 @@ const server = http.createServer((req, res) => {
342346
return;
343347
}
344348

349+
if (parsedUrl.pathname.startsWith('/proxy/assets')) {
350+
const targetPath = parsedUrl.pathname.replace('/proxy/assets', '');
351+
const originalUrl = req.url;
352+
req.url = targetPath + (parsedUrl.search || '');
353+
req._proxyTarget = ASSETS_SERVER;
354+
355+
if (!config.silent) {
356+
console.log(`[PROXY] ${req.method} ${originalUrl} -> ${ASSETS_SERVER}${req.url}`);
357+
}
358+
359+
proxy.web(req, res, {
360+
target: ASSETS_SERVER,
361+
changeOrigin: true,
362+
secure: true
363+
});
364+
return;
365+
}
366+
345367
// Serve static files
346368
let filePath = path.join(config.root, parsedUrl.pathname);
347369

@@ -385,6 +407,7 @@ server.listen(config.port, config.host, () => {
385407
console.log(` http://${config.host === '0.0.0.0' ? 'localhost' : config.host}:${config.port}`);
386408
console.log(`Proxy routes:`);
387409
console.log(` /proxy/accounts/* -> ${accountServer}/*`);
410+
console.log(` /proxy/assets/* -> ${ASSETS_SERVER}/*`);
388411
console.log('Hit CTRL-C to stop the server');
389412
}
390413
});

0 commit comments

Comments
 (0)