This is an automated email from the ASF dual-hosted git repository. maximebeauchemin pushed a commit to branch cache_bootstrap_data in repository https://gitbox.apache.org/repos/asf/superset.git
commit faf618e8a5a102bbff995ec8639c36c7124236cb Author: Maxime Beauchemin <[email protected]> AuthorDate: Sun Jan 26 22:20:19 2025 -0800 feat: cache the fontend's bootstrap data While reviewing https://github.com/apache/superset/pull/30134 which seemed to make heavy use of `getBootstrapData`, I realized that it's usage is occurs 21 times across the codebase while it may be a fairly expensive operation (JSON.parse on an object that may be significant in size at times (?)). Now it's easy to cache the result in a closure and ensure it happens only once per SPA-load, but opening this as a DRAFT as we probably already store it in many places. Tradeoff here is compute VS memory, and clearly we need in memory somewhere, but probably in most cases, already have it in the state app somewhere. Ideally the app would read it once and make it available in its state, as opposed to different modules calling it on-demand... PR is more of a request for comments. --- superset-frontend/src/utils/getBootstrapData.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/superset-frontend/src/utils/getBootstrapData.ts b/superset-frontend/src/utils/getBootstrapData.ts index e426498e57..88ab11f544 100644 --- a/superset-frontend/src/utils/getBootstrapData.ts +++ b/superset-frontend/src/utils/getBootstrapData.ts @@ -16,12 +16,18 @@ * specific language governing permissions and limitations * under the License. */ - import { BootstrapData } from 'src/types/bootstrapTypes'; import { DEFAULT_BOOTSTRAP_DATA } from 'src/constants'; +let cachedBootstrapData: BootstrapData | null = null; + export default function getBootstrapData(): BootstrapData { - const appContainer = document.getElementById('app'); - const dataBootstrap = appContainer?.getAttribute('data-bootstrap'); - return dataBootstrap ? JSON.parse(dataBootstrap) : DEFAULT_BOOTSTRAP_DATA; + if (cachedBootstrapData === null) { + const appContainer = document.getElementById('app'); + const dataBootstrap = appContainer?.getAttribute('data-bootstrap'); + cachedBootstrapData = dataBootstrap + ? JSON.parse(dataBootstrap) + : DEFAULT_BOOTSTRAP_DATA; + } + return cachedBootstrapData; }
