-
Notifications
You must be signed in to change notification settings - Fork 155
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
[feat] Option to set dev server URL manually for @vite (without touching server.hmr) #83
Comments
Hi @mandrasch,
The Laravel Vite plugin also looks at this configuration to determine how to write to the hot file, which makes it consistent with Vite. I would imagine you'd need at least the following config: server: {
// Listen on all addresses inside the container
host: true,
hmr: {
// Force the Vite client to connect via SSL
// This will also force a "https://" URL in the hot file
protocol: 'wss',
// The host where the Vite dev server can be accessed
// This will also force this host to be written to the hot file
host: 'my-laravel-project.ddev.site',
},
}, The port config is optional, but with Vite 3 changing the default port to 5173, you may want to specify it as we have running inside using Sail. I'm not sure how your traffic is routed from |
Closing this as this is more a support question. Please try a support channel if you need help integrating this with Docker. |
Thanks very much for responding so fast @jessarcher! 👍 I tried your suggestion and it breaks vite responding to requests in DDEVs (reverse proxy) router. Unfortunately I'm not so deep into this to describe why it breaks. Guess the answer is somewhere in https://v2.vitejs.dev/config/#server-hmr (and as I stated other DDEV users advised not to use hmr in the first place). The port exposing works fine. When I manually replace I'll try a support channel as suggested by @driesvints as well. |
Got Enzos take on this (via Discord vite - laravel), asked him why he chose
|
Quick update, details for the above comment for interested readers of this issue. If I understood Enzo correctly,
This would allow it to work in use cases like DDEV / reverse proxy / Docker with the following vite.config without
(Background: Others and I had no luck with |
This is a misunderstanding of server.hmr. server.hmr controls what parameters are passed from the Vite dev server to the client.ts script that initiates the HMR web socket after the vite 'nub' script starts up and loads its assets from the vite dev server. It does not "to look for the dev server in a different location than it's served". It tells client.ts where to look for web socket end point. That's ALL it does.
I'm not a Laravel guy. "hot file" is not a Vite term. What does this mean?
Nope. wrong. The problem is that See this issue for more info. But your information here is incorrect. I'm reasonably sure (from knowing Drupal and Symfony internals) that Laravel has access to info from the HTTP request as to what's on the other side of the proxy. You should use that info as defaults in resolving <script type="module" src="[http://[::]:5173/@vite/client](view-source:http://[::]:5173/@vite/client)"></script><link rel="stylesheet" href="[http://[::]:5173/resources/css/app.css](view-source:http://[::]:5173/resources/css/app.css)" /><script type="module" src="[http://[::]:5173/resources/js/app.js](view-source:http://[::]:5173/resources/js/app.js)"></script> |
I'm not sure how this is really different from what I said. Perhaps I could have used better terminology:
The "hot file" is a plain text file that this plugin writes to
I'm not sure what version of this plugin you are using, but since Lines 340 to 343 in 89d74e4
I'm not aware of any situation where the |
The trouble with most of the settings under server.hmr is that they either have side effects, or the client.ts script can infer what to do correctly, so the settings are irrelevant (this from sapphi-red, who reviewed some of my code on a vite integration). server.hmr.host is Mostly Harmless, but Vite itself doesn't really need it. Were I developing your integration, I'd hesitate to use it to change Laravel's behavior. In particular, I've seen frustrated Laravel users play with server.hmr.https and server.hmr.port, which just makes their lives worse :-) At most they might want to play with server.hmr.clientPort.
To the file system?? Still haven't figured out where you guys mean with that.
What version is bundled with Laravel v9.23.0? I don't see a separate entry in composer.lock for the vite plugin. In v9.23.0,
Nope. Straight out of the view-source window on Firefox, without any formatting. That is exactly what @Vite() resolves to. |
@jessarcher If you have the chance, take a look at this issue I just posted. It links a repo of a Laravel user I've been working with, which might make it easier for you to reproduce what we're seeing. |
Not sure how the "view-source" stuff ended up in the link. Odd Firefox behavior on copy. Here's what I just pulled off view-source for the page in question, using Chrome instead:
|
We're using Vite's Lines 146 to 153 in 89d74e4
The This plugin only has access to what Vite gives us and what the user configures. It has no knowledge of hostnames outside of that.
How would Laravel Framework integration determine the appropriate Vite server address, including the port, from the proxy headers? I've spun up the project you mentioned and can confirm that by default it tries to retrieve the assets from If I use the following configuration, then it will successfully make the request to the intended location:
I'm not sure what the pnpm error is - I'm not using pnpm though. |
Not sure what the pnpm error is either, but probably not our problem, at least here :-) I can confirm that with the hmr settings you recommend, that the tags now resolve to: <script type="module" src="https://my-laravel-app.ddev.site:5173/@vite/client"></script><link rel="stylesheet" href="https://my-laravel-app.ddev.site:5173/resources/css/app.css" /><script type="module" src="https://my-laravel-app.ddev.site:5173/resources/js/app.js"></script>
Also, HMR comes up correctly, which means we have the thing working. As for:
It's been a few years since I've played with these, but X-Forwarded-For, X-Forwarded-Proto, X-Forwarded-Host get transmitted across the reverse proxy IIRC. You'd want the latter two. I'm not sure what PHP or Symfony's request handling (which IIRC Laravel uses, as Drupal and Symfony do) does with these. But you'd get the port as you do now, and pull host and protocol from the X-Forwarded headers. |
It feels like a bit of a stretch for Laravel to assume that the proxy it's behind is also where it will find Vite, only on a different port. I'd imagine there are as many users where this is not the case as there are where it is. I do agree that using
Users are of course also free to use other plugins like laravel-vite.dev or even just configure things themselves. Our primary focus is to make sure things work well with Laravel's first-party dev environments like Valet and Sail. |
Hi, very glad to see an official integration for vite in laravel. Much respect for the work! 👍
I tried to integrate this with DDEV, a local PHP development environment system based on Docker.
Vite runs inside the docker container. The following
vite.config.ts
is needed to get this working with DDEV router (a reverse proxy. Vite must respond to all network requests via0.0.0.0
:For the HTML output of
@vite
it would be needed to use the following custom URLwhich would result in
But: For the above posted config larave/vite-plugin currently resolves the following in
public/hot
, which doesn't work (of course):I saw that the dev server URL resolve is handled here:
I discussed this at length with DDEV users and DDEV maintainer and they gave me the advice that I should not fiddle around with
server.hmr
-settings invite.config.ts
because this is not suited for a reverse proxy usage. (Tested it, vite doesn't connect when using hmr.host etc.). (Discussion)Demo repo: https://github.com/mandrasch/ddev-laravel-breeze-vite#readme
Proposal:
When I manually overwrite
public/hot
withhttps://my-laravel-project.ddev.site:3000
everything works fine.Therefore I would like to suggest to introduce another config option to manually overwrite the dev server url output for
@vite
.For example innocenzi/laravel-vite offers an manual override with
DEV_SERVER_URL
in.env
/config/vite.php
.Thanks very much in advance, this may be really helpful for other docker use cases as well I guess!
Best regards, Matthias
The text was updated successfully, but these errors were encountered: