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 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:closed
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

4reactions
stale[bot]commented, Jun 9, 2021

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.

3reactions
thecrypticacecommented, Jan 8, 2021

This might be caused by issues with public path stripping from chunk names. I’ll take a look when I can (most likely tomorrow).

Read more comments on GitHub >

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

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