eleventy icon indicating copy to clipboard operation
eleventy copied to clipboard

Document returning binary data from transforms

Open cfjedimaster opened this issue 3 years ago • 2 comments

So maybe this wasn't the intent, but... I've built a demo that uses transforms to notice when the destination is PDF. When it has, it creates a PDF version of the HTML and returns the binary data. Initially I was getting a corrupted PDF. But I noticed that if I returned null from transform, I got an error:

[11ty] TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received null

So a whim, I did this:

        return Buffer.from(contents,'binary');

And it worked well. I'd say this should be documented, or if it's not intended, then only allow strings back. (I won't be upset. ;)

cfjedimaster avatar Oct 13 '21 17:10 cfjedimaster

Note that you can bypass the output of a file altogether with

return;

as the sole return line.

mwt avatar Mar 08 '23 02:03 mwt

this is documented for 11ty.js templates here btw https://www.11ty.dev/docs/languages/javascript/#function

and i think it makes sense that that can be done in a transform too, even if it's not explicitly mentioned in the transform documentation, because a transform is about taking content and returning transformed content.

chee avatar Apr 08 '24 19:04 chee