Skip to content

Commit

Permalink
SWRの改善
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-20 committed Mar 23, 2024
1 parent de83bcd commit eb8a842
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
5 changes: 3 additions & 2 deletions workspaces/client/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import { registerServiceWorker } from './utils/registerServiceWorker';

const main = async () => {
await registerServiceWorker();
// await preloadImages();

const fallback = JSON.parse(document.getElementById('inject-data')?.textContent ?? '{}');

$(document).ready(() => {
ReactDOM.hydrateRoot(
$('#root').get(0)!,
<SWRConfig value={{ revalidateIfStale: true, revalidateOnFocus: false, revalidateOnReconnect: false }}>
<SWRConfig value={{ fallback, revalidateOnFocus: false, revalidateOnReconnect: false }}>
<BrowserRouter>
<ClientApp />
</BrowserRouter>
Expand Down
24 changes: 14 additions & 10 deletions workspaces/client/src/utils/preloadImages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@ async function wait(milliseconds: number) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}

export async function preloadImages() {
if (process.env['PATH_LIST'] == null) {
return;
}

const imagePathList: string[] = process.env['PATH_LIST'].split(',').filter((imagePath) => {
const extension = path.parse(imagePath).ext.toLowerCase();
return ['.bmp', '.jpg', '.jpeg', '.gif', '.png', '.webp', '.avif'].includes(extension);
});

export async function preloadImages(imagePathList: string[]) {
const prefetch = Promise.all(
imagePathList.map((imagePath) => {
return new Promise((resolve) => {
Expand All @@ -35,3 +26,16 @@ export async function preloadImages() {

await Promise.race([prefetch, wait(5000)]);
}

export async function preloadAllImages() {
if (process.env['PATH_LIST'] == null) {
return;
}

const imagePathList: string[] = process.env['PATH_LIST'].split(',').filter((imagePath) => {
const extension = path.parse(imagePath).ext.toLowerCase();
return ['.bmp', '.jpg', '.jpeg', '.gif', '.png', '.webp', '.avif'].includes(extension);
});

await preloadImages(imagePathList);
}

0 comments on commit eb8a842

Please sign in to comment.