Skip to content

Commit

Permalink
Merge pull request #1972 from navikt/nav-dekoratoren-moduler-update
Browse files Browse the repository at this point in the history
Oppdaterer nav-dekoratoren-moduler. Invaliderer render-cache ved ny versjon av dekoratøren.
  • Loading branch information
anders-nom authored Jul 19, 2024
2 parents 0ef1030 + 3850d06 commit 117b2f0
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.dev2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
ADMIN_ORIGIN: https://portal-admin-q6.oera.no
APP_ORIGIN: https://www-2.ansatt.dev.nav.no
REVALIDATOR_PROXY_ORIGIN: http://nav-enonicxp-frontend-revalidator-proxy-dev2
DECORATOR_URL: https://dekoratoren.ekstern.dev.nav.no
DECORATOR_URL: https://dekoratoren-beta.intern.dev.nav.no
XP_ORIGIN: https://www-q6.nav.no
TELEMETRY_URL: https://telemetry.ekstern.dev.nav.no/collect
INNLOGGINGSSTATUS_URL: https://www.ekstern.dev.nav.no/person/nav-dekoratoren-api/auth
Expand Down
1 change: 1 addition & 0 deletions .nais/vars/vars-dev2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dekoratorenApp: nav-dekoratoren-beta
externalHosts:
- www-q6.nav.no
- www-2-failover.intern.dev.nav.no
- dekoratoren-beta.intern.dev.nav.no
secret: nav-enonicxp-dev2
ingresses:
- https://www-2.ansatt.dev.nav.no
Expand Down
4 changes: 0 additions & 4 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,6 @@ const config = {
{
source: '/:path*',
headers: [
{
key: 'app-name',
value: 'nav-enonicxp-frontend',
},
{
key: 'Content-Security-Policy',
value: await csp(),
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@navikt/ds-css": "6.13.0",
"@navikt/ds-react": "6.13.0",
"@navikt/ds-tokens": "6.13.0",
"@navikt/nav-dekoratoren-moduler": "2.1.6",
"@navikt/nav-dekoratoren-moduler": "3.0.0-beta.4",
"@navikt/nav-office-reception-info": "1.0.6",
"@reduxjs/toolkit": "2.2.6",
"csp-header": "5.2.1",
Expand Down
3 changes: 2 additions & 1 deletion server/jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default {
"^.+\\.tsx?$": "ts-jest"
},
"moduleNameMapper": {
"srcCommon/(.*)": "<rootDir>/../srcCommon/$1"
"srcCommon/(.*)": "<rootDir>/../srcCommon/$1",
"cache/(.*)": "<rootDir>/src/cache/$1"
},
};
2 changes: 1 addition & 1 deletion server/src/cache/revalidator-proxy-heartbeat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Revalidator proxy heartbeat', () => {

fetchMock.mockResponse('Hello!');

initRevalidatorProxyHeartbeat('dummy-build-id');
initRevalidatorProxyHeartbeat();

expect(fetchMock.mock.calls.length).toEqual(1);
expect(fetchMock.mock.calls[0][0]).toMatch(new RegExp(`^${revalidatorProxyOrigin}`));
Expand Down
26 changes: 11 additions & 15 deletions server/src/cache/revalidator-proxy-heartbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// See: https://github.com/navikt/nav-enonicxp-frontend-revalidator-proxy
import { networkInterfaces } from 'os';
import { logger } from 'srcCommon/logger';
import { getRenderCacheKeyPrefix, getResponseCacheKeyPrefix } from 'srcCommon/redis';
import { objectToQueryString } from 'srcCommon/fetch-utils';
import { redisCache } from 'cache/page-cache-handler';

const { ENV, NODE_ENV, DOCKER_HOST_ADDRESS, REVALIDATOR_PROXY_ORIGIN, SERVICE_SECRET } =
process.env;
Expand All @@ -22,31 +22,26 @@ const getPodAddress = () => {
const podAddress = nets?.eth0?.[0]?.address;

if (!podAddress) {
logger.error(
'Error: pod IP address could not be determined' +
' - Event driven cache regeneration will not be active for this instance'
);
logger.error('Error: pod IP address could not be determined!');
return null;
}

return podAddress;
};

const getProxyLivenessUrl = (buildId: string) => {
const getProxyLivenessUrl = () => {
const podAddress = getPodAddress();
return podAddress
? `${REVALIDATOR_PROXY_ORIGIN}/liveness${objectToQueryString({
address: podAddress,
redisPrefixes: [getRenderCacheKeyPrefix(buildId), getResponseCacheKeyPrefix()].join(
','
),
redisPrefixes: redisCache.getKeyPrefixes().join(','),
})}`
: null;
};

let didStart = false;

export const initRevalidatorProxyHeartbeat = (buildId: string) => {
export const initRevalidatorProxyHeartbeat = () => {
if (NODE_ENV === 'development') {
return;
}
Expand All @@ -56,16 +51,17 @@ export const initRevalidatorProxyHeartbeat = (buildId: string) => {
return;
}

const url = getProxyLivenessUrl(buildId);
if (!url) {
return;
}

didStart = true;

logger.info('Starting heartbeat loop');

const heartbeatFunc = () => {
const url = getProxyLivenessUrl();
if (!url) {
logger.error('Failed to determine revalidator heartbeat url!');
return;
}

fetch(url, {
headers: { secret: SERVICE_SECRET },
}).catch((e) => logger.error(`Failed to send heartbeat signal - ${e}`));
Expand Down
2 changes: 0 additions & 2 deletions server/src/server-setup/server-setup-dev.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Express } from 'express';
import { NextServer } from 'next/dist/server/next';
import { logger } from 'srcCommon/logger';

const DEV_NAIS_DOMAIN = 'ansatt.dev.nav.no';
const APP_ORIGIN = process.env.APP_ORIGIN;
Expand All @@ -26,7 +25,6 @@ export const serverSetupDev = (expressApp: Express, nextApp: NextServer) => {
if (APP_ORIGIN.endsWith(DEV_NAIS_DOMAIN)) {
expressApp.all('*', (req, res, next) => {
if (!req.hostname.endsWith(DEV_NAIS_DOMAIN)) {
logger.info('Redirecting!');
return res.redirect(302, `${APP_ORIGIN}${req.path}`);
}

Expand Down
20 changes: 18 additions & 2 deletions server/src/server-setup/server-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import { handleInvalidateAllReq } from 'req-handlers/invalidate-all';
import { handleGetPendingResponses } from 'req-handlers/pending-responses';
import { serverSetupDev } from 'server-setup/server-setup-dev';
import { logger } from 'srcCommon/logger';
import { redisCache } from 'cache/page-cache-handler';
import PageCacheHandler, { redisCache } from 'cache/page-cache-handler';
import {
addDecoratorUpdateListener,
getDecoratorVersionId,
} from '@navikt/nav-dekoratoren-moduler/ssr';
import { decoratorEnvProps } from 'srcCommon/decorator-utils-serverside';

// Set the no-cache header on json files from the incremental cache to ensure
// data requested during client side navigation is always validated if cached
Expand All @@ -29,7 +34,18 @@ export const serverSetup = async (expressApp: Express, nextApp: NextServer) => {
const nextServer = await getNextServer(nextApp);
const currentBuildId = getNextBuildId(nextServer);

await redisCache.init(currentBuildId);
const decoratorVersionId = await getDecoratorVersionId(decoratorEnvProps);
if (!decoratorVersionId) {
logger.error('Failed to fetch decorator version id!');
}

await redisCache.init(currentBuildId, decoratorVersionId);

addDecoratorUpdateListener(decoratorEnvProps, (versionId) => {
logger.info(`New decorator version: ${versionId} - clearing render caches`);
redisCache.updateRenderCacheKeyPrefix(versionId);
new PageCacheHandler().clear();
});

logger.info(`Current build id: ${currentBuildId}`);

Expand Down
10 changes: 7 additions & 3 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import promBundle from 'express-prom-bundle';
import { initRevalidatorProxyHeartbeat } from 'cache/revalidator-proxy-heartbeat';
import { serverSetupFailover } from 'server-setup/server-setup-failover';
import { serverSetup } from 'server-setup/server-setup';
import { getNextBuildId, getNextServer } from 'next-utils';
import { getNextServer } from 'next-utils';
import { logger } from 'srcCommon/logger';
import path from 'path';
import { injectNextImageCacheDir } from 'cache/image-cache-handler';
Expand Down Expand Up @@ -47,6 +47,11 @@ nextApp.prepare().then(async () => {
next();
});

expressApp.all('*', (req, res, next) => {
res.setHeader('app-name', 'nav-enonicxp-frontend');
next();
});

if (isFailover) {
serverSetupFailover(expressApp, nextApp);
} else {
Expand Down Expand Up @@ -78,8 +83,7 @@ nextApp.prepare().then(async () => {
}

if (!isFailover) {
const buildId = getNextBuildId(nextServer);
initRevalidatorProxyHeartbeat(buildId);
initRevalidatorProxyHeartbeat();
}

logger.info(`Server started on port ${port}`);
Expand Down
2 changes: 1 addition & 1 deletion src/components/PageWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { onBreadcrumbClick, onLanguageSelect, setParams } from '@navikt/nav-deko
import { ContentProps } from 'types/content-props/_content-common';
import { hookAndInterceptInternalLink, prefetchOnMouseover } from 'utils/links';
import { hasWhiteHeader, hasWhitePage } from 'utils/appearance';
import { getDecoratorParams } from 'utils/decorator/decorator-utils';
import { getDecoratorParams } from 'utils/decorator-utils';
import { getInternalRelativePath } from 'utils/urls';
import { store } from 'store/store';
import { fetchAndSetInnloggingsstatus } from 'utils/fetch/fetch-innloggingsstatus';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import Head from 'next/head';
import { ContentProps } from 'types/content-props/_content-common';
import { getDecoratorParams } from 'utils/decorator/decorator-utils';
import { getDecoratorParams } from 'utils/decorator-utils';
import { isLegacyContentType } from 'utils/content-types';

type Props = {
Expand Down
11 changes: 9 additions & 2 deletions src/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DocumentInitialProps } from 'next/dist/pages/_document';
import { DecoratorComponents } from '@navikt/nav-dekoratoren-moduler/ssr';
import { Language } from 'translations';
import { DocumentParameter } from 'components/_common/metatags/DocumentParameterMetatags';
import { getDecoratorComponents } from 'utils/decorator/decorator-utils-serverside';
import { getDecoratorComponents } from 'srcCommon/decorator-utils-serverside';

type DocumentProps = {
language: Language;
Expand Down Expand Up @@ -57,7 +57,14 @@ class MyDocument extends Document<DocumentProps> {

return (
<Html lang={language || 'no'}>
<Head>{Decorator && <Decorator.Styles />}</Head>
<Head>
{Decorator && (
<>
<Decorator.Styles />
<Decorator.HeadAssets />
</>
)}
</Head>
<body className={isLegacyContentType ? 'legacyContentType' : undefined}>
{Decorator && <Decorator.Header />}
<Main />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { DecoratorParams } from '@navikt/nav-dekoratoren-moduler';
import { Language } from 'translations';
import { getContentLanguages } from 'utils/languages';
import { ContentProps, ContentType } from 'types/content-props/_content-common';
import { LanguageProps } from 'types/language';
import { stripXpPathPrefix } from 'utils/urls';
import { Audience, getAudience } from 'types/component-props/_mixins';
import { hasWhiteHeader } from 'utils/appearance';
import { stripXpPathPrefix } from './urls';
import { getContentLanguages } from './languages';
import { hasWhiteHeader } from './appearance';

const defaultLanguage: DecoratorParams['language'] = 'nb';

Expand Down
2 changes: 1 addition & 1 deletion src/utils/fetch/fetch-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const getXpCacheKey =
})
: () => ({});

const redisCache = await new RedisCache().init(process.env.BUILD_ID);
const redisCache = await new RedisCache().init(process.env.BUILD_ID, '');

const fetchConfig = {
headers: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
fetchDecoratorReact,
DecoratorEnvProps,
DecoratorFetchProps,
fetchDecoratorReact,
DecoratorParams,
} from '@navikt/nav-dekoratoren-moduler/ssr';
import { DecoratorParams } from '@navikt/nav-dekoratoren-moduler';

type AppEnv = typeof process.env.ENV;
type DecoratorEnv = DecoratorEnvProps['env'];
Expand All @@ -17,7 +17,7 @@ const envMap: Record<AppEnv, DecoratorEnv> = {

const decoratorEnv = envMap[process.env.ENV] || 'prod';

const envProps: DecoratorFetchProps = {
export const decoratorEnvProps: DecoratorFetchProps = {
noCache: process.env.DECORATOR_NOCACHE === 'true',
...(decoratorEnv === 'localhost'
? { env: 'localhost', localUrl: process.env.DECORATOR_URL }
Expand All @@ -26,7 +26,7 @@ const envProps: DecoratorFetchProps = {

export const getDecoratorComponents = async (params?: DecoratorParams) => {
const decoratorComponents = fetchDecoratorReact({
...envProps,
...decoratorEnvProps,
params,
});

Expand Down
Loading

0 comments on commit 117b2f0

Please sign in to comment.