Skip to content

added host option to plugin config, fixed devServer value in nette.json#2

Open
jeremy-step wants to merge 8 commits into
nette:masterfrom
jeremy-step:master
Open

added host option to plugin config, fixed devServer value in nette.json#2
jeremy-step wants to merge 8 commits into
nette:masterfrom
jeremy-step:master

Conversation

@jeremy-step

@jeremy-step jeremy-step commented Jun 24, 2025

Copy link
Copy Markdown
Contributor

This PR adds a new host option to the plugin configuration. This allows to override the vite host config when needed.
Additionally, if the host value is set to true or '0.0.0.0', it is replaced with 'localhost', fixing incorrect urls in the generated nette.json file.

While http://0.0.0.0:5173 does work, it redirects to http://localhost:5173 for each asset loaded.

For example:

export default defineConfig({
	plugins: [nette({ host: 'custom-host.local' })],
	server: {
		host: true,
	},
});

will result in:

{ devServer: 'http://custom-host.local:5173' }

@jeremy-step

Copy link
Copy Markdown
Contributor Author

I probably should have included a test to make sure the plugin option overrides the vite host config. Will add it later today.

@jeremy-step

Copy link
Copy Markdown
Contributor Author

Should be ready to go.

@jeremy-step

jeremy-step commented Jun 26, 2025

Copy link
Copy Markdown
Contributor Author

When I was writing the Docker Dev section in the docs, I noticed that there is a bug with the host also being set to true in the autogenerated CORS config. Will fix it later today.

Edit: Done, also added auto configuration of the allowedHosts option, needed for Vite to work properly.

@MaN0fy

MaN0fy commented Jul 10, 2025

Copy link
Copy Markdown

Hi, thank you for your work. This works great for fixing the host issue.

However I ran into a case where I’d need the generated dev server URL to omit the port (i.e. just vite.localhost instead of vite.localhost:5173).

Would you consider adding a port option to the config (e.g. port: false, port: 80, port: 5173, etc.) to support that?

Could be helpful for setups with reverse proxies.

Thanks!

@jeremy-step

Copy link
Copy Markdown
Contributor Author

Hi, yeah, should be an easy fix. Will look into it later when I have a bit more time. In the meantime you could add a patch to the vite plugin yourself. There is a npm package (patch-package) for these situations that allows you to modify a package until whatever feature you need is released.

@jeremy-step

Copy link
Copy Markdown
Contributor Author

Should work now. The generated devServer url with the following example config would be http://vite.localhost.
While the plugin does a lot of the work for you when it comes to configuring CORS and Allowed Hosts, it doesn't account for a lot of cases that might need more complex configuration. Depending on your needs, you might need to configure that stuff yourself.

export default defineConfig({
    plugins: [
        nette({
            host: "vite.localhost",
        }),
    ],
    server: {
        host: true, // Or '0.0.0.0'
        port: 80,
        strictPort: true,
    },
    ...
});

@sazmajaroslav

Copy link
Copy Markdown

Thanks for the plugin and this modification. Please merge the modification and release a new version of the package?

@hanisko

hanisko commented Jan 22, 2026

Copy link
Copy Markdown

Could you please address this pull request? I have also encountered the same issue.

@tttpapi

tttpapi commented Mar 16, 2026

Copy link
Copy Markdown

Is this going to be merged?

@dg

dg commented Jul 5, 2026

Copy link
Copy Markdown
Member

Thanks a lot, @cima-alfa — the core idea is solid and I've merged it in (reworked onto the current master, since the branch had drifted apart from it). The host option and the true / 0.0.0.0localhost normalization landed as you proposed, and your commit authorship is preserved.

One thing I added on top while looking at proxied setups: the plugin used to unconditionally overwrite server.origin, so a user-defined origin never took effect. Now, if server.origin is set (e.g. a public URL when Vite runs behind a reverse proxy, where host, port and protocol can all differ from the local socket), it wins and is written straight into nette.json. The host option alone couldn't cover that case.

Thanks again! 🙏

dg pushed a commit that referenced this pull request Jul 5, 2026
Adds a `host` plugin option to override the Vite server host, and
normalizes `host: true` / '0.0.0.0' to 'localhost' in the generated
nette.json (http://0.0.0.0 otherwise redirects to localhost for every
asset). Port 80 is omitted from the URL, and a custom host is added to
`allowedHosts` for proxied/Docker development.

Reworked from #2 onto current master. Closes #1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants