I have a few React components that are lazy imported in App.tsx
. App.tsx
is used in Index.tsx
where it is rendered and appended to the body
.
const IndexPage = lazy(() => import("../features//IndexPage"));
const TagsPage = lazy(
() => import("../features/tags/TagsPage")
);
const ArticlePage = lazy(() => import("../features/article/ArticlePage"));
const SearchResultPage = lazy(
() => import("../features/search-result/SearchResultPage")
);
const ErrorPage = lazy(() => import("../features/error/ErrorPage"));
----
<BrowserRouter basename={basename}>
<Suspense fallback={<Fallback />}>
<Routes>
<Route path={INDEX} element={<IndexPage />} />
<Route path={ARTICLE} element={<ArticlePage />} />
<Route path={TAGS} element={<TagsPage />} />
<Route path={SEARCH} element={<SearchResultPage />} />
<Route path={ERROR} element={<ErrorPage />} />
<Route path="/*" element={<ErrorPage />} />
</Routes>
</Suspense>
</BrowserRouter>
Often, the following error happens in production.
Failed to fetch dynamically imported module:
It has happened in all routes.
https://help.example.io/static/js/SearchResultPage-c1900fe3.js
https://help.example.io/static/js/TagsPage-fb64584c.js
https://help.example.io/static/js/ArticlePage-ea64584c.js
https://help.example.io/static/js/IndexPage-fbd64584c.js
I have changed the build path. Therefore, it is /static/js
.
build: {
assetsInlineLimit: 0,
minify: true,
rollupOptions: {
output: {
assetFileNames: (assetInfo) => {
var info = assetInfo.name.split(".");
var extType = info[info.length - 1];
if (/png|jpe?g|svg|gif|tiff|bmp|ico/i.test(extType)) {
extType = "img";
} else if (/woff|woff2/.test(extType)) {
extType = "css";
}
return `static/${extType}/[name]-[hash][extname]`;
},
chunkFileNames: "static/js/[name]-[hash].js",
entryFileNames: "static/js/[name]-[hash].js",
},
},
outDir: "./../backend/src/main/resources/static/articles/",
emptyOutDir: true,
},
Does someone know how to fix this issue?
Update:
I have never got this error in development. I use Sentry to track errors. It has happened at least 274 times in two months.
This is all that is on Sentry.
{
arguments: [
{
message: Failed to fetch dynamically imported module: https://help.example.io/static/js/SearchResultPage-c1900fe3.js,
name: TypeError,
stack: TypeError: Failed to fetch dynamically imported module: https://help.example.io/static/js/SearchResultPage-c1900fe3.js
}
],
logger: console
}
Update
We had 500000 visits in the past two months. It happened 274 times. Since tracesSampleRate
is 0.3
, it is definitely more than that.
Sentry.init({
dsn: "",
integrations: [new BrowserTracing()],
tracesSampleRate: 0.3,
});
It has happened on all kinds of browsers but mostly on Chrome.
I can not reproduce it either in dev nor in prod.
Update
I reproduced this bug finally. It happens if you are on a page and you release a new version. The file that contains the dynamically imported module, does not exist anymore, for eg:
https://help.example.io/static/js/IndexPage-fbd64584c.js
The above link returns 404
.