Dynamic imports not working when using mix.setPublicPath()
See original GitHub issue- Laravel Mix Version: 6.0.9
- Node Version: v14.15.3
- Yarn Version: Yarn 1.22.5
- OS: node:14 docker image
Description:
Dynamic imports fail when using the mix.setPublicPath()
option.
Steps To Reproduce:
Given this set up
routes/web.php
:
Route::get('/playground', function () {
return view('foo');
});
resources/assets/js/index.js
:
console.log('index.js: Load');
import('./dynamic').then(module => {
console.log('index.js: Run');
const dynamic = module.default;
dynamic();
});
resources/assets/js/dynamic.js
:
console.log('dynamic.js: Load');
function dynamic() {
console.log('dyanmic.js: Run')
}
export default dynamic;
Doesn’t work
foo.blade.php
:
<html>
{{--Notice second argument here passing path to mix-manifest--}}
<script src="{{ mix('index.js', 'foo') }}"></script>
<body></body>
</html>
webpack.mix.js
:
const mix = require('laravel-mix');
mix
.setPublicPath('public/foo') // Notice we use mix.setPublicPath here
.js('resources/assets/js/index.js', 'public/foo');
In the browser when I go to /playground
it fails with this error:
index.js: Load index.js:233:9
Loading failed for the <script> with source “http://resources_assets_js_dynamic_js.js/”. playground:1:1
Uncaught (in promise) ChunkLoadError: Loading chunk resources_assets_js_dynamic_js failed.
(error: http://resources_assets_js_dynamic_js.js/)
Does work
foo.blade.php
:
<html>
{{--Notice NO second argument here, so uses default path to mix-manifest--}}
<head><script src="{{ mix('foo/index.js') }}"></script></head>
<body></body>
</html>
webpack.mix.js
:
const mix = require('laravel-mix');
// Notice not using mix.setPublicPath()
mix.js('resources/assets/js/index.js', 'public/foo');
In the browser when I go to /playground
everything worked:
index.js: Load index.js:233:9
dynamic.js: Load resources_assets_js_dynamic_js.js:11:9
index.js: Run index.js:235:11
dyanmic.js: Run resources_assets_js_dynamic_js.js:14:11
Issue Analytics
- State:
- Created 3 years ago
- Comments:8
Top Results From Across the Web
Dynamic imports not working when using mix.setPublicPath()
Dynamic imports fail when using the mix.setPublicPath() option. Steps To Reproduce: Given this set up. routes/web.php : Route ...
Read more >How to setup laravel mix for dynamic import for project in ...
Ok, found solution by myself. Looks like setting webpack publicPath to empty string combined with <base href="{{ url('/') }}/"> worked just ...
Read more >Dynamic imports not working when using mix.setPublicPath() -
Description: Dynamic imports fail when using the mix.setPublicPath() option. Steps To Reproduce: Given this set up. routes/web.php :
Read more >Using Dynamic Imports with Laravel Mix
Dynamic imports is a method of code-splitting that allow us to easily split our JavaScript components, packages, and other modules into separate ...
Read more >The Recommended Setup - Single SPA
It is recommended to use dynamic import ( import() ) instead of multiple entry points for code splits in a single-spa application. For...
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 FreeTop 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
Top GitHub Comments
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This might be caused by issues with public path stripping from chunk names. I’ll take a look when I can (most likely tomorrow).