client: og:site_name served via lua content script

This commit is contained in:
Ben Klein 2021-07-09 23:54:49 -04:00
parent b2800ad779
commit 44d28f3f63
4 changed files with 39 additions and 18 deletions

View file

@ -18,6 +18,7 @@ COPY docker-start.sh /
WORKDIR /etc/nginx WORKDIR /etc/nginx
COPY nginx.conf.docker ./nginx.conf COPY nginx.conf.docker ./nginx.conf
COPY metatags.lua .
WORKDIR /var/www WORKDIR /var/www
COPY --from=builder /opt/app/public/ . COPY --from=builder /opt/app/public/ .
@ -26,7 +27,8 @@ COPY --from=builder /opt/app/public/ .
FROM openresty/openresty:alpine-fat as release FROM openresty/openresty:alpine-fat as release
RUN apk --no-cache add dumb-init RUN apk --no-cache add dumb-init
RUN /usr/local/openresty/luajit/bin/luarocks install gumbo RUN opm get bungle/lua-resty-template
# RUN /usr/local/openresty/luajit/bin/luarocks install gumbo
COPY --from=approot / / COPY --from=approot / /
CMD ["/docker-start.sh"] CMD ["/docker-start.sh"]

View file

@ -22,6 +22,7 @@
<link rel='apple-touch-startup-image' href='img/apple-touch-startup-image-1668x2224.png' media='(min-device-width: 834px) and (max-device-width: 834px) and (-webkit-min-device-pixel-ratio: 2) and (orientation: portrait)'/> <link rel='apple-touch-startup-image' href='img/apple-touch-startup-image-1668x2224.png' media='(min-device-width: 834px) and (max-device-width: 834px) and (-webkit-min-device-pixel-ratio: 2) and (orientation: portrait)'/>
<link rel='apple-touch-startup-image' href='img/apple-touch-startup-image-2048x2732.png' media='(min-device-width: 1024px) and (max-device-width: 1024px) and (-webkit-min-device-pixel-ratio: 2) and (orientation: portrait)'/> <link rel='apple-touch-startup-image' href='img/apple-touch-startup-image-2048x2732.png' media='(min-device-width: 1024px) and (max-device-width: 1024px) and (-webkit-min-device-pixel-ratio: 2) and (orientation: portrait)'/>
<link rel='manifest' href='manifest.json'/> <link rel='manifest' href='manifest.json'/>
{{ generated_head_tags }}
</head> </head>
<body> <body>
<div id='top-navigation-holder'></div> <div id='top-navigation-holder'></div>

32
client/metatags.lua Normal file
View file

@ -0,0 +1,32 @@
ngx.req.read_body()
local page_html = ngx.location.capture("/index.htm")
ngx.req.set_header("Accept", "application/json")
local server_info = cjson.decode((ngx.location.capture("/_internal_api/info")).body)
-- local document = gumbo.parse(page_html.body)
--
-- function add_meta_tag (property, content)
-- local new_element = document:createElement("meta")
-- document.head:appendChild(new_element)
-- new_element:setAttribute("property", property)
-- new_element:setAttribute("content", content)
-- end
local additional_tags = ""
local function add_meta_tag (property, content)
additional_tags = additional_tags .. "<meta property=\"" .. property .. "\" content=\"" .. content:gsub('"', '\\"') .. "\" />"
end
-- Add the site name tag
add_meta_tag("og:site_name", server_info.config.name)
local final_response = page_html.body:gsub("{{ generated_head_tags }}", additional_tags)
-- Set the content type back to HTML
ngx.header.content_type = 'text/html';
-- ngx.say(page_html.body)
ngx.say(final_response)

View file

@ -27,7 +27,7 @@ http {
init_by_lua_block { init_by_lua_block {
cjson = require("cjson") cjson = require("cjson")
gumbo = require("gumbo") -- gumbo = require("gumbo")
} }
server { server {
@ -81,7 +81,7 @@ http {
location / { location / {
root /var/www; root /var/www;
try_files $uri /index.htm; try_files $uri /_og_tags_html;
sendfile on; sendfile on;
tcp_nopush on; tcp_nopush on;
@ -104,23 +104,9 @@ http {
proxy_pass http://backend/$1; proxy_pass http://backend/$1;
} }
location ~ ^/_internal_tag_generator/(.*)$ {
content_by_lua_block {
ngx.say("")
}
}
location /_og_tags_html { location /_og_tags_html {
root /var/www; root /var/www;
content_by_lua_block { content_by_lua_file /etc/nginx/metatags.lua;
ngx.req.read_body()
local page_html = ngx.location.capture("/index.htm")
ngx.say(page_html.body)
ngx.req.set_header("Accept", "application/json")
local server_info = cjson.decode((ngx.location.capture("/_internal_api/info")).body)
ngx.say(server_info.config.name)
}
} }
location @unauthorized { location @unauthorized {