2023-10-30 08:02:37 +00:00
|
|
|
|
import { getBaseURL } from "@appsmith/utils/preloadHelpers";
|
2023-06-22 05:28:10 +00:00
|
|
|
|
// This file preloads chunks for the edit and view modes ahead of the import()
|
|
|
|
|
|
// call that will actually require them. This puts these chunks into HTTP cache
|
|
|
|
|
|
// (so they can be executed immediately) but doesn’t execute them (so that the
|
|
|
|
|
|
// `retryPromise()` logic around the import() calls can still work).
|
|
|
|
|
|
//
|
|
|
|
|
|
// The list of chunks to be preloaded is taken from `index.html`, as it’s only
|
|
|
|
|
|
// available from webpack stats in the end of the build.
|
|
|
|
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
|
|
interface Window {
|
|
|
|
|
|
// __APPSMITH_CHUNKS_TO_PRELOAD is added in a script tag in index.html
|
|
|
|
|
|
__APPSMITH_CHUNKS_TO_PRELOAD?: {
|
|
|
|
|
|
"edit-mode": string[];
|
|
|
|
|
|
"view-mode": string[];
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-05 13:34:03 +00:00
|
|
|
|
// Preloading is disabled in LinkRelPreload_spec.js
|
|
|
|
|
|
const isPreloadingDisabled =
|
|
|
|
|
|
new URL(window.location.href).searchParams.get("disableChunkPreload") ===
|
|
|
|
|
|
"true";
|
|
|
|
|
|
|
2023-06-22 05:28:10 +00:00
|
|
|
|
const currentMode = getModeForPathname(window.location.pathname);
|
2023-07-05 13:34:03 +00:00
|
|
|
|
if (
|
|
|
|
|
|
!isPreloadingDisabled &&
|
|
|
|
|
|
window.__APPSMITH_CHUNKS_TO_PRELOAD &&
|
|
|
|
|
|
currentMode
|
|
|
|
|
|
) {
|
2023-10-30 08:02:37 +00:00
|
|
|
|
const BASE_URL = getBaseURL();
|
2023-06-22 05:28:10 +00:00
|
|
|
|
window.__APPSMITH_CHUNKS_TO_PRELOAD[currentMode]
|
2023-10-30 08:02:37 +00:00
|
|
|
|
.map((url) => BASE_URL + url)
|
2023-06-22 05:28:10 +00:00
|
|
|
|
.forEach((url) => {
|
|
|
|
|
|
const link = document.createElement("link");
|
|
|
|
|
|
link.rel = "preload";
|
|
|
|
|
|
link.as = getPreloadValueForFile(url);
|
|
|
|
|
|
link.href = url;
|
|
|
|
|
|
document.head.appendChild(link);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getPreloadValueForFile(fileName: string) {
|
|
|
|
|
|
if (fileName.endsWith(".js")) {
|
|
|
|
|
|
return "script";
|
|
|
|
|
|
} else if (fileName.endsWith(".css")) {
|
|
|
|
|
|
return "style";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
throw new Error(`Unknown preload type for file: ${fileName}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getModeForPathname(
|
|
|
|
|
|
pathname: string,
|
|
|
|
|
|
): keyof NonNullable<Window["__APPSMITH_CHUNKS_TO_PRELOAD"]> | null {
|
|
|
|
|
|
if (/^\/app\/[^\/]+\/[^\/]+\/edit\b/.test(pathname)) {
|
|
|
|
|
|
return "edit-mode";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (pathname.startsWith("/app/")) {
|
|
|
|
|
|
return "view-mode";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export {};
|