Fix theme error on ssr

This commit is contained in:
Zoe Roux 2024-03-17 23:16:55 +01:00
parent b6d122e449
commit 64031668c1
No known key found for this signature in database
2 changed files with 31 additions and 29 deletions

View File

@ -210,37 +210,40 @@ App.getInitialProps = async (ctx: AppContext) => {
if (typeof window !== "undefined") return { pageProps: superjson.serialize(appProps.pageProps) }; if (typeof window !== "undefined") return { pageProps: superjson.serialize(appProps.pageProps) };
const getUrl = Component.getFetchUrls; try {
const getLayoutUrl = const getUrl = Component.getFetchUrls;
Component.getLayout && "Layout" in Component.getLayout const getLayoutUrl =
? Component.getLayout.Layout.getFetchUrls Component.getLayout && "Layout" in Component.getLayout
: Component.getLayout?.getFetchUrls; ? Component.getLayout.Layout.getFetchUrls
const urls: QueryIdentifier[] = [ : Component.getLayout?.getFetchUrls;
...(getUrl ? getUrl(ctx.router.query as any, items) : []), const urls: QueryIdentifier[] = [
...(getLayoutUrl ? getLayoutUrl(ctx.router.query as any, items) : []), ...(getUrl ? getUrl(ctx.router.query as any, items) : []),
// always include server info for guest permissions. ...(getLayoutUrl ? getLayoutUrl(ctx.router.query as any, items) : []),
{ path: ["info"], parser: ServerInfoP }, // always include server info for guest permissions.
]; { path: ["info"], parser: ServerInfoP },
];
setSsrApiUrl(); setSsrApiUrl();
const account = readCookie(ctx.ctx.req?.headers.cookie, "account", AccountP); const account = readCookie(ctx.ctx.req?.headers.cookie, "account", AccountP);
if (account) urls.push({ path: ["auth", "me"], parser: UserP }); if (account) urls.push({ path: ["auth", "me"], parser: UserP });
const [authToken, token, error] = await getTokenWJ(account); const [authToken, token, error] = await getTokenWJ(account);
if (error) appProps.pageProps.ssrError = error; if (error) appProps.pageProps.ssrError = error;
else { else {
const client = (await fetchQuery(urls, authToken))!; const client = (await fetchQuery(urls, authToken))!;
appProps.pageProps.queryState = dehydrate(client); appProps.pageProps.queryState = dehydrate(client);
if (account) { if (account) {
appProps.pageProps.token = token; appProps.pageProps.token = token;
appProps.pageProps.account = { appProps.pageProps.account = {
...client.getQueryData(["auth", "me"]), ...client.getQueryData(["auth", "me"]),
...account, ...account,
}; };
}
} }
appProps.pageProps.theme = readCookie(ctx.ctx.req?.headers.cookie, "theme") ?? "auto";
} catch (e) {
console.error("SSR error, disabling it.");
} }
appProps.pageProps.theme = readCookie(ctx.ctx.req?.headers.cookie, "theme") ?? "auto";
return { pageProps: superjson.serialize(appProps.pageProps) }; return { pageProps: superjson.serialize(appProps.pageProps) };
}; };

View File

@ -62,8 +62,7 @@ export const readCookie = <T extends ZodTypeAny>(
} }
if (c.indexOf(name) == 0) { if (c.indexOf(name) == 0) {
const str = c.substring(name.length, c.length); const str = c.substring(name.length, c.length);
const ret = JSON.parse(str); return parser ? parser.parse(JSON.parse(str)) : str;
return parser ? parser.parse(ret) : ret;
} }
} }
return null; return null;