Skip to content

Commit 2227eee

Browse files
authored
Merge pull request #737 from inertiajs/vite-before-mix-manifest
[2.x] Check Vite manifest before Mix manifest
2 parents 2ac6600 + 0acdad3 commit 2227eee

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

.github/workflows/tests.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ jobs:
4343
timeout_minutes: 5
4444
max_attempts: 5
4545
command: |
46-
composer require phpunit/phpunit:^9.5.8 --dev --${{ matrix.stability }} --no-update --no-interaction
4746
composer require vlucas/phpdotenv:^5.3.1 --${{ matrix.stability }} --no-update --no-interaction
4847
if: matrix.php >= 8.1 && matrix.stability == 'prefer-lowest'
4948

@@ -63,7 +62,6 @@ jobs:
6362
max_attempts: 5
6463
command: |
6564
composer require "orchestra/testbench:^9.2|^10.0" --dev --${{ matrix.stability }} --no-update --no-interaction
66-
composer require "phpunit/phpunit:^10.4|^11.5" --dev --${{ matrix.stability }} --no-update --no-interaction
6765
if: matrix.php >= 8.2 && matrix.stability == 'prefer-lowest' && matrix.laravel >= 11
6866

6967
- name: Set Laravel version

src/Middleware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public function version(Request $request)
3232
return hash('xxh128', config('app.asset_url'));
3333
}
3434

35-
if (file_exists($manifest = public_path('mix-manifest.json'))) {
35+
if (file_exists($manifest = public_path('build/manifest.json'))) {
3636
return hash_file('xxh128', $manifest);
3737
}
3838

39-
if (file_exists($manifest = public_path('build/manifest.json'))) {
39+
if (file_exists($manifest = public_path('mix-manifest.json'))) {
4040
return hash_file('xxh128', $manifest);
4141
}
4242

tests/MiddlewareTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Inertia\Tests;
44

5+
use Illuminate\Filesystem\Filesystem;
56
use Illuminate\Http\Request;
67
use Illuminate\Session\Middleware\StartSession;
78
use Illuminate\Support\Facades\Route;
@@ -13,9 +14,16 @@
1314
use Inertia\Middleware;
1415
use Inertia\Tests\Stubs\ExampleMiddleware;
1516
use LogicException;
17+
use PHPUnit\Framework\Attributes\After;
1618

1719
class MiddlewareTest extends TestCase
1820
{
21+
#[After]
22+
public function cleanupPublicFolder(): void
23+
{
24+
(new Filesystem)->cleanDirectory(public_path());
25+
}
26+
1927
public function test_no_response_value_by_default_means_automatically_redirecting_back_for_inertia_requests(): void
2028
{
2129
$fooCalled = false;
@@ -241,6 +249,49 @@ public function rootView(Request $request): string
241249
$response->assertViewIs('welcome');
242250
}
243251

252+
public function test_determine_the_version_by_a_hash_of_the_asset_url(): void
253+
{
254+
config(['app.asset_url' => $url = 'https://example.com/assets']);
255+
256+
$this->prepareMockEndpoint(middleware: new Middleware);
257+
258+
$response = $this->get('/');
259+
$response->assertOk();
260+
$response->assertViewHas('page.version', hash('xxh128', $url));
261+
}
262+
263+
public function test_determine_the_version_by_a_hash_of_the_vite_manifest(): void
264+
{
265+
$filesystem = new Filesystem;
266+
$filesystem->ensureDirectoryExists(public_path('build'));
267+
$filesystem->put(
268+
public_path('build/manifest.json'),
269+
$contents = json_encode(['vite' => true])
270+
);
271+
272+
$this->prepareMockEndpoint(middleware: new Middleware);
273+
274+
$response = $this->get('/');
275+
$response->assertOk();
276+
$response->assertViewHas('page.version', hash('xxh128', $contents));
277+
}
278+
279+
public function test_determine_the_version_by_a_hash_of_the_mix_manifest(): void
280+
{
281+
$filesystem = new Filesystem;
282+
$filesystem->ensureDirectoryExists(public_path());
283+
$filesystem->put(
284+
public_path('mix-manifest.json'),
285+
$contents = json_encode(['mix' => true])
286+
);
287+
288+
$this->prepareMockEndpoint(middleware: new Middleware);
289+
290+
$response = $this->get('/');
291+
$response->assertOk();
292+
$response->assertViewHas('page.version', hash('xxh128', $contents));
293+
}
294+
244295
private function prepareMockEndpoint($version = null, $shared = [], $middleware = null): \Illuminate\Routing\Route
245296
{
246297
if (is_null($middleware)) {

0 commit comments

Comments
 (0)