Nitro useStorage use Brotli encoded json data
I have a Nuxt server route that should deliver a 400kb Brotli encoded JSON object. The json file is delivered over a custom server api route, because I need to set some custom headers for cache management with etags and set the correct content-type header.
How do I read the file correctly and make it fetchable by the user?
This is my current code:
2 Replies
Hi @Felix. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Your approach to serving the Brotli-encoded JSON file using a custom Nuxt server route is generally correct. However, there are a few adjustments we can make to ensure the file is read and served correctly. Let's go through the code and make some improvements:
The main changes and explanations are:
1. We use
getItemRaw
instead of getItem
to retrieve the Brotli-encoded file. This is because we want to get the raw buffer data, not a parsed JSON object. According to the unstorage documentation, getItemRaw
is used to get the value of a key in storage in raw format.
2. We specify the type of characterParts
as Buffer
to correctly handle the binary data.
3. We use setResponseHeaders
instead of setHeaders
to set multiple headers at once. This is based on the h3 documentation.
These changes should allow you to correctly read and serve the Brotli-encoded JSON file. The browser will receive the encoded data along with the correct headers, and it will handle the decoding on the client side.
Remember that for this to work, the client must support Brotli decompression. Most modern browsers do, but you might want to consider providing a fallback for older browsers if necessary.
Unstorage Documentation
H3 Documentation
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@278150133932097536> Kapa.ai is still learning and improving, please let me know how I did by reacting below.