Replies: 12 comments 6 replies
-
@mxswat I'm having the same problem. Did you manage to find a solution? |
Beta Was this translation helpful? Give feedback.
-
Sadly no.
…On Wed, Jun 14, 2023, 8:49 AM Sergio Toshio Minei ***@***.***> wrote:
@mxswat <https://github.com/mxswat> I'm having the same problem. Did you
manage to find a solution?
—
Reply to this email directly, view it on GitHub
<#49860 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEIL4ABWVJPVWJ6HTYUU57TXLFUG5ANCNFSM6AAAAAAYDV7XLY>
.
You are receiving this because you were mentioned.Message ID: <vercel/next
.***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Aren't these inlined into your project? Like, The main thing here is that I am not sure the other files are copied to the edge. As far as I understood, only the edge bundle is copied. Maybe I am way off though. |
Beta Was this translation helpful? Give feedback.
-
Yes they are in line, but I'd like a reliable way to access that path.
Because my main concern is that if that path changes, the image generation
will break.
…On Wed, Jun 14, 2023, 10:05 AM Joseph ***@***.***> wrote:
Aren't these inlined into your project? Like,
https://mysite.tld/_next/static/media/b323b5bc770ba6cb-s.p.woff2 - I am
not sure it is possible to get these in the edge either. I once saw
someone who made a GET request for a site, and then parse the HTML you
receive and look for the link tag that has the font.
The main thing here is that I am not sure the other files are copied to
the edge. As far as I understood, only the edge bundle is copied. Maybe I
am way off though.
—
Reply to this email directly, view it on GitHub
<#49860 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEIL4AHJO7ZQHEP3FACRY3TXLF5D7ANCNFSM6AAAAAAYDV7XLY>
.
You are receiving this because you were mentioned.Message ID: <vercel/next
.***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I’d also be interested in accessing the URL to the font (eg. Our use case is an iframe we embed. We’re able to pass in a Another example of this use-case is customizing the font for Stripe Elements embeds: |
Beta Was this translation helpful? Give feedback.
-
+1. Use case is reusing a brand-wide default font across multiple web based projects with the Next.JS app being the "main" app of the company. |
Beta Was this translation helpful? Give feedback.
-
the only way I found is the fetch to the API route, with node.js check like I did below import { promises as fs } from "fs";
// ...
const file = await fs
.readFile(process.cwd() + "/.next/server/next-font-manifest.json", "utf8")
.catch(() => "");
const data = ((file && JSON.parse(file)) || {}) as {
pages: {};
app: { [k: string]: string[] };
};
const layoutKey = Object.keys(data?.app).shift();
const layoutFont = layoutKey && data.app[layoutKey]?.at(0); |
Beta Was this translation helpful? Give feedback.
-
Here is how I did it, You need to have it on a folder to load it. // Image generation
export default async function OGImage({
params,
}: {
params: { slug: string };
}) {
// Font
const barlowCondensed600 = fetch(
new URL("../../fonts/BarlowCondensed-600.ttf", import.meta.url),
).then((res) => res.arrayBuffer());
const barlow500 = fetch(
new URL("../../fonts/Barlow-500.ttf", import.meta.url),
).then((res) => res.arrayBuffer());
return new ImageResponse(
(
// ImageResponse JSX element
<div
style={{
width: 1200,
height: 630,
display: "flex",
flexDirection: "row",
justifyContent: "center",
position: "relative",
background: "#0C0A09",
}}
>
<div
style={{
left: 120,
top: 50,
position: "absolute",
display: "flex",
flexDirection: "row",
justifyContent: "flex-start",
}}
>
<p
style={{
width: 912,
height: "auto",
maxHeight: "302.4px",
color: "white",
fontSize: 96,
fontFamily: "Barlow Condensed",
fontWeight: "600",
wordWrap: "break-word",
textTransform: "uppercase",
lineHeight: "105%",
display: "-webkit-box",
WebkitLineClamp: 3,
WebkitBoxOrient: "vertical",
overflow: "hidden",
}}
>
{deSlugify(params.slug)}
</p>
</div>
<div
style={{
left: 120,
top: 440,
position: "absolute",
display: "flex",
flexDirection: "row",
justifyContent: "flex-start",
}}
>
<span
style={{
color: "white",
fontSize: 64,
fontFamily: "Barlow",
fontWeight: "500",
wordWrap: "break-word",
}}
>
Arunavo{" "}
</span>
<span
style={{
color: "rgba(255, 255, 255, 0.40)",
fontSize: 64,
fontFamily: "Barlow",
fontWeight: "500",
wordWrap: "break-word",
}}
>
Ray
</span>
<span
style={{
color: "white",
fontSize: 64,
fontFamily: "Barlow",
fontWeight: "500",
wordWrap: "break-word",
}}
>
.
</span>
</div>
<img
style={{
width: 420,
height: 420,
left: 780,
top: 210,
position: "absolute",
}}
src={`http${process.env.NODE_ENV === "production" ? "s" : ""}://${
process.env.NEXT_PUBLIC_WEBSITE_DOMAIN
}/assets/avatar.png`}
alt={deSlugify(params.slug)}
width={420}
height={420}
/>
</div>
),
// ImageResponse options
{
// For convenience, we can re-use the exported opengraph-image
// size config to also set the ImageResponse's width and height.
...size,
fonts: [
{
name: "Barlow Condensed",
data: await barlowCondensed600,
style: "normal",
weight: 600,
},
{
name: "Barlow",
data: await barlow500,
style: "normal",
weight: 500,
},
],
},
);
} |
Beta Was this translation helpful? Give feedback.
-
How about extracting the loaded font from the documents' cssRules?
type FontFaceRule = CSSFontFaceRule & {
style: CSSFontFaceRule["style"] & {
src: string;
fontDisplay: FontDisplay;
};
};
const [fonts, setFonts] = useState<CustomFontSource[]>([]);
useEffect(() => {
const f = Array.from(document.styleSheets)
.flatMap((sheet) => {
return Array.from(sheet.cssRules);
})
.filter((rule): rule is FontFaceRule => {
return (
rule instanceof CSSFontFaceRule &&
rule.style.getPropertyValue("src") != null &&
rule.style.getPropertyValue("fontDisplay") != null
);
})
.map((rule) => {
return {
family: rule.style.fontFamily.replace(/"/g, ""),
weight: rule.style.fontWeight,
display: rule.style.fontDisplay,
style: rule.style.fontStyle as CustomFontSource["style"],
src: rule.style.src.replace(/"/g, ""),
};
});
setFonts(f);
}, []); |
Beta Was this translation helpful? Give feedback.
-
Same here, I would like to either use |
Beta Was this translation helpful? Give feedback.
-
Hey guys! import { ImageResponse } from 'next/og'
export const runtime = 'edge'
export const alt = 'ALT';
export const size = {
width: 1200,
height: 630,
}
export const contentType = 'image/png'
export default async function Image() {
return new ImageResponse(
(
<div
style={{
background: 'white',
width: '100%',
height: '100%',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
fontSize: 150,
fontWeight: 600
}}
>
Lorem ipsum
<span style={{ fontSize: 150, fontWeight: 400 }}>
Lorem ipsum
</span>
</div>
),
{
...size,
fonts: [
{
name: 'Ubuntu Sans',
data: await loadGoogleFont('Ubuntu Sans', 600),
style: 'normal',
weight: 600
},
{
name: 'Ubuntu Sans',
data: await loadGoogleFont('Ubuntu Sans', 400),
style: 'normal',
weight: 400
}
],
}
)
}
async function loadGoogleFont(font: string, weight: number) {
const url = `https://fonts.googleapis.com/css2?family=${font}:wght@${weight}`
const css = await (await fetch(url)).text()
const resource = css.match(/src: url\((.+)\) format\('(opentype|truetype)'\)/)
if (resource) {
const response = await fetch(resource[1])
if (response.status == 200) {
return await response.arrayBuffer()
}
}
throw new Error('❌ Failed to load font data!')
} The image is then rendered like this: Hope this helps! 😊 |
Beta Was this translation helpful? Give feedback.
-
Summary
Hi, I'm trying to read the font Montserrat file path so I can use it in my
@vercel/og
API.This font is being used in my app using the following code, and it works perfectly.
But I want to access the local path of this file so that I can use it inside my
@vercel/og
API.I have been trying to figure this out by looking inside this https://github.com/vercel/next.js/blob/canary/packages/font/src/google/loader.ts but I can't find a way around this problem
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions