Skip to content

Commit

Permalink
general: log server load events to tweak vercel bill
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz committed Nov 25, 2024
1 parent f6fa353 commit d5d477e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
44 changes: 43 additions & 1 deletion pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import {
DocumentHeadTagsProps,
} from '@mui/material-nextjs/v13-pagesRouter';
import { getServerIntl } from '../src/services/intlServer';
import { InjectIntl, setIntl } from '../src/services/intl';
import { InjectIntl, Intl, setIntl } from '../src/services/intl';
import { FaviconsOsmapp } from '../src/helpers/FaviconsOsmapp';
import { PROJECT_ID, setProjectForSSR } from '../src/services/project';
import { FaviconsOpenClimbing } from '../src/helpers/FaviconsOpenClimbing';
import { LANGUAGES } from '../src/config.mjs';
import styled from '@emotion/styled';
import { fetchJson } from '../src/services/fetch';

const Body = styled.body`
@media (prefers-color-scheme: light) {
Expand Down Expand Up @@ -94,6 +95,45 @@ type InitialProps = DocumentInitialProps &
asPath: string | undefined;
};

const logRequest = (ctx: DocumentContext, intl: Intl) => {
const WEBSITE_ID = '0b0e0d7c-f27b-429c-b0c4-736b2725a1c1';
const { host, referrer } = ctx.req.headers;

const data = {
type: 'event',
payload: {
hostname: host,
website: WEBSITE_ID,
language: intl.lang,
referrer,
// screen: `${window.screen.width}x${window.screen.height}`,
// title: document.title,
url: ctx.asPath,
name: 'next-server',
},
};
const headers = {
'X-Client-IP': ctx.req.socket.remoteAddress,
'User-Agent': ctx.req.headers['user-agent'],
};

console.log('umami data', JSON.stringify({ data, headers }, null, 2));

// we dont wait to await !
fetchJson('https://cloud.umami.is/api/send', {
nocache: true,
headers,
body: JSON.stringify(data),
method: 'POST',
}).then(
(response) => {
console.log('umami response', response);
},
(error) => {
console.error('umami error', error);
},
);
};
MyDocument.getInitialProps = async (
ctx: DocumentContext,
): Promise<InitialProps> => {
Expand All @@ -104,6 +144,8 @@ MyDocument.getInitialProps = async (

const initialProps = await documentGetInitialProps(ctx);

logRequest(ctx, serverIntl);

return {
...initialProps,
serverIntl,
Expand Down
2 changes: 1 addition & 1 deletion src/services/intl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { publishDbgObject } from '../utils';

type Values = { [variable: string]: string | number };

interface Intl {
export interface Intl {
lang: string;
messages: MessagesType | {};
}
Expand Down
3 changes: 2 additions & 1 deletion src/services/intlServer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import acceptLanguageParser from 'accept-language-parser';
import nextCookies from 'next-cookies';
import { LANGUAGES } from '../config.mjs';
import { Intl } from './intl';

// the files are not imported in main bundle
const getMessages = async (lang) =>
Expand Down Expand Up @@ -33,7 +34,7 @@ const resolveCurrentLang = (ctx) => {
return getLangFromAcceptHeader(ctx) ?? DEFAULT_LANG;
};

export const getServerIntl = async (ctx) => {
export const getServerIntl = async (ctx): Promise<Intl> => {
const lang = resolveCurrentLang(ctx);
const vocabulary = await getMessages('vocabulary');
const messages = lang === DEFAULT_LANG ? {} : await getMessages(lang);
Expand Down

0 comments on commit d5d477e

Please sign in to comment.