Skip to content

Commit a4433c5

Browse files
committed
fix: static page generation issues with language
LIIKUNTA-372. Currently, the Sports app did not fail, but the Hobbies and the Events did. This was because there was a different kind of locale handling: The locale which was destructed from the context was set to null. Now there is a fallback language set when the context locale is null. The issue was easier to find and understand, when I understood where the error was printed. The error was our own message from the Apollo-Client's ErrorLink. If the operation-variable from the onError-handler was printed in the console, the actual error place was shown!
1 parent 28ac411 commit a4433c5

6 files changed

Lines changed: 18 additions & 27 deletions

File tree

apps/events-helsinki/src/domain/app/getEventsStaticProps.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import type { ApolloClient, NormalizedCacheObject } from '@apollo/client';
22
import { isApolloError } from '@apollo/client';
3-
import type {
4-
AppLanguage,
5-
CmsLanguage,
6-
Menu,
7-
Language,
8-
} from 'events-helsinki-components';
3+
import type { CmsLanguage, Menu, Language } from 'events-helsinki-components';
94
import {
105
DEFAULT_FOOTER_MENU_NAME,
116
DEFAULT_HEADER_MENU_NAME,
@@ -62,7 +57,7 @@ export default async function getEventsStaticProps<P = Record<string, any>>(
6257
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6358
} catch (e: any) {
6459
// Generic error handling
65-
staticGenerationLogger.error('Error while generating a page:', e);
60+
staticGenerationLogger.error(`Error while generating a page: ${e}`, e);
6661
if (isApolloError(e)) {
6762
return {
6863
props: {
@@ -94,8 +89,8 @@ async function getGlobalCMSData({
9489
client,
9590
context,
9691
}: GetGlobalCMSDataParams): Promise<ReturnedGlobalCMSData> {
97-
const locale: AppLanguage = (context?.locale ?? 'fi') as AppLanguage;
98-
const headerNavigationMenuName = DEFAULT_HEADER_MENU_NAME[locale];
92+
const language = getLanguageOrDefault(context.locale);
93+
const headerNavigationMenuName = DEFAULT_HEADER_MENU_NAME[language];
9994
const { data: headerMenuData } = await client.query({
10095
query: MenuDocument,
10196
variables: {
@@ -113,7 +108,7 @@ async function getGlobalCMSData({
113108
},
114109
});
115110

116-
const footerNavigationMenuName = DEFAULT_FOOTER_MENU_NAME[locale];
111+
const footerNavigationMenuName = DEFAULT_FOOTER_MENU_NAME[language];
117112
const { data: footerMenuData } = await client.query({
118113
query: MenuDocument,
119114
variables: {

apps/events-helsinki/src/domain/clients/eventsFederationApolloClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ export function createApolloClient() {
7878
});
7979
});
8080
const httpLink = getHttpLink(AppConfig.federationGraphqlEndpoint);
81-
const errorLink = onError(({ graphQLErrors, networkError }) => {
81+
const errorLink = onError(({ graphQLErrors, networkError, operation }) => {
8282
if (graphQLErrors) {
8383
graphQLErrors.forEach(({ message, locations, path }) => {
84-
const errorMessage = `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`;
84+
const errorMessage = `[GraphQL error]: OperationName: ${operation.operationName}, Message: ${message}, Location: ${locations}, Path: ${path}`;
8585
graphqlClientLogger.error(errorMessage);
8686
Sentry.captureMessage(errorMessage);
8787
});

apps/hobbies-helsinki/src/domain/app/getHobbiesStaticProps.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import type { ApolloClient, NormalizedCacheObject } from '@apollo/client';
22
import { isApolloError } from '@apollo/client';
3-
import type {
4-
AppLanguage,
5-
CmsLanguage,
6-
Menu,
7-
Language,
8-
} from 'events-helsinki-components';
3+
import type { CmsLanguage, Menu, Language } from 'events-helsinki-components';
94
import {
105
DEFAULT_FOOTER_MENU_NAME,
116
DEFAULT_HEADER_MENU_NAME,
@@ -63,7 +58,8 @@ export default async function getHobbiesStaticProps<P = Record<string, any>>(
6358
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6459
} catch (e: any) {
6560
// Generic error handling
66-
staticGenerationLogger.error('Error while generating a page:', e);
61+
staticGenerationLogger.error(`Error while generating a page: ${e}`, e);
62+
6763
if (isApolloError(e)) {
6864
return {
6965
props: {
@@ -95,8 +91,8 @@ async function getGlobalCMSData({
9591
client,
9692
context,
9793
}: GetGlobalCMSDataParams): Promise<ReturnedGlobalCMSData> {
98-
const locale: AppLanguage = (context?.locale ?? 'fi') as AppLanguage;
99-
const headerNavigationMenuName = DEFAULT_HEADER_MENU_NAME[locale];
94+
const language = getLanguageOrDefault(context.locale);
95+
const headerNavigationMenuName = DEFAULT_HEADER_MENU_NAME[language];
10096
const { data: headerMenuData } = await client.query({
10197
query: MenuDocument,
10298
variables: {
@@ -114,7 +110,7 @@ async function getGlobalCMSData({
114110
},
115111
});
116112

117-
const footerNavigationMenuName = DEFAULT_FOOTER_MENU_NAME[locale];
113+
const footerNavigationMenuName = DEFAULT_FOOTER_MENU_NAME[language];
118114
const { data: footerMenuData } = await client.query({
119115
query: MenuDocument,
120116
variables: {

apps/hobbies-helsinki/src/domain/clients/eventsFederationApolloClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ export function createApolloClient() {
7878
});
7979
});
8080
const httpLink = getHttpLink(AppConfig.federationGraphqlEndpoint);
81-
const errorLink = onError(({ graphQLErrors, networkError }) => {
81+
const errorLink = onError(({ graphQLErrors, networkError, operation }) => {
8282
if (graphQLErrors) {
8383
graphQLErrors.forEach(({ message, locations, path }) => {
84-
const errorMessage = `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`;
84+
const errorMessage = `[GraphQL error]: OperationName: ${operation.operationName}, Message: ${message}, Location: ${locations}, Path: ${path}`;
8585
graphqlClientLogger.error(errorMessage);
8686
Sentry.captureMessage(errorMessage);
8787
});

apps/sports-helsinki/src/domain/app/getSportsStaticProps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default async function getSportsStaticProps<P = Record<string, any>>(
5858
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5959
} catch (e: any) {
6060
// Generic error handling
61-
staticGenerationLogger.error('Error while generating a page:', e);
61+
staticGenerationLogger.error(`Error while generating a page: ${e}`, e);
6262
if (isApolloError(e)) {
6363
return {
6464
props: {

apps/sports-helsinki/src/domain/clients/eventsFederationApolloClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ export function createApolloClient() {
7878
});
7979
});
8080
const httpLink = getHttpLink(AppConfig.federationGraphqlEndpoint);
81-
const errorLink = onError(({ graphQLErrors, networkError }) => {
81+
const errorLink = onError(({ graphQLErrors, networkError, operation }) => {
8282
if (graphQLErrors) {
8383
graphQLErrors.forEach(({ message, locations, path }) => {
84-
const errorMessage = `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`;
84+
const errorMessage = `[GraphQL error]: OperationName: ${operation.operationName}, Message: ${message}, Location: ${locations}, Path: ${path}`;
8585
graphqlClientLogger.error(errorMessage);
8686
Sentry.captureMessage(errorMessage);
8787
});

0 commit comments

Comments
 (0)