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

Typed get server side props #262

Merged
merged 3 commits into from
Aug 13, 2021
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
5 changes: 5 additions & 0 deletions .changeset/eight-ways-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'web-app': minor
---

Example with typed getServerSideProps
5 changes: 5 additions & 0 deletions .changeset/many-monkeys-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'web-app': minor
---

Refactor app structure and configs
9 changes: 0 additions & 9 deletions apps/web-app/src/config/features.config.ts

This file was deleted.

3 changes: 3 additions & 0 deletions apps/web-app/src/core/i18n/i18n-namespaces.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { CustomTypeOptions } from 'react-i18next';

export type I18nNamespaces = (keyof CustomTypeOptions['resources'])[];
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const PoemGridWithReactQueryAndKy: React.FC<NoChildrenProps> = () => {
return <>{data && <PoemList poems={data} />}</>;
};

export const DemoApiSection: React.FC<NoChildrenProps> = () => {
export const DemoApiBlock: React.FC<NoChildrenProps> = () => {
return (
<div>
<div className="lg:container lg:mx-auto">
Expand Down
8 changes: 6 additions & 2 deletions apps/web-app/src/features/demo/demo.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { FeaturesConfig } from '@/config/features.config';
import { I18nNamespaces } from '@/core/i18n/i18n-namespaces.type';

export const demoConfig: FeaturesConfig = {
export type DemoConfig = {
i18nNamespaces: Readonly<I18nNamespaces>;
};

export const demoConfig: DemoConfig = {
i18nNamespaces: ['common', 'demo'],
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { NextSeo } from 'next-seo';
import { MainLayout } from '@/components/layout/main-layout';
import { Banner } from '@/components/banner';
import { useTranslation } from 'next-i18next';
import { demoConfig } from './demo.config';
import { demoConfig } from '../demo.config';
import { sayHello } from '@your-org/core-lib';
import { InfoCard } from '@your-org/ui-lib/component/info-card';
import { AsyncMessage, Message } from '@your-org/ui-lib';
import Image from 'next/image';
import { DemoApiSection } from './sections/demo-api.section';
import { DemoApiBlock } from '../blocks/demo-api.block';

type Props = {
children?: never;
Expand Down Expand Up @@ -41,7 +41,7 @@ export const DemoPage: React.FC<Props> = () => {
width={400}
height={240}
/>
<DemoApiSection />
<DemoApiBlock />
</MainLayout>
</>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type Props = {
children?: never;
};
export const CTASection: React.FC<Props> = () => {
export const CtaBlock: React.FC<Props> = () => {
return (
<div className="bg-gray-50">
<div className="max-w-7xl mx-auto py-12 px-4 sm:px-6 lg:py-16 lg:px-8 lg:flex lg:items-center lg:justify-between">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const Ctn = styled.div`
);
`;

export const FeaturesSection: React.FC<Props> = () => {
export const FeaturesBlock: React.FC<Props> = () => {
return (
<Ctn>
<div className="py-12 bg-white" style={{ width: '100%' }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const navigation = [
{ name: 'Company', href: '#' },
];

export const HeroSection: React.FC<Props> = () => {
export const HeroBlock: React.FC<Props> = () => {
const { t } = useTranslation(['home', 'common']);

return (
Expand Down
7 changes: 5 additions & 2 deletions apps/web-app/src/features/home/home.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { FeaturesConfig } from '@/config/features.config';
import { I18nNamespaces } from '@/core/i18n/i18n-namespaces.type';

export const homeConfig: FeaturesConfig = {
export type HomeConfig = {
i18nNamespaces: Readonly<I18nNamespaces>;
};
export const homeConfig: HomeConfig = {
/** Namespaces that should be loaded for this page */
i18nNamespaces: ['common', 'home'],
} as const;
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { NextSeo } from 'next-seo';
import { MainLayout } from '@/components/layout/main-layout';
import { Banner } from '@/components/banner';
import { HeroSection } from './sections/hero-section';
import { FeaturesSection } from './sections/features-section';
import { CTASection } from './sections/cta-section';
import { HeroBlock } from '../blocks/hero-block';
import { FeaturesBlock } from '../blocks/features-block';
import { CtaBlock } from '../blocks/cta-block';
import { useTranslation } from 'next-i18next';
import { homeConfig } from './home.config';
import { homeConfig } from '../home.config';

type Props = {
children?: never;
Expand All @@ -22,9 +22,9 @@ export const HomePage: React.FC<Props> = () => {
/>
<MainLayout>
<Banner />
<HeroSection />
<FeaturesSection />
<CTASection />
<HeroBlock />
<FeaturesBlock />
<CtaBlock />
</MainLayout>
</>
);
Expand Down
23 changes: 16 additions & 7 deletions apps/web-app/src/pages/demo.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import { GetServerSideProps } from 'next';
import { GetServerSideProps, InferGetServerSidePropsType } from 'next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import { BadRequest } from '@tsed/exceptions';
import { DemoPage } from '../features/demo/demo.page';
import { demoConfig } from '../features/demo/demo.config';
import { DemoPage } from '@/features/demo/pages/demo.page';
import { demoConfig } from '@/features/demo/demo.config';

export default function DemoRoute() {
type Props = {
/** Add HomeRoute props here */
};

export default function DemoRoute(
props: InferGetServerSidePropsType<typeof getServerSideProps>
) {
return <DemoPage />;
}

export const getServerSideProps: GetServerSideProps = async ({ locale }) => {
export const getServerSideProps: GetServerSideProps<Props> = async (
context
) => {
const { locale } = context;
if (locale === undefined) {
throw new BadRequest('locale is missing');
}
const { i18nNamespaces } = demoConfig;
return {
props: {
// @see https:/github.com/i18next/react-i18next/pull/1340#issuecomment-874728587
...(await serverSideTranslations(locale, i18nNamespaces?.slice())),
// i18nNamespaces.slice() is needed here to get rid off readonly
...(await serverSideTranslations(locale, i18nNamespaces.slice())),
},
};
};
22 changes: 16 additions & 6 deletions apps/web-app/src/pages/home.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import { GetServerSideProps } from 'next';
import { HomePage } from '../features/home/home.page';
import { GetServerSideProps, InferGetServerSidePropsType } from 'next';
import { HomePage } from '@/features/home/pages/home.page';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import { BadRequest } from '@tsed/exceptions';
import { homeConfig } from '../features/home/home.config';
import { homeConfig } from '@/features/home/home.config';

export default function HomeRoute() {
type Props = {
/** Add HomeRoute props here */
};

export default function HomeRoute(
props: InferGetServerSidePropsType<typeof getServerSideProps>
) {
return <HomePage />;
}

export const getServerSideProps: GetServerSideProps = async ({ locale }) => {
export const getServerSideProps: GetServerSideProps<Props> = async (
context
) => {
const { locale } = context;
if (locale === undefined) {
throw new BadRequest('locale is missing');
}
const { i18nNamespaces } = homeConfig;
return {
props: {
...(await serverSideTranslations(locale, i18nNamespaces?.slice())),
// i18nNamespaces.slice() is needed here to get rid off readonly
...(await serverSideTranslations(locale, i18nNamespaces.slice())),
},
};
};
1 change: 1 addition & 0 deletions apps/web-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"paths": {
"@/test-utils": ["../config/jest/test-utils"],
"@/config/*": ["./config/*"],
"@/core/*": ["./core/*"],
"@/components/*": ["./components/*"],
"@/backend/*": ["./backend/*"],
"@/features/*": ["./features/*"],
Expand Down