🎥 Make videos programmatically with React
prefetching
Jonny Burger33fdad0e473e
diff --git a/packages/docs/docs/player/player.md b/packages/docs/docs/player/player.mdindex 9f7c021a2f..350382143b 100644--- a/packages/docs/docs/player/player.md+++ b/packages/docs/docs/player/player.md@@ -12,3 +12,8 @@ Using the Remotion Player you can embed Remotion videos in any React app and cusPlay the video, then tweak the parameters below the video.<PlayerExampleWithControls />++## See also++- [Installation](/docs/player/installation)+- [API](/docs/player/api)diff --git a/packages/docs/docs/player/preloading.md b/packages/docs/docs/player/preloading.mdindex 9944458cf7..49798c66c5 100644--- a/packages/docs/docs/player/preloading.md+++ b/packages/docs/docs/player/preloading.md@@ -30,7 +30,9 @@ unpreloadAudio();## Prefetching using `prefetch()`-By prefetching, the full media is downloaded and turned into a Blob URL using [`URL.createObjectURL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL?retiredLocale=de).+_Available in v3.2.23_++By prefetching, the full media is downloaded and turned into a Blob URL using [`URL.createObjectURL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL).If you pass the original URL into either an [`<Audio>`](/docs/audio), [`<Video>`](/docs/video), [`<OffthreadVideo>`](/docs/offthreadvideo) or [`<Img>`](/docs/img) tag and the asset is prefetched, those components will use Blob URL instead.```tsx twoslash@@ -50,6 +52,8 @@ free();[`prefetch()`](/docs/prefetch) is a more reliable way of ensuring the media is ready when it needs to displayed, but the asset needs to be downloaded in full for it.+[`@remotion/preload`](/docs/preload) is preferrable if the asset is large since you don't have to wait for it finish downloading,+<table><tr><th></th>@@ -151,3 +155,8 @@ free();</td></tr></table>++## See also++- [`@remotion/preload`](/docs/preload)+- [`prefetch()`](/docs/prefetch)diff --git a/packages/docs/docs/prefetch.md b/packages/docs/docs/prefetch.mdnew file mode 100644index 0000000000..a3f9a796a1--- /dev/null+++ b/packages/docs/docs/prefetch.md@@ -0,0 +1,40 @@+---+id: prefetch+title: prefetch()+---++_Available in v3.2.23_++By calling `prefetch()`, an asset will be fetched and turned into a Blob URL using [`URL.createObjectURL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL).++If you pass the original URL into either an [`<Audio>`](/docs/audio), [`<Video>`](/docs/video), [`<OffthreadVideo>`](/docs/offthreadvideo) or [`<Img>`](/docs/img) tag and the asset is fully fetched, those components will use Blob URL instead.++```tsx twoslash+import { prefetch } from "remotion";++const { free, waitUntilDone } = prefetch("https://example.com/video.mp4");++waitUntilDone().then(() => {+ console.log("Video has finished loading");+});++// Call free() if you want to un-prefetch and free up the memory:+free();+```++This API is useful if you are using the [`<Player />`](/docs/player) component and you want to display a media asset and want to be sure it is fully loaded before it appears.++An alternative to `prefetch()` are the [`@remotion/preload`](/docs/preload) APIs. See [`@remotion/preload` vs `prefetch()`](/docs/player/preloading#remotionpreload-vs-prefetch) to decide which one is better for your usecase.++## API++The function takes a `src`, which can be a remote URL, an imported asset or an asset loaded using [`staticFile()`](/docs/staticfile) (see: [Importing assets](/docs/assets)). Once called, prefetching starts and an object with two properties are returned:++- `free()` will abort the prefetching and free up the memory. Components using the asset might re-request the asset+- `waitUntilDone()` will return a promise if called that resolves once asset has finished downloading and is ready to be using in [`<Audio>`](/docs/audio), [`<Video>`](/docs/video), [`<OffthreadVideo>`](/docs/offthreadvideo) or [`<Img>`](/docs/img).++## See also++- [`@remotion/preload`](/docs/preload)+- [`@remotion/preload` vs `prefetch()`](/docs/player/preloading#remotionpreload-vs-prefetch)+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/prefetch.ts)diff --git a/packages/docs/docs/preload/preload-audio.md b/packages/docs/docs/preload/preload-audio.mdindex 84a4785c04..581f8cb421 100644--- a/packages/docs/docs/preload/preload-audio.md+++ b/packages/docs/docs/preload/preload-audio.md@@ -10,6 +10,8 @@ This function preloads audio in the DOM so that when a audio tag is mounted, itWhile preload is not necessary for rendering, it can help with seamless playback in the [`<Player />`](/docs/player) and in the preview.+An alternative to `preloadAudio()` is the [`prefetch()`](/docs/prefetch) API. See [`@remotion/preload` vs `prefetch()`](/docs/player/preloading#remotionpreload-vs-prefetch) to decide which one is better for your usecase.+## Usage```tsx twoslash@@ -39,8 +41,8 @@ If the resource does not support CORS, `resolveRedirect()` will fail. If the resThis snippet tries to preload a audio on a best-effort basis. If the redirect cannot be resolved, it tries to preload the original URL.```tsx twoslash+import { preloadAudio, resolveRedirect } from "@remotion/preload";import { Audio } from "remotion";-import { resolveRedirect, preloadAudio } from "@remotion/preload";// This code gets executed immediately once the page loadslet urlToLoad = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3";diff --git a/packages/docs/docs/preload/preload-video.md b/packages/docs/docs/preload/preload-video.mdindex ab8500b80f..3bc4c4b626 100644--- a/packages/docs/docs/preload/preload-video.md+++ b/packages/docs/docs/preload/preload-video.md@@ -11,6 +11,8 @@ This function preloads a video in the DOM so that when a video tag is mounted, iWhile preload is not necessary for rendering, it can help with seamless playback in the [`<Player />`](/docs/player) and in the preview.+An alternative to `preloadVideo()` is the [`prefetch()`](/docs/prefetch) API. See [`@remotion/preload` vs `prefetch()`](/docs/player/preloading#remotionpreload-vs-prefetch) to decide which one is better for your usecase.+## Usage```tsx twoslash@@ -40,8 +42,8 @@ If the resource does not support CORS, `resolveRedirect()` will fail. If the resThis snippet tries to preload a video on a best-effort basis. If the redirect cannot be resolved, it tries to preload the original URL.```tsx twoslash+import { preloadVideo, resolveRedirect } from "@remotion/preload";import { Video } from "remotion";-import { resolveRedirect, preloadVideo } from "@remotion/preload";// This code gets executed immediately once the page loadslet urlToLoad =diff --git a/packages/docs/docs/preload/preload.md b/packages/docs/docs/preload/preload.mdindex 314af6f278..9c7912946f 100644--- a/packages/docs/docs/preload/preload.md+++ b/packages/docs/docs/preload/preload.md@@ -12,6 +12,8 @@ This package provides functions for preloading assets. While preload is not nece<TableOfContents />+An alternative to `@remotion/preload` is the [`prefetch()`](/docs/prefetch) API. See [`@remotion/preload` vs `prefetch()`](/docs/player/preloading#remotionpreload-vs-prefetch) to decide which one is better for your usecase.+## Installation<Tabsdiff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex 2deabb9ad0..a4bfd55be5 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -64,6 +64,7 @@ module.exports = {"register-root","spring","staticfile",+ "prefetch","audio","sequence","loop",