"Distilling" StaticHttp Data #172
Replies: 1 comment
-
My initial thoughts on implementing this: Don't store intermediary StaticHttp responsesAdd an additional argument to the request resolver function in a few places to determine whether to store the results or not. When we're running a distilled part of a elm-pages/src/Pages/StaticHttp.elm Line 566 in be8d145 elm-pages/src/Pages/StaticHttpRequest.elm Line 13 in be8d145 Then the implementation will look similar to elm-pages/src/Pages/StaticHttp.elm Lines 156 to 171 in be8d145 And then the top-level distill call will store the final result after running the encoder from the distill Codec. Distilled result storage keyWe need a unique key to distill the result. This may end up being a case where we need to just ask the user to take on the responsibility of making sure they use a unique key. The signature to provide a unique storage key could look like this: distill : String -> Codec a -> StaticHttp.Request a -> StaticHttp.Request a If we had the ability to do Lisp-style meta-programming, then we could inspect the ASTs of the Codec and There are two things that could go wrong:
|
Beta Was this translation helpful? Give feedback.
-
I was thinking about the idea of "distilling" optimized decoders the wrong way.
Why do we need distillation?
Here are some distillation use cases:
Distillation wouldn't be relevant in a context like a file generator, where no resulting data is bundled into the app. For file generators, it gets the data, and then it's already just distilling things down to a single thing: a file to generate (like an RSS feed file, for example).
Distillation is a performance optimization tool and a data protection tool.
At first, I was thinking about providing some places to hook into isolating data, like taking an incoming server response for a server-side rendered page request, and building up some context where you get a user.
You might have some tokens or other sensitive data coming back from an intermediary response in a chain of
StaticHttp.andThen
s. All of the data that came along, including just a token you fetched for example, would get included in the bundle for that page. But you shouldn't be returning that data in the page.I could provide a specific hook for that spot where you can "isolate" the data with this Codec approach (explicitly put the data you need in here, and tell me how to build it up as JSON and then Decode it - it gives you a clean slate). But I realized, why not make this a general-purpose tool if we can, so you can use it in any context, whether you need it for performance reasons or for privacy/security.
Finding the right place to distill data
The first big thing I got wrong was that I was thinking about it as a way of modifying some JSON in-place. That leads to all sorts of consistency problems:
It totally breaks down.
The second problem is that I was assuming that distillation would be an abstraction on top of an
OptimizedDecoder
. I realize now it should be an abstraction of StaticHttp.OptimizedDecoders calculate what unused data can be pruned in the JSON. But StaticHttp actually prunes the data and bundles the data you depend on. For example,
StaticHttp.succeed ()
doesn't depend on any data.So I'm realizing now that the concept of distilling could be done as an abstraction in StaticHttp.
The
Codec
part means that you can distill the payload down to JSON, so that resulting encoded JSON is what gets included in the bundle. Then you can use the Codec to decode the data on the other side.Beta Was this translation helpful? Give feedback.
All reactions