Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

How to use post-processing functions? #319

Closed
filmaj opened this issue May 9, 2019 · 21 comments
Closed

How to use post-processing functions? #319

filmaj opened this issue May 9, 2019 · 21 comments
Labels

Comments

@filmaj
Copy link
Contributor

filmaj commented May 9, 2019

The readme mentions it, but I'm not sure how to assemble it. pre.js seems to magically work based on the filename and export names? I tried copying a working pre.js I have and renaming it to post.js, and renaming the exports to post, but that didn't work.

@tripodsan
Copy link
Contributor

like here: https://github.com/adobe/helix-pipeline#using-extension-points ?

or what do you want to do?

@trieloff
Copy link
Contributor

trieloff commented May 9, 2019

@filmaj you can find a real-life example here: https://github.com/adobe/helix-cli/blob/9b1b05c80f9976cb83c0331278e0109af3944572/test/integration/src/html.pre.js#L98-L110

The list of extension points that you can hook in .before or .after are listed here: https://github.com/adobe/helix-pipeline#common-extension-points

@filmaj
Copy link
Contributor Author

filmaj commented May 9, 2019

Thanks! I don't know how I missed the text in common extension points...

What I'm trying to do: Instead of loading all of spectrum-css's SVG icons in one massive go as part of our current global HTML template, I want to post-process HTML in the global HTML template, search through the HTML and find which spectrum-css icons are used, and inject only the ones being used into the DOM. We are thinking this would improve loading times.

@trieloff
Copy link
Contributor

trieloff commented May 9, 2019

The optimal extension point for that might not yet be exposed. It would be here:

.after(tohast) // start HTML post-processing
(so that you can use the complete parsed HAST)

@ryanbetts
Copy link
Contributor

@trieloff Any timeline for having that tohast extension point exposed to the wrapper functions? Or a way to access it before it is exposed? Sounds like exactly what we need.

I tried playing with the existing common extension points you mention above, yesterday. In our global template, they all seem to execute before the esi templates have been rendered, so not as useful.

@trieloff
Copy link
Contributor

trieloff commented May 9, 2019

@ryanbetts as soon as #324 is merged ;)

@ryanbetts
Copy link
Contributor

@trieloff Music to my ears! Thx.

adobe-bot pushed a commit that referenced this issue May 10, 2019
# [1.12.0](v1.11.0...v1.12.0) (2019-05-10)

### Features

* **html:** introduce new hast extension point ([#324](#324)) ([325a6b9](325a6b9)), closes [#319](#319)
@ryanbetts
Copy link
Contributor

@trieloff Looking at the contents of the payload in the after:hast stage of the pipeline, I'm still not sure where to find the final HTML that I'm hoping to post-process. But still new to Helix so I'm probably missing something.

Here's where I've looked:

  • context.content.document.body/context.content.body is empty.
  • context.content.html is undefined
  • context.response.body contains the template, with the <esi:include ...> tags still unrendered.

Am I just looking in the wrong spot?

@tripodsan
Copy link
Contributor

I think, it's in context.response.hast :

@ryanbetts
Copy link
Contributor

@tripodsan That object is not empty, but seems to also be incomplete. I'm still unfamiliar with working with these hast objects, though. As far as I'm aware I turn them into a workable DOM with the helix VDOM utility, correct? Something like: new VDOM(context.response.hast)?

When I do that with the the hast object I don't end up getting what is rendered in the browser. I just get a bunch of empty <div> tags for the most part.

@tripodsan
Copy link
Contributor

I don't think you go back to vdom again. I think the hast is meant to last-minute cleanup.

btw:

In our global template, they all seem to execute before the esi templates have been rendered, so not as useful.

hmm, I think there is a misconception. the response from the esi:includes are not incorporated into the response of the htl that includes them during the pipeline, this is an operation that happens on the (edge) server. the idea is, that the esi:included responses are cacheable independent. if you really need to perform an operation on the very final dom, then there is no other way as to do it clientside.

@ryanbetts
Copy link
Contributor

Thanks for the speedy replies! I think I need to spend some more time in Helix school.

Okay, so if I'm following you correctly: .htl files are all run through the pipeline individually, and only combined at the moment of a request. Makes sense that you want all the components to be independently cacheable.

So, if we wanted to have something that was run for every template, we have 3 options:

  1. Wait until it has been published to the edge and run the cleanup client side on the final DOM. This is the current approach that we are trying to move away from.
  2. Run the cleanup in the pre function of each individual template it applies to. Would like to avoid this in order to keep things DRY.
  3. Write our own pipeline that has that as a step to take care of that.

Am I correct that number (3) is an option?

@tripodsan
Copy link
Contributor

tripodsan commented May 10, 2019

yes, you can provide your own pipeline, via a *.pipe.js file. but you'd still need to have one for every template.
you can copy-paste it from here: https://github.com/adobe/helix-pipeline/blob/e9bb5ac292d0c56277c7b5d33e8053248648ebee/src/defaults/html.pipe.js

I would keep your cleanup-function in a helper, and then add it to every template. something like:

/src/cleanup.js

module.exports = (context, action) => { ... cleanup code ... }.

/src/footer.html.pre.js

module.after = { hast: require('./cleanup.js') };

@ryanbetts
Copy link
Contributor

Gotcha. Thanks!

koraa pushed a commit that referenced this issue May 10, 2019
# [1.12.0](v1.11.0...v1.12.0) (2019-05-10)

### Features

* **html:** introduce new hast extension point ([#324](#324)) ([325a6b9](325a6b9)), closes [#319](#319)
@ryanbetts
Copy link
Contributor

@tripodsan Still quite confused as to what I should be doing with the HAST. Is there any documentation on how to work with that format?

@tripodsan
Copy link
Contributor

tripodsan commented May 14, 2019

I'm not too familiar myself. @trieloff might know more.
for for example: add-headers.js is removing the meta tags and populate the headers.

@trieloff
Copy link
Contributor

@ryanbetts you can find a short introduction to the format here: https://github.com/syntax-tree/hast (it's a lightweight representation of the HTML syntax tree with a focus on ease of transformation, embedded into the unifiedjs ecosystem – i.e. the same stuff we use for Markdown handling)

There is a long list utilities specifically for HAST and another list of utilities for all unified syntax trees that make working with HAST really easy. The most useful are:

@ryanbetts
Copy link
Contributor

@trieloff This is great, thx! Between this list and examples in your source I think I'm off to the races.

@trieloff
Copy link
Contributor

trieloff commented Jun 6, 2019

@ryanbetts please note that we made hast obsolete in v3.0.0: https://github.com/adobe/helix-pipeline/releases/tag/v3.0.0

@ryanbetts
Copy link
Contributor

Duly noted. Will refactor to that now. Thx!

@rofe
Copy link
Contributor

rofe commented Aug 9, 2019

Can this be closed?

@trieloff trieloff closed this as completed Aug 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants