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.

Duplicated prefetch link (with SSR)

See original GitHub issue

I’m creating a component chunk that needs to be prefetched. I’m rendering the prefetch link on SSR for component chunk through ChunkExtractorManager

// loadableComponents.js
const LoadableLibrary = loadable.lib(() => import(
  /* webpackChunkName: "component" */
  /* webpackPrefetch: true */
  'external-library',
));
export const ExampleComponent = props => (
  <LoadableLibrary>
    {({ default: Component }) => <Component {...props} />}
  </LoadableLibrary>
);

// renderer.js
const extractor = new ChunkExtractor({ statsFile, entrypoints: ['client'] });
const content = (
  <ChunkExtractorManager extractor={extractor}>
    { /* content management */ }
  </ChunkExtractorManager>
);
const linkTags = renderToStaticMarkup(extractor.getLinkElements());
return `
  <!DOCTYPE html>
  <html>
    <head>
      ${linkTags}
    </head>
    <body>
      <div id="root">${content}</div>
    </body>
    <!-- bundle script -->
  </html>
`;

After the client receives the template, and the browser evaluates my bundle.js, webpack runtime finds those dynamic imports and add again the prefetch link for script.js, causing:

  • duplicated prefetch link tags
  • another fired request for the same resource.

Is this the correct behavior? Am I missing something?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
aseem2625commented, May 17, 2019

Facing same issue. Most probably a webpack issue only. But very difficult to get a way around. In all cases I’m having extractor.getLinkTags() inserted in html

With prefetch magic comment

const Home = loadable (() => import(/* webpackChunkName: "home" */ /* webpackPrefetch: true */ 'views/Home'));

const People = loadable (() => import(/* webpackChunkName: "people" */ 'views/People'));

const UI = loadable (() => import(/* webpackChunkName: "ui" */ 'views/UI/UI'));

const NotFound = loadable (() => import(/* webpackChunkName: "404" */ /* webpackPrefetch: true */ 'views/NotFound'));
  1. RED: loadable-components preloading the chunks.
  2. GREEN: loadable-components also takes prefetch magic comment and inserts them as prefetch
  3. BLUE: When main.js is loaded on client side, it again inserts the prefetch links.

1

Having preload magic comment instead of prefetch image

Without any magic comment image

Is there any way to figure this out, or otherwise manually can add prefetch links for desired routes based on their corresponding generated chunks. But that’s one ugly approach I wanted to keep for last ?

0reactions
gerhardslettencommented, Jun 19, 2020

Its also possible to filter out which files that should be listed for prefetch like this:

const srcFiles = extractor
    .getScriptElements()
    .map((item) => item.props.src)
    .filter(Boolean)
  // Filter out prefetch files allready loaded in scriptfiles
  const prefetchFilter = (item) =>
    !srcFiles.some((file) => file === item.props.href)
return (
<html lang={config.locale}>
      <head>
        {extractor.getLinkElements().filter(prefetchFilter)}
      </head>
</html>
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Duplicated prefetch link (with SSR) · Issue #247 - GitHub
I'm rendering the prefetch link on SSR for component chunk through ChunkExtractorManager // loadableComponents.js const LoadableLibrary ...
Read more >
Next.js sends duplicate (redundant) chunks - Stack Overflow
I experience an issue in my web app, where 80% of the .js chunks are loaded twice, redundantly without a clear reason.
Read more >
Improving performance in Apollo Client - Apollo GraphQL Docs
Prefetching involves executing queries for data before that data needs to be rendered. It helps your application's UI feel more responsive to the...
Read more >
PreFetch Feature - Quasar Framework
The PreFetch is a feature (only available when using Quasar CLI) which allows the components picked up by Vue Router (defined in /src/router/routes.js...
Read more >
Data Pre-Fetching and State - Vue SSR Guide
On the server, we can pre-fetch and fill data into the store while rendering. In addition, we will serialize and inline the state...
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