-
Notifications
You must be signed in to change notification settings - Fork 821
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
[2.x] Inertia: Automatically publish & register Inertia Middleware #327
[2.x] Inertia: Automatically publish & register Inertia Middleware #327
Conversation
Alright, so, tons of changes. Let me guide you through it. 54db22aFirst, I've solved the issue of the Because as a result of this we ended up with a lot of duplicate methods ( 7025200Next, I ran into the dilemma that while Jetstream's To solve this issue, I remembered a thing I learned from @taylorotwell (Laravel ☁️ ) where he used Blade views to 'generate' scripts. I basically did the same thing here by moving the Hope this makes sense! |
To me it feels odd to call Inertia::share outside of a web request context. For example, I would pretty much expect it to be done in a middleware or controller where there is definitely an HTTP request happening. A console command will fire up all service providers and this will call Inertia::share in that case. |
Alright, I went through the Laravel request pipeline, and having it as part of the priority middleware is indeed by far the most preferable approach. I'll revert some of these changes. |
Done. I've reverted some of the changes and will update the original message of this PR to reflect this. In addition, I've briefly had a chat about this topic with @reinink, since we're using |
Also reverts the Inertia\Inertia import, as it was the only 'change' in the file, and will actually cause issues on v2 if the changes get merged into master
Is this ready? If so, can you mark it as ready. I have my GitHub set to filter out draft PRs. |
Yeah, it's ready, but @reinink asked me to re-draft it so he could talk to you about it.. |
Status? Should this be made active? |
Sorry, forgot to post an update here. Because of the conversation that happened here, we've decided to change Inertia's Laravel adapter to use Middleware as well, resulting in this PR: inertiajs/inertia-laravel#161 The plan is to release that PR tomorrow, at which point I'll update this PR and will mark it ready for review. Basically, the idea is that this PR will call |
@taylorotwell Alright, everything seems to be working 👍 I also went ahead and split the |
# Conflicts: # src/Console/InstallCommand.php
@claudiodekker Can this be re-targeted to master so we can release all of these Inertia improvements at once? |
@taylorotwell Sure, give me 5 mins.. verifying that everything will keep working |
# Conflicts: # src/JetstreamServiceProvider.php
@taylorotwell Done 👍 |
I have a feeling all that middleware replacement using PHP_EOL isn't going to work on Windows. 👀 |
src/Console/InstallCommand.php
Outdated
protected function installMiddlewareAfter($after, $name, $group = 'web') | ||
{ | ||
$httpKernel = file_get_contents(app_path('Http/Kernel.php')); | ||
$middlewareGroups = Str::before(Str::after($httpKernel, '$middlewareGroups = ['.PHP_EOL), '];'.PHP_EOL); |
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.
Particularly here. On Windows I'm not sure this Str::after call will return what you want.
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.
Yeah, I think you're right.. I totally forgot about Windows doing things like this differently.
Let me see if I can adjust this.
Honestly my gut would be to just replace the HTTP kernel entirely with a stub that contains exactly what you want. It will have some duplication with Laravel itself but would prevent the need for this complicated replace logic. |
@taylorotwell What about now? Do you still prefer creating a HTTP kernel stub? |
Note that error would only occur on a very fresh install so you might not be seeing it if your testing on app that already had Inertia installed. |
Ah shoot! Yeah, I get what you're saying, it's re-using the container that's already booted. |
Probably you would need to use Symfony Process to run the command. |
OK seems fine to me now. Good to merge? |
Yep, sure. I just got a pretty crazy idea that might actually be a better alternative to the 'patching' that I'm doing now, but I can just PR that later as it's not breaking 👍 |
@taylorotwell In case you were curious: I played around with that idea I mentioned for a bit, but I don't think the end result would be better. Basically, what I wanted to do, is use ReflectionClass::getDefaultProperties to load the Kernel's class properties as an array, grab the middleware group, and add the middleware to that. Then, I'd basically implode the array, and inject it into a While this would work just fine, all comments or middleware that's included but 'commented' by default would be stripped out, and without a good way to grab those I think the end result would be less useful compared to what we have now. |
Solves #326
This PR automatically publishes and registers the new
HandleInertiaRequests
Inertia Middleware with the Http Kernel, to enable automatic asset versioning (cache busting) and error sharing as of default. To take full advantage of this, it also enables versioning as of default in thewebpack.mix.js
.Additionally, because Jetstream automatically registers the
ShareInertiaData
middleware on the 'web' group as priority middleware, this PR also adds logic to ensure theHandleInertiaRequests
middleware always runs directly after it when it exists. This gives the user the ability to overwrite the Inertia built-in defaults (such as theuser
key) if they want to.Finally, because this Middleware will exist entirely in userland, it allows the user to change it in to any configuration they want, including disabling it entirely.