admin管理员组文章数量:1026900
Ignoring the fact that this is generally considered a bad practice, I'm trying to understand how I can preload a set of images either before a page transition or prior to rendering my page. My use case requires quite a few large image files to be displayed at the same time and animated onto the screen. I'd like to essentially have a loading spinner on the page while the initial set of large images is downloaded and cached in the browser and then I can show them all at once.
If I want to do this with standard react, I can do something like this:
await Promise.all(
images.map(async (url) => {
return new Promise((resolve) => {
const image = new Image();
image.src = url;
image.onload = () => resolve(true);
});
})
)}
And then have an isLoading
boolean get flipped when everything is done loading. With the nextjs Image
ponents, though, I can't load those initial images until they are actually added to the dom. The URL for those images changes based on various conditions so I can't really use the original solution to preload them.
Is there a way to force the browser to download the image sources generated from my nextjs <Image>
ponents before they get added to the dom?
Ignoring the fact that this is generally considered a bad practice, I'm trying to understand how I can preload a set of images either before a page transition or prior to rendering my page. My use case requires quite a few large image files to be displayed at the same time and animated onto the screen. I'd like to essentially have a loading spinner on the page while the initial set of large images is downloaded and cached in the browser and then I can show them all at once.
If I want to do this with standard react, I can do something like this:
await Promise.all(
images.map(async (url) => {
return new Promise((resolve) => {
const image = new Image();
image.src = url;
image.onload = () => resolve(true);
});
})
)}
And then have an isLoading
boolean get flipped when everything is done loading. With the nextjs Image
ponents, though, I can't load those initial images until they are actually added to the dom. The URL for those images changes based on various conditions so I can't really use the original solution to preload them.
Is there a way to force the browser to download the image sources generated from my nextjs <Image>
ponents before they get added to the dom?
- I am not very familiar with nextjs, but you could "prefetch" the images by appending them to the DOM as hidden. Then when they are all loaded, let nextjs do its stuff and the images should be displayed instantly as cached by the browser. – user3252327 Commented Sep 19, 2022 at 21:33
1 Answer
Reset to default 3Have you tried adding priority=true
to the Image ponent?
Read details here: next-image docs
Ignoring the fact that this is generally considered a bad practice, I'm trying to understand how I can preload a set of images either before a page transition or prior to rendering my page. My use case requires quite a few large image files to be displayed at the same time and animated onto the screen. I'd like to essentially have a loading spinner on the page while the initial set of large images is downloaded and cached in the browser and then I can show them all at once.
If I want to do this with standard react, I can do something like this:
await Promise.all(
images.map(async (url) => {
return new Promise((resolve) => {
const image = new Image();
image.src = url;
image.onload = () => resolve(true);
});
})
)}
And then have an isLoading
boolean get flipped when everything is done loading. With the nextjs Image
ponents, though, I can't load those initial images until they are actually added to the dom. The URL for those images changes based on various conditions so I can't really use the original solution to preload them.
Is there a way to force the browser to download the image sources generated from my nextjs <Image>
ponents before they get added to the dom?
Ignoring the fact that this is generally considered a bad practice, I'm trying to understand how I can preload a set of images either before a page transition or prior to rendering my page. My use case requires quite a few large image files to be displayed at the same time and animated onto the screen. I'd like to essentially have a loading spinner on the page while the initial set of large images is downloaded and cached in the browser and then I can show them all at once.
If I want to do this with standard react, I can do something like this:
await Promise.all(
images.map(async (url) => {
return new Promise((resolve) => {
const image = new Image();
image.src = url;
image.onload = () => resolve(true);
});
})
)}
And then have an isLoading
boolean get flipped when everything is done loading. With the nextjs Image
ponents, though, I can't load those initial images until they are actually added to the dom. The URL for those images changes based on various conditions so I can't really use the original solution to preload them.
Is there a way to force the browser to download the image sources generated from my nextjs <Image>
ponents before they get added to the dom?
- I am not very familiar with nextjs, but you could "prefetch" the images by appending them to the DOM as hidden. Then when they are all loaded, let nextjs do its stuff and the images should be displayed instantly as cached by the browser. – user3252327 Commented Sep 19, 2022 at 21:33
1 Answer
Reset to default 3Have you tried adding priority=true
to the Image ponent?
Read details here: next-image docs
本文标签: javascriptPreload nextjs Images before page loadStack Overflow
版权声明:本文标题:javascript - Preload nextjs Images before page load - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745651485a2161346.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论