Skip to content

Commit 40c35a7

Browse files
committed
it actually works
1 parent 4c36deb commit 40c35a7

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

posthog/api/site_app.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from statshog.defaults.django import statsd
88

99
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
1013
from posthog.logging.timing import timed
1114
from posthog.plugins.site import get_site_config_from_schema, get_transpiled_site_source
1215

@@ -22,7 +25,21 @@ def get_site_app(request: HttpRequest, id: int, token: str, hash: str) -> HttpRe
2225
id = source_file.id
2326
source = source_file.source
2427
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}']}});"
2643

2744
statsd.incr(f"posthog_cloud_raw_endpoint_success", tags={"endpoint": "site_app"})
2845
return HttpResponse(content=response, content_type="application/javascript")

0 commit comments

Comments
 (0)