This repository is the source code for the web worker that powers the AMP project's CDN server. This web worker runs on Cloudflare Workers.
A detailed explanation of the inner workings of this server is available in its original design document.
- The AMP meta repository has information about the AMP open source project, including AMP's governance.
- The amphtml repository contains the source code for the AMP runtime itself and its extensions.
- AMP's code of conduct documents how all members, committers and volunteers in the community are required to act. AMP strives for a positive and growing project community that provides a safe environment for everyone.
Pre-requisite: install the Wrangler CLI tool (npm install --global wrangler
) and login (wrangler login
). Ensure you have access to Lamentis@amp.dev's Account with wrangler whoami
.
We are using three (3) different environments (defined in wrangler.toml):
development
: uses separateRTV
andCONFIG
KV namespaces fromstaging
andproduction
staging
: uses the sameRTV
andCONFIG
KV namespaces asproduction
, but does not serve traffic on https://ampjs.org/production
: serves traffic https://ampjs.org/
Make your changes and run wrangler dev --env development
(or staging
, if you really need live RTV
/CONFIG
namespaces).
Note that development
's KV namespaces are not updated automatically, so you will need to manually update the RTV
namespace with an active RTV number for (at the very least) the stable
field.
Deploy in rolling stages, and verify that the changes are stable. Do not skip this. Deployment command is: wrangler publish --env development
(or staging
or production
)
Files that get dynamically modified before serving (/v0.[m]js
and /v0/amp-geo-*.[m]js
) have special handling to maximize their Brotli compression level after they are first requested by each edge node.
When a dynamic file is first requested, it will be served with Cloudflare Worker's default Brotli compression level, then a background thread in the Worker (our code) will compress the file with the maximum compression level and store it in the edge node's cache, to be served to subsequent requests. This process takes <1s.
There are three (3) ways to verify that such files have been compressed successfully after publishing code updates:
-
(easy, but unscientific) open the browser's DevTools, request a dynamic file in the
development
environment and look at the fetch size in the Network tab. Wait 3 seconds, force-refresh the page, and see if the fetched size decreased by more than 0.2kb (which could indicate just network overhead differences) -
(still easy, slightly more scientific but relies on a server implementation detail that might change in the future) inspect the response headers in the DevTools' Network tab. If the response header contains a
content-encoding: br
and has acontent-length
field, the file has been compressed by our code and cached succesfully. This is because currently Cloudflare default Brotli compression does not indicate the content length, but requests that have been pre-cached do -
(hard, but most accurate) in Cloudflare worker dashboard go to
Workers > cdn-worker-development > Logs > Begin log stream
, then request the dynamic file and look for these lines in the log:> Plain response cached
and> Brotli response cached
, orServing <URL> with dynamic key <KEY> from cache
Static files are pre-compressed on the storage backend and do not require any verification.