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.

On the fly publicPath when lazy-loading using @loadable/component?

See original GitHub issue

We have configured publicPath in ChunkExtractor, so it can be set dynamically/on the fly when deployed.

This works for the initial bundles, publicPath set to /pro/public E.g. the paths look like this

https://local.moz.com/pro/public/bundle~._client_components_t.js

However it doesn’t work for us when using lazy-loading via @loadable/component:

const SelectReport = loadable(() => import('components/page-optimization/select-report'));

It will not use publicPath defined for ChunkExtractor:

https://local.moz.com/pro/research/components-page-optimization-select-report~._client_components_page-optimization_se.js

If we define output.publicPath for webpack, this works in local dev. However we do rely on the ‘on the fly’ configuration to configure the paths for use with a CDN when deployed to prod.

How do I make sure @loadable/component uses the publicPath defined for the chunk extractor?

This is how we use ChunkExtractor:

const getPublicPath = exports.getPublicPath = () => {
  if (yn(process.env.WEBPACK_DEV_MODE)) {
    return constants.SERVER.WEBPACK_PUBLIC_PATH;
  }

  const { ASSETS_PATH, CDN } = process.env;
  return CDN ? `${CDN}${ASSETS_PATH}` : ASSETS_PATH;
};

exports.getBundleElements = (bundle) => {
  const extractor = new ChunkExtractor({
    entrypoints: [bundle],
    publicPath: getPublicPath(),
    statsFile: STATS_FILE,
  });

  return {
    scripts: extractor.getScriptElements(),
    styles: extractor.getStyleElements(),
  };
};

I’ve been looking at the source code for @loadable/component, but I can’t figure out how it resolves paths.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
theKasheycommented, Nov 25, 2020

Yeah 😅, a couple of small details, small enough to be hardly noticeable and hardly fixable. Probably we should not document “Server” and “Client” parts separately, but explain how one configuration is matching another.

0reactions
stale[bot]commented, Jan 25, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack (v4) Code Splitting using SplitChunksPlugin - Medium
In this article, we will explore SplitChunksPlugin , how to separate code into different chunks, how to lazy load chunks on demand and...
Read more >
A Higher Order Component For Loading ... - Morioh
"Oh hey - using loadable component I knocked 13K off my initial load. ... When you go to render all these dynamically loaded...
Read more >
Dynamic path import for lazy loading of components using ...
What I want to do is import dynamic paths for the loader object or the Loadable function of react-loadble module. Code: const LoadableComponent...
Read more >
Fundamentals of SSR in React - Emergent Systems
Opting out of SSR via Lazy loading; Opting out of SSR with a Higher-Order Component. Layout Effect Warnings on the Server. Closing Thoughts ......
Read more >
Lazy loading React components - LogRocket Blog
In this tutorial, we'll show you how lazy loading works in ... For React apps, code-splitting using dynamic import() happens on the fly...
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