dynamic publicPath support
See original GitHub issueClear and concise description of the problem
I want to inject a dynamic publicPath(base in vite config),lick this:
export default defineConfig({
base: `window.baseFromServer`
})
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
Issue Analytics
- State:
- Created 2 years ago
- Reactions:19
- Comments:25 (9 by maintainers)
Top Results From Across the Web
webpack-dynamic-public-path - npm
Start using webpack-dynamic-public-path in your project by running `npm i webpack-dynamic-public-path`. There are 8 other projects in the ...
Read more >Public Path - webpack
The publicPath configuration option can be quite useful in a variety of scenarios. It allows you to specify the base path for all...
Read more >How to force webpack to generate dynamic publicPath for ...
I'm trying to figure out how to implement a MicroFrontend Host application that would be able to bootstrap one or more React applications...
Read more >How code-splitting and dynamic Import works - WPACK.IO
Check out to support my efforts (made by me). Glad you asked. The documentation at webpack output.publicPath says this is one free variable...
Read more >dynamic-public-path-webpack-plugin - npm package - Snyk
Learn more about dynamic-public-path-webpack-plugin: package health score, popularity, security, maintenance, versions and more.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I created a plugin to do this, support preload’s link. https://github.com/jy0529/vite-plugin-dynamic-publicpath
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:
emitted assets should use relative URLs where possible, as we can know this structure absolutely, as you say above. this is a relatively easy solution and can be implemented as a first step.
ideally we should distinguish the built assets directory from the public directory as one containes hashed assets and the other doesn’t - and performance-conscious users might like to have different caching rules for public assets vs built assets
therefore it should be possible to configure (once) a publicAssetsURL + buildAssetsURL (ideally functions that would take a string and return a url?). All asset strings that would be produced in the build process can simply use these values in a template literal - e.g.:
We might allow users to provide their own
publicAssetsURL
as follows:Updating the URLs would be as simple as injecting a script tag into the HTML that sets those global functions and allows users to have full control depending on their own setups.