Open amitdahan opened 1 year ago
Start a new pull request in StackBlitz Codeflow.
I was also surprised that server.origin
is not dynamically constructed (or at least there is no way to do it)
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?
<script type="module" src="http://localhost:5173/@vite/client?origin=http://localhost:5173"></script>
(this would be my favorite)origin: true
(or similar) is set in vite config filevite dev --origin http://localhost:5173
All variants would make the integration of vite
in external backends much easier.
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.
@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?
@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.
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.)
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:
http://localhost:5173/@vite/client
http://localhost:5173/main.js
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? (orserver.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
yarn
yarn dev
andyarn custom-backend
in split terminalshttp://localhost:8000
Both images aren't rendered.
origin
line invite.config.js
Images are rendered correctly.
System Info
Validations