Fix realtime compiler loading assets from production URL#2418
Fix realtime compiler loading assets from production URL#2418emmadesilva wants to merge 1 commit intomasterfrom
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
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')toscheme://host[:port]for realtime-compiled responses. - Skip the override when
hyde.server.save_previewis enabled. - Add PHPUnit coverage for scheme/host fallback and the
save_previewguard.
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.
| $this->mockCompilerRoute(''); | ||
| $_SERVER['HTTP_HOST'] = 'localhost:8080'; | ||
|
|
| $host = $_SERVER['HTTP_HOST'] ?? 'localhost'; | ||
|
|
||
| config(['hyde.url' => "$scheme://$host"]); | ||
| } | ||
|
|
||
| /** |
| $host = $_SERVER['HTTP_HOST'] ?? 'localhost'; | ||
|
|
| $_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')); |
| $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')); |
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