Skip to content

Commit 117b2f0

Browse files
authored
Merge pull request #1972 from navikt/nav-dekoratoren-moduler-update
Oppdaterer nav-dekoratoren-moduler. Invaliderer render-cache ved ny versjon av dekoratøren.
2 parents 0ef1030 + 3850d06 commit 117b2f0

18 files changed

+92
-60
lines changed

.github/workflows/deploy.dev2.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
ADMIN_ORIGIN: https://portal-admin-q6.oera.no
2525
APP_ORIGIN: https://www-2.ansatt.dev.nav.no
2626
REVALIDATOR_PROXY_ORIGIN: http://nav-enonicxp-frontend-revalidator-proxy-dev2
27-
DECORATOR_URL: https://dekoratoren.ekstern.dev.nav.no
27+
DECORATOR_URL: https://dekoratoren-beta.intern.dev.nav.no
2828
XP_ORIGIN: https://www-q6.nav.no
2929
TELEMETRY_URL: https://telemetry.ekstern.dev.nav.no/collect
3030
INNLOGGINGSSTATUS_URL: https://www.ekstern.dev.nav.no/person/nav-dekoratoren-api/auth

.nais/vars/vars-dev2.yml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ dekoratorenApp: nav-dekoratoren-beta
44
externalHosts:
55
- www-q6.nav.no
66
- www-2-failover.intern.dev.nav.no
7+
- dekoratoren-beta.intern.dev.nav.no
78
secret: nav-enonicxp-dev2
89
ingresses:
910
- https://www-2.ansatt.dev.nav.no

next.config.js

-4
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,6 @@ const config = {
279279
{
280280
source: '/:path*',
281281
headers: [
282-
{
283-
key: 'app-name',
284-
value: 'nav-enonicxp-frontend',
285-
},
286282
{
287283
key: 'Content-Security-Policy',
288284
value: await csp(),

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"@navikt/ds-css": "6.13.0",
4343
"@navikt/ds-react": "6.13.0",
4444
"@navikt/ds-tokens": "6.13.0",
45-
"@navikt/nav-dekoratoren-moduler": "2.1.6",
45+
"@navikt/nav-dekoratoren-moduler": "3.0.0-beta.4",
4646
"@navikt/nav-office-reception-info": "1.0.6",
4747
"@reduxjs/toolkit": "2.2.6",
4848
"csp-header": "5.2.1",

server/jest.config.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default {
88
"^.+\\.tsx?$": "ts-jest"
99
},
1010
"moduleNameMapper": {
11-
"srcCommon/(.*)": "<rootDir>/../srcCommon/$1"
11+
"srcCommon/(.*)": "<rootDir>/../srcCommon/$1",
12+
"cache/(.*)": "<rootDir>/src/cache/$1"
1213
},
1314
};

server/src/cache/revalidator-proxy-heartbeat.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('Revalidator proxy heartbeat', () => {
1414

1515
fetchMock.mockResponse('Hello!');
1616

17-
initRevalidatorProxyHeartbeat('dummy-build-id');
17+
initRevalidatorProxyHeartbeat();
1818

1919
expect(fetchMock.mock.calls.length).toEqual(1);
2020
expect(fetchMock.mock.calls[0][0]).toMatch(new RegExp(`^${revalidatorProxyOrigin}`));

server/src/cache/revalidator-proxy-heartbeat.ts

+11-15
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// See: https://github.com/navikt/nav-enonicxp-frontend-revalidator-proxy
44
import { networkInterfaces } from 'os';
55
import { logger } from 'srcCommon/logger';
6-
import { getRenderCacheKeyPrefix, getResponseCacheKeyPrefix } from 'srcCommon/redis';
76
import { objectToQueryString } from 'srcCommon/fetch-utils';
7+
import { redisCache } from 'cache/page-cache-handler';
88

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

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

3229
return podAddress;
3330
};
3431

35-
const getProxyLivenessUrl = (buildId: string) => {
32+
const getProxyLivenessUrl = () => {
3633
const podAddress = getPodAddress();
3734
return podAddress
3835
? `${REVALIDATOR_PROXY_ORIGIN}/liveness${objectToQueryString({
3936
address: podAddress,
40-
redisPrefixes: [getRenderCacheKeyPrefix(buildId), getResponseCacheKeyPrefix()].join(
41-
','
42-
),
37+
redisPrefixes: redisCache.getKeyPrefixes().join(','),
4338
})}`
4439
: null;
4540
};
4641

4742
let didStart = false;
4843

49-
export const initRevalidatorProxyHeartbeat = (buildId: string) => {
44+
export const initRevalidatorProxyHeartbeat = () => {
5045
if (NODE_ENV === 'development') {
5146
return;
5247
}
@@ -56,16 +51,17 @@ export const initRevalidatorProxyHeartbeat = (buildId: string) => {
5651
return;
5752
}
5853

59-
const url = getProxyLivenessUrl(buildId);
60-
if (!url) {
61-
return;
62-
}
63-
6454
didStart = true;
6555

6656
logger.info('Starting heartbeat loop');
6757

6858
const heartbeatFunc = () => {
59+
const url = getProxyLivenessUrl();
60+
if (!url) {
61+
logger.error('Failed to determine revalidator heartbeat url!');
62+
return;
63+
}
64+
6965
fetch(url, {
7066
headers: { secret: SERVICE_SECRET },
7167
}).catch((e) => logger.error(`Failed to send heartbeat signal - ${e}`));

server/src/server-setup/server-setup-dev.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Express } from 'express';
22
import { NextServer } from 'next/dist/server/next';
3-
import { logger } from 'srcCommon/logger';
43

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

server/src/server-setup/server-setup.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import { handleInvalidateAllReq } from 'req-handlers/invalidate-all';
99
import { handleGetPendingResponses } from 'req-handlers/pending-responses';
1010
import { serverSetupDev } from 'server-setup/server-setup-dev';
1111
import { logger } from 'srcCommon/logger';
12-
import { redisCache } from 'cache/page-cache-handler';
12+
import PageCacheHandler, { redisCache } from 'cache/page-cache-handler';
13+
import {
14+
addDecoratorUpdateListener,
15+
getDecoratorVersionId,
16+
} from '@navikt/nav-dekoratoren-moduler/ssr';
17+
import { decoratorEnvProps } from 'srcCommon/decorator-utils-serverside';
1318

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

32-
await redisCache.init(currentBuildId);
37+
const decoratorVersionId = await getDecoratorVersionId(decoratorEnvProps);
38+
if (!decoratorVersionId) {
39+
logger.error('Failed to fetch decorator version id!');
40+
}
41+
42+
await redisCache.init(currentBuildId, decoratorVersionId);
43+
44+
addDecoratorUpdateListener(decoratorEnvProps, (versionId) => {
45+
logger.info(`New decorator version: ${versionId} - clearing render caches`);
46+
redisCache.updateRenderCacheKeyPrefix(versionId);
47+
new PageCacheHandler().clear();
48+
});
3349

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

server/src/server.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import promBundle from 'express-prom-bundle';
55
import { initRevalidatorProxyHeartbeat } from 'cache/revalidator-proxy-heartbeat';
66
import { serverSetupFailover } from 'server-setup/server-setup-failover';
77
import { serverSetup } from 'server-setup/server-setup';
8-
import { getNextBuildId, getNextServer } from 'next-utils';
8+
import { getNextServer } from 'next-utils';
99
import { logger } from 'srcCommon/logger';
1010
import path from 'path';
1111
import { injectNextImageCacheDir } from 'cache/image-cache-handler';
@@ -47,6 +47,11 @@ nextApp.prepare().then(async () => {
4747
next();
4848
});
4949

50+
expressApp.all('*', (req, res, next) => {
51+
res.setHeader('app-name', 'nav-enonicxp-frontend');
52+
next();
53+
});
54+
5055
if (isFailover) {
5156
serverSetupFailover(expressApp, nextApp);
5257
} else {
@@ -78,8 +83,7 @@ nextApp.prepare().then(async () => {
7883
}
7984

8085
if (!isFailover) {
81-
const buildId = getNextBuildId(nextServer);
82-
initRevalidatorProxyHeartbeat(buildId);
86+
initRevalidatorProxyHeartbeat();
8387
}
8488

8589
logger.info(`Server started on port ${port}`);

src/components/PageWrapper.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { onBreadcrumbClick, onLanguageSelect, setParams } from '@navikt/nav-deko
44
import { ContentProps } from 'types/content-props/_content-common';
55
import { hookAndInterceptInternalLink, prefetchOnMouseover } from 'utils/links';
66
import { hasWhiteHeader, hasWhitePage } from 'utils/appearance';
7-
import { getDecoratorParams } from 'utils/decorator/decorator-utils';
7+
import { getDecoratorParams } from 'utils/decorator-utils';
88
import { getInternalRelativePath } from 'utils/urls';
99
import { store } from 'store/store';
1010
import { fetchAndSetInnloggingsstatus } from 'utils/fetch/fetch-innloggingsstatus';

src/components/_common/metatags/DocumentParameterMetatags.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import Head from 'next/head';
33
import { ContentProps } from 'types/content-props/_content-common';
4-
import { getDecoratorParams } from 'utils/decorator/decorator-utils';
4+
import { getDecoratorParams } from 'utils/decorator-utils';
55
import { isLegacyContentType } from 'utils/content-types';
66

77
type Props = {

src/pages/_document.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DocumentInitialProps } from 'next/dist/pages/_document';
44
import { DecoratorComponents } from '@navikt/nav-dekoratoren-moduler/ssr';
55
import { Language } from 'translations';
66
import { DocumentParameter } from 'components/_common/metatags/DocumentParameterMetatags';
7-
import { getDecoratorComponents } from 'utils/decorator/decorator-utils-serverside';
7+
import { getDecoratorComponents } from 'srcCommon/decorator-utils-serverside';
88

99
type DocumentProps = {
1010
language: Language;
@@ -57,7 +57,14 @@ class MyDocument extends Document<DocumentProps> {
5757

5858
return (
5959
<Html lang={language || 'no'}>
60-
<Head>{Decorator && <Decorator.Styles />}</Head>
60+
<Head>
61+
{Decorator && (
62+
<>
63+
<Decorator.Styles />
64+
<Decorator.HeadAssets />
65+
</>
66+
)}
67+
</Head>
6168
<body className={isLegacyContentType ? 'legacyContentType' : undefined}>
6269
{Decorator && <Decorator.Header />}
6370
<Main />

src/utils/decorator/decorator-utils.ts src/utils/decorator-utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { DecoratorParams } from '@navikt/nav-dekoratoren-moduler';
22
import { Language } from 'translations';
3-
import { getContentLanguages } from 'utils/languages';
43
import { ContentProps, ContentType } from 'types/content-props/_content-common';
54
import { LanguageProps } from 'types/language';
6-
import { stripXpPathPrefix } from 'utils/urls';
75
import { Audience, getAudience } from 'types/component-props/_mixins';
8-
import { hasWhiteHeader } from 'utils/appearance';
6+
import { stripXpPathPrefix } from './urls';
7+
import { getContentLanguages } from './languages';
8+
import { hasWhiteHeader } from './appearance';
99

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

src/utils/fetch/fetch-content.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const getXpCacheKey =
2626
})
2727
: () => ({});
2828

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

3131
const fetchConfig = {
3232
headers: {

src/utils/decorator/decorator-utils-serverside.ts srcCommon/decorator-utils-serverside.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
2+
fetchDecoratorReact,
23
DecoratorEnvProps,
34
DecoratorFetchProps,
4-
fetchDecoratorReact,
5+
DecoratorParams,
56
} from '@navikt/nav-dekoratoren-moduler/ssr';
6-
import { DecoratorParams } from '@navikt/nav-dekoratoren-moduler';
77

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

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

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

2727
export const getDecoratorComponents = async (params?: DecoratorParams) => {
2828
const decoratorComponents = fetchDecoratorReact({
29-
...envProps,
29+
...decoratorEnvProps,
3030
params,
3131
});
3232

0 commit comments

Comments
 (0)