Files
webstudio/apps/builder/app/shared/context.server.ts
T
Bogdan Chadkin 3edd4ea3cc Optimize canvas loading (#1310)
Ref https://github.com/webstudio-is/webstudio-builder/issues/1044
https://github.com/webstudio-is/webstudio-builder/issues/360

Canvas no longer need to load any data. No longer need pages. No longer
need auth token. No longer need static css.

Here I cleaned all unnecessary logic to load canvas. It may load much
faster now.
2023-03-28 19:02:02 +07:00

47 lines
1.3 KiB
TypeScript

import type { AppContext } from "@webstudio-is/trpc-interface/server";
import { authenticator } from "~/services/auth.server";
import { trpcClient } from "~/services/trpc.server";
const createAuthorizationContext = async (
request: Request,
buildEnv: AppContext["authorization"]["buildEnv"]
): Promise<AppContext["authorization"]> => {
const originUrl = new URL(request.url);
let url = originUrl;
// In case of css use referrer as url to extract tokens from canvas iframe href
if (originUrl.pathname === "/s/css") {
const referer = request.headers.get("referer");
if (referer !== null) {
url = new URL(referer);
}
}
const authToken = url.searchParams.get("authToken") ?? url.hostname;
const user = await authenticator.isAuthenticated(request);
const context: AppContext["authorization"] = {
userId: user?.id,
authToken,
buildEnv,
authorizeTrpc: trpcClient.authorize,
};
return context;
};
/**
* argument buildEnv==="prod" only if we are loading project with production build
*/
export const createContext = async (
request: Request,
buildEnv: AppContext["authorization"]["buildEnv"] = "dev"
): Promise<AppContext> => {
const authorization = await createAuthorizationContext(request, buildEnv);
return {
authorization,
};
};