Updating deploy via API results in "Not found - Request ID"

We have a Netlify function that we use for programmatically updating specific files that are deployed. We do this for previews, as it’s faster than rebuilding the entire site.

For updating JSON files, this has worked really well.

Now we’re trying to update HTML files, and we’re stuck.

We generate our HTML files via Pug templates (which is rendered with data, which we fetch during build).

It’s close to working really well, but when viewing the deploy preview created we simply see (where the ID is different each time, I think):

Not found - Request ID: c746cac3-5728-4f6d-8db5-144310e23443-11884272

There bug seems to be somewhere in the upload (PUTing the updated HTML files) to Neltify via the API.

We can see that the deploy instance gets successfully created but looking at the Netlify UI:

So we’re successfully creating the deploy instance, but the files aren’t getting there.

The response for each PUT (to /deploys/[deploy-id]/files/[file-name]) suggests a problem:

{
    "size": 0,
    "timeout": 0
}

The strange thing is that all of these responses return 200s (suggesting all is good, even thought it’s not).

Here’s the body of our PUT (for passing the updated data):

method: 'PUT',
body: Buffer.from('string-of-HTML', 'utf8'), // Where `string-of-HTML` is the actual string of HTML. Have also tried not passing an encoding type, and also padding `base64` as the encoding type.
headers: headers('application/octet-stream'),

Any thoughts/suggestions/ideas?

Alright, the error was in the function for composing the PUT requests (which get pushed into an array for all files needing to be uploaded, then called with Promise.all).

I had forgotten that with node-fetch it’s necessary to manually check for a successful response. It’s still not 100% clear to me why this would have caused the PUTs to fail.


For further context (in case it’s helpful for someone)…

Previously I had this (DOES NOT WORK):

const uploadPromises = newDeployResponse.required.map((requiredFileHash) => {
   const fileName = getKeyByValue(filesForNewDeploy, requiredFileHash)

   return nodeFetch(`${netlifyApiUrl}/deploys/${newDeployResponse.id}/files${fileName}`, {
      method: 'PUT',
      body: Buffer.from(fileContentsByHash.get(requiredFileHash), 'utf-8'),
      headers: headers('application/octet-stream'),
   })
})

return Promise.all(uploadPromises)
   .then((response) => {
      return {
         statusCode: 200,
         body: JSON.stringify({
            response,
            previewUrl: newDeployResponse.links.permalink,
         }),
      }
   })

Now I have this (WORKING):

const uploadPromises = newDeployResponse.required.map((requiredFileHash) => {
  const fileName = getKeyByValue(filesForNewDeploy, requiredFileHash);

  return nodeFetch(
    `${netlifyApiUrl}/deploys/${newDeployResponse.id}/files${fileName}`,
    {
      method: "PUT",
      body: Buffer.from(fileContentsByHash.get(requiredFileHash), "utf-8"),
      headers: headers("application/octet-stream"),
    }
  ).then((response) => {
    if (response.ok) {
      return Promise.resolve(response);
    } else {
      return Promise.reject();
    }
  });
});

return Promise.all(uploadPromises)
  .then((response) => {
    return {
      statusCode: 200,
      body: JSON.stringify({
        response,
        previewUrl: newDeployResponse.links.permalink,
      }),
    };
  })
  .catch((response) => {
    return {
      statusCode: 500,
      body: JSON.stringify({
        response,
      }),
    };
  });