Skip to content

Fix realtime compiler loading assets from production URL#2418

Draft
emmadesilva wants to merge 1 commit intomasterfrom
fix/realtime-compiler-localhost-urls
Draft

Fix realtime compiler loading assets from production URL#2418
emmadesilva wants to merge 1 commit intomasterfrom
fix/realtime-compiler-localhost-urls

Conversation

@emmadesilva
Copy link
Copy Markdown
Member

When the realtime compiler served pages with a configured site URL (e.g. hydephp.com), asset and media file references were generated as absolute URLs pointing to the production host, causing the browser to request files from production instead of the local server.

Override the configured site URL to the current request's host:port when booting the application for page compilation. This ensures all generated asset links resolve against localhost (or the configured dev server) rather than production.

Skip the override when save_preview is enabled to avoid baking the local URL into persisted output files.

Closes #2327

When the realtime compiler served pages with a configured site URL (e.g.
hydephp.com), asset and media file references were generated as absolute
URLs pointing to the production host, causing the browser to request files
from production instead of the local server.

Override the configured site URL to the current request's host:port when
booting the application for page compilation. This ensures all generated
asset links resolve against localhost (or the configured dev server) rather
than production.

Skip the override when save_preview is enabled to avoid baking the local
URL into persisted output files.

Closes #2327

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (aa4be0b) to head (87c8d36).
⚠️ Report is 9 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##              master     #2418   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
  Complexity      2003      2003           
===========================================
  Files            197       197           
  Lines           5135      5135           
===========================================
  Hits            5135      5135           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes the realtime compiler generating absolute asset/media URLs pointing at the configured production hyde.url by overriding the site URL to the current request host during local page compilation (while avoiding persisted output when save_preview is enabled).

Changes:

  • Override config('hyde.url') to scheme://host[:port] for realtime-compiled responses.
  • Skip the override when hyde.server.save_preview is enabled.
  • Add PHPUnit coverage for scheme/host fallback and the save_preview guard.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
packages/realtime-compiler/src/Routing/Router.php Overrides hyde.url during request handling so compiled pages reference the local server host instead of production.
packages/realtime-compiler/tests/RealtimeCompilerTest.php Adds tests validating the override behavior (host, HTTPS, fallback, and save_preview opt-out).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +252 to +254
$this->mockCompilerRoute('');
$_SERVER['HTTP_HOST'] = 'localhost:8080';

Comment on lines +92 to 97
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';

config(['hyde.url' => "$scheme://$host"]);
}

/**
Comment on lines +92 to +93
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';

Comment on lines +218 to +262
$_SERVER['HTTP_HOST'] = 'localhost:8080';

config(['hyde.url' => 'https://hydephp.com']);

$this->invokeOverrideSiteUrl();

$this->assertSame('http://localhost:8080', config('hyde.url'));
}

public function testOverridesSiteUrlUsesHttpsSchemeForSecureRequests()
{
$this->mockCompilerRoute('');
$_SERVER['HTTP_HOST'] = 'hyde.test';
$_SERVER['HTTPS'] = 'on';

$this->invokeOverrideSiteUrl();

$this->assertSame('https://hyde.test', config('hyde.url'));

unset($_SERVER['HTTPS']);
}

public function testOverridesSiteUrlFallsBackToLocalhostWhenHostIsMissing()
{
$this->mockCompilerRoute('');
unset($_SERVER['HTTP_HOST']);

$this->invokeOverrideSiteUrl();

$this->assertSame('http://localhost', config('hyde.url'));
}

public function testDoesNotOverrideSiteUrlWhenSavePreviewIsEnabled()
{
$this->mockCompilerRoute('');
$_SERVER['HTTP_HOST'] = 'localhost:8080';

config([
'hyde.server.save_preview' => true,
'hyde.url' => 'https://hydephp.com',
]);

$this->invokeOverrideSiteUrl();

$this->assertSame('https://hydephp.com', config('hyde.url'));
Comment on lines +217 to +247
$this->mockCompilerRoute('');
$_SERVER['HTTP_HOST'] = 'localhost:8080';

config(['hyde.url' => 'https://hydephp.com']);

$this->invokeOverrideSiteUrl();

$this->assertSame('http://localhost:8080', config('hyde.url'));
}

public function testOverridesSiteUrlUsesHttpsSchemeForSecureRequests()
{
$this->mockCompilerRoute('');
$_SERVER['HTTP_HOST'] = 'hyde.test';
$_SERVER['HTTPS'] = 'on';

$this->invokeOverrideSiteUrl();

$this->assertSame('https://hyde.test', config('hyde.url'));

unset($_SERVER['HTTPS']);
}

public function testOverridesSiteUrlFallsBackToLocalhostWhenHostIsMissing()
{
$this->mockCompilerRoute('');
unset($_SERVER['HTTP_HOST']);

$this->invokeOverrideSiteUrl();

$this->assertSame('http://localhost', config('hyde.url'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Realtime Compiler] Images trying to load from production URL when on localhost

2 participants