-
-
Notifications
You must be signed in to change notification settings - Fork 642
[6.x] Build the control panel nav only when an Inertia page is rendered #15477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lazerg
wants to merge
3
commits into
statamic:6.x
Choose a base branch
from
lazerg:fix/issue-15475-lazy-cp-nav-share
base: 6.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+118
−6
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
tests/Http/Middleware/HandleAuthenticatedInertiaRequestsTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Http\Middleware; | ||
|
|
||
| use Illuminate\Http\Request; | ||
| use Illuminate\Support\Facades\Route; | ||
| use Inertia\Inertia; | ||
| use PHPUnit\Framework\Attributes\Test; | ||
| use Statamic\Facades\CP\Nav; | ||
| use Statamic\Facades\User; | ||
| use Statamic\Statamic; | ||
| use Tests\PreventSavingStacheItemsToDisk; | ||
| use Tests\TestCase; | ||
|
|
||
| class HandleAuthenticatedInertiaRequestsTest extends TestCase | ||
| { | ||
| use PreventSavingStacheItemsToDisk; | ||
|
|
||
| protected $shouldPreventNavBeingBuilt = false; | ||
|
|
||
| protected function resolveApplicationConfiguration($app) | ||
| { | ||
| parent::resolveApplicationConfiguration($app); | ||
|
|
||
| Statamic::pushCpRoutes(function () { | ||
| Route::get('json-response-test', fn () => ['foo' => 'bar']); | ||
|
|
||
| Route::get('non-inertia-page-data-test', fn () => Statamic::nonInertiaPageData()); | ||
| }); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function it_doesnt_build_the_nav_for_responses_that_arent_inertia_pages() | ||
| { | ||
| $built = false; | ||
|
|
||
| Nav::extend(function () use (&$built) { | ||
| $built = true; | ||
| }); | ||
|
|
||
| $this | ||
| ->actingAs(User::make()->makeSuper()->save()) | ||
| ->get('/cp/json-response-test') | ||
| ->assertOk(); | ||
|
|
||
| $this->assertFalse($built); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function it_builds_the_nav_for_inertia_pages() | ||
| { | ||
| $built = false; | ||
|
|
||
| Nav::extend(function () use (&$built) { | ||
| $built = true; | ||
| }); | ||
|
|
||
| $this | ||
| ->actingAs(User::make()->makeSuper()->save()) | ||
| ->get('/cp/dashboard') | ||
| ->assertOk(); | ||
|
|
||
| $this->assertTrue($built); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function it_resolves_the_nav_for_pages_rendered_outside_of_inertia() | ||
| { | ||
| $data = $this | ||
| ->actingAs(User::make()->makeSuper()->save()) | ||
| ->get('/cp/non-inertia-page-data-test') | ||
| ->assertOk() | ||
| ->json(); | ||
|
|
||
| $this->assertNotEmpty($data['props']['_statamic']['nav']); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function it_resolves_shared_callables_the_same_way_inertia_does() | ||
| { | ||
| Inertia::share([ | ||
| 'closure_with_dependency' => fn (Request $request) => $request->path(), | ||
| 'invokable' => new SharedInvokable, | ||
| ]); | ||
|
|
||
| $data = $this | ||
| ->actingAs(User::make()->makeSuper()->save()) | ||
| ->get('/cp/non-inertia-page-data-test') | ||
| ->assertOk() | ||
| ->json(); | ||
|
|
||
| $this->assertEquals('cp/non-inertia-page-data-test', $data['props']['closure_with_dependency']); | ||
| $this->assertEquals('invoked', $data['props']['invokable']); | ||
| } | ||
| } | ||
|
|
||
| class SharedInvokable | ||
| { | ||
| public function __invoke() | ||
| { | ||
| return 'invoked'; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't quite match how Inertia itself resolves props.
ResolvesCallables::resolveCallable()does:Two differences:
Inertia::share(['foo' => new SomeInvokable])resolves on an Inertia page; here the object passes straight through tojson_encode()and comes out as{}.Our own three closures take no arguments, so core is unaffected either way. But
Inertia::share()is reachable by addons, and this PR is what first puts callables into the shared props at all, so this seems like the moment to make the two paths agree:Illuminate\Support\Facades\Appis already imported in this file, and theClosureimport stays since it's used elsewhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in ea4546a. I reproduced both cases first. The type-hinted closure threw the same
ArgumentCountErroryou quoted, atStatamic.php:527. The invokable came back as[]in the JSON.resolveProps()now does theis_object() && is_callable()check and callsApp::call(), so both paths behave the same.I also added a test that shares a closure with a
Requestdependency and an invokable object, and asserts both resolve outside Inertia.