Skip to content
This repository has been archived by the owner on Jul 13, 2022. It is now read-only.

Commit

Permalink
add current build id to footer
Browse files Browse the repository at this point in the history
  • Loading branch information
kymppi committed Mar 22, 2022
1 parent 09c1025 commit e5ea93d
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 4 deletions.
Binary file not shown.
Binary file modified .yarn/install-state.gz
Binary file not shown.
6 changes: 3 additions & 3 deletions components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Link from 'next/link';
import { ReactNode, useState } from 'react';
import { Calendar, File, Home } from 'react-feather';
import { Messages } from 'tabler-icons-react';
import useBuildId from '../hooks/useBuildId';
import LayoutLink from './LayoutLink';
import Logo from './Logo';
import ThemeToggle from './ThemeToggle';
Expand All @@ -35,6 +36,7 @@ const links = [
export default function Layout({ children }: { children: ReactNode }) {
const [opened, setOpened] = useState(false);
const theme = useMantineTheme();
const buildId = useBuildId();

return (
<AppShell
Expand All @@ -61,9 +63,7 @@ export default function Layout({ children }: { children: ReactNode }) {
<LayoutLink key={ele.label} {...ele} />
))}
</Navbar.Section>
<Navbar.Section>
Build `insert commit id and branch here`
</Navbar.Section>
<Navbar.Section>Build {buildId}</Navbar.Section>
</Navbar>
}
header={
Expand Down
17 changes: 17 additions & 0 deletions components/NewUpdateAvailible.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React, { useEffect } from 'react';
import useBuildId from '../hooks/useBuildId';

function NewUpdateAvailible() {
const buildId = useBuildId();

useEffect(() => {
if (buildId && process.env.BUILD_ID && buildId !== process.env.BUILD_ID) {
// There's a new version deployed that we need to load
// SHOW MODAL
}
}, [buildId]);

return <div>NewUpdateAvailible</div>;
}

export default NewUpdateAvailible;
26 changes: 26 additions & 0 deletions hooks/useBuildId.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useState } from 'react';
import useInterval from './useInterval';

function useBuildId() {
let [buildId, setBuildId] = useState('unknown');

useInterval(async () => {
let id = await fetchId();

setBuildId(id);
}, 30000); // 30 seconds

return buildId;
}

async function fetchId() {
let response = await fetch('/api/build-id');

if (!response.ok) return;

let data = await response.json();

return data.buildId;
}

export default useBuildId;
30 changes: 30 additions & 0 deletions hooks/useInterval.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useRef, useEffect } from 'react';
import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect';

function useInterval(callback: () => void, delay: number | null) {
const savedCallback = useRef(callback);

// Remember the latest callback if it changes.

useIsomorphicLayoutEffect(() => {
savedCallback.current = callback;
}, [callback]);

// Set up the interval.

useEffect(() => {
// Don't schedule if no delay is specified.

// Note: 0 is a valid value for delay.

if (!delay && delay !== 0) {
return;
}

const id = setInterval(() => savedCallback.current(), delay);

return () => clearInterval(id);
}, [delay]);
}

export default useInterval;
6 changes: 6 additions & 0 deletions hooks/useIsomorphicLayoutEffect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useEffect, useLayoutEffect } from 'react';

const useIsomorphicLayoutEffect =
typeof window !== 'undefined' ? useLayoutEffect : useEffect;

export default useIsomorphicLayoutEffect;
12 changes: 11 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
/** @type {import('next').NextConfig} */
const nextBuildId = require('next-build-id');

module.exports = {
generateBuildId: () => nextBuildId({ dir: __dirname, describe: true }),
reactStrictMode: true,
images: {
domains: ['images.unsplash.com'],
},

webpack: (config) => {
webpack: (config, { buildId, webpack }) => {
config.module.rules.push({
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
use: ['@svgr/webpack'],
});

config.plugins.push(
new webpack.DefinePlugin({
'process.env': {
BUILD_ID: JSON.stringify(buildId),
},
})
);

return config;
},
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dayjs": "^1.10.7",
"graphql": "^16.2.0",
"next": "^12.1.0",
"next-build-id": "^3.0.0",
"postcss": "^8.4.5",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
8 changes: 8 additions & 0 deletions pages/api/build-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NextApiRequest, NextApiResponse } from 'next';

// eslint-disable-next-line import/no-anonymous-default-export
export default (_req: NextApiRequest, res: NextApiResponse): void => {
res.status(200).json({
buildId: process.env.BUILD_ID,
});
};
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3769,6 +3769,7 @@ __metadata:
eslint-config-next: ^12.0.8
graphql: ^16.2.0
next: ^12.1.0
next-build-id: ^3.0.0
postcss: ^8.4.5
react: ^17.0.2
react-dom: ^17.0.2
Expand Down Expand Up @@ -4876,6 +4877,13 @@ fsevents@~2.3.2:
languageName: node
linkType: hard

"next-build-id@npm:^3.0.0":
version: 3.0.0
resolution: "next-build-id@npm:3.0.0"
checksum: e36e15979420c343e32a5e7a55efc1300a4b12aea1637df8300f0085ca862bf78e4fd3ac969ce7c7757cfad40c3c77e02ae4859be77692c9bdcb4243cbabb810
languageName: node
linkType: hard

"next@npm:^12.1.0":
version: 12.1.0
resolution: "next@npm:12.1.0"
Expand Down

0 comments on commit e5ea93d

Please sign in to comment.