7
7
from statshog .defaults .django import statsd
8
8
9
9
from posthog .exceptions import generate_exception_response
10
+ from posthog .hogql import ast
11
+ from posthog .hogql .compiler .javascript import create_javascript
12
+ from posthog .hogql .parser import parse_string_template
10
13
from posthog .logging .timing import timed
11
14
from posthog .plugins .site import get_site_config_from_schema , get_transpiled_site_source
12
15
@@ -22,7 +25,21 @@ def get_site_app(request: HttpRequest, id: int, token: str, hash: str) -> HttpRe
22
25
id = source_file .id
23
26
source = source_file .source
24
27
config = get_site_config_from_schema (source_file .config_schema , source_file .config )
25
- response = f"{ source } ().inject({{config:{ json .dumps (config )} ,posthog:window['__$$ph_site_app_{ id } ']}})"
28
+
29
+ response = f"globalConfig = { json .dumps (config )} ;"
30
+ response += "function get_global(key) {return globalConfig[key]}"
31
+
32
+ extra = ""
33
+ for key , value in config .items ():
34
+ if isinstance (value , str ) and "{" in value :
35
+ stmt = ast .ReturnStatement (expr = parse_string_template (value ))
36
+ code = create_javascript (
37
+ ast .Function (name = f"__get_config_{ key } " , body = ast .Block (declarations = [stmt ]), params = ["event" ])
38
+ ).code
39
+ extra += code + "\n "
40
+ response += extra
41
+
42
+ response += f"{ source } ().inject({{config:globalConfig,posthog:window['__$$ph_site_app_{ id } ']}});"
26
43
27
44
statsd .incr (f"posthog_cloud_raw_endpoint_success" , tags = {"endpoint" : "site_app" })
28
45
return HttpResponse (content = response , content_type = "application/javascript" )
0 commit comments