PromucFlow_constructor/app/client/src/preload-route-chunks.ts
Satish Gandham 62383c975c
fix: Refactor preloads to fix an issue with CDN on EE (#28402)
## Description
Our preloads were not using CDN, this PR fixes it. More details in the
linked issue.

#### PR fixes following issue(s)
Fixes #28359
 
 
#### Type of change
> Please delete options that are not relevant.
- Bug fix (non-breaking change which fixes an issue)
 
## Testing
>
#### How Has This Been Tested?
- [x] Manual

Should verify CDN once deployed on release.
 
## Checklist:
#### Dev activity
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed

---------

Co-authored-by: Satish Gandham <hello@satishgandham.com>
2023-10-30 13:32:37 +05:30

68 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { getBaseURL } from "@appsmith/utils/preloadHelpers";
// 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 doesnt 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 its 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[];
};
}
}
// Preloading is disabled in LinkRelPreload_spec.js
const isPreloadingDisabled =
new URL(window.location.href).searchParams.get("disableChunkPreload") ===
"true";
const currentMode = getModeForPathname(window.location.pathname);
if (
!isPreloadingDisabled &&
window.__APPSMITH_CHUNKS_TO_PRELOAD &&
currentMode
) {
const BASE_URL = getBaseURL();
window.__APPSMITH_CHUNKS_TO_PRELOAD[currentMode]
.map((url) => BASE_URL + url)
.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 {};