Skip to content

Allow to use different base for dev server and generated import paths #19980

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
4 tasks done
vincesp opened this issue May 1, 2025 · 6 comments
Open
4 tasks done

Allow to use different base for dev server and generated import paths #19980

vincesp opened this issue May 1, 2025 · 6 comments

Comments

@vincesp
Copy link

vincesp commented May 1, 2025

Description

As a developer using Vite in development mode, I want to configure the prefix used to generate the import paths separately from the prefix used by the dev server, so that I can run Vite in dev mode in a scenario with a reverse proxy that forwards traffic to different applications based on a URL prefix but removes that prefix when forwarding the request.
Currently, I can only set base in the Vite config which will apply both to the prefix used by the dev server and to the import paths.

Suggested solution

Add an option server.base that can override the shared base option. With this, we could do:

import { defineConfig } from "vite";

export default defineConfig({
  base: "/foo/",
  server: {
    base: "/",
  },
});

Alternative

Alternatively, the overriding option could be added as build.base:

import { defineConfig } from "vite";

export default defineConfig({
  build: {
    base: "/foo/",
  },
});

Additional context

No response

Validations

@sapphi-red
Copy link
Member

I'd recommend configuring the reverse proxy to keep the prefix if possible as that's much simplier.
But if that's not an option, you can use a plugin like this: #19219 (comment)

@sapphi-red
Copy link
Member

I'm not sure if I get what you are suggesting with build.base. But if you want to generate the base depending on whether it's a build or dev, you can use the conditional config feature in that case.

@vincesp
Copy link
Author

vincesp commented May 1, 2025

The imports in the generated code always should have the prefix, both in dev as well as in prod. However, the dev server should not mount the root of the project under the prefix, but rather under '/'. This is for a setup where multiple sub products run in different containers, and there is a reverse proxy in the front that distinguished by the prefix to which container to forward each request. However, the reverse proxy will strip the prefix.

The Foo app could run on http://localhost:4321/

The reverse proxy on http://localhost:80/

A request to http://localhost:80/foo/main.js would be forwarded to http://localhost:4321/main.js.

So I need the Vite dev server to generate import URLs in the form of /foo/main.js but still serve the application at /.

@sapphi-red
Copy link
Member

Then, I think the plugin I linked in #19980 (comment) would work.

@vincesp
Copy link
Author

vincesp commented May 2, 2025

@sapphi-red is there already an NPM package for that which we can import?

@sapphi-red
Copy link
Member

I'm not aware of any existing package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants