Skip to content

Commit

Permalink
upload: refactor - setProjectForSSR(), etc. (#869)
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz authored Jan 5, 2025
1 parent 943ce83 commit f751aaf
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ MyDocument.getInitialProps = async (
// server intl is not available in App, only in this file (because we don't want to sent messages over and over again)
const serverIntl = await getServerIntl(ctx);
setIntl(serverIntl); // for ssr
setProjectForSSR(ctx);
setProjectForSSR(ctx.req);

const initialProps = await documentGetInitialProps(ctx);

Expand Down
4 changes: 4 additions & 0 deletions src/services/intl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MessagesType, TranslationId } from './types';
import { isBrowser, isServer } from '../components/helpers';
import { getServerIntl } from './intlServer';
import { publishDbgObject } from '../utils';
import { LANGUAGES } from '../config.mjs';

type Values = { [variable: string]: string | number };

Expand Down Expand Up @@ -62,6 +63,9 @@ export const changeLang = (langId: string) => {

export const setIntl = (initialIntl: Intl) => {
if (initialIntl) {
if (!LANGUAGES[initialIntl.lang]) {
throw new Error(`Invalid language: ${initialIntl.lang}`);
}
intl.lang = initialIntl.lang;
intl.messages = initialIntl.messages;
publishDbgObject('intl', intl);
Expand Down
5 changes: 3 additions & 2 deletions src/services/project.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { DocumentContext } from 'next/dist/shared/lib/utils';
import { publishDbgObject } from '../utils';
import { isBrowser } from '../components/helpers';
import type { TranslationId } from './types';
import { IncomingMessage } from 'node:http';

type Project = {
id: string;
Expand Down Expand Up @@ -64,8 +65,8 @@ const setProject = (host: string) => {
};

// server - runs in document getInitialProps()
export const setProjectForSSR = (ctx: DocumentContext) => {
const { host } = ctx.req.headers;
export const setProjectForSSR = (req: IncomingMessage) => {
const { host } = req.headers;
setProject(host);
};

Expand Down
2 changes: 1 addition & 1 deletion src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export interface Feature {
osmappLabel?: string;
};
center: Position;
countryCode?: string; // ISO3166-1 code, undefined = no country
countryCode?: string; // ISO3166-1 code lowercase, undefined = no country
roundedCenter?: LonLatRounded;
error?: 'network' | 'unknown' | '404' | '500'; // etc.
deleted?: boolean;
Expand Down

0 comments on commit f751aaf

Please sign in to comment.