webpack icon indicating copy to clipboard operation
webpack copied to clipboard

Automatic publicPath selection fails due to additional external script

Open yvanham opened this issue 3 years ago • 6 comments

Bug report

What is the current behavior?

If the publicPath value (under output) is not set in the Webpack config, Webpack calls the AutoPublicPathRuntimeModule (link) to find the current path by looking at the last included script file. This works if the last script file is [a] not an inline script and [b] part of the webpack output and therefore the src attribute is a reasonable reference to the intended public path.

If neither of those criteria is matched, e.g. a script added by Google Tag Manager, the AutoPublicPathRuntimeModule cannot find the current path, and throws the following error: Automatic publicPath is not supported in this browser

The red rectangle on the screenshot below highlights an example of an external script added to the end of the script list. It is added by Google Tag Manager on Linux devices only and results in the error described above.

Screenshot_webpackissue

On Windows and macOS, this Google Tag Manager script is not added and the last script is main.5dd691d8552e0c47.js. This script is then selected by the AutoPublicPathRuntimeModule to find the current path, and works without issues.

If the current behavior is a bug, please provide the steps to reproduce.

See above.

What is the expected behavior?

While it can be solved by setting the publicPath value in the Webpack config file, the AutoPublicPathRuntimeModule should not use the last script to find the current path, as this is prone to errors caused by other packages adding additional external scripts. If that is not possible, the logged error message (Automatic publicPath is not supported in this browser) could be improved, as it is supported in this browser, but obstructed by a script added by a different package.

Other relevant information: webpack version: 5.64.0 Node.js version: v16.11.1 Operating System: Linux (issue), Windows (no issue), MacOS (no issue) Additional tools: Chrome (V103), Firefox

yvanham avatar Jul 12 '22 10:07 yvanham

It is fallback for old browsers... Can you provide value of RuntimeGlobals.global, but yes, we can improve this

alexander-akait avatar Jul 12 '22 20:07 alexander-akait

hm.. strange in your example for main.js should be document.currentScript.src, but yes else case could be improved..

vankop avatar Jul 21 '22 11:07 vankop

It should use import.meta.url. Maybe you forgot to set the script type to module in webpack config, as you are using script tags with type="module"?

sokra avatar Jul 25 '22 07:07 sokra

This issue had no activity for at least three months.

It's subject to automatic issue closing if there is no activity in the next 15 days.

webpack-bot avatar Nov 01 '22 04:11 webpack-bot

bump

alexander-akait avatar Nov 01 '22 13:11 alexander-akait

Issue was closed because of inactivity.

If you think this is still a valid issue, please file a new issue with additional information.

webpack-bot avatar Feb 15 '23 22:02 webpack-bot