question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dynamic publicPath support

See original GitHub issue

Clear 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:closed
  • Created 2 years ago
  • Reactions:19
  • Comments:25 (9 by maintainers)

github_iconTop GitHub Comments

14reactions
jy0529commented, Jul 6, 2021

I created a plugin to do this, support preload’s link. https://github.com/jy0529/vite-plugin-dynamic-publicpath

// 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);
}
5reactions
danielroecommented, Apr 6, 2022

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.:

    // instead of
    const logo = './logo.svg'
    const someHTML = '<img src="./test.234k.svg">'
    // we generate
    const logo = publicAssetsURL('logo.svg')
    const someHTML = `<img src="${buildAssetsURL('test.234k.svg')}">`
    

    We might allow users to provide their own publicAssetsURL as follows:

    const publicAssetsURL = window.__vite_public_assets_url || (id) => '/' + id
    const buildAssetsURL = window.__vite_build_assets_url || (id) => '/assets/' + id
    

    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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found