React Native - Better way to load image very fast without caching it
Asked Answered
R

1

8

I am using FastImage for caching image and it loads image very fast after caching data as expected. But my server is generating new uri (s3 presigned url) each time for same image.

So, FastImage is considering it as new image and tries to download everytime which affects my app performance.

My question is, Is there any optimistic way to render images fast as possible without caching it?

Realist answered 4/9, 2018 at 13:11 Comment(2)
How did you overcome this?Unseasonable
I have set presigned url expiry time to 1 year. So, I get same image url whenever I access it which solved my issue. Now, Fast Image shows cached image dataRealist
M
1

If you have chance to modify the server side application, you can create Authorization headers instead of creating presigned urls.

This function should help.

import aws4 from 'aws4';

export function getURIWithSignedHeaders(imagePath) {
    if(!imagePath){
        return null;
    }
    const expires = 86400; // 24 hours
    const host = `${process.env.YOUR_S3_BUCKET_NAME}.s3.${process.env.YOUR_S3_REGION}.amazonaws.com`;
    // imagePath should be something like images/3_profileImage.jpg
    const path = `/${imagePath}?X-Amz-Expires=${expires}`;
    const opts = {
        host,
        path,
        headers: {
            'Content-Type': 'image/jpeg'
        }
    };
    const { headers } = aws4.sign(opts, {accessKeyId: process.env.YORU_ACCESS_KEY_ID, secretAccessKey: process.env.YOUR_SECRET_ACCESS_KEY});
    return {
        uri: `https://${host}${path}`,
        headers: {
            Authorization: headers['Authorization'],
            'X-Amz-Content-Sha256': headers['X-Amz-Content-Sha256'],
            'X-Amz-Date': headers['X-Amz-Date'],
            'Content-Type': 'image/jpeg',
        }
    }
}

See: 609974221

Morrison answered 6/4, 2020 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.