Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Initialize maintenance mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sshmaxime committed Sep 21, 2023
1 parent 759e2f7 commit 82acfd1
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@types/node": "20.6.0",
"@types/react": "18.2.21",
"@types/react-dom": "18.2.7",
"@vercel/edge-config": "^0.4.1",
"eslint": "8.49.0",
"eslint-config-next": "13.4.19",
"next": "^13.5.2",
Expand Down
3 changes: 3 additions & 0 deletions src/app/[locale]/maintenance/page.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.main {
min-height: 100vh;
}
18 changes: 18 additions & 0 deletions src/app/[locale]/maintenance/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use client";

import { useI18n } from "@/i18n/client";
import styles from "./page.module.css";

export default function Home() {
const { t } = useI18n();

return (
<main className={styles.main}>
<div>{t("welcome")}</div>
<br />
<br />
<br />
<div>Maintenance page</div>
</main>
);
}
5 changes: 3 additions & 2 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { chain } from "@/middleware/utils";
import { withI18n } from "@/middleware/index";
import { withI18n } from "@/middleware/withI18n";
import { withMaintenance } from "@/middleware/withMaintenance";

const middlewares = [withI18n];
const middlewares = [withI18n, withMaintenance];

export default chain(middlewares);

Expand Down
5 changes: 0 additions & 5 deletions src/middleware/index.ts

This file was deleted.

14 changes: 11 additions & 3 deletions src/middleware/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// middlewares/stackMiddlewares
import { LiveAppConfig, MiddlewareFactory } from "@/middleware/utils/types";
import { get } from "@vercel/edge-config";
import { NextMiddleware, NextResponse } from "next/server";

export type MiddlewareFactory = (middleware: NextMiddleware) => NextMiddleware;
import PackageJson from "~/package.json";

/**
* @dev Utils function in order to chain middlewares.
* Utils function in order to chain middlewares.
*/
export function chain(functions: MiddlewareFactory[] = [], index = 0): NextMiddleware {
const current = functions[index];
Expand All @@ -14,3 +15,10 @@ export function chain(functions: MiddlewareFactory[] = [], index = 0): NextMiddl
}
return () => NextResponse.next();
}

/**
* Get live app config from the Edge Config.
*/
export const getAppConfig = async (): Promise<LiveAppConfig | undefined> => {
return await get(PackageJson.name);
};
13 changes: 13 additions & 0 deletions src/middleware/utils/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NextMiddleware } from "next/server";

/**
* Middleware util types.
*/
export type MiddlewareFactory = (middleware: NextMiddleware) => NextMiddleware;

/**
* Edge Config util types.
*/
export type LiveAppConfig = {
enabled: boolean;
};
2 changes: 1 addition & 1 deletion src/middleware/withI18n.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// middlewares/withHeaders.ts
import { NextFetchEvent, NextMiddleware, NextRequest } from "next/server";
import { I18nMiddleware } from "@/i18n/middleware";
import { MiddlewareFactory } from "@/middleware/utils";
import { MiddlewareFactory } from "@/middleware/utils/types";

export const withI18n: MiddlewareFactory = (next: NextMiddleware) => {
return async (request: NextRequest, _next: NextFetchEvent) => {
Expand Down
17 changes: 17 additions & 0 deletions src/middleware/withMaintenance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NextFetchEvent, NextMiddleware, NextRequest, NextResponse } from "next/server";
import { MiddlewareFactory } from "@/middleware/utils/types";
import { getAppConfig } from "@/middleware/utils";

export const withMaintenance: MiddlewareFactory = (next: NextMiddleware) => {
return async (request: NextRequest, _next: NextFetchEvent) => {
await next(request, _next);

const appConfig = await getAppConfig();

if (appConfig && !appConfig.enabled) {
request.nextUrl.pathname = `/maintenance`;

return NextResponse.rewrite(request.nextUrl);
}
};
};
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
"@/middleware/*": ["./src/middleware/*"],
"@/styles/*": ["./src/styles/*"],
"@/redux/*": ["./src/redux/*"],
"@/hooks/*": ["./src/hooks/*"],
"@/tools-test/*": ["./tools/test/*"],
"@/hooks/*": ["./src/hooks/*"]
"~/*": ["./*"]
}
},
"include": [
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3383,6 +3383,18 @@
"@typescript-eslint/types" "6.7.2"
eslint-visitor-keys "^3.4.1"

"@vercel/edge-config-fs@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@vercel/edge-config-fs/-/edge-config-fs-0.1.0.tgz#cda8327f611418c1d1cbb28c6bed02d782be928b"
integrity sha512-NRIBwfcS0bUoUbRWlNGetqjvLSwgYH/BqKqDN7vK1g32p7dN96k0712COgaz6VFizAm9b0g6IG6hR6+hc0KCPg==

"@vercel/edge-config@^0.4.1":
version "0.4.1"
resolved "https://registry.yarnpkg.com/@vercel/edge-config/-/edge-config-0.4.1.tgz#c247011dcd05ce6a6b4d17139215c8f684d83cb3"
integrity sha512-4Mc3H7lE+x4RrL17nY8CWeEorvJHbkNbQTy9p8H1tO7y11WeKj5xeZSr07wNgfWInKXDUwj5FZ3qd/jIzjPxug==
dependencies:
"@vercel/edge-config-fs" "0.1.0"

"@xmldom/xmldom@^0.8.3":
version "0.8.10"
resolved "https://registry.yarnpkg.com/@xmldom/xmldom/-/xmldom-0.8.10.tgz#a1337ca426aa61cef9fe15b5b28e340a72f6fa99"
Expand Down

0 comments on commit 82acfd1

Please sign in to comment.