Hi!
Based on https://www.drupal.org/node/2592307#comment-10454049, it is clear that once a page is served from cache we cannot invoke any hook or functions. However, I might need to serve a javascript snippet(different per user) which lives on the < header > of cached pages. Would you mind pointing me on the right direction? or, is this even possible?
AFAIK, the authcache_builtin_cacheinc_retrieve_cache_page function gets the cache object then the authcache_serve_page_from_cache function serves it. I would just modify any of those two functions. Is there any other solution?
-Another approach could be serve the script within the body of the document (maybe a cached block per user), but I need the javascript snippet in the < header >
Comments
Comment #2
znerol CreditAttribution: znerol commentedInstead of trying to serve different assets (scripts) for different users, I'd recommend to build the scripts in a way such that they can be delivered to any user. Then use some value from
Drupal.settings
to decide whether or not to run the script. Authcache is capable of populatingDrupal.settings
selectively via Ajax/ESI. You can provide your own callbacks viahook_authcache_p13n_setting
. Take a look at the Authcache User module for a good example. It is responsible for populating the contact form.You might want to use the following skeleton:
MYMODULE.module
MYMODULE.js
Comment #3
angel.angelio CreditAttribution: angel.angelio as a volunteer commentedComment #4
angel.angelio CreditAttribution: angel.angelio as a volunteer commentedComment #5
angel.angelio CreditAttribution: angel.angelio as a volunteer commentedThanks for replying. Really appreciate it.
Assuming that I can build the script in such way that I can delivery it to any user then it does make sense to use the hook_authcache_p13n_setting. However, I think I cannot invoke any hook when a page is being serve from cache (which I think is what I need in order to embebed a customize javascript snippet per user in the < header >). Or, am I overthinking this? I think the problem is that I cannot modify the HTML from the < header > once the page it is being serve from cache.
Comment #6
znerol CreditAttribution: znerol commentedThere is no session at the time when a cached page is sent to the user. So even customizing authcache_serve_page_from_cache() does not buy you much because it is impossible to tell users apart yet.
What is the use case you are trying to solve? What does require a user specific inline script in the head element?
Comment #7
angel.angelio CreditAttribution: angel.angelio as a volunteer commentedWhat if I bootstrap the session before authcache_builtin_cacheinc_retrieve_cache_page ? then modify the cached page on authcache_serve_page_from_cache? maybe not the best idea.
The use case is Google Analytics custom dimensions, the GA tracking code should/has to be place on the < head >
I am sending custom dimensions to GA based on users's data / roles and IMO the best way to do it is placing them into the < head >
Comment #8
znerol CreditAttribution: znerol commentedDo you want to use User ID tracking (or any per-user dimension) or are you only interested in roles?
Comment #9
znerol CreditAttribution: znerol commentedAs a general rule I'd recommend to just set a cookie in the client during login/logout and store any ga-settings there. In order to do that just implement
hook_authcache_cookie()
:Then in your ga javascript snipped to something like this (reference):
Comment #10
angel.angelio CreditAttribution: angel.angelio as a volunteer commentedYes I am interested on roles and some user's data.
Understood, that makes a lot of sense. I will give it a try.
Comment #11
znerol CreditAttribution: znerol commentedI guess the original question is answered. Please open a new issue if there is still something unclear.