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.

__webpack_public_path__ does not work?

See original GitHub issue

Is this a bug report?

yes

I want to set html files assetsPath in dist directory produced by htmlwebpackplugin at runtime, so I tried to figure out __webpack_public_path__ = "http://cdn.mydomain.com instead of setting publicUrl , but it doesnot work at all。 how can i set publicPath at runtime ? does not webpack_public_path support in CRA?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
xsteadybcgocommented, Dec 13, 2018

@ianschmitz thank you for you patience,it was not a CRA issue but my misunderstanding , now i can fix my project by adding a webpack plugin。 thanks a lot , thank you !!! 😄

0reactions
ianschmitzcommented, Dec 13, 2018

Gotcha. Thanks for the repository.

I think there may be some confusion between publicPath in the webpack config, versus __webpack_public_path__ which is used at runtime. I’ll do my best to clear things up based on my knowledge of the two.

publicPath is used during the webpack build to help construct URLs to your assets, whether it be for loading your lazy loaded chunks via dynamic import(), or via HtmlWebpackPlugin to configure the urls to your entrypoint .js and .css for example. If you were to set PUBLIC_URL in CRA, it will internally configure the webpack publicPath, such that if you gave it an absolute URL to a CDN for example, HtmlWebpackPlugin would use this absolute URL to prefix our .js and .css files in index.html.

__webpack_public_path__ allows you to configure the webpack asset loader to tweak the publicPath at runtime. So for example if you left publicPath alone, CRA will default to the root of the host (/). So requests to load a lazy chunk would be loaded from /static/js/foo.abc123.js for example. However you can set __webpack_public_path__ in the entry point of your app, so that from that time forward in your application lifecycle, any asset requests would use __webpack_public_path__ as the prefix. So if you set __webpack_public_path__ = https://bar.com, then the chunk would be requested from https://bar.com/static/js/foo.abc123.js. You’re essentially telling the webpack asset loader:

Hey i know i told you during the build that you should prefix requests with /, but i’m telling you that from this point forward instead prefix the requests with https://bar.com

Does that help clear things up?

Read more comments on GitHub >

github_iconTop Results From Across the Web

publicPath not doing anything · Issue #403 - GitHub
Using publicPath doesn't seem to do anything. If it's defined as a function it does get called, but the return value is never...
Read more >
Webpack publicPath don't work - Stack Overflow
I have known the answer. Webpack publicPath just work for output file. So just the url in the bundle.js will be replaced. 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 >
Webpack — Understanding the 'publicPath' mystery - Medium
The 2 main issues which are caused by incorrect publicPath config configuration are : webpack-dev-server : live-reload feature doesn't work ...
Read more >
Public Path - webpack
The publicPath configuration option can be quite useful in a variety of ... To approach this problem you can easily use a good...
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