English | 中文
If a “dynamic module” fails to load, the browser will not load it again forever. so, I wrote this lib to attempt to solve the issue.
-
If 'import('a.js')' has failed, the lib will load it again as the load process is called. The default behavior will fail immediately.
-
If the user can’t access the internet, the library will fail immediately, but this is not the same as the browser’s default behavior, it doesn’t do the load process.
-
The lib will attempt to reload any failed CSS files.
If a.js dependencies b.js and b.js is a 'static import module', the load of a.js will fail if b.js has failed to load. Unfortunately, it can't be fixed.
Add “vite-plugin-retrying-dynamic-import” to Vite configuration.
import retryingDynamicImport from "vite-plugin-retrying-dynamic-import";
export default defineConfig({
plugins: [retryingDynamicImport()],
});
Add “retrying-dynamic-import” to the entry file(main.ts or main.js).
Put it at the top of the entry file, because the lib will register a global function to the window object.(the name is "__retrying_dynamic_loader__ ")
import retryingDynamicImport from "retrying-dynamic-import"
retryingDynamicImport(// your options);
Finished.
export type Options = {
/**
* Message to report when the user is offline
* @type {string}
* @default "No internet connection"
*/
offlineMessage?: string;
/**
* Callback to call when the user is offline.
* @default undefined
*/
offlineCallback?: () => void;
/**
* Whether to retry CSS when loads dynamic modules
* @default false
*/
disableRetryingCSS?: boolean;
/**
* When the value of 'window.navigator.onLine' is false, request the URL to detect if the network
* is offline. Sometimes, even if the value is false, the browser can still connect
* to the internet.
*/
checkOnlineUrl?: string;
};
No options.
If the code is similar below:
// main.js
import("a.js");
// a.js
import b from "b.js";
Vite will preload the b.js before dynamic importing the a.js. If the b.js has failed, the a.js will fail too.
We can't control how to load b.js because it’s a static import. So, we need to turn off preloading js in Vite.
// vite.config.ts
export default defineConfig({
build: {
modulePreload: {
resolveDependencies: (filename, deps, { hostId, hostType }) => {
return deps.filter((file: string) => !file.match(/\.js$/));
},
},
},
});
If it has failed when preloading CSS Files, Vite will not retry. The lib will reload all the failed CSS files before loading each dynamic import module.
If the modulePreload option is false, similar to the following:
// vite.config.ts
export default defineConfig({
build: {
cssCodeSplit: false,
modulePreload: false,
},
});
You need to set the disableRetryingCSS to true that will not do retrying loadings CSS files.
// main.js
import retryingDynamicImport from "retrying-dynamic-import.";
retryingDynamicImport({
disableRetryingCSS: true,
});
- “import(’a.js’)” fails.
- The lib change ‘a.js’ to ‘a.js?t=xxxxxx’ and try again.
- just that.