Skip to content

Commit 2ea2435

Browse files
Aghassisnitin315
andauthored
docs: add note about how publicPath interacts with asset modules (#7091)
Co-authored-by: Nitin Kumar <[email protected]>
1 parent efe88de commit 2ea2435

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/content/guides/asset-modules.mdx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,32 @@ module.exports = {
7878
}
7979
```
8080

81+
## Public Path
82+
83+
By default, under the hood, the `asset` type does `__webpack_public_path__ + import.meta`. This means that setting the `output.publicPath` in your config will allow you to override the URL from which the `asset` loads.
84+
85+
### On The Fly Override
86+
87+
If you set the `__webpack_public_path__` in code, the way you need to achieve it so as not to break the `asset` loading logic is to make sure you run it as the first code in your app and not use a function to do so. An example of this would be having a file called `publicPath.js` with contents
88+
89+
```javascript
90+
__webpack_public_path__ = 'https://cdn.url.com';
91+
```
92+
93+
And then in your `webpack.config.js` updating your `entry` field to look like
94+
95+
```javascript
96+
module.exports = {
97+
entry: ['./publicPath.js', './App.js'],
98+
};
99+
```
100+
101+
Alternatively, you can do the following in your `App.js` without modifying your webpack config. The only downside is you have to enforce ordering here and that can collide with some linting tools.
102+
103+
```javascript
104+
import './publicPath.js';
105+
```
106+
81107
## Resource assets
82108

83109
**webpack.config.js**

0 commit comments

Comments
 (0)