From 16ae1abbc3b99879b687dd20c15fae7e05c03335 Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Wed, 20 Dec 2023 11:21:59 +0100 Subject: [PATCH 01/10] Development: use `wrangler` locally (update NGINX/Dockerfile config) Related https://github.com/readthedocs/addons/issues/217 --- common | 2 +- dockerfiles/Dockerfile.wrangler | 2 ++ dockerfiles/nginx/proxito.conf.template | 13 ++----------- dockerfiles/nginx/wrangler.conf.template | 20 ++++++++++++++++++++ 4 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 dockerfiles/Dockerfile.wrangler create mode 100644 dockerfiles/nginx/wrangler.conf.template diff --git a/common b/common index 6093983195e..e598102991c 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 6093983195ea4699169e7652635e437e603a93c1 +Subproject commit e598102991c5a927024f06f187f51f7000e67023 diff --git a/dockerfiles/Dockerfile.wrangler b/dockerfiles/Dockerfile.wrangler new file mode 100644 index 00000000000..19c62a2d94b --- /dev/null +++ b/dockerfiles/Dockerfile.wrangler @@ -0,0 +1,2 @@ +FROM node:18.15 +RUN npm install -g wrangler@3.21.0 diff --git a/dockerfiles/nginx/proxito.conf.template b/dockerfiles/nginx/proxito.conf.template index 8b5699d1a2b..f1d950c7710 100644 --- a/dockerfiles/nginx/proxito.conf.template +++ b/dockerfiles/nginx/proxito.conf.template @@ -1,7 +1,7 @@ # Proxito server { - listen 80 default_server; - server_name $NGINX_PROXITO_SERVER_NAME; + listen 8080; + server_name nginx; # Docker Compose's "logging.driver: none" is not working anymore. # So, we are disabling the logs from NGINX directly. @@ -23,10 +23,8 @@ server { # Proxito doc serving location / { proxy_pass http://proxito:8000; - proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Host $host; proxy_intercept_errors on; error_page 404 = @notfoundfallback; @@ -110,13 +108,6 @@ server { add_header X-RTD-Hosting-Integrations $rtd_hosting_integrations always; set $rtd_force_addons $upstream_http_x_rtd_force_addons; add_header X-RTD-Force-Addons $rtd_force_addons always; - - # Inject our own script dynamically and project/version slugs into the HTML to emulate what CF worker does - # TODO: find a way to make this work _without_ running `npm run dev` from the `addons` repository - sub_filter '' '\n\n\n'; - sub_filter_types text/html; - sub_filter_last_modified on; - sub_filter_once on; } # Serve 404 pages here diff --git a/dockerfiles/nginx/wrangler.conf.template b/dockerfiles/nginx/wrangler.conf.template new file mode 100644 index 00000000000..b99d1faef1d --- /dev/null +++ b/dockerfiles/nginx/wrangler.conf.template @@ -0,0 +1,20 @@ +# Proxito +server { + listen 80 default_server; + server_name $NGINX_PROXITO_SERVER_NAME; + + # Docker Compose's "logging.driver: none" is not working anymore. + # So, we are disabling the logs from NGINX directly. + access_log off; + + # Wrangler serving + location / { + proxy_pass http://wrangler:8000; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Host $host; + + add_header X-Served CF-Wrangler always; + } +} From 802c76ca4b24d06250c911e66df9cd8610331270 Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Tue, 6 Feb 2024 13:15:03 +0100 Subject: [PATCH 02/10] Add CF Worker .js script --- dockerfiles/force-readthedocs-addons.js | 219 ++++++++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 dockerfiles/force-readthedocs-addons.js diff --git a/dockerfiles/force-readthedocs-addons.js b/dockerfiles/force-readthedocs-addons.js new file mode 100644 index 00000000000..161ff58c494 --- /dev/null +++ b/dockerfiles/force-readthedocs-addons.js @@ -0,0 +1,219 @@ +/* + + Script to inject the new Addons implementation on pages served by El Proxito. + + This script is ran on a Cloudflare Worker and modifies the HTML with two different purposes: + + 1. remove the old implementation of our flyout (``readthedocs-doc-embed.js`` and others) + 2. inject the new addons implementation (``readthedocs-addons.js``) script + + Currently, we are doing 1) only when users opt-in into the new beta addons. + In the future, when our addons become stable, we will always remove the old implementation, + making all the projects to use the addons by default. + +*/ + +// add "readthedocs-addons.js" inside the "" +const addonsJs = + ''; + +// selectors we want to remove +// https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/#selectors +const analyticsJs = + 'script[src="/_/static/javascript/readthedocs-analytics.js"]'; +const docEmbedCss = 'link[href="/_/static/css/readthedocs-doc-embed.css"]'; +const docEmbedJs = + 'script[src="/_/static/javascript/readthedocs-doc-embed.js"]'; +const analyticsJsAssets = + 'script[src="https://assets.readthedocs.org/static/javascript/readthedocs-analytics.js"]'; +const docEmbedCssAssets = + 'link[href="https://assets.readthedocs.org/static/css/readthedocs-doc-embed.css"]'; +const docEmbedJsAssets = + 'script[src="https://assets.readthedocs.org/static/javascript/readthedocs-doc-embed.js"]'; +const docEmbedJsAssetsCore = + 'script[src="https://assets.readthedocs.org/static/core/js/readthedocs-doc-embed.js"]'; +const badgeOnlyCssAssets = + 'link[href="https://assets.readthedocs.org/static/css/badge_only.css"]'; +const badgeOnlyCssAssetsProxied = 'link[href="/_/static/css/badge_only.css"]'; +const readthedocsExternalVersionWarning = "[role=main] > div:first-child > div:first-child.admonition.warning"; +const readthedocsFlyout = "div.rst-versions"; + +// "readthedocsDataParse" is the "