nuxt-og-image 3.0.0-rc.2 → 3.0.0-rc.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/README.md +0 -4
  2. package/dist/client/200.html +9 -9
  3. package/dist/client/404.html +9 -9
  4. package/dist/client/_nuxt/IconCSS.5O0-SmyH.js +1 -0
  5. package/dist/client/_nuxt/IconCSS.6C__fd0x.css +1 -0
  6. package/dist/client/_nuxt/builds/latest.json +1 -1
  7. package/dist/client/_nuxt/builds/meta/dadcb033-8bc6-47cc-a46a-9ba69b83d96c.json +1 -0
  8. package/dist/client/_nuxt/entry.1-ZveGsq.css +1 -0
  9. package/dist/client/_nuxt/{entry.e9f3db7f.js → entry.Nh9pQ4Ti.js} +64 -58
  10. package/dist/client/_nuxt/error-404.DkXpI38i.css +1 -0
  11. package/dist/client/_nuxt/{error-404.4c6f5cb5.js → error-404.F86mkCGj.js} +1 -1
  12. package/dist/client/_nuxt/{error-500.64875434.js → error-500.L0AVuHMw.js} +1 -1
  13. package/dist/client/index.html +9 -9
  14. package/dist/module.d.mts +6 -1
  15. package/dist/module.d.ts +6 -1
  16. package/dist/module.json +2 -2
  17. package/dist/module.mjs +42 -20
  18. package/dist/runtime/cache.d.ts +2 -1
  19. package/dist/runtime/cache.mjs +33 -18
  20. package/dist/runtime/components/OgImage/OgImage.d.ts +1 -1
  21. package/dist/runtime/components/OgImage/OgImageScreenshot.d.ts +1 -1
  22. package/dist/runtime/components/Templates/Community/NuxtSeo.vue +1 -1
  23. package/dist/runtime/components/Templates/Community/UnJs.vue +2 -2
  24. package/dist/runtime/composables/defineOgImage.mjs +8 -10
  25. package/dist/runtime/core/bindings/css-inline/node.d.ts +8 -2
  26. package/dist/runtime/core/bindings/css-inline/node.mjs +7 -2
  27. package/dist/runtime/core/bindings/css-inline/wasm-fs.d.ts +8 -0
  28. package/dist/runtime/core/bindings/css-inline/wasm-fs.mjs +8 -0
  29. package/dist/runtime/core/bindings/css-inline/wasm.d.ts +8 -0
  30. package/dist/runtime/core/bindings/css-inline/wasm.mjs +8 -0
  31. package/dist/runtime/core/font/fetch.mjs +4 -1
  32. package/dist/runtime/core/html/applyInlineCss.mjs +23 -19
  33. package/dist/runtime/core/html/devIframeTemplate.mjs +3 -2
  34. package/dist/runtime/core/html/fetchIsland.mjs +1 -1
  35. package/dist/runtime/core/options/extract.d.ts +1 -0
  36. package/dist/runtime/core/options/extract.mjs +9 -0
  37. package/dist/runtime/core/options/fetch.mjs +7 -1
  38. package/dist/runtime/core/renderers/chromium/index.mjs +2 -1
  39. package/dist/runtime/core/renderers/satori/index.mjs +6 -5
  40. package/dist/runtime/core/renderers/satori/instances.mjs +2 -1
  41. package/dist/runtime/core/renderers/satori/plugins/encoding.mjs +14 -5
  42. package/dist/runtime/core/renderers/satori/vnodes.mjs +3 -1
  43. package/dist/runtime/core/utils/resolveRendererContext.mjs +14 -16
  44. package/dist/runtime/nitro/kit.d.ts +3 -0
  45. package/dist/runtime/nitro/kit.mjs +23 -0
  46. package/dist/runtime/nitro/plugins/prerender.d.ts +1 -1
  47. package/dist/runtime/nitro/plugins/prerender.mjs +5 -12
  48. package/dist/runtime/nitro/tsconfig.json +3 -0
  49. package/dist/runtime/nitro/utils.mjs +2 -3
  50. package/dist/runtime/nuxt/plugins/og-image-canonical-urls.server.mjs +9 -2
  51. package/dist/runtime/nuxt/utils.mjs +8 -3
  52. package/dist/runtime/server/routes/__og-image__/debug.json.d.ts +0 -4
  53. package/dist/runtime/server/routes/__og-image__/debug.json.mjs +0 -6
  54. package/dist/runtime/server/routes/__og-image__/font-[name]-[weight].[extension].mjs +31 -5
  55. package/dist/runtime/server/routes/__og-image__/image.mjs +8 -5
  56. package/dist/runtime/types.d.ts +6 -1
  57. package/dist/runtime/utils.pure.d.ts +2 -0
  58. package/dist/runtime/utils.pure.mjs +10 -1
  59. package/dist/types.d.mts +2 -2
  60. package/package.json +40 -36
  61. package/dist/client/_nuxt/IconCSS.12878ccd.js +0 -1
  62. package/dist/client/_nuxt/IconCSS.f0b56d3e.css +0 -1
  63. package/dist/client/_nuxt/builds/meta/5aeabb75-f7c0-4eb8-a883-25ad30a518dd.json +0 -1
  64. package/dist/client/_nuxt/entry.a30f63d0.css +0 -1
  65. package/dist/client/_nuxt/error-404.b751fa02.css +0 -1
  66. /package/dist/client/_nuxt/{error-500.69009e70.css → error-500.SLhS9LVu.css} +0 -0
  67. /package/dist/client/_nuxt/{vanilla-picker-NKbIFE8h.23409a58.js → vanilla-picker-NKbIFE8h.vtyeXros.js} +0 -0
@@ -4,7 +4,7 @@ export function fetchIsland({ options, e }) {
4
4
  if (!options.component) {
5
5
  throw createError({
6
6
  statusCode: 500,
7
- statusMessage: `Nuxt OG Image trying to render an invalid component. Received options ${JSON.stringify(options)}`
7
+ statusMessage: `[Nuxt OG Image] Rendering an invalid component. Received options: ${JSON.stringify(options)}.`
8
8
  });
9
9
  }
10
10
  const hashId = hash([options.component, options]);
@@ -1,3 +1,4 @@
1
1
  import type { OgImageOptions } from '../../types';
2
+ export declare function htmlDecodeQuotes(html: string): string;
2
3
  export declare function decodeHtml(html: string): string;
3
4
  export declare function extractAndNormaliseOgImageOptions(html: string): OgImageOptions | false;
@@ -1,3 +1,6 @@
1
+ export function htmlDecodeQuotes(html) {
2
+ return html.replace(/"/g, '"').replace(/'/g, "'").replace(/'/g, "'");
3
+ }
1
4
  export function decodeHtml(html) {
2
5
  return html.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&").replace(/&cent;/g, "\xA2").replace(/&pound;/g, "\xA3").replace(/&yen;/g, "\xA5").replace(/&euro;/g, "\u20AC").replace(/&copy;/g, "\xA9").replace(/&reg;/g, "\xAE").replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&#x27;/g, "'").replace(/&#x2F;/g, "/").replace(/&#([0-9]+);/g, (full, int) => {
3
6
  return String.fromCharCode(Number.parseInt(int));
@@ -28,6 +31,12 @@ export function extractAndNormaliseOgImageOptions(html) {
28
31
  }
29
32
  if (!options)
30
33
  return false;
34
+ const description = html.match(/<meta[^>]+name="description"[^>]*>/)?.[0];
35
+ if (description) {
36
+ const [, content] = description.match(/content="([^"]+)"/) || [];
37
+ if (content && !options.props.description)
38
+ options.props.description = content;
39
+ }
31
40
  const payload = decodeObjectHtmlEntities(options);
32
41
  if (import.meta.dev) {
33
42
  const socialPreview = {};
@@ -11,7 +11,13 @@ export async function fetchPathHtmlAndExtractOptions(e, path, key) {
11
11
  } catch (err) {
12
12
  return createError({
13
13
  statusCode: 500,
14
- statusMessage: `Failed to read the path ${path} for og-image extraction. ${err.message}.`
14
+ statusMessage: `[Nuxt OG Image] Failed to read the path ${path} for og-image extraction. ${err.message}.`
15
+ });
16
+ }
17
+ if (typeof html !== "string") {
18
+ return createError({
19
+ statusCode: 500,
20
+ statusMessage: `[Nuxt OG Image] Got invalid response from ${path} for og-image extraction.`
15
21
  });
16
22
  }
17
23
  const payload = extractAndNormaliseOgImageOptions(html);
@@ -14,7 +14,8 @@ const ChromiumRenderer = {
14
14
  if (screenshot instanceof Error) {
15
15
  return createError({
16
16
  statusCode: 400,
17
- statusMessage: `Failed to create screenshot ${screenshot.message}.`
17
+ cause: screenshot,
18
+ statusMessage: `[Nuxt OG Image] Failed to create screenshot ${screenshot.message}.`
18
19
  });
19
20
  }
20
21
  return screenshot;
@@ -7,10 +7,10 @@ import { useResvg, useSatori, useSharp } from "./instances.mjs";
7
7
  import { theme } from "#nuxt-og-image/unocss-config.mjs";
8
8
  export async function createSvg(event) {
9
9
  const { options } = event;
10
- const { fonts, satoriOptions } = useOgImageRuntimeConfig();
10
+ const { fonts, satoriOptions: _satoriOptions } = useOgImageRuntimeConfig();
11
11
  const vnodes = await createVNodes(event);
12
12
  await event._nitro.hooks.callHook("nuxt-og-image:satori:vnodes", vnodes, event);
13
- const normalisedFonts = normaliseFontInput([...fonts, ...event.options.fonts || []]);
13
+ const normalisedFonts = normaliseFontInput([...event.options.fonts || [], ...fonts]);
14
14
  const localFontPromises = [];
15
15
  const preloadedFonts = [];
16
16
  for (const font of normalisedFonts) {
@@ -30,15 +30,16 @@ export async function createSvg(event) {
30
30
  }
31
31
  const awaitedFonts = await Promise.all(localFontPromises);
32
32
  const satori = await useSatori();
33
- return satori(vnodes, defu(options.satori, satoriOptions, {
33
+ const satoriOptions = defu(options.satori, _satoriOptions, {
34
34
  fonts: [...preloadedFonts, ...awaitedFonts].map((_f) => {
35
- return { ..._f, weight: Number(_f.weight) };
35
+ return { name: _f.name, data: _f.data, style: _f.style, weight: Number(_f.weight) };
36
36
  }),
37
37
  tailwindConfig: { theme },
38
38
  embedFont: true,
39
39
  width: options.width,
40
40
  height: options.height
41
- }));
41
+ });
42
+ return satori(vnodes, satoriOptions);
42
43
  }
43
44
  async function createPng(event) {
44
45
  const { resvgOptions } = useOgImageRuntimeConfig();
@@ -28,5 +28,6 @@ export async function useSharp() {
28
28
  }
29
29
  export async function useCssInline() {
30
30
  cssInlineInstance.instance = cssInlineInstance.instance || await import("#nuxt-og-image/bindings/css-inline").then((m) => m.default);
31
- return cssInlineInstance.instance;
31
+ await cssInlineInstance.instance.initWasmPromise;
32
+ return cssInlineInstance.instance.cssInline;
32
33
  }
@@ -1,8 +1,17 @@
1
1
  import { defineSatoriTransformer } from "../utils.mjs";
2
2
  import { decodeHtml } from "../../../options/extract.mjs";
3
- export default defineSatoriTransformer({
4
- filter: (node) => typeof node.props?.children === "string",
5
- transform: async (node) => {
6
- node.props.children = decodeHtml(node.props.children);
3
+ export default defineSatoriTransformer([
4
+ // clean up
5
+ {
6
+ filter: (node) => node.props?.["data-v-inspector"],
7
+ transform: async (node) => {
8
+ delete node.props["data-v-inspector"];
9
+ }
10
+ },
11
+ {
12
+ filter: (node) => typeof node.props?.children === "string",
13
+ transform: async (node) => {
14
+ node.props.children = decodeHtml(node.props.children);
15
+ }
7
16
  }
8
- });
17
+ ]);
@@ -2,6 +2,7 @@ import { html as convertHtmlToSatori } from "satori-html";
2
2
  import { fetchIsland } from "../../html/fetchIsland.mjs";
3
3
  import { applyInlineCss } from "../../html/applyInlineCss.mjs";
4
4
  import { applyEmojis } from "../../html/applyEmojis.mjs";
5
+ import { htmlDecodeQuotes } from "../../options/extract.mjs";
5
6
  import { walkSatoriTree } from "./utils.mjs";
6
7
  import unocss from "./plugins/unocss.mjs";
7
8
  import emojis from "./plugins/emojis.mjs";
@@ -13,11 +14,12 @@ export async function createVNodes(ctx) {
13
14
  let html = ctx.options.html;
14
15
  if (!html) {
15
16
  const island = await fetchIsland(ctx);
17
+ island.html = htmlDecodeQuotes(island.html);
16
18
  await applyInlineCss(ctx, island);
17
19
  await applyEmojis(ctx, island);
18
20
  html = island.html;
19
21
  }
20
- const template = `<div data-v-inspector-ignore="true" style="position: relative; display: flex; margin: 0 auto; width: ${ctx.options.width}px; height: ${ctx.options.height}px; overflow: hidden;">${html}</div>`;
22
+ const template = `<div style="position: relative; display: flex; margin: 0 auto; width: ${ctx.options.width}px; height: ${ctx.options.height}px; overflow: hidden;">${html}</div>`;
21
23
  const satoriTree = convertHtmlToSatori(template);
22
24
  await walkSatoriTree(ctx, satoriTree, [
23
25
  unocss,
@@ -1,13 +1,13 @@
1
- import { parseURL, withoutBase, withoutTrailingSlash } from "ufo";
1
+ import { parseURL, withQuery, withoutTrailingSlash } from "ufo";
2
2
  import { createError, getQuery } from "h3";
3
3
  import { defu } from "defu";
4
- import { createRouter as createRadixRouter, toRouteMatcher } from "radix3";
5
4
  import { fetchPathHtmlAndExtractOptions } from "../options/fetch.mjs";
6
5
  import { prerenderOptionsCache } from "../cache/prerender.mjs";
7
6
  import { useChromiumRenderer, useSatoriRenderer } from "../renderers/satori/instances.mjs";
8
7
  import { separateProps, useOgImageRuntimeConfig } from "../../utils.mjs";
9
8
  import { resolvePathCacheKey } from "../../nitro/utils.mjs";
10
- import { useNitroApp, useRuntimeConfig } from "#internal/nitro";
9
+ import { createNitroRouteRuleMatcher } from "../../nitro/kit.mjs";
10
+ import { useNitroApp } from "#internal/nitro/app";
11
11
  export async function resolveRendererContext(e) {
12
12
  const runtimeConfig = useOgImageRuntimeConfig();
13
13
  const path = parseURL(e.path).pathname;
@@ -15,21 +15,23 @@ export async function resolveRendererContext(e) {
15
15
  if (!extension) {
16
16
  return createError({
17
17
  statusCode: 400,
18
- statusMessage: `Missing OG Image type.`
18
+ statusMessage: `[Nuxt OG Image] Missing OG Image type.`
19
19
  });
20
20
  }
21
21
  if (!["png", "jpeg", "jpg", "svg", "html", "json"].includes(extension)) {
22
22
  return createError({
23
23
  statusCode: 400,
24
- statusMessage: `Unknown OG Image type ${extension}.`
24
+ statusMessage: `[Nuxt OG Image] Unknown OG Image type ${extension}.`
25
25
  });
26
26
  }
27
- const basePath = withoutTrailingSlash(
28
- path.replace(`/__og-image__/image`, "").replace(`/og.${extension}`, "")
29
- );
30
27
  let queryParams = { ...getQuery(e) };
31
28
  queryParams.props = JSON.parse(queryParams.props || "{}");
32
29
  queryParams = separateProps(queryParams);
30
+ let basePath = withoutTrailingSlash(
31
+ path.replace(`/__og-image__/image`, "").replace(`/og.${extension}`, "")
32
+ );
33
+ if (queryParams._query)
34
+ basePath = withQuery(basePath, JSON.parse(queryParams._query));
33
35
  const isDebugJsonPayload = extension === "json" && runtimeConfig.debug;
34
36
  const key = resolvePathCacheKey(e, basePath);
35
37
  let options = queryParams.options;
@@ -44,12 +46,8 @@ export async function resolveRendererContext(e) {
44
46
  }
45
47
  }
46
48
  delete queryParams.options;
47
- const _routeRulesMatcher = toRouteMatcher(
48
- createRadixRouter({ routes: useRuntimeConfig().nitro?.routeRules })
49
- );
50
- const routeRules = defu({}, ..._routeRulesMatcher.matchAll(
51
- withoutBase(basePath.split("?")[0], useRuntimeConfig().app.baseURL)
52
- ).reverse());
49
+ const routeRuleMatcher = createNitroRouteRuleMatcher();
50
+ const routeRules = routeRuleMatcher(basePath);
53
51
  if (typeof routeRules.ogImage === "undefined" && !options) {
54
52
  return createError({
55
53
  statusCode: 400,
@@ -61,7 +59,7 @@ export async function resolveRendererContext(e) {
61
59
  if (!options) {
62
60
  return createError({
63
61
  statusCode: 404,
64
- statusMessage: "OG Image not found."
62
+ statusMessage: "[Nuxt OG Image] OG Image not found."
65
63
  });
66
64
  }
67
65
  let renderer;
@@ -76,7 +74,7 @@ export async function resolveRendererContext(e) {
76
74
  if (!renderer || renderer.__unenv__) {
77
75
  throw createError({
78
76
  statusCode: 400,
79
- statusMessage: `Renderer ${options.renderer} is missing.`
77
+ statusMessage: `[Nuxt OG Image] Renderer ${options.renderer} is missing.`
80
78
  });
81
79
  }
82
80
  const ctx = {
@@ -0,0 +1,3 @@
1
+ import type { NitroRouteRules } from 'nitropack';
2
+ export declare function withoutQuery(path: string): string;
3
+ export declare function createNitroRouteRuleMatcher(): (path: string) => NitroRouteRules;
@@ -0,0 +1,23 @@
1
+ import { createRouter as createRadixRouter, toRouteMatcher } from "radix3";
2
+ import { defu } from "defu";
3
+ import { withoutBase, withoutTrailingSlash } from "ufo";
4
+ import { useRuntimeConfig } from "#imports";
5
+ export function withoutQuery(path) {
6
+ return path.split("?")[0];
7
+ }
8
+ export function createNitroRouteRuleMatcher() {
9
+ const { nitro, app } = useRuntimeConfig();
10
+ const _routeRulesMatcher = toRouteMatcher(
11
+ createRadixRouter({
12
+ routes: Object.fromEntries(
13
+ Object.entries(nitro?.routeRules || {}).map(([path, rules]) => [withoutTrailingSlash(path), rules])
14
+ )
15
+ })
16
+ );
17
+ return (path) => {
18
+ return defu({}, ..._routeRulesMatcher.matchAll(
19
+ // radix3 does not support trailing slashes
20
+ withoutBase(withoutTrailingSlash(withoutQuery(path)), app.baseURL)
21
+ ).reverse());
22
+ };
23
+ }
@@ -1,2 +1,2 @@
1
- declare const _default: import("nitropack").NitroAppPlugin;
1
+ declare const _default: import("nitropack/dist/runtime/plugin").NitroAppPlugin;
2
2
  export default _default;
@@ -1,12 +1,10 @@
1
- import { parseURL, withoutBase } from "ufo";
1
+ import { parseURL } from "ufo";
2
2
  import { defineNitroPlugin } from "nitropack/dist/runtime/plugin";
3
- import { createRouter as createRadixRouter, toRouteMatcher } from "radix3";
4
- import { defu } from "defu";
5
3
  import { extractAndNormaliseOgImageOptions } from "../../core/options/extract.mjs";
6
4
  import { prerenderOptionsCache } from "../../core/cache/prerender.mjs";
7
5
  import { isInternalRoute } from "../../utils.pure.mjs";
8
6
  import { resolvePathCacheKey } from "../utils.mjs";
9
- import { useRuntimeConfig } from "#internal/nitro";
7
+ import { createNitroRouteRuleMatcher } from "../kit.mjs";
10
8
  export default defineNitroPlugin(async (nitro) => {
11
9
  if (!import.meta.prerender)
12
10
  return;
@@ -15,14 +13,9 @@ export default defineNitroPlugin(async (nitro) => {
15
13
  const path = parseURL(ctx.event.path).pathname;
16
14
  if (isInternalRoute(path))
17
15
  return;
18
- const runtimeConfig = useRuntimeConfig();
19
- const _routeRulesMatcher = toRouteMatcher(
20
- createRadixRouter({ routes: runtimeConfig.nitro?.routeRules })
21
- );
22
- const routeRules = defu({}, ..._routeRulesMatcher.matchAll(
23
- withoutBase(path.split("?")[0], runtimeConfig.app.baseURL)
24
- ).reverse()).ogImage;
25
- if (routeRules === false)
16
+ const routeRuleMatcher = createNitroRouteRuleMatcher();
17
+ const routeRules = routeRuleMatcher(path);
18
+ if (routeRules.ogImage === false)
26
19
  return;
27
20
  const options = extractAndNormaliseOgImageOptions([
28
21
  head.join("\n"),
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../../.nuxt/tsconfig.server.json"
3
+ }
@@ -2,12 +2,11 @@ import { withoutLeadingSlash, withoutTrailingSlash } from "ufo";
2
2
  import { hash } from "ohash";
3
3
  import { normalizeKey } from "unstorage";
4
4
  import { getQuery } from "h3";
5
- import { useSiteConfig } from "#imports";
6
5
  export function resolvePathCacheKey(e, path) {
7
- const siteConfig = useSiteConfig(e);
6
+ const siteConfig = e.context.siteConfig.get();
8
7
  const basePath = withoutTrailingSlash(withoutLeadingSlash(normalizeKey(path || e.path)));
9
8
  return [
10
- !basePath ? "index" : basePath,
9
+ !basePath || basePath === "/" ? "index" : basePath,
11
10
  hash([
12
11
  basePath,
13
12
  siteConfig.url,
@@ -15,11 +15,18 @@ export default defineNuxtPlugin({
15
15
  key: "nuxt-og-image:overrides-and-canonical-urls",
16
16
  hooks: {
17
17
  "tags:resolve": async (ctx2) => {
18
+ const hasPrimaryPayload = ctx2.tags.some((tag) => tag.tag === "script" && tag.props.id === "nuxt-og-image-options");
18
19
  let overrides;
19
20
  for (const tag of ctx2.tags) {
20
21
  if (tag.tag === "script" && tag.props.id === "nuxt-og-image-overrides") {
21
- overrides = separateProps(JSON.parse(tag.innerHTML || "{}"));
22
- delete ctx2.tags[ctx2.tags.indexOf(tag)];
22
+ if (hasPrimaryPayload) {
23
+ overrides = separateProps(JSON.parse(tag.innerHTML || "{}"));
24
+ delete ctx2.tags[ctx2.tags.indexOf(tag)];
25
+ } else {
26
+ tag.props.id = "nuxt-og-image-options";
27
+ tag.innerHTML = JSON.stringify(separateProps(JSON.parse(tag.innerHTML || "{}")));
28
+ tag._d = "script:id:nuxt-og-image-options";
29
+ }
23
30
  break;
24
31
  }
25
32
  }
@@ -1,11 +1,16 @@
1
1
  import { defu } from "defu";
2
- import { separateProps } from "../utils.mjs";
2
+ import { withQuery } from "ufo";
3
+ import { getExtension, separateProps } from "../utils.mjs";
3
4
  import { unref, useServerHead } from "#imports";
4
5
  import { componentNames } from "#build/nuxt-og-image/components.mjs";
5
6
  export function createOgImageMeta(src, input, resolvedOptions, ssrContext) {
6
7
  const _input = separateProps(defu(input, ssrContext._ogImagePayload));
7
- const url = src || input.url || resolvedOptions.url;
8
- let urlExtension = (url.split("/").pop() || url).split(".").pop() || resolvedOptions.extension;
8
+ let url = src || input.url || resolvedOptions.url;
9
+ if (!url)
10
+ return;
11
+ if (input._query && Object.keys(input._query).length && url)
12
+ url = withQuery(url, { _query: input._query });
13
+ let urlExtension = getExtension(url) || resolvedOptions.extension;
9
14
  if (urlExtension === "jpg")
10
15
  urlExtension = "jpeg";
11
16
  const meta = [
@@ -1,8 +1,4 @@
1
1
  declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<{
2
- siteConfigUrl: {
3
- value: any;
4
- source: any;
5
- };
6
2
  componentNames: any;
7
3
  runtimeConfig: import("../../../types").OgImageRuntimeConfig;
8
4
  compatibility: any;
@@ -1,17 +1,11 @@
1
1
  import { defineEventHandler, setHeader } from "h3";
2
2
  import { useOgImageRuntimeConfig } from "../../../utils.mjs";
3
- import { useSiteConfig } from "#imports";
4
3
  import compatibility from "#nuxt-og-image/compatibility";
5
4
  import { componentNames } from "#nuxt-og-image/component-names.mjs";
6
5
  export default defineEventHandler(async (e) => {
7
6
  setHeader(e, "Content-Type", "application/json");
8
7
  const runtimeConfig = useOgImageRuntimeConfig();
9
- const siteConfig = await useSiteConfig(e, { debug: true });
10
8
  return {
11
- siteConfigUrl: {
12
- value: siteConfig.url,
13
- source: siteConfig._context.url || "unknown"
14
- },
15
9
  componentNames,
16
10
  runtimeConfig,
17
11
  compatibility
@@ -1,10 +1,36 @@
1
- import { createError, defineEventHandler, proxyRequest, sendRedirect } from "h3";
1
+ import { createError, defineEventHandler, getQuery, proxyRequest, sendRedirect, setHeader } from "h3";
2
2
  import { parseURL } from "ufo";
3
+ import { getExtension, normaliseFontInput, useOgImageRuntimeConfig } from "../../../utils.mjs";
4
+ import { assets } from "#internal/nitro/virtual/server-assets";
3
5
  export default defineEventHandler(async (e) => {
4
6
  const path = parseURL(e.path).pathname;
5
- const [name, weight] = path.split("/font/")[1].split(".")[0].split("/");
6
- if (!name || !weight)
7
+ const [_name, _weight] = path.split("/font/")[1].split(".")[0].split("/");
8
+ if (!_name || !_weight)
7
9
  return "Provide a font name and weight";
10
+ const name = _name[0].toUpperCase() + _name.slice(1);
11
+ const weight = Math.round(Number.parseInt(_weight) / 100) * 100;
12
+ const config = useOgImageRuntimeConfig();
13
+ const normalisedFonts = normaliseFontInput(config.fonts);
14
+ let font;
15
+ if (typeof getQuery(e).font === "string")
16
+ font = JSON.parse(getQuery(e).font);
17
+ if (!font) {
18
+ font = normalisedFonts.find((font2) => {
19
+ return font2.name.toLowerCase() === name.toLowerCase() && weight === Number(font2.weight);
20
+ });
21
+ }
22
+ if (!font) {
23
+ return createError({
24
+ statusCode: 404,
25
+ statusMessage: `[Nuxt OG Image] Font ${name}:${weight} not found`
26
+ });
27
+ }
28
+ if (import.meta.dev || import.meta.prerender) {
29
+ if (font.key && await assets.hasItem(font.key)) {
30
+ setHeader(e, "Content-Type", `font/${getExtension(font.path)}`);
31
+ return assets.getItemRaw(font.key);
32
+ }
33
+ }
8
34
  const css = await globalThis.$fetch(`https://fonts.googleapis.com/css2?family=${name}:wght@${weight}`, {
9
35
  headers: {
10
36
  // Make sure it returns TTF.
@@ -14,7 +40,7 @@ export default defineEventHandler(async (e) => {
14
40
  if (!css) {
15
41
  return createError({
16
42
  statusCode: 500,
17
- statusMessage: `Invalid Google Font ${name}:${weight}`
43
+ statusMessage: `[Nuxt OG Image] Invalid Google Font ${name}:${weight}`
18
44
  });
19
45
  }
20
46
  const ttfResource = css.match(/src: url\((.+)\) format\('(opentype|truetype)'\)/);
@@ -25,6 +51,6 @@ export default defineEventHandler(async (e) => {
25
51
  return sendRedirect(e, woff2Resource[1]);
26
52
  return createError({
27
53
  statusCode: 500,
28
- statusMessage: `Malformed Google Font CSS ${css}`
54
+ statusMessage: `[Nuxt OG Image] Malformed Google Font CSS ${css}`
29
55
  });
30
56
  });
@@ -5,7 +5,6 @@ import { devIframeTemplate } from "../../../core/html/devIframeTemplate.mjs";
5
5
  import { applyInlineCss } from "../../../core/html/applyInlineCss.mjs";
6
6
  import { useOgImageBufferCache } from "../../../cache.mjs";
7
7
  import { useOgImageRuntimeConfig } from "../../../utils.mjs";
8
- import { useSiteConfig } from "#imports";
9
8
  export default defineEventHandler(async (e) => {
10
9
  const ctx = await resolveRendererContext(e);
11
10
  if (ctx instanceof H3Error)
@@ -23,10 +22,12 @@ export default defineEventHandler(async (e) => {
23
22
  compatibilityHints.push("Inlining CSS is only supported in Node based environments.");
24
23
  setHeader(e, "Content-Type", "application/json");
25
24
  return {
25
+ siteConfig: {
26
+ url: e.context.siteConfig.get().url
27
+ },
26
28
  compatibilityHints,
27
29
  cacheKey: ctx.key,
28
30
  options: ctx.options,
29
- siteConfig: useSiteConfig(e),
30
31
  ...options.renderer === "satori" ? await renderer.debug(ctx) : void 0
31
32
  };
32
33
  }
@@ -43,7 +44,7 @@ export default defineEventHandler(async (e) => {
43
44
  if (ctx.renderer.name !== "satori") {
44
45
  return createError({
45
46
  statusCode: 400,
46
- statusMessage: `Generating ${extension}'s with ${renderer.name} is not supported.`
47
+ statusMessage: `[Nuxt OG Image] Generating ${extension}'s with ${renderer.name} is not supported.`
47
48
  });
48
49
  }
49
50
  setHeader(e, "Content-Type", `image/svg+xml`);
@@ -54,7 +55,7 @@ export default defineEventHandler(async (e) => {
54
55
  if (!renderer.supportedFormats.includes(extension)) {
55
56
  return createError({
56
57
  statusCode: 400,
57
- statusMessage: `Generating ${extension}'s with ${renderer.name} is not supported.`
58
+ statusMessage: `[Nuxt OG Image] Generating ${extension}'s with ${renderer.name} is not supported.`
58
59
  });
59
60
  }
60
61
  setHeader(e, "Content-Type", `image/${extension === "jpg" ? "jpeg" : extension}`);
@@ -62,7 +63,7 @@ export default defineEventHandler(async (e) => {
62
63
  default:
63
64
  return createError({
64
65
  statusCode: 400,
65
- statusMessage: `Invalid request for og.${extension}.`
66
+ statusMessage: `[Nuxt OG Image] Invalid request for og.${extension}.`
66
67
  });
67
68
  }
68
69
  const cacheApi = await useOgImageBufferCache(ctx, {
@@ -71,6 +72,8 @@ export default defineEventHandler(async (e) => {
71
72
  });
72
73
  if (typeof cacheApi === "undefined")
73
74
  return;
75
+ if (cacheApi instanceof H3Error)
76
+ return cacheApi;
74
77
  let image = cacheApi.cachedItem;
75
78
  if (!image) {
76
79
  image = await renderer.createImage(ctx);
@@ -106,9 +106,14 @@ export interface OgImageOptions<T extends keyof OgImageComponents = 'NuxtSeo'> {
106
106
  sharp?: SharpOptions;
107
107
  fonts?: InputFontConfig[];
108
108
  cacheMaxAgeSeconds?: number;
109
+ /**
110
+ * @internal
111
+ */
112
+ _query?: Record<string, any>;
109
113
  }
110
114
  export interface FontConfig {
111
115
  name: string;
116
+ style?: string;
112
117
  weight?: string | number;
113
118
  path?: string;
114
119
  key?: string;
@@ -120,7 +125,7 @@ export interface ResolvedFontConfig extends FontConfig {
120
125
  export type InputFontConfig = (`${string}:${number}` | string | FontConfig);
121
126
  export interface RuntimeCompatibilitySchema {
122
127
  chromium: 'node' | false;
123
- ['css-inline']: 'node' | false;
128
+ ['css-inline']: 'node' | 'wasm' | 'wasm-fs' | false;
124
129
  resvg: 'node' | 'wasm' | 'wasm-fs' | false;
125
130
  satori: 'node' | 'wasm' | 'wasm-fs' | false;
126
131
  sharp: 'node' | false;
@@ -4,3 +4,5 @@ export declare function separateProps(options: OgImageOptions | undefined, ignor
4
4
  props: Record<string, any>;
5
5
  };
6
6
  export declare function normaliseFontInput(fonts: InputFontConfig[]): ResolvedFontConfig[];
7
+ export declare function withoutQuery(path: string): string;
8
+ export declare function getExtension(path: string): string;
@@ -17,6 +17,7 @@ function filterIsOgImageOption(key) {
17
17
  "component",
18
18
  "renderer",
19
19
  "emojis",
20
+ "_query",
20
21
  "satori",
21
22
  "resvg",
22
23
  "sharp",
@@ -48,7 +49,7 @@ export function normaliseFontInput(fonts) {
48
49
  return {
49
50
  cacheKey: f,
50
51
  name,
51
- weight: weight || "400",
52
+ weight: weight || 400,
52
53
  style: "normal",
53
54
  path: void 0
54
55
  };
@@ -61,3 +62,11 @@ export function normaliseFontInput(fonts) {
61
62
  };
62
63
  });
63
64
  }
65
+ export function withoutQuery(path) {
66
+ return path.split("?")[0];
67
+ }
68
+ export function getExtension(path) {
69
+ path = withoutQuery(path);
70
+ const lastSegment = path.split("/").pop() || path;
71
+ return lastSegment.split(".").pop() || lastSegment;
72
+ }
package/dist/types.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- import type { ModuleOptions, ModuleHooks } from './module'
2
+ import type { ModuleOptions, ModuleHooks } from './module.js'
3
3
 
4
4
 
5
5
  declare module '@nuxt/schema' {
@@ -15,4 +15,4 @@ declare module 'nuxt/schema' {
15
15
  }
16
16
 
17
17
 
18
- export type { ModuleHooks, ModuleOptions, default } from './module'
18
+ export type { ModuleHooks, ModuleOptions, default } from './module.js'