Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

feat(apps): preload app runtime#140

Draft
JanPokorny wants to merge 1 commit into
mainfrom
janpokorny/feat-apps-preload-app-runtime
Draft

feat(apps): preload app runtime#140
JanPokorny wants to merge 1 commit into
mainfrom
janpokorny/feat-apps-preload-app-runtime

Conversation

@JanPokorny

@JanPokorny JanPokorny commented Dec 13, 2024

Copy link
Copy Markdown
Contributor

Preload resources for the app runtime once the app list is loaded. Depends on i-am-bee/bee-usercontent-site#26

Signed-off-by: Jan Pokorný <JenomPokorny@gmail.com>
@JanPokorny JanPokorny requested a review from a team as a code owner December 13, 2024 15:25
Comment on lines +41 to +63
useEffect(() => {
(async () => {
fetch(USERCONTENT_SITE_URL, { priority: 'low', mode: 'no-cors' });
const preload = await (
await fetch(`${USERCONTENT_SITE_URL}/meta/preload.json`)
).json();
for (const item of preload.items) {
fetch(
item.url.startsWith('/')
? `${USERCONTENT_SITE_URL}${item.url}`
: item.url,
{
priority: 'low',
mode: 'no-cors',
headers: {
Accept: `${item.contentType}, */*;q=0.01`,
'Accept-Encoding': '',
},
},
);
}
})();
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such implementation would cause memory leaks and crash the rendering.

Consider something like this.

useEffect(() => {
  const controller = new AbortController();

  fetch(USERCONTENT_SITE_URL, {
    priority: 'low',
    mode: 'no-cors',
    signal: controller.signal,
  }).catch(noop);

  fetch(`${USERCONTENT_SITE_URL}/meta/preload.json`, { signal: controller.signal })
    .then((response) => response.json())
    .then((preload) => Promise.allSettled((preload?.items ?? []).map((item) =>
      fetch(
        item.url.startsWith('/')
          ? `${USERCONTENT_SITE_URL}${item.url}`
          : item.url,
        {
          priority: 'low',
          mode: 'no-cors',
          headers: {
            Accept: `${item.contentType}, */*;q=0.01`,
            'Accept-Encoding': '',
          },
          signal: controller.signal,
        }
      )
    })
    .catch(noop);

  return () => {
    controller.abort();
  };
}, []);

@JanPokorny JanPokorny marked this pull request as draft January 6, 2025 10:05
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants