netlify/netlify-plugin-edge-handlers

Feat(bundling): generate predictable filenames for bundles

Closed this issue · 4 comments

Which problem is this feature request solving?

I'm working netlify/cli#1236 on and need to read both the bundle file and manifest file from the .netlify/edge-handlers directory.
The manifest file has a predictable name manifest.json, but bundles have a hash as the filename.
Moreover, old bundles are not deleted between build commands and a new bundle is generated even if the code remains the same:
image

To reproduce:

  1. Set up a project with an edge-handlers directory.
  2. Run netlify build multiple times (with CLI version >=v2.63.2)
  3. See that each time a new bundle is created.

Describe the solution you'd like

We can keep the current behaviour and also save the latest bundle as latest or bundle.
Also, not sure why the hash changes if the code remains the same, but that is less of an issue (the main issue here is creating redundant files on users machines).
I believe the code responsible for that is here:

const id = "func" + crypto.randomBytes(16).toString("hex");

Describe alternatives you've considered

I can look at the creation date of files and use the most recent one.

Can you submit a pull request?

Yes, I believe we can update the code here to write bundle with a predictable name:

await fsPromises.writeFile(outputFile, bundle, "utf-8");

Update

As @ehmicky pointed out, I can read the manifest.json file and then use the sha property to determine the last bundle.

we can totally think about using a fixed name for handler bundles. i'd like to change that once we're tackling local serving of edge handlers and watching for changes / reloads

I have added #48, #49 and #50 to track some points mentioned in this issue.

Done!