diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1a7314a5..3591aea7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -43,7 +43,6 @@ jobs: timeout_minutes: 5 max_attempts: 5 command: | - composer require phpunit/phpunit:^9.5.8 --dev --${{ matrix.stability }} --no-update --no-interaction composer require vlucas/phpdotenv:^5.3.1 --${{ matrix.stability }} --no-update --no-interaction if: matrix.php >= 8.1 && matrix.stability == 'prefer-lowest' @@ -63,7 +62,6 @@ jobs: max_attempts: 5 command: | composer require "orchestra/testbench:^9.2|^10.0" --dev --${{ matrix.stability }} --no-update --no-interaction - composer require "phpunit/phpunit:^10.4|^11.5" --dev --${{ matrix.stability }} --no-update --no-interaction if: matrix.php >= 8.2 && matrix.stability == 'prefer-lowest' && matrix.laravel >= 11 - name: Set Laravel version diff --git a/src/Middleware.php b/src/Middleware.php index ba4d9a34..8c2d9e24 100644 --- a/src/Middleware.php +++ b/src/Middleware.php @@ -32,11 +32,11 @@ public function version(Request $request) return hash('xxh128', config('app.asset_url')); } - if (file_exists($manifest = public_path('mix-manifest.json'))) { + if (file_exists($manifest = public_path('build/manifest.json'))) { return hash_file('xxh128', $manifest); } - if (file_exists($manifest = public_path('build/manifest.json'))) { + if (file_exists($manifest = public_path('mix-manifest.json'))) { return hash_file('xxh128', $manifest); } diff --git a/tests/MiddlewareTest.php b/tests/MiddlewareTest.php index c00bf7b1..002499fa 100644 --- a/tests/MiddlewareTest.php +++ b/tests/MiddlewareTest.php @@ -2,6 +2,7 @@ namespace Inertia\Tests; +use Illuminate\Filesystem\Filesystem; use Illuminate\Http\Request; use Illuminate\Session\Middleware\StartSession; use Illuminate\Support\Facades\Route; @@ -13,9 +14,16 @@ use Inertia\Middleware; use Inertia\Tests\Stubs\ExampleMiddleware; use LogicException; +use PHPUnit\Framework\Attributes\After; class MiddlewareTest extends TestCase { + #[After] + public function cleanupPublicFolder(): void + { + (new Filesystem)->cleanDirectory(public_path()); + } + public function test_no_response_value_by_default_means_automatically_redirecting_back_for_inertia_requests(): void { $fooCalled = false; @@ -241,6 +249,49 @@ public function rootView(Request $request): string $response->assertViewIs('welcome'); } + public function test_determine_the_version_by_a_hash_of_the_asset_url(): void + { + config(['app.asset_url' => $url = 'https://example.com/assets']); + + $this->prepareMockEndpoint(middleware: new Middleware); + + $response = $this->get('/'); + $response->assertOk(); + $response->assertViewHas('page.version', hash('xxh128', $url)); + } + + public function test_determine_the_version_by_a_hash_of_the_vite_manifest(): void + { + $filesystem = new Filesystem; + $filesystem->ensureDirectoryExists(public_path('build')); + $filesystem->put( + public_path('build/manifest.json'), + $contents = json_encode(['vite' => true]) + ); + + $this->prepareMockEndpoint(middleware: new Middleware); + + $response = $this->get('/'); + $response->assertOk(); + $response->assertViewHas('page.version', hash('xxh128', $contents)); + } + + public function test_determine_the_version_by_a_hash_of_the_mix_manifest(): void + { + $filesystem = new Filesystem; + $filesystem->ensureDirectoryExists(public_path()); + $filesystem->put( + public_path('mix-manifest.json'), + $contents = json_encode(['mix' => true]) + ); + + $this->prepareMockEndpoint(middleware: new Middleware); + + $response = $this->get('/'); + $response->assertOk(); + $response->assertViewHas('page.version', hash('xxh128', $contents)); + } + private function prepareMockEndpoint($version = null, $shared = [], $middleware = null): \Illuminate\Routing\Route { if (is_null($middleware)) {