Skip to content

Commit

Permalink
support pagefind test on test environments
Browse files Browse the repository at this point in the history
  • Loading branch information
argl committed Jan 15, 2025
1 parent 0c190a0 commit 25fd7ec
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
26 changes: 24 additions & 2 deletions cloud-function/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { Router } from "express";
import { ANY_ATTACHMENT_EXT } from "./internal/constants/index.js";

import { Origin } from "./env.js";
import { proxyContent } from "./handlers/proxy-content.js";
import {
proxyContent,
proxyContentPagefind,
} from "./handlers/proxy-content.js";
import { proxyApi } from "./handlers/proxy-api.js";
import { handleStripePlans } from "./handlers/handle-stripe-plans.js";
import { proxyTelemetry } from "./handlers/proxy-telemetry.js";
Expand All @@ -24,7 +27,10 @@ import { notFound } from "./middlewares/not-found.js";
import { stripForwardedHostHeaders } from "./middlewares/stripForwardedHostHeaders.js";
import { proxyPong } from "./handlers/proxy-pong.js";
import { handleRunner } from "./internal/play/index.js";
import { proxyContentAssets } from "./handlers/proxy-content-assets.js";
import {
proxyContentAssets,
proxyContentAssetsPagefind,
} from "./handlers/proxy-content-assets.js";

const router = Router();
router.use(cookieParser());
Expand Down Expand Up @@ -101,6 +107,22 @@ router.get(
resolveIndexHTML,
proxyContent
);

// pagefind test
router.get(
["/[^/]+/pagefind/$"],
requireOrigin(Origin.main),
redirectLocale,
redirectEnforceTrailingSlash,
resolveIndexHTML,
proxyContentPagefind
);
router.get(
["/[^/]+/pagefind/*"],
lowercasePathname,
proxyContentAssetsPagefind
);

// MDN Plus, static pages, etc.
router.get(
"*",
Expand Down
37 changes: 37 additions & 0 deletions cloud-function/src/handlers/proxy-content-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,40 @@ export const proxyContentAssets = createProxyMiddleware({
),
},
});

export const proxyContentAssetsPagefind = createProxyMiddleware({
target,
changeOrigin: true,
autoRewrite: true,
proxyTimeout: PROXY_TIMEOUT,
xfwd: true,
selfHandleResponse: true,
on: {
proxyReq: fixRequestBody,
proxyRes: responseInterceptor(
async (responseBuffer, proxyRes, req, res) => {
// withContentResponseHeaders(proxyRes, req, res);
const [, locale] = req.url?.split("/") || [];
if (
proxyRes.statusCode === 404 &&
locale &&
locale != "en-US" &&
ACTIVE_LOCALES.has(locale.toLowerCase())
) {
const enUsAsset = await fetch(
`${target}${req.url?.slice(1).replace(locale, "en-us")}`
);
if (enUsAsset?.ok) {
res.statusCode = enUsAsset.status;
enUsAsset.headers.forEach((value, key) =>
res.setHeader(key, value)
);
return Buffer.from(await enUsAsset.arrayBuffer());
}
}

return responseBuffer;
}
),
},
});
36 changes: 36 additions & 0 deletions cloud-function/src/handlers/proxy-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const proxyContent = createProxyMiddleware({
proxyTimeout: PROXY_TIMEOUT,
xfwd: true,
selfHandleResponse: true,
logger: console,
on: {
proxyReq: fixRequestBody,
proxyRes: responseInterceptor(
Expand All @@ -49,3 +50,38 @@ export const proxyContent = createProxyMiddleware({
),
},
});

export const proxyContentPagefind = createProxyMiddleware({
target,
changeOrigin: true,
autoRewrite: true,
proxyTimeout: PROXY_TIMEOUT,
xfwd: true,
selfHandleResponse: true,
logger: console,
on: {
proxyReq: fixRequestBody,
proxyRes: responseInterceptor(
async (responseBuffer, proxyRes, req, res) => {
// withContentResponseHeaders(proxyRes, req, res);
if (proxyRes.statusCode === 404 && !isLiveSampleURL(req.url ?? "")) {
const tryHtml = await fetch(
`${target}${req.url?.slice(1)}/index.html`
);
if (tryHtml.ok) {
res.statusCode = 200;
res.setHeader("Content-Type", "text/html");
return Buffer.from(await tryHtml.arrayBuffer());
} else if (!notFoundBuffer) {
const response = await fetch(`${target}${NOT_FOUND_PATH}`);
notFoundBuffer = await response.arrayBuffer();
}
res.setHeader("Content-Type", "text/html");
return Buffer.from(notFoundBuffer);
}

return responseBuffer;
}
),
},
});

0 comments on commit 25fd7ec

Please sign in to comment.