Skip to content

Uploading multiple images on iOS devices failing #59

@typefox09

Description

@typefox09

Hi,

We are getting the following error when uploading files from Safari (using client side upload with the JS SDK:

"The token with value '8cbdb59b-6246-4f8f-b0e0-77b34c4e9e63' has been used before. We suggest using V4 UUIDs, or another random string with enough entropy to avoid collisions.”

The first file (in a list) will work, while the following fail. This error is only occurring on iOS devices.

Near identical issue (however it's using Uppy):
imagekit-developer/imagekit-uppy-plugin#3

After some debugging I have noticed that on an Android device, or a computer, running a for loop to upload images ImageKit will call the our cloud function to retrieve authenticationParameters for every single iteration, however, on an iOS device the cloud function will only be called on the first iteration. Based on the issue above, it seems like it's caching the result on iOS so it doesn't need to make the same call again. Issue is this is causing subsequent uploads to fail......

A potential fix could be to do the following after line 68 at

xhr.open('GET', authenticationEndpoint);

xhr.setRequestHeader('Cache-Control', 'no-cache');`

Edit:
After much trail and error I have found the only way to solve the issue is by making the call to authenticationEndpoint a POST request. I have tried adding every form of cache control header (both on server & client), however Safari ignores all of them.

Suggested fix: Change API call to authenticationEndpoint from GET to POST request

Code for reference:

Initialise Imagekit:

new ImageKit({
publicKey: "PUBLIC_KEY",
urlEndpoint: "URL_ENDPOINT",
authenticationEndpoint: "API_ENDPOINT,
}),

Authentication endpoint (cloud function):

exports.createImageKitSignature = functions
.https.onRequest((req, res) => {
cors(req, res, () => {
const authenticationParameters = imagekit.getAuthenticationParameters()
res.status(200).send(authenticationParameters)
})
})

Upload function:

async uploadImage({ state }, file) {
  try {
    const result = await imagekit.upload({file: file, fileName: file.name })
    console.log(result)
    return result      
  } catch (error) {
    console.log('error occured', error)
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions