Skip to content
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

[0.8.x] Respect vite server.origin in viteDevServerUrl #255

Merged
merged 4 commits into from
Dec 14, 2023

Conversation

nurdism
Copy link
Contributor

@nurdism nurdism commented Oct 25, 2023

When running vite behind a reverse proxy, often you can get into situation where your publicly facing URL differs from the internally served URL, I am running into such an issue.

My vite server is being served on http://0.0.0.0:8080, I am reverse proxying http://0.0.0.0:8080 (via nginx) over to a domain example.com, so the publicly facing URL for my vite server is https://example.com. My browser has 0 access to http://0.0.0.0:8080, it is on a remote machine inside a container. I only have access to it via the reverse proxy.

This is usually solved by setting the server.origin: 'https://example.com' so all assets from vite are pointed to the right domain and location. But this vite-plugin does not respect the user configured server.origin and renders the plugin unusable in an environment that is using a reverse proxy in such a manor.

@nurdism nurdism changed the title Respect vite server.origin in viteDevServerUr Respect vite server.origin in viteDevServerUrl Oct 25, 2023
@nurdism
Copy link
Contributor Author

nurdism commented Oct 25, 2023

Looking at the issues and other PRs, I can see this could solve a few issues. There are countless edge cases where the hot file and origin could not be configured correctly automatically. This approach just respects what is configured by the user in server.origin in the original vite config.

Another approach it to just let us configure the viteDevServerUrl via the PluginConfig

@RobertoNegro
Copy link

I would love to have this PR merged. I have a project running locally in Laravel Sail behind a reverse proxy, so while Vite is running inside of docker, the browser has to load the assets from the domain managed by the reverse proxy.

I'm using this fork and it's working perfectly. It also makes sense to use server.origin since it's its purpose: https://vitejs.dev/config/server-options.html#server-origin

@nurdism
Copy link
Contributor Author

nurdism commented Nov 25, 2023

for a workaround/temp fix you can use something like patch-package

laravel-vite-plugin+0.8.1.patch

diff --git a/node_modules/laravel-vite-plugin/dist/index.cjs b/node_modules/laravel-vite-plugin/dist/index.cjs
index 00b1be6..6a444da 100644
--- a/node_modules/laravel-vite-plugin/dist/index.cjs
+++ b/node_modules/laravel-vite-plugin/dist/index.cjs
@@ -131,7 +131,7 @@ function resolveLaravelPlugin(pluginConfig) {
         const address = server.httpServer?.address();
         const isAddressInfo = (x) => typeof x === "object";
         if (isAddressInfo(address)) {
-          viteDevServerUrl = resolveDevServerUrl(address, server.config, userConfig);
+          viteDevServerUrl = userConfig.server?.origin ? userConfig.server.origin : resolveDevServerUrl(address, server.config, userConfig);
           import_fs.default.writeFileSync(pluginConfig.hotFile, viteDevServerUrl);
           setTimeout(() => {
             server.config.logger.info(`
diff --git a/node_modules/laravel-vite-plugin/dist/index.mjs b/node_modules/laravel-vite-plugin/dist/index.mjs
index eee8f31..c4178a0 100644
--- a/node_modules/laravel-vite-plugin/dist/index.mjs
+++ b/node_modules/laravel-vite-plugin/dist/index.mjs
@@ -100,7 +100,7 @@ function resolveLaravelPlugin(pluginConfig) {
         const address = server.httpServer?.address();
         const isAddressInfo = (x) => typeof x === "object";
         if (isAddressInfo(address)) {
-          viteDevServerUrl = resolveDevServerUrl(address, server.config, userConfig);
+          viteDevServerUrl = userConfig.server?.origin ? userConfig.server.origin : resolveDevServerUrl(address, server.config, userConfig);
           fs.writeFileSync(pluginConfig.hotFile, viteDevServerUrl);
           setTimeout(() => {
             server.config.logger.info(`

@timacdonald timacdonald marked this pull request as draft December 10, 2023 23:25
@timacdonald
Copy link
Member

@nurdism I'm wondering if this can be handled using the server.proxy configuration option instead of making changes here in the plugin?

https://vitejs.dev/config/server-options.html#server-proxy

@nurdism
Copy link
Contributor Author

nurdism commented Dec 13, 2023

@timacdonald The issue is the hot file not echoing the correct url, it breaks the HMR and assets regardless of what I set for the proxy inside the dev server. The hot file is loading the base url for HMR and assets on the vite server. No matter how I proxy it on the dev server, the url will continue to not be accessible on my browser. I don't have access to the vite server via any localhost as its not on my local machine its all remote, in a container. I only have access to the configured domain via a reverse proxy to the remote dev server.

@timacdonald
Copy link
Member

Gotcha.

I think this fix makes sense.

@timacdonald timacdonald marked this pull request as ready for review December 13, 2023 07:28
@timacdonald timacdonald changed the title Respect vite server.origin in viteDevServerUrl [0.8.x] Respect vite server.origin in viteDevServerUrl Dec 13, 2023
@timacdonald timacdonald self-assigned this Dec 13, 2023
@taylorotwell taylorotwell merged commit 0344788 into laravel:main Dec 14, 2023
3 checks passed
@jessarcher jessarcher mentioned this pull request Dec 15, 2023
tisnamuliarta added a commit to tisnamuliarta/laravel-shadcn that referenced this pull request Mar 11, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [laravel-vite-plugin](https://togithub.com/laravel/vite-plugin) |
[`^0.8.0` ->
`^1.0.0`](https://renovatebot.com/diffs/npm/laravel-vite-plugin/0.7.8/1.0.2)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/laravel-vite-plugin/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/laravel-vite-plugin/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/laravel-vite-plugin/0.7.8/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/laravel-vite-plugin/0.7.8/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>laravel/vite-plugin (laravel-vite-plugin)</summary>

###
[`v1.0.2`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v102---2024-02-28)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v1.0.1...v1.0.2)

- \[1.x] Fix HMR issue when `resources/lang` directory doesn't exist and
a symlink is present in the root directory by
[@&#8203;jessarcher](https://togithub.com/jessarcher) in
[laravel/vite-plugin#285

###
[`v1.0.1`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v101---2023-12-27)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v1.0.0...v1.0.1)

- \[1.x] Simpler conditional by
[@&#8203;Jubeki](https://togithub.com/Jubeki) in
[laravel/vite-plugin#273
- \[1.x] Account for imported CSS files while cleaning by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[laravel/vite-plugin#275
- \[1.x] Fix exit error messages by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[laravel/vite-plugin#276

###
[`v1.0.0`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v100---2023-12-19)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v0.8.1...v1.0.0)

- \[1.0] Drop CJS build and export types first by
[@&#8203;benmccann](https://togithub.com/benmccann) in
[laravel/vite-plugin#235
- \[1.x] Introduce `clean-orphaned-assets` binary by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[laravel/vite-plugin#251
- \[0.8.x] Respect vite server.origin in viteDevServerUrl by
[@&#8203;nurdism](https://togithub.com/nurdism) in
[laravel/vite-plugin#255
- \[1.x] Vite 5 by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[laravel/vite-plugin#269
- \[0.8.x] Fallback pages by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[laravel/vite-plugin#271
- \[1.x] Auto detect Valet / Herd TLS certificates by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[laravel/vite-plugin#180

###
[`v0.8.1`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v081---2023-09-26)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v0.8.0...v0.8.1)

- \[0.8] Fix issue with `0.0.0.0` network resolution by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[laravel/vite-plugin#241
- Upgrade vitest by
[@&#8203;sapphi-red](https://togithub.com/sapphi-red) in
[laravel/vite-plugin#246

###
[`v0.8.0`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v080---2023-08-08)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v0.7.8...v0.8.0)

- fix: compile error following upgrade.md's vite to mix guide by
[@&#8203;AshboDev](https://togithub.com/AshboDev) in
[laravel/vite-plugin#231
- Support Laravel Herd by
[@&#8203;claudiodekker](https://togithub.com/claudiodekker) in
[laravel/vite-plugin#233

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/tisnamuliarta/laravel-shadcn).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4yMzAuMCIsInVwZGF0ZWRJblZlciI6IjM3LjIzMC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->
tisnamuliarta added a commit to tisnamuliarta/laravel-shadcn that referenced this pull request Mar 22, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [laravel-vite-plugin](https://togithub.com/laravel/vite-plugin) |
[`^0.7.5` ->
`^1.0.0`](https://renovatebot.com/diffs/npm/laravel-vite-plugin/0.7.8/1.0.2)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/laravel-vite-plugin/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/laravel-vite-plugin/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/laravel-vite-plugin/0.7.8/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/laravel-vite-plugin/0.7.8/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>laravel/vite-plugin (laravel-vite-plugin)</summary>

###
[`v1.0.2`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v102---2024-02-28)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v1.0.1...v1.0.2)

- \[1.x] Fix HMR issue when `resources/lang` directory doesn't exist and
a symlink is present in the root directory by
[@&#8203;jessarcher](https://togithub.com/jessarcher) in
[laravel/vite-plugin#285

###
[`v1.0.1`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v101---2023-12-27)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v1.0.0...v1.0.1)

- \[1.x] Simpler conditional by
[@&#8203;Jubeki](https://togithub.com/Jubeki) in
[laravel/vite-plugin#273
- \[1.x] Account for imported CSS files while cleaning by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[laravel/vite-plugin#275
- \[1.x] Fix exit error messages by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[laravel/vite-plugin#276

###
[`v1.0.0`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v100---2023-12-19)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v0.8.1...v1.0.0)

- \[1.0] Drop CJS build and export types first by
[@&#8203;benmccann](https://togithub.com/benmccann) in
[laravel/vite-plugin#235
- \[1.x] Introduce `clean-orphaned-assets` binary by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[laravel/vite-plugin#251
- \[0.8.x] Respect vite server.origin in viteDevServerUrl by
[@&#8203;nurdism](https://togithub.com/nurdism) in
[laravel/vite-plugin#255
- \[1.x] Vite 5 by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[laravel/vite-plugin#269
- \[0.8.x] Fallback pages by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[laravel/vite-plugin#271
- \[1.x] Auto detect Valet / Herd TLS certificates by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[laravel/vite-plugin#180

###
[`v0.8.1`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v081---2023-09-26)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v0.8.0...v0.8.1)

- \[0.8] Fix issue with `0.0.0.0` network resolution by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[laravel/vite-plugin#241
- Upgrade vitest by
[@&#8203;sapphi-red](https://togithub.com/sapphi-red) in
[laravel/vite-plugin#246

###
[`v0.8.0`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v080---2023-08-08)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v0.7.8...v0.8.0)

- fix: compile error following upgrade.md's vite to mix guide by
[@&#8203;AshboDev](https://togithub.com/AshboDev) in
[laravel/vite-plugin#231
- Support Laravel Herd by
[@&#8203;claudiodekker](https://togithub.com/claudiodekker) in
[laravel/vite-plugin#233

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/tisnamuliarta/laravel-shadcn).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4yMzguMSIsInVwZGF0ZWRJblZlciI6IjM3LjIzOC4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants