Files
webstudio/apps/builder/app/shared/db/canvas.server.ts
T
Ivan Starkov 8be665e68d experimental: SSG download support (#3783)
## Description

See corresponding [SaaS
PR](https://github.com/webstudio-is/webstudio-saas/pull/311) too

## Steps for reproduction

Click Publish/Export

<img width="363" alt="image"
src="https://github.com/user-attachments/assets/fbc1f4e5-3a2c-4bb9-9242-025196f991bc">


Deploy zip to netlify
<img width="1710" alt="image"
src="https://github.com/user-attachments/assets/daca4ead-6272-4729-8e82-0986301e5c8b">


## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
5de6)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env` file
2024-07-30 08:57:33 +03:00

94 lines
2.5 KiB
TypeScript

import type { Data } from "@webstudio-is/http-client";
import { loadBuildById } from "@webstudio-is/project-build/index.server";
import { loadAssetsByProject } from "@webstudio-is/asset-uploader/index.server";
import type { AppContext } from "@webstudio-is/trpc-interface/index.server";
import { findPageByIdOrPath } from "@webstudio-is/sdk";
import type { Build } from "@webstudio-is/prisma-client";
import { db as domainDb } from "@webstudio-is/domain/index.server";
export const loadProductionCanvasData = async (
buildId: Build["id"],
context: AppContext
): Promise<Data> => {
const build = await loadBuildById(context, buildId);
if (build === undefined) {
throw new Error("The project is not published");
}
const { deployment } = build;
if (deployment === undefined) {
throw new Error("The project is not published");
}
const currentProjectDomainsResult = await domainDb.findMany(
{ projectId: build.projectId },
context
);
if (currentProjectDomainsResult.success === false) {
throw new Error(currentProjectDomainsResult.error);
}
const currentProjectDomains = currentProjectDomainsResult.data;
// Check that build deployment domains are still active and verified
// for examle: redeploy created few days later
const domains =
deployment.destination === "static"
? []
: deployment.domains.filter((domain) =>
currentProjectDomains.some(
(projectDomain) =>
projectDomain.domain.domain === domain &&
projectDomain.domain.status === "ACTIVE" &&
projectDomain.verified
)
);
const page = findPageByIdOrPath("/", build.pages);
if (page === undefined) {
throw new Error(`Page / not found`);
}
const allAssets = await loadAssetsByProject(build.projectId, context);
const canvasData = {
build: {
...build,
deployment: {
...deployment,
domains,
},
},
page,
pages: [build.pages.homePage, ...build.pages.pages],
};
const styles = canvasData.build?.styles ?? [];
// Find all fonts referenced in styles
const fontFamilySet = new Set<string>();
for (const [, { value }] of styles) {
if (value.type === "fontFamily") {
for (const fontFamily of value.value) {
fontFamilySet.add(fontFamily);
}
}
}
// Filter unused font assets
const assets = allAssets.filter(
(asset) =>
asset.type === "image" ||
(asset.type === "font" && fontFamilySet.has(asset.meta.family))
);
return {
...canvasData,
assets,
};
};