vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68.5k stars 6.18k forks source link

(Backend integration) `server.origin` pointing to Vite's dev server #12597

Open amitdahan opened 1 year ago

amitdahan commented 1 year ago

Describe the bug

Hi guys!

I'm working on integrating Vite for developing assets that would be loaded (both locally and when deployed) on a custom, separate backend.

After reading https://vitejs.dev/guide/backend-integration.html our setup is almost complete. We're left with one issue during local development.

We'll load what's necessary in our custom HTML using absolute URLs to Vite's local dev server, for example:

This works great, until some entrypoint tries to load some non-js asset. When working directly with Vite's dev server - everything is relative to the origin, so it just works.

In our case - we found that configuring server.origin with Vite's dev server URL works great.

While this works, we now have to know Vite's dev server URL in config-time, which means we have to also configure server.port, thus giving-up on the built-in available-port getter.

While this isn't too bad, I hoped to find a way to set server.origin to Vite's dev server, while also maintaining its defaults for server port, along with the port-taken behavior.

Proposed solution

While I understand why assets are loaded relative to origin by default, maybe to help with the above, a "server.origin": "absolute" configuration can be accepted? (or server.absoluteOrigin: true? server.relativeOrigin: false? obviously open for more API ideas)

If this sounds good I can contribute this.

Reproduction

https://stackblitz.com/edit/vitejs-vite-m6xtbc

Steps to reproduce

  1. yarn
  2. yarn dev and yarn custom-backend in split terminals
  3. Access http://localhost:8000

Both images aren't rendered.

  1. Uncomment origin line in vite.config.js

Images are rendered correctly.

System Info

  System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    vite: ^4.2.0 => 4.2.1

Validations

stackblitz[bot] commented 1 year ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

pySilver commented 1 year ago

I was also surprised that server.origin is not dynamically constructed (or at least there is no way to do it)

s2b commented 1 year ago

I would also appreciate if origin could be specified in a more dynamic way. I don't know the technical details inside vite, but maybe one of these approaches would be possible?

All variants would make the integration of vite in external backends much easier.

s2b commented 1 year ago

I don't know if this is helpful to anyone else, but I created a vite plugin that more or less covers my use case:

https://www.npmjs.com/package/vite-plugin-auto-origin

The plugin tries to extract the protocol/host/port from the last request to the vite dev server and uses that value as server.origin. I think that there might be timing issues if you use multiple domains in parallel, but for normal usage this seems to work just fine. Let me know if this helps you in any way or if there is a missing feature you'd like to add.

tietoevry-johan commented 1 year ago

@s2b Nice work. Besides the downside of a hardcoded url to the dev server, are there any other reasons to use your plugin over just setting server.origin?

s2b commented 1 year ago

@tietoevry-johan The main reason for this plugin is that the server uri might be different in various environments:

So yes, you could specify it manually and it would do the same thing, but you would have to adjust it sometimes.

sapphi-red commented 2 months ago

we now have to know Vite's dev server URL in config-time, which means we have to also configure server.port, thus giving-up on the built-in available-port getter.

Isn't this true even if Vite generated server.origin automatically? Won't you need to set the port value in the <script type="module" src="" /> and turn on strictPort: true?

(I agree that it would be a bit easier if Vite generated server.origin automatically.)