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

upload: refactor - setProjectForSSR(), etc. #869

Merged
merged 1 commit into from
Jan 5, 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 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
Loading