Skip to content

Commit

Permalink
Merge pull request #170 from supportingami/fix/cloud-functions
Browse files Browse the repository at this point in the history
Fix/cloud functions
  • Loading branch information
chrismclarke authored Sep 24, 2024
2 parents 89da7a5 + 90d7878 commit 743d062
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 2,493 deletions.
7 changes: 6 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@

**Functions**

- [ ] ...
- [x] Upgrade to v2 SEOHost
- [ ] Used pinned tags to [colocate hosting and function assets](https://firebase.google.com/docs/hosting/functions#direct-requests-to-function)
- [ ] Consider using predeploy to ensure index.html copied over
- [ ] Update source files uploaded to contain bare minimum
https://console.cloud.google.com/run/detail/europe-west1/seohost2/source
- [ ] Fix web deployment action

**Docs**

Expand Down
14 changes: 7 additions & 7 deletions maths-club-app/firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
"rewrites": [
{
"source": "**",
"function": "seoHost2"
"function": {
"functionId": "seoHost2",
"pinTag": true
}
}
]
},
"functions": {
"predeploy": [
"yarn --cwd \"$RESOURCE_DIR\" install",
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
],
"source": "functions"
"predeploy": ["npm --prefix \"$RESOURCE_DIR\" run lint"],
"source": "functions",
"runtime": "nodejs20"
}
}
14 changes: 7 additions & 7 deletions maths-club-app/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
"logs": "firebase functions:log"
},
"engines": {
"node": "12"
"node": "20"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.11.0",
"jsdom": "^16.4.0"
"firebase-admin": "^12.5.0",
"firebase-functions": "^6.0.1",
"jsdom": "^25.0.1"
},
"devDependencies": {
"@types/jsdom": "^16.2.4",
"firebase-functions-test": "^0.2.2",
"@types/jsdom": "^21.1.7",
"firebase-functions-test": "^3.3.0",
"ts-node": "^9.0.0",
"tslint": "^6.1.3",
"typescript": "^4.0.3"
"typescript": "~5.4.5"
},
"private": true
}
11 changes: 10 additions & 1 deletion maths-club-app/functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import { setGlobalOptions } from "firebase-functions/options";
import { seoHost } from "./seoHost";
import { onRequest } from "firebase-functions/https";

exports.seoHost2 = seoHost;
setGlobalOptions({ region: "europe-west1" });

// NOTE - will need to allow unauthenticated invocation
// https://cloud.google.com/functions/docs/securing/managing-access-iam#allowing_unauthenticated_http_function_invocation
exports.seoHost2 = onRequest((req, res) => {
const indexHtml = seoHost(req);
res.status(200).send(indexHtml);
});
9 changes: 5 additions & 4 deletions maths-club-app/functions/src/seoHost.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as functions from "firebase-functions";
import type { Request } from "firebase-functions/https";

import * as fs from "fs";
import { JSDOM } from "jsdom";

Expand All @@ -18,7 +19,7 @@ import { JSDOM } from "jsdom";
* Workarounds attempted with http get requests but messy
* */

export const seoHost = functions.https.onRequest(async (req, res) => {
export const seoHost = (req: Request) => {
const userAgent = req.headers["user-agent"];
const baseURL = `${req.protocol}://${req.hostname}`;
let indexHTML = fs.readFileSync("assets/index.html").toString();
Expand All @@ -33,8 +34,8 @@ export const seoHost = functions.https.onRequest(async (req, res) => {
} else {
console.log("is-not-bot", userAgent);
}
res.status(200).send(indexHTML);
});
return indexHTML;
};

const updateProblemTags = (
slug: string,
Expand Down
7 changes: 3 additions & 4 deletions maths-club-app/functions/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017"
"target": "es2017",
"skipLibCheck": true
},
"compileOnSave": true,
"include": [
"src"
]
"include": ["src"]
}
Loading

0 comments on commit 743d062

Please sign in to comment.