Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
refactor(website): use es modules in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nlm-pro committed Oct 2, 2021
1 parent 34afaa0 commit bef17b9
Show file tree
Hide file tree
Showing 7 changed files with 2,057 additions and 725 deletions.
1,527 changes: 1,138 additions & 389 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -94,6 +94,7 @@
"@commitlint/prompt-cli": "^11.0.0",
"@daucus/pandoc": "./packages/daucus/pandoc/",
"@lhci/cli": "~0.8.1",
"@modern-helpers/router": "*",
"@monocli/cli": "^0.5.0",
"@open-wc/eslint-config": "^4.2.0",
"@open-wc/testing": "^2.5.32",
Expand All @@ -112,7 +113,7 @@
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-compat": "^3.9.0",
"eslint-plugin-vue": "^7.6.0",
"firebase-tools": "^9.10.0",
"firebase-tools": "^9.19.0",
"http-server": "^0.12.3",
"husky": "^4.3.8",
"lerna": "^3.22.1",
Expand All @@ -133,8 +134,7 @@
"typedoc-plugin-markdown": "^3.7.1",
"typescript": "^4.3.0-beta",
"typescript-json-schema": "^0.50.0",
"web-component-analyzer": "^1.1.6",
"@modern-helpers/router": "*"
"web-component-analyzer": "^1.1.6"
},
"husky": {
"hooks": {
Expand Down
88 changes: 46 additions & 42 deletions website/functions/hosting/index.js
@@ -1,8 +1,10 @@
// eslint-disable-next-line import/no-unresolved
const functions = require("firebase-functions");
const fs = require("fs");
const path = require("path");
const { routes, dirs } = require("./routes");
import functions from "firebase-functions";
import * as fs from "fs";
import * as path from "path";
import { fileURLToPath } from "url";
import { routes, dirs } from "./routes.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const INDEX_TEMPLATE = fs
.readFileSync(path.resolve(__dirname, "index.html"))
Expand Down Expand Up @@ -55,46 +57,48 @@ const DEFAULT_METAS = {
},
};

exports.httpRequestHandler = functions.https.onRequest((request, result) => {
let reqPath = request.path;
let lang = "fr";
const langPathMatch = /^\/(en|fr)(\/.*)/.exec(reqPath);
if (langPathMatch) {
[, lang, reqPath] = langPathMatch;
}
let route = routes[reqPath];
export const httpRequestHandler = functions.https.onRequest(
(request, result) => {
let reqPath = request.path;
let lang = "fr";
const langPathMatch = /^\/(en|fr)(\/.*)/.exec(reqPath);
if (langPathMatch) {
[, lang, reqPath] = langPathMatch;
}
let route = routes[reqPath];

if (!route) {
const found = dirs.find(
([key, dirRoute]) => reqPath.startsWith(key) && dirRoute
) || ["", {}];
[, route] = found;
if (route[lang]) {
if (route.default) {
route = {
...route.default,
...route[lang],
};
} else {
route = route[lang];
if (!route) {
const found = dirs.find(
([key, dirRoute]) => reqPath.startsWith(key) && dirRoute
) || ["", {}];
[, route] = found;
if (route[lang]) {
if (route.default) {
route = {
...route.default,
...route[lang],
};
} else {
route = route[lang];
}
}
route = {
...DEFAULT_METAS[lang],
...route,
};
}
route = {
...DEFAULT_METAS[lang],
...route,
};
}

let indexHTML = INDEX_TEMPLATE;
let indexHTML = INDEX_TEMPLATE;

if (isBot(request.headers["user-agent"]) && route) {
indexHTML = indexHTML.replace(
/<!-- meta-tags:start -->[\s\S]*<!-- meta-tags:end -->/,
metasForRoute(route)
);
}
if (isBot(request.headers["user-agent"]) && route) {
indexHTML = indexHTML.replace(
/<!-- meta-tags:start -->[\s\S]*<!-- meta-tags:end -->/,
metasForRoute(route)
);
}

// caching
// res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
result.status(200).send(indexHTML);
});
// caching
// res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
result.status(200).send(indexHTML);
}
);
4 changes: 2 additions & 2 deletions website/functions/hosting/routes.js
@@ -1,4 +1,4 @@
exports.routes = {
export const routes = {
"/blog/qui-a-invente-hypertexte": {
"og:type": "article",
"og:title": "Qui a inventé les ordis et l'hypertexte ?",
Expand Down Expand Up @@ -68,7 +68,7 @@ exports.routes = {
},
};

exports.dirs = [
export const dirs = [
[
"/daucus",
{
Expand Down
4 changes: 2 additions & 2 deletions website/functions/index.js
@@ -1,3 +1,3 @@
const hosting = require("./hosting");
import { httpRequestHandler } from "./hosting/index.js";

exports.httpRequestHandler = hosting.httpRequestHandler;
export { httpRequestHandler };

0 comments on commit bef17b9

Please sign in to comment.