next.js - 如何在下一个js应用程序文件夹中使用vercel og图像?

标签 next.js vercel beta

但是我在实现 ImageResponse 以作为 NextResponse 返回时遇到问题。

我有一个问题,我尝试在 app/somedirectory/route.js 中使用 @vercel/og 实现 og 图像生成

但是我在实现 ImageResponse 以作为 NextResponse 返回时遇到问题。

我收到错误 405,有时甚至收到错误 500。

关于如何正确实现它的任何想法或代码示例? 我在 App/api/hello/route.js 中尝试了以下内容

import { ImageResponse } from '@vercel/og';
 
export const config = {
  runtime: 'edge',
};
 
const ogImage =  function () {
  return new ImageResponse(
    (
      <div
        style={{
          fontSize: 128,
          background: 'white',
          width: '100%',
          height: '100%',
          display: 'flex',
          textAlign: 'center',
          alignItems: 'center',
          justifyContent: 'center',
        }}
      >
        Hello world!
      </div>
    ),
    {
      width: 1200,
      height: 600,
    },
  );
}

export async function GET(request) {
  return new Response(ogImage)
}

它返回了整个函数

function() {
return new _vercel_og__WEBPACK_IMPORTED_MODULE_1__.ImageResponse(/*#__PURE__*/ (0,react_jsx_dev_runtime__WEBPACK_IMPORTED_MODULE_0__.jsxDEV)("div", {
    style: {
        fontSize: 128,
        background: "white",
        width: "100%",
        height: "100%",
        display: "flex",
        textAlign: "center",
        alignItems: "center",
        justifyContent: "center"
    },
    children: "Hello world!"
}, void 0, false, {
    fileName: "C:\\Users\\.. ..test\\app\\api\\hello\\route.js",
    lineNumber: 10,
    columnNumber: 7
}, this), {
    width: 1200,
    height: 600
});

}

当使用 NextResponse 时,我无法得到任何返回。 当我使用以下内容时

  const ogImage =  new ImageResponse(
    (
      <div></div>

     );

export async function GET(request) {
  return new Response(ogImage)
}

它只是返回[对象响应]

最佳答案

终于知道怎么做了:

import { ImageResponse } from '@vercel/og';
 
export const config = {
  runtime: 'edge',
};
 


export async function GET(request) {
   return  new ImageResponse(
      (
        <div
          style={{
            fontSize: 128,
            background: 'white',
            width: '100%',
            height: '100%',
            display: 'flex',
            textAlign: 'center',
            alignItems: 'center',
            justifyContent: 'center',
          }}
        >
          Hello world!
        </div>
      ),
      {
        width: 1200,
        height: 600,
      },
    );
    // return new Response(imageG);
}

这应该放在:

App/directoryName/route.js

这是迄今为止从 NextJs 新应用程序结构中的 Api 端点获取 imageResponse 的唯一方法。

Next App Structure, with App directory, name of the folder 'Api' can be anything, it is not analogous to Pages/Api directory. It is just a route path, see nextjs v13 documentation about route path

Showing the code, where imageResponse being returned inside route.js

showing the generated image

它正在接受请求并返回 ImageResponse。 如果 ImageResponse 存储在 const 中,并且使用 new Response(const) 返回该 const,则它将返回 [object response]。

关于next.js - 如何在下一个js应用程序文件夹中使用vercel og图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75858925/

相关文章:

ios - Testflight 应用程序需要 iOS 8。我们如何使用 iOS 7 进行 Beta 测试?

objective-c - cocoa 应用程序 : How distribute beta versions?

java - Sphinx4 Beta API 与 Eclipse 的问题

html - Next.js 从/到另一个页面重定向

next.js - Cloudflare 和 next.js 静态导出不起作用?

next.js - 如何在部署到 Vercel 的 Next js 中禁用目录列表

javascript - Zeit (Vercel) 现在无服务器身份验证请求因 CORS 而失败

javascript - 如何在 Vercel 上的 SvelteKit 应用程序中创建 Web Worker?

javascript - Next Js SSG 千万条记录

Material-UI V5 RTL with stylis-plugin-rtl