ws-nextjs-app/pages/[service]/live/[id]/[[...variant]].page.tsx
import { GetServerSideProps } from 'next';
import { LIVE_PAGE } from '#app/routes/utils/pageTypes';
import nodeLogger from '#lib/logger.node';
import logResponseTime from '#server/utilities/logResponseTime';
import isAppPath from '#app/routes/utils/isAppPath';
import { ROUTING_INFORMATION } from '#app/lib/logger.const';
import { OK } from '#app/lib/statusCodes.const';
import sendCustomMetric from '#server/utilities/customMetrics';
import { NON_200_RESPONSE } from '#server/utilities/customMetrics/metrics.const';
import isLitePath from '#app/routes/utils/isLitePath';
import PageDataParams from '#app/models/types/pageDataParams';
import LivePageLayout from './LivePageLayout';
import extractHeaders from '../../../../../src/server/utilities/extractHeaders';
import isValidPageNumber from '../../../../utilities/pageQueryValidator';
import getPageData from '../../../../utilities/pageRequests/getPageData';
const logger = nodeLogger(__filename);
export const getServerSideProps: GetServerSideProps = async context => {
context.res.setHeader(
'Cache-Control',
'public, stale-if-error=300, stale-while-revalidate=120, max-age=30',
);
logResponseTime(
{
path: context.resolvedUrl,
},
context.res,
() => null,
);
const {
id,
service,
variant,
renderer_env: rendererEnv,
page = '1',
} = context.query as PageDataParams;
const { headers: reqHeaders } = context.req;
const isApp = isAppPath(context.resolvedUrl);
const isLite = isLitePath(context.resolvedUrl);
if (!isValidPageNumber(page)) {
context.res.statusCode = 404;
sendCustomMetric({
metricName: NON_200_RESPONSE,
statusCode: context.res.statusCode,
pageType: LIVE_PAGE,
requestUrl: context.resolvedUrl,
});
return {
props: {
isApp,
isLite,
isNextJs: true,
service,
status: 404,
timeOnServer: Date.now(),
variant: variant?.[0] || null,
...extractHeaders(reqHeaders),
},
};
}
const { data, toggles } = await getPageData({
id,
page,
service,
variant,
rendererEnv,
resolvedUrl: context.resolvedUrl,
pageType: LIVE_PAGE,
});
let routingInfoLogger = logger.debug;
if (data.status !== OK) {
routingInfoLogger = logger.error;
}
routingInfoLogger(ROUTING_INFORMATION, {
url: context.resolvedUrl,
status: data.status,
pageType: LIVE_PAGE,
});
context.res.statusCode = data.status;
return {
props: {
error: data?.error || null,
id,
isApp,
isLite,
isAmp: false,
isNextJs: true,
page: page || null,
pageData: data?.pageData
? {
...data.pageData,
metadata: {
...data.pageData.metadata,
type: LIVE_PAGE,
},
}
: null,
pageType: LIVE_PAGE,
pathname: context.resolvedUrl,
service,
status: data.status,
timeOnServer: Date.now(), // TODO: check if needed?
toggles,
variant: variant?.[0] || null,
...extractHeaders(reqHeaders),
},
};
};
export default LivePageLayout;