Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update next #935

Merged
merged 10 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ services:

pam-geografi:
platform: linux/amd64
image: europe-north1-docker.pkg.dev/nais-management-233d/teampam/pam-geografi:25.070.162959
image: europe-north1-docker.pkg.dev/nais-management-233d/teampam/pam-geografi:25.085.151750
restart: unless-stopped
ports:
- "9009:8080"
Expand Down
171 changes: 82 additions & 89 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"date-fns": "^3.6.0",
"html-react-parser": "^5.1.18",
"isomorphic-dompurify": "^2.4.0",
"next": "14.1.3",
"next": "^14.2.26",
"prom-client": "^15.1.2",
"prop-types": "^15.8.1",
"react": "^18.2.0",
Expand Down
4 changes: 2 additions & 2 deletions sentry.server.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Sentry.init({
tracesSampleRate: 0.1,
debug: false,

beforeSend(event) {
event.tags = { ...event.tags, navCallId: getCallId() };
beforeSend: async (event) => {
event.tags = { ...event.tags, navCallId: await getCallId() };
return event;
},
});
30 changes: 15 additions & 15 deletions src/app/sommerjobb/_utils/fetchSommerjobber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ We can't use the built-in 'cache' in React either, since the route segment is dy
*/
export async function fetchElasticSearch(
query: SommerjobbQuery,
headers: HeadersInit,
fetchOptions = {},
performSearchIfDrivingDistanceError = true,
) {
Expand All @@ -75,42 +76,41 @@ export async function fetchElasticSearch(
errors.push(...withinDrivingDistanceResult.errors);

if (!performSearchIfDrivingDistanceError) {
return {
errors: errors,
};
return { errors };
}
}
}

const measureSearchDuration = elasticSearchDurationHistogram.startTimer();

const body = sommerjobbElasticSearchRequestBody(elasticSearchQuery);
const res = await fetch(`${process.env.PAMSEARCHAPI_URL}/stillingsok/ad/_search`, {
method: "POST",
headers: getDefaultHeaders(),
headers,
body: JSON.stringify(body),
...fetchOptions,
});

measureSearchDuration();

incrementElasticSearchRequests(res.ok);

return {
errors: errors,
response: res,
};
return { errors, response: res };
}

export const fetchSommerjobber = unstable_cache(
async (query) => fetchSimplifiedElasticSearch(query),
["elastic-search-query"],
{
revalidate: 60,
async (query) => {
const headers = await getDefaultHeaders();
return fetchSimplifiedElasticSearch(query, headers);
},
["elastic-search-query"],
{ revalidate: 60 },
);

async function fetchSimplifiedElasticSearch(query: SommerjobbQuery): Promise<FetchResult<SommerjobbResultData>> {
const result = await fetchElasticSearch(query);
async function fetchSimplifiedElasticSearch(
query: SommerjobbQuery,
headers: HeadersInit,
): Promise<FetchResult<SommerjobbResultData>> {
const result = await fetchElasticSearch(query, headers);

const { response } = result;

Expand Down
41 changes: 16 additions & 25 deletions src/app/stillinger/(sok)/_utils/fetchElasticSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,13 @@ import { SearchResult } from "@/app/stillinger/_common/types/SearchResult";
import { SearchQuery } from "@/app/stillinger/(sok)/_utils/query";
import { logZodError } from "@/app/stillinger/_common/actions/LogZodError";

/*
Manually cached because Next.js won't cache it. We break these:
* The fetch request comes after the usage of headers or cookies.
* The fetch request uses Authorization or Cookie headers and there's an uncached request above it in the component tree.
https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#opting-out-of-data-caching

We can't use the built-in 'cache' in React either, since the route segment is dynamic:
"... If the segment is dynamic, the output of the request will not be cached and will be re-fetched on every request when the segment is rendered."
https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#fetching-data-on-the-server-with-third-party-libraries
*/
export type ExtendedQuery = SearchQuery & {
withinDrivingDistance?: Locations | undefined;
};

export async function fetchElasticSearch(
query: SearchQuery,
headers: HeadersInit,
fetchOptions = {},
performSearchIfDrivingDistanceError = true,
) {
Expand All @@ -51,42 +43,41 @@ export async function fetchElasticSearch(
errors.push(...withinDrivingDistanceResult.errors);

if (!performSearchIfDrivingDistanceError) {
return {
errors: errors,
};
return { errors };
}
}
}

const measureSearchDuration = elasticSearchDurationHistogram.startTimer();

const body = elasticSearchRequestBody(elasticSearchQuery);
const res = await fetch(`${process.env.PAMSEARCHAPI_URL}/stillingsok/ad/_search`, {
method: "POST",
headers: getDefaultHeaders(),
headers,
body: JSON.stringify(body),
...fetchOptions,
});

measureSearchDuration();

incrementElasticSearchRequests(res.ok);

return {
errors: errors,
response: res,
};
return { errors, response: res };
}

export const fetchCachedSimplifiedElasticSearch = unstable_cache(
async (query) => fetchSimplifiedElasticSearch(query),
["elastic-search-query"],
{
revalidate: 60,
async (query) => {
const headers = await getDefaultHeaders();
return fetchSimplifiedElasticSearch(query, headers);
},
["elastic-search-query"],
{ revalidate: 60 },
);

async function fetchSimplifiedElasticSearch(query: SearchQuery): Promise<FetchResult<SearchResult>> {
const result = await fetchElasticSearch(query);
async function fetchSimplifiedElasticSearch(
query: SearchQuery,
headers: HeadersInit,
): Promise<FetchResult<SearchResult>> {
const result = await fetchElasticSearch(query, headers);

const { response } = result;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ export async function fetchLocationsWithinDrivingDistance(
referencePostCode?: string,
distance?: string,
): Promise<FetchResult<Locations>> {
const headers = await getDefaultHeaders();
const res = await fetch(
`${process.env.PAM_GEOGRAFI_API_URL}/innen-avstand/${referencePostCode}?avstand=${distance}`,
{
headers: getDefaultHeaders(),
headers: headers,
},
);

Expand Down
11 changes: 5 additions & 6 deletions src/app/stillinger/(sok)/_utils/fetchPostcodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ export interface Postcode {
interface PostdataDto {
postkode: string;
by: string;
// kommune: KommuneDTO
// fylke: FylkeDTO
}

async function fetchPostcodes(): Promise<FetchResult<Postcode[]>> {
async function fetchPostcodes(headers: HeadersInit): Promise<FetchResult<Postcode[]>> {
const res = await fetch(`${process.env.PAM_GEOGRAFI_API_URL}/postdata?sort=asc`, {
headers: getDefaultHeaders(),
headers,
});

if (!res.ok) {
Expand All @@ -43,12 +41,13 @@ async function fetchPostcodes(): Promise<FetchResult<Postcode[]>> {

const CACHE_KEY = "postcodes-query";

const fetchCachedPostcodesInternal = unstable_cache(async () => fetchPostcodes(), [CACHE_KEY], {
const fetchCachedPostcodesInternal = unstable_cache(async (headers) => fetchPostcodes(headers), [CACHE_KEY], {
revalidate: 3600,
});

export async function fetchCachedPostcodes(): Promise<FetchResult<Postcode[]>> {
const result = await fetchCachedPostcodesInternal();
const headers = await getDefaultHeaders();
const result = await fetchCachedPostcodesInternal(headers);

if (result.errors && result.errors.length > 0) {
logger.warn("Errors when fetching postcodes, manually purging cache");
Expand Down
32 changes: 13 additions & 19 deletions src/app/stillinger/(sok)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ export async function generateMetadata() {
};
}

const fetchCachedLocations = unstable_cache(async () => fetchLocations(), ["locations-query"], {
revalidate: 10,
});
const fetchCachedLocations = unstable_cache(
async () => {
const headers = await getDefaultHeaders();
return fetchLocations(headers);
},
["locations-query"],
{ revalidate: 10 },
);

type Municipal = { key: string; code: string };
export type SearchLocation = {
Expand All @@ -65,14 +70,10 @@ type FetchResults = {
searchResult?: FetchResult<SearchResult> | undefined;
};

async function fetchLocations(): Promise<FetchResult<SearchLocation[]>> {
async function fetchLocations(headers: HeadersInit): Promise<FetchResult<SearchLocation[]>> {
const [kommunerRespons, fylkerRespons] = await Promise.all([
fetch(`${process.env.PAM_GEOGRAFI_API_URL}/kommuner`, {
headers: getDefaultHeaders(),
}),
fetch(`${process.env.PAM_GEOGRAFI_API_URL}/fylker`, {
headers: getDefaultHeaders(),
}),
fetch(`${process.env.PAM_GEOGRAFI_API_URL}/kommuner`, { headers }),
fetch(`${process.env.PAM_GEOGRAFI_API_URL}/fylker`, { headers }),
]);

const errors: FetchError[] = [];
Expand All @@ -88,10 +89,7 @@ async function fetchLocations(): Promise<FetchResult<SearchLocation[]>> {
}

if (errors.length > 0) {
return {
errors,
data: [],
};
return { errors, data: [] };
}

const municipals: KommuneRaw[] = await kommunerRespons.json();
Expand All @@ -109,11 +107,7 @@ async function fetchLocations(): Promise<FetchResult<SearchLocation[]>> {
code: m.fylkesnummer,
})),
})),
{
key: "UTLAND",
code: "999",
municipals: [],
},
{ key: "UTLAND", code: "999", municipals: [] },
],
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function checkIfUserAgreementIsAccepted(): Promise<UserAgreement> {

const res = await fetch(`${process.env.PAMADUSER_URL}/api/v1/user`, {
method: "GET",
headers: getDefaultAuthHeaders(oboToken),
headers: await getDefaultAuthHeaders(oboToken),
});

if (!res.ok) {
Expand Down
6 changes: 3 additions & 3 deletions src/app/stillinger/_common/actions/favouritesActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function getFavouritesAction() {

const res = await fetch(`${ADUSER_FAVOURITES_URL}?size=9999`, {
method: "GET",
headers: getDefaultAuthHeaders(oboToken),
headers: await getDefaultAuthHeaders(oboToken),
});

incrementAdUserRequests("get_favourites", res.ok);
Expand All @@ -49,7 +49,7 @@ export async function addFavouriteAction(favouriteAd: Favourite) {
const res = await fetch(ADUSER_FAVOURITES_URL, {
method: "POST",
body: JSON.stringify({ favouriteAd }),
headers: getAdUserDefaultAuthHeadersWithCsrfToken(oboToken),
headers: await getAdUserDefaultAuthHeadersWithCsrfToken(oboToken),
});

incrementAdUserRequests("create_favourite", res.ok);
Expand All @@ -69,7 +69,7 @@ export async function deleteFavouriteAction(uuid: string) {
const oboToken = await getAdUserOboToken();
const res = await fetch(`${ADUSER_FAVOURITES_URL}/${uuid}`, {
method: "DELETE",
headers: getAdUserDefaultAuthHeadersWithCsrfToken(oboToken),
headers: await getAdUserDefaultAuthHeadersWithCsrfToken(oboToken),
});

incrementAdUserRequests("delete_favourite", res.ok);
Expand Down
2 changes: 1 addition & 1 deletion src/app/stillinger/_common/actions/personaliaActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function getPersonalia() {

const res = await fetch(ADUSER_PERSONALIA_URL, {
method: "GET",
headers: getDefaultAuthHeaders(oboToken),
headers: await getDefaultAuthHeaders(oboToken),
});

incrementAdUserRequests("get_personalia", res.ok);
Expand Down
12 changes: 6 additions & 6 deletions src/app/stillinger/_common/actions/savedSearchActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function getAllSavedSearchesAction(): Promise<SavedSearch[]> {
const oboToken = await getAdUserOboToken();
const res = await fetch(`${SAVED_SEARCH_URL}?size=999&sort=updated,desc`, {
method: "GET",
headers: getDefaultAuthHeaders(oboToken),
headers: await getDefaultAuthHeaders(oboToken),
});

incrementAdUserRequests("get_saved_searches", res.ok);
Expand All @@ -54,7 +54,7 @@ export async function getSavedSearchAction(uuid: string): Promise<GetSavedSearch
const oboToken = await getAdUserOboToken();
const res = await fetch(`${SAVED_SEARCH_URL}/${uuid}`, {
method: "GET",
headers: getDefaultAuthHeaders(oboToken),
headers: await getDefaultAuthHeaders(oboToken),
});

incrementAdUserRequests("get_saved_search", res.ok);
Expand All @@ -75,7 +75,7 @@ export async function saveSavedSearchAction(savedSearch: SavedSearch): Promise<A
const res = await fetch(SAVED_SEARCH_URL, {
method: "POST",
body: JSON.stringify(savedSearch),
headers: getAdUserDefaultAuthHeadersWithCsrfToken(oboToken),
headers: await getAdUserDefaultAuthHeadersWithCsrfToken(oboToken),
});

incrementAdUserRequests("create_saved_search", res.ok);
Expand All @@ -102,7 +102,7 @@ export async function updateSavedSearchAction(savedSearch: SavedSearch): Promise
const res = await fetch(`${SAVED_SEARCH_URL}/${savedSearch.uuid}`, {
method: "PUT",
body: JSON.stringify(savedSearch),
headers: getAdUserDefaultAuthHeadersWithCsrfToken(oboToken),
headers: await getAdUserDefaultAuthHeadersWithCsrfToken(oboToken),
});

incrementAdUserRequests("update_saved_search", res.ok);
Expand All @@ -129,7 +129,7 @@ export async function deleteSavedSearchAction(uuid: string): Promise<ActionRespo

const res = await fetch(`${SAVED_SEARCH_URL}/${uuid}`, {
method: "DELETE",
headers: getAdUserDefaultAuthHeadersWithCsrfToken(oboToken),
headers: await getAdUserDefaultAuthHeadersWithCsrfToken(oboToken),
});

incrementAdUserRequests("delete_saved_search", res.ok);
Expand All @@ -154,7 +154,7 @@ export async function restartSavedSearchAction(

const res = await fetch(`${SAVED_SEARCH_URL}/${uuid}`, {
method: "PUT",
headers: getAdUserDefaultAuthHeadersWithCsrfToken(oboToken),
headers: await getAdUserDefaultAuthHeadersWithCsrfToken(oboToken),
body: JSON.stringify(savedSearch),
});

Expand Down
Loading
Loading