Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cloud-function/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { proxyPong } from "./handlers/proxy-pong.js";
import { handleRunner } from "./internal/play/index.js";
import { proxyContentAssets } from "./handlers/proxy-content-assets.js";
import { proxySharedAssets } from "./handlers/proxy-shared-assets.js";
import { resolveFaviconRobots } from "./middlewares/resolve-favicon-robots.js";

const router = Router();
router.use(cookieParser());
Expand Down Expand Up @@ -59,6 +60,7 @@ router.get("/shared-assets/*", requireOrigin(Origin.play), proxySharedAssets);
router.get(
["/assets/*", "/sitemaps/*", "/static/*", "/[^/]+.[^/]+"],
requireOrigin(Origin.main),
resolveFaviconRobots,
proxyContent
);
router.get(
Expand Down
32 changes: 32 additions & 0 deletions cloud-function/src/middlewares/resolve-favicon-robots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NextFunction, Request, Response } from "express";

const MAPPING: Record<string, string> = {
"/static/favicon.ico": "/favicon.ico",
"/static/robots.txt": "/robots.txt",
};

/**
* Workaround for Google Cloud Function always returning HTTP 404 for favicon.ico/robots.txt.
*
* See: https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/5679093ecb850ceece3af8b313c0d8aa04870635/src/server.ts#L148-L153
*
* From our load balancer, we point to `/static/{favicon.ico,robots.txt}` instead.
*/
export async function resolveFaviconRobots(
req: Request,
_res: Response,
next: NextFunction
) {
const { pathname } = new URL(
req.url,
`${req.protocol}://${req.headers.host}`
);
console.log({ pathname });
if (typeof MAPPING[pathname] === "string") {
req.url = MAPPING[pathname];
// Workaround for http-proxy-middleware v2 using `req.originalUrl`.
// See: https://github.com/chimurai/http-proxy-middleware/pull/731
req.originalUrl = req.url;
}
next();
}