-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
dynamic publicPath support #3522
Comments
This is also very important for me, and I hope to use something like |
I think we have to consider both of in compile-time, I want to inject a variable to <!--dist/index.html-->
<div id="app"></div>
<script src="{{dynamicPublicPath}}/app.js"></script> it's very useful for multi-tenant system in runtime, just like so, I think the base api maybe like this: export default defineConfig({
// base: '/',
base: {
compileTime: '{{dynamicPublicPath}}',
runtime: 'window.dynamicPublicPath' // string way: `'https://img.xxx.com'`
}
}) |
I also need such program capabilities at runtime,same as |
Agree, something like webpack_public_path is really necessary to have the ability to build once and deploy on different domains where relative paths isn't an option |
This comment was marked as abuse.
This comment was marked as abuse.
It works on all relative paths |
That wouldn't work for dynamic imports right? |
I can use rollup-plugin-replace too, but the point is I can't replace the preload's link, because all plugins is run before see https://github.com/vitejs/vite/blob/main/packages/vite/src/node/build.ts#L284 of course it can be realized with node's |
I created a plugin to do this, support preload's link. // vite.config.ts
import { useDynamicPublicPath } from 'vite-plugin-dynamic-publicpath'
export default defineConfig({
plugins: [
useDynamicPublicPath(/** option */),
]
}) // main.ts
// Your dynamic cdn
const dynamicCdn = oneOf(['cdn.xxx.com', 'cdn1.xxx.com']);
window.__dynamicImportHandler__ = function(importer) {
return dynamicCdn + importer;
}
window.__dynamicImportPreload__ = function(preloads) {
return preloads.map(preload => dynamicCdn + preload);
} |
Any suggestion for this feature? |
I'd also like this feature to be builtin. I work with Salesforce Commerce Cloud, and we don't have control over our static URL path, it looks something like this The |
+1 for this feature |
this would be really useful for us as well! |
Is this the same as |
+1 for this feature. It will be really helpful to generate agnostic builds. |
This comment was marked as spam.
This comment was marked as spam.
There are a couple of issues with this.
|
I wonder if the best would be to use the
@danielroe How does this work for the example that you showed:
How do you update the base at runtime? Do you crawl the HTML to update all the assets If this is unknown at build time, but fixed at the server, I imagine the HTML also needs to be transformed in the same way during SSR. |
@patak-dev We're using SSR and our vite plugin currently generates code approximating this: const logo = buildAssetsURL("logo.148d9522.svg");
const _imports_1 = publicAssetsURL("public.svg");
// ...
const _sfc_main = {
setup(__props) {
return (_ctx, _push, _parent, _attrs) => {
_push(`
<img${serverRenderer.exports.ssrRenderAttr("src", _imports_1)}>
<img${serverRenderer.exports.ssrRenderAttr("src", unref(logo))}>
`);
};
}
};
|
What kind of API do you imagine Vite would expose for this feature? Just a note, but I think that |
The client-side state does need to match the HTML from the server, so that it continues to fetch assets from the correct locations as users navigate through the site. Once the JS is loaded, I am not imagining it will change or need to be reactive. Here's a stab at an API:
|
@danielroe once we merge #7644, we are going to end up with: // we generate const logo = new URL('../logo.svg', import.meta.url)
const someHTML = '<img src="' + new URL("../assets/test.234k.svg", import.meta.url) + '">' (changed the interpolation to showcase more closely the output that Vite would generate, but it is the same as in your prev message) The URL needs to be absolute because the Getting back to your proposed API for dynamic publicPath support, we now have the infra to add the feature. It would mean changing the way we deal with generated public paths in JS here: https://github.com/vitejs/vite/pull/7644/files#diff-91776e7c6039d23a070162f02a69cd46046a2095bd5ecb384ae9e27f2ea5288fR123. So we could support something like you proposed or an equivalent configuration:
Two questions:
And then update these vars in an inlined script module in the HTML? Or are you not planning to use public assets from CSS at all? |
There is initial work to support this feature in: Reviews and feedback from the folks involved in this issue are appreciated, especially around the API design and coverage for the use cases exposed here. |
I'm just doing some playing around with this feature, and it still seems like these dynamic paths are static once set at build time. I was kind of expecting something that could be configured at runtime but that doesn't seem to be the case. Am I wrong? |
Clear and concise description of the problem
I want to inject a dynamic publicPath(base in vite config),lick this:
Suggested solution
provide some build hook, just like webpack's hook, in this hook I can write a plugin to replace some string, reference:
https://github.com/m8ms/dynamic-public-path-webpack-plugin
Alternative
or support it in core
Additional context
none
The text was updated successfully, but these errors were encountered: