Closed EliaECoyote closed 5 years ago
Hello @asulta you got a point. I don't know if it is really a problem, but if it is, we should try to talk to webpack team. Currently it is not possible to tell webpack "do not prefetch these".
A somewhat related issue: when loadable detects chunks during render they get preloaded on the head, but if the preloaded dependency was marked as "webpackPrefetch: true" it gets added as a prefetch as well (in addition to the preload), which is not ideal.
There should be a way to detect the dependency is already being included in a link preload
, and don't prefetch it in that case.
I think it is more a webpack issue, if the chunk is already prefetched in the page, they should not prefetch it. I think it has no implication by having it twice in the page (if already prefetched it should be ignored by browser).
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'));
loadable-components
preloading the chunks.loadable-components
also takes prefetch magic comment and inserts them as prefetchHaving preload magic comment instead of prefetch
Without any magic comment
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 ?
We have to open an issue in webpack, actually, it is a webpack problem. It duplicated prefetch links by not checking if they are already inserted.
@gregberge, I don't completely understand the issue, but I am still experiencing this. I also tried to switch out loadable with react.lazy (I'm not working with SSR) and there are no duplicated requests. This alone would lead me to believe that something can be done in loadable-components.
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>
)
I'm creating a
component
chunk that needs to be prefetched. I'm rendering theprefetch
link on SSR forcomponent
chunk throughChunkExtractorManager
After the client receives the template, and the browser evaluates my
bundle.js
, webpack runtime finds those dynamic imports and add again theprefetch
link forscript.js
, causing:Is this the correct behavior? Am I missing something?