From f3c95fa3de5b90859fde71cfe387dec8cc064fba Mon Sep 17 00:00:00 2001 From: Alex Johansson Date: Tue, 16 Nov 2021 18:12:08 +0100 Subject: [PATCH] fix i18n flicker on auth pages (#1183) * fix flicker on `/auth/login` * fix flicker on logout * fix flicker on error * fix i18n flicker on signup --- pages/auth/error.tsx | 13 +++++++++++++ pages/auth/login.tsx | 29 ++++++++++++++++++++--------- pages/auth/logout.tsx | 13 +++++++++++++ pages/auth/signup.tsx | 15 +++++++++++++-- 4 files changed, 59 insertions(+), 11 deletions(-) diff --git a/pages/auth/error.tsx b/pages/auth/error.tsx index f9a05dd944..0571b3e550 100644 --- a/pages/auth/error.tsx +++ b/pages/auth/error.tsx @@ -1,4 +1,5 @@ import { XIcon } from "@heroicons/react/outline"; +import { GetServerSidePropsContext } from "next"; import Link from "next/link"; import { useRouter } from "next/router"; @@ -6,6 +7,8 @@ import { useLocale } from "@lib/hooks/useLocale"; import { HeadSeo } from "@components/seo/head-seo"; +import { ssrInit } from "@server/lib/ssr"; + export default function Error() { const { t } = useLocale(); const router = useRouter(); @@ -48,3 +51,13 @@ export default function Error() { ); } + +export async function getServerSideProps(context: GetServerSidePropsContext) { + const ssr = await ssrInit(context); + + return { + props: { + trpcState: ssr.dehydrate(), + }, + }; +} diff --git a/pages/auth/login.tsx b/pages/auth/login.tsx index 3d77ca849d..51d805b295 100644 --- a/pages/auth/login.tsx +++ b/pages/auth/login.tsx @@ -1,3 +1,4 @@ +import { GetServerSidePropsContext } from "next"; import { getCsrfToken, signIn } from "next-auth/client"; import Link from "next/link"; import { useRouter } from "next/router"; @@ -5,12 +6,15 @@ import { useState } from "react"; import { ErrorCode, getSession } from "@lib/auth"; import { useLocale } from "@lib/hooks/useLocale"; +import { inferSSRProps } from "@lib/types/inferSSRProps"; import AddToHomescreen from "@components/AddToHomescreen"; import Loader from "@components/Loader"; import { HeadSeo } from "@components/seo/head-seo"; -export default function Login({ csrfToken }) { +import { ssrInit } from "@server/lib/ssr"; + +export default function Login({ csrfToken }: inferSSRProps) { const { t } = useLocale(); const router = useRouter(); const [email, setEmail] = useState(""); @@ -90,7 +94,7 @@ export default function Login({ csrfToken }) {
- +
); } + +export async function getServerSideProps(context: GetServerSidePropsContext) { + const ssr = await ssrInit(context); + + return { + props: { + trpcState: ssr.dehydrate(), + }, + }; +} diff --git a/pages/auth/signup.tsx b/pages/auth/signup.tsx index b1733fee93..7b28e1087f 100644 --- a/pages/auth/signup.tsx +++ b/pages/auth/signup.tsx @@ -13,6 +13,8 @@ import { HeadSeo } from "@components/seo/head-seo"; import { Alert } from "@components/ui/Alert"; import Button from "@components/ui/Button"; +import { ssrInit } from "@server/lib/ssr"; + type Props = inferSSRProps; type FormValues = { @@ -133,6 +135,7 @@ export default function Signup({ email }: Props) { } export const getServerSideProps = async (ctx: GetServerSidePropsContext) => { + const ssr = await ssrInit(ctx); const token = asStringOrNull(ctx.query.token); if (!token) { return { @@ -169,9 +172,17 @@ export const getServerSideProps = async (ctx: GetServerSidePropsContext) => { if (existingUser) { return { - redirect: { permanent: false, destination: "/auth/login?callbackUrl=" + ctx.query.callbackUrl }, + redirect: { + permanent: false, + destination: "/auth/login?callbackUrl=" + ctx.query.callbackUrl, + }, }; } - return { props: { email: verificationRequest.identifier } }; + return { + props: { + email: verificationRequest.identifier, + trpcState: ssr.dehydrate(), + }, + }; };