-
Notifications
You must be signed in to change notification settings - Fork 7
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
Assets url prefix support #17
base: main
Are you sure you want to change the base?
Conversation
Thank you very much for the PR. I think this is really useful. I'm still looking to understand how this all plays together with Vite's Would also be lovely to have a working example in |
Hi @istickz, thanks for the great PR. @olivere my initial reaction was the same as yours, how does this interact with the I just had a look at how Laravel supports this (with it's built in Vite code that does the same as this package). In Laravel you can provide a path to the build directory and it uses that to build the tag URL, although that is more specific to how laravel serves the files. So you can do things like this: use Illuminate\Support\Facades\Vite;
Vite::createAssetPathsUsing(function ($path, $secure = null) {
// Custom URL generation logic here
return 'https://custom-domain.com/' . ltrim($path, '/');
}); I think there is a good use case for @istickz's PR, or we could implement a callback for greater flexibility. |
Yes, I want this PR to be merged as well. It is really valuable. @danclaytondev I'm very happy looking at what the libraries for other platforms/languages have done, as they exist quite a bit longer and hence have more experience with the right approach. So I would be happy with the As to possible misconfigurations: I always love when an error gives me hints on how to fix an error. Something like "Make sure the base URL in the Vite config is equal to the AssetsPrefixURL" would already make me happy. I spend so much time in my career fixing the same issue over and over again (but couldn't quite remember how to solve it). A simple hint would've really helped me. Well, maybe it's just me. So, if we can have another example application that would be 👍 I can work on it, but it might be Friday before I can finish it. |
The base option from Vite is generally not necessary if we're only changing the prefix for where we fetch assets. However, it becomes relevant when rendering HTML from a different URL, for example: If our application has a router or uses import.meta.env.BASE_URL, we also need to specify the base option as /admin when building the frontend application. If we're just setting assetPrefixURL and rendering the page at /, there's no need to modify the base option. |
I have added examples to provide better understanding of how everything works. |
Description of the Problem
When working with multiple frontend applications, such as
app
andadmin
, each with its own Vite configuration, we can create multiple Vite fragment tags, like so:This works perfectly in development mode because assets are served with distinct URLs like:
http://localhost:5173/
(forapp
)http://localhost:5174/
(foradmin
)However, in production mode, this approach fails because asset URLs default to
/assets/*
. This results in a collision when serving multiple applications.Proposed Solution
This PR introduces an
AssetsURLPrefix
option to thevite.Config
struct for production mode. This allows asset links to be generated with a custom prefix, avoiding collisions.Here’s how it works:
Updated Vite Configurations
Correctly Serving Assets in Handlers
To support this configuration, the assets should be served with the appropriate prefixes in the HTTP handlers:
Benefits
AssetsURLPrefix
option is explicitly set.