Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache control not applied when uploading files #550

Open
2 tasks done
florian-mlr opened this issue Oct 2, 2024 · 8 comments · May be fixed by #551
Open
2 tasks done

Cache control not applied when uploading files #550

florian-mlr opened this issue Oct 2, 2024 · 8 comments · May be fixed by #551
Assignees
Labels
bug Something isn't working
Milestone

Comments

@florian-mlr
Copy link

florian-mlr commented Oct 2, 2024

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

I have noticed an issue regarding the "Cache-Control" header. After uploading an image and explicitly specifying the "cacheControl" option, I find that when I subsequently retrieve the image, the header does not include the intended "Cache-Control" configuration. Instead, it appears that the cache control is set to "no-cache."

try await NetworkClient.shared.storage
            .from("photos")
            .upload(
                path: "\(userId)/\(photoName).\(fileExtension)",
                file: data,
                options: FileOptions(
                    cacheControl: "172800",
                    contentType: "image/jpeg"
                )
            )

To Reproduce

Upload an image and set the cache control
When fetching the uploaded image, the header contains "Cache-Control: no-cache"

Expected behavior

When fetching the uploaded image, the header should contains "Cache-Control: max-age=172800"

System information

  • OS: iOS 17.4
  • Version of supabase-swift: 2.8.1

Additional context

I also want to mention that I tried updating the library to v2.20.0 to check if the issue is resolved. However, when I proceeded with the update, I encountered an error: "error: errSecItemNotFound: The item cannot be found." Due to this error, I decided to abandon the migration.

@florian-mlr florian-mlr added the bug Something isn't working label Oct 2, 2024
@grdsdev
Copy link
Collaborator

grdsdev commented Oct 2, 2024

Hi @florian-mlr thansk for the report.

Regarding cache control, I was able to reproduce it, I'm checking with backend, the lib seems to be correctly sending the request.

About error: errSecItemNotFound error you got when updating the library, does it break anything for you? Or is it just the log message?

@florian-mlr
Copy link
Author

Hi @florian-mlr thansk for the report.

Regarding cache control, I was able to reproduce it, I'm checking with backend, the lib seems to be correctly sending the request.

Hey @grdsdev , thank you for the quick reply

How do you log headers ?
I thought it came from the library because I have an android app where I created my own network library and when I send the cache control in the header, I can see that my image has the expected cache control.

About error: errSecItemNotFound error you got when updating the library, does it break anything for you? Or is it just the log message?

Yes it breaks the logic in my app, the error is thrown when i make this call try await NetworkClient.shared.auth.session
and I treat this error as an undefined error, I would have preferred to receive an error like AuthError.sessionNotFound

@grdsdev grdsdev linked a pull request Oct 3, 2024 that will close this issue
@florian-mlr
Copy link
Author

Hello @grdsdev
When do you plan to release the fix ?

@florian-mlr
Copy link
Author

In the meantime i've created my own function :

static func uploadImage(
        bucket: String,
        path: String,
        data: Data,
        upsert: Bool = false,
        cacheControl: String = "no-cache"
    ) async throws -> String {
        let session = try await NetworkClient.shared.auth.session
                
        guard let url = URL(string: "\(myUrl)/storage/v1/object/\(bucket)/\(path)") else {
            throw URLError(.badURL)
        }
        
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue(myApiKey, forHTTPHeaderField: "apikey")
        request.setValue("Bearer \(session.accessToken)", forHTTPHeaderField: "Authorization")
        request.setValue("image/jpeg", forHTTPHeaderField: "Content-Type")
        request.setValue(cacheControl, forHTTPHeaderField: "Cache-Control")
        request.setValue(upsert.description, forHTTPHeaderField: "x-upsert")
        request.httpBody = data
        
        let (responseData, response) = try await URLSession.shared.data(for: request)
        
        if let httpResponse = response as? HTTPURLResponse, !(200...299).contains(httpResponse.statusCode) {
            throw URLError(.badServerResponse)
        }
        
        guard let json = try? JSONSerialization.jsonObject(with: responseData, options: []),
              let jsonObject = json as? [String: Any],
              let key = jsonObject["Key"] as? String else {
            throw URLError(.cannotParseResponse)
        }
        
        return key
    }

@grdsdev
Copy link
Collaborator

grdsdev commented Oct 7, 2024

Hi @florian-mlr can you confirm you get cache-control other than no-cache when using your code?

I'm still doing a few tests and wasn't able to get cache-control other than no-cache as response.

@florian-mlr
Copy link
Author

florian-mlr commented Oct 7, 2024

Hi @florian-mlr can you confirm you get cache-control other than no-cache when using your code?

I'm still doing a few tests and wasn't able to get cache-control other than no-cache as response.

Hello @grdsdev !
Yes i can confirm it works with my code.

When checking on the browser, i get
Capture d’écran 2024-10-07 à 22 30 43

On your side, does my code work too ?

@grdsdev
Copy link
Collaborator

grdsdev commented Oct 8, 2024

@florian-mlr when I download the file uploaded by your code, I still get no-cache, are you using download method from the library? How are you downloading it?

@florian-mlr
Copy link
Author

@florian-mlr when I download the file uploaded by your code, I still get no-cache, are you using download method from the library? How are you downloading it?

I'm downloading the image with AsyncImage in iOS and with Coil in Android by giving the public url. I'm not using the library to download image.
Also you can check the cache-control header on your browser

@grdsdev grdsdev added this to the v2 milestone Oct 14, 2024
@grdsdev grdsdev self-assigned this Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants