Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Fix font assets resolving (#1443)

Bogdan Chadkin22962e1a460c
diff --git a/apps/builder/app/canvas/shared/styles.ts b/apps/builder/app/canvas/shared/styles.ts
index 42af654f6c..4b2261223c 100644
--- a/apps/builder/app/canvas/shared/styles.ts
+++ b/apps/builder/app/canvas/shared/styles.ts
@@ -125,7 +125,12 @@ export const GlobalStyles = () => {
useIsomorphicLayoutEffect(() => {
fontsAndDefaultsCssEngine.clear();
- addGlobalRules(fontsAndDefaultsCssEngine, { assets });
+ const params = getParams();
+ addGlobalRules(fontsAndDefaultsCssEngine, {
+ assets,
+ publicPath: params.publicPath,
+ cdnUrl: params.cdnUrl,
+ });
fontsAndDefaultsCssEngine.render();
}, [assets]);
diff --git a/packages/fonts/src/get-font-faces.test.ts b/packages/fonts/src/get-font-faces.test.ts
index 72b1fe5ffe..28c6fbd2f6 100644
--- a/packages/fonts/src/get-font-faces.test.ts
+++ b/packages/fonts/src/get-font-faces.test.ts
@@ -11,7 +11,8 @@ describe("getFontFaces()", () => {
style: "normal",
weight: 400,
},
- path: "/fonts/roboto.woff",
+ location: "FS",
+ name: "roboto.woff",
},
{
format: "ttf",
@@ -20,10 +21,11 @@ describe("getFontFaces()", () => {
style: "normal",
weight: 400,
},
- path: "/fonts/roboto.ttf",
+ location: "FS",
+ name: "roboto.ttf",
},
];
- expect(getFontFaces(assets)).toMatchSnapshot();
+ expect(getFontFaces(assets, { publicPath: "/fonts/" })).toMatchSnapshot();
});
test("different style", () => {
@@ -35,7 +37,8 @@ describe("getFontFaces()", () => {
style: "normal",
weight: 400,
},
- path: "/fonts/roboto.ttf",
+ location: "FS",
+ name: "roboto.ttf",
},
{
format: "ttf",
@@ -44,10 +47,11 @@ describe("getFontFaces()", () => {
style: "italic",
weight: 400,
},
- path: "/fonts/roboto-italic.ttf",
+ location: "FS",
+ name: "roboto-italic.ttf",
},
];
- expect(getFontFaces(assets)).toMatchSnapshot();
+ expect(getFontFaces(assets, { publicPath: "/fonts/" })).toMatchSnapshot();
});
test("different weight", () => {
@@ -59,7 +63,8 @@ describe("getFontFaces()", () => {
style: "normal",
weight: 400,
},
- path: "/fonts/roboto.ttf",
+ location: "FS",
+ name: "roboto.ttf",
},
{
format: "ttf",
@@ -68,10 +73,11 @@ describe("getFontFaces()", () => {
style: "normal",
weight: 500,
},
- path: "/fonts/roboto-bold.ttf",
+ location: "FS",
+ name: "roboto-bold.ttf",
},
];
- expect(getFontFaces(assets)).toMatchSnapshot();
+ expect(getFontFaces(assets, { publicPath: "/fonts/" })).toMatchSnapshot();
});
test("variable font", () => {
@@ -96,9 +102,10 @@ describe("getFontFaces()", () => {
YTFI: { name: "YTFI", min: 560, default: 738, max: 788 },
},
},
- path: "/fonts/inter.ttf",
+ location: "FS",
+ name: "inter.ttf",
},
];
- expect(getFontFaces(assets)).toMatchSnapshot();
+ expect(getFontFaces(assets, { publicPath: "/fonts/" })).toMatchSnapshot();
});
});
diff --git a/packages/fonts/src/get-font-faces.ts b/packages/fonts/src/get-font-faces.ts
index cf9298a6f7..be2c2eec35 100644
--- a/packages/fonts/src/get-font-faces.ts
+++ b/packages/fonts/src/get-font-faces.ts
@@ -4,7 +4,8 @@ import type { FontMeta, FontFormat, FontMetaStatic } from "./schema";
export type PartialFontAsset = {
format: FontFormat;
meta: FontMeta;
- path: string;
+ name: string;
+ location: "FS" | "REMOTE";
};
export type FontFace = {
@@ -16,14 +17,18 @@ export type FontFace = {
fontStretch?: string;
};
-const formatFace = (asset: PartialFontAsset, format: string): FontFace => {
+const formatFace = (
+ asset: PartialFontAsset,
+ format: string,
+ url: string
+): FontFace => {
if ("variationAxes" in asset.meta) {
const { wght, wdth } = asset.meta?.variationAxes ?? {};
return {
fontFamily: asset.meta.family,
fontStyle: "normal",
fontDisplay: "swap",
- src: `url('${asset.path}') format('${format}')`,
+ src: `url('${url}') format('${format}')`,
fontStretch: wdth ? `${wdth.min}% ${wdth.max}%` : undefined,
fontWeight: wght ? `${wght.min} ${wght.max}` : undefined,
};
@@ -33,7 +38,7 @@ const formatFace = (asset: PartialFontAsset, format: string): FontFace => {
fontStyle: asset.meta.style,
fontWeight: asset.meta.weight,
fontDisplay: "swap",
- src: `url('${asset.path}') format('${format}')`,
+ src: `url('${url}') format('${format}')`,
};
};
@@ -45,10 +50,19 @@ const getKey = (asset: PartialFontAsset) => {
};
export const getFontFaces = (
- assets: Array<PartialFontAsset>
+ assets: Array<PartialFontAsset>,
+ options: {
+ publicPath?: string;
+ cdnUrl?: string;
+ }
): Array<FontFace> => {
+ const { publicPath = "/", cdnUrl = "/" } = options;
const faces = new Map();
for (const asset of assets) {
+ const url =
+ asset.location === "REMOTE"
+ ? `${cdnUrl}${asset.name}`
+ : `${publicPath}${asset.name}`;
const assetKey = getKey(asset);
const face = faces.get(assetKey);
const format = FONT_FORMATS.get(asset.format);
@@ -58,13 +72,13 @@ export const getFontFaces = (
}
if (face === undefined) {
- const face = formatFace(asset, format);
+ const face = formatFace(asset, format, url);
faces.set(assetKey, face);
continue;
}
// We already have that font face, so we need to add the new src
- face.src += `, url('${asset.path}') format('${format}')`;
+ face.src += `, url('${url}') format('${format}')`;
}
return Array.from(faces.values());
};
diff --git a/packages/react-sdk/src/css/css.ts b/packages/react-sdk/src/css/css.ts
index dbf952ca25..6e5f075cfc 100644
--- a/packages/react-sdk/src/css/css.ts
+++ b/packages/react-sdk/src/css/css.ts
@@ -56,7 +56,11 @@ export const generateCssText = (data: Data, options: CssOptions) => {
const engine = createCssEngine({ name: "ssr" });
- addGlobalRules(engine, { assets });
+ addGlobalRules(engine, {
+ assets,
+ publicPath: options.publicPath,
+ cdnUrl: options.cdnUrl,
+ });
for (const breakpoint of breakpoints.values()) {
engine.addMediaRule(breakpoint.id, breakpoint);
diff --git a/packages/react-sdk/src/css/global-rules.ts b/packages/react-sdk/src/css/global-rules.ts
index 26bc86212e..359c0f30e5 100644
--- a/packages/react-sdk/src/css/global-rules.ts
+++ b/packages/react-sdk/src/css/global-rules.ts
@@ -1,28 +1,31 @@
import type { CssEngine } from "@webstudio-is/css-engine";
import type { Assets, FontAsset } from "@webstudio-is/asset-uploader";
-import {
- type FontFormat,
- FONT_FORMATS,
- getFontFaces,
-} from "@webstudio-is/fonts";
+import { getFontFaces } from "@webstudio-is/fonts";
export const addGlobalRules = (
engine: CssEngine,
- { assets }: { assets: Assets }
+ {
+ assets,
+ publicPath,
+ cdnUrl,
+ }: { assets: Assets; publicPath?: string; cdnUrl?: string }
) => {
// @todo we need to figure out all global resets while keeping
// the engine aware of all of them.
// Ideally, the user is somehow aware and in control of the reset
engine.addPlaintextRule("html {margin: 0; height: 100%}");
- const fontAssets: Array<FontAsset> = [];
+ const fontAssets: FontAsset[] = [];
for (const asset of assets.values()) {
- if (asset && FONT_FORMATS.has(asset.format as FontFormat)) {
- fontAssets.push(asset as FontAsset);
+ if (asset?.type === "font") {
+ fontAssets.push(asset);
}
}
- const fontFaces = getFontFaces(fontAssets);
+ const fontFaces = getFontFaces(fontAssets, {
+ publicPath,
+ cdnUrl,
+ });
for (const fontFace of fontFaces) {
engine.addFontFaceRule(fontFace);
}