Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement defer #12587

Merged
merged 1 commit into from
Jun 23, 2024
Merged

Implement defer #12587

merged 1 commit into from
Jun 23, 2024

Conversation

bep
Copy link
Member

@bep bep commented Jun 9, 2024

Closes #8086

My motivation for taking on this now is that I want to try to support JS code splitting, and that would need some more powerful "post build" synchronization than what we have today. This PR would eventually, I guess, replace resources.PostProcess. This PR needs more testing/polishing, but on the technical level this is simpler and without the many restrictions of resources.PostProcess.

A typical use case for resources.PostProcess is TailwindCSS where we need to know the CSS classes etc. in use before we can build the CSS.

Written using the new defer keyword, the TailwindCSS setup could look something like this:

 {{ with (templates.Defer (dict "key" "styles" "data" "foo")) }}
      {{ $options := dict "inlineImports" true }}
      {{ $styles := resources.Get "css/styles.css" }}
      {{ $styles = $styles | resources.PostCSS $options }}
      {{ if hugo.IsProduction }}
        {{ $styles = $styles | minify | fingerprint }}
      {{ end }}
      <link href="{{ $styles.RelPermalink }}" rel="stylesheet" />
 {{ end }}
  • defer only works with with
  • It takes an option map as its argument. Options:
    • key: This is optional and can be thought about as being part of the cache key. In the example above, the defer block will be executed exactly once and then reused.
    • data: This becomes the "dot" in the defer block.

Note that variables defined on the outside are not visible on the inside and vice versa. Any data must be passed in the data option, e.g.:

{{ $data := dict "page" . }}
{{ with (defer (dict "data" $data)) }}
   Page: {{ .page.Title }}
{{ end }}

@bep
Copy link
Member Author

bep commented Jun 9, 2024

@jmooring does the API as outlined above make sense to you?

@jmooring
Copy link
Member

jmooring commented Jun 9, 2024

I think the above is OK, but this caught my attention:

defer only works with with

If that's the case, and if you anticipate that it will always be used this way, could we just have a defer block where the with is implied?

Not a big deal, but it may be easier to comprehend the intent.

@bep
Copy link
Member Author

bep commented Jun 9, 2024

defer block where the with is implied?

So, I thought about that, but

  1. The Go template parser would throw an error.
  2. It would also certainly break automatic code formatting.

The first one is the deal breaker (unless we do string replacements), but the second bullet is also important (to me, anyhow).

@jmooring
Copy link
Member

jmooring commented Jun 9, 2024

I thought about that

Yeah, I figured you did. LGTM. Things move fast in Hugo land.

@jmooring
Copy link
Member

jmooring commented Jun 9, 2024

I assume you should not be able use defer within a content adapter. For example, to defer content adapter page creation until the other pages are rendered.

@bep
Copy link
Member Author

bep commented Jun 9, 2024

I assume you should not be able use defer within a content adapter.

No, that would not make sense.

@bep bep force-pushed the feat/defer-8086 branch 4 times, most recently from baa407b to 2547c9d Compare June 21, 2024 09:23
bep added a commit to bep/hugo-starter-tailwind-basic that referenced this pull request Jun 21, 2024
A new feature coming in next Hugo.

See gohugoio/hugo#12587
bep added a commit to bep/hugo-starter-tailwind-basic that referenced this pull request Jun 21, 2024
A new feature coming in next Hugo.

See gohugoio/hugo#12587
bep added a commit to bep/hugo-starter-tailwind-basic that referenced this pull request Jun 21, 2024
A new feature coming in next Hugo.

See gohugoio/hugo#12587
bep added a commit to bep/hugo-starter-tailwind-basic that referenced this pull request Jun 21, 2024
A new feature coming in next Hugo.

See gohugoio/hugo#12587
bep added a commit to bep/hugo-starter-tailwind-basic that referenced this pull request Jun 21, 2024
A new feature coming in next Hugo.

See gohugoio/hugo#12587
@bep
Copy link
Member Author

bep commented Jun 21, 2024

I must say that this works incredibly well with my new TailwindCSS setup in bep/hugo-starter-tailwind-basic#18

bep added a commit to bep/hugo-starter-tailwind-basic that referenced this pull request Jun 21, 2024
A new feature coming in next Hugo.

See gohugoio/hugo#12587
bep added a commit to bep/hugo-starter-tailwind-basic that referenced this pull request Jun 21, 2024
A new feature coming in next Hugo.

See gohugoio/hugo#12587
bep added a commit to bep/hugo-starter-tailwind-basic that referenced this pull request Jun 21, 2024
A new feature coming in next Hugo.

See gohugoio/hugo#12587
bep added a commit to bep/hugo-starter-tailwind-basic that referenced this pull request Jun 21, 2024
A new feature coming in next Hugo.

See gohugoio/hugo#12587
bep added a commit to bep/hugo-starter-tailwind-basic that referenced this pull request Jun 21, 2024
A new feature coming in next Hugo.

See gohugoio/hugo#12587
bep added a commit to bep/hugo-starter-tailwind-basic that referenced this pull request Jun 21, 2024
A new feature coming in next Hugo.

See gohugoio/hugo#12587
@bep bep force-pushed the feat/defer-8086 branch 3 times, most recently from d17c2c1 to 37a4bbd Compare June 22, 2024 16:10
@bep bep marked this pull request as ready for review June 22, 2024 17:12
@bep bep force-pushed the feat/defer-8086 branch 2 times, most recently from 1744ac2 to f657180 Compare June 22, 2024 17:25
@bep bep merged commit 6cd0784 into gohugoio:master Jun 23, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add deferred template blocks
2 participants