简体   繁体   English

Next.js - 使用 Cloflare Workers 进行无服务器渲染?

[英]Next.js - Serverless rendering with Clouflare Workers?

I'm using Next.js v9 and would like to take advantage of Next's serverless deployment option by using my application with Cloudflare Workers.我正在使用 Next.js v9,并希望通过将我的应用程序与 Cloudflare Workers 结合使用来利用 Next 的无服务器部署选项。

From the Next.js docs, I know that every serverless function created by Next has the following signature:从 Next.js 文档中,我知道 Next 创建的每个无服务器函数都具有以下签名:

export function render(req: http.IncomingMessage, res: http.ServerResponse) => void

When using Node's http.Server class (using express for example), passing in the req and res object becomes trivial and there is an example shown in the docs of doing this.当使用 Node 的http.Server类(例如使用express )时,传入reqres对象变得微不足道,并且在执行此操作的文档中显示了一个示例。

However, the issue is that Cloudflare Workers use a different Request and Response object than Next expects.但是,问题在于 Cloudflare Workers 使用的RequestResponse对象与 Next 预期的不同。 The docs for their Request object can be found here and the docs for their Response object can be found here .他们的Request对象的文档可以在这里找到,他们的Response对象的文档可以在这里找到。

So while a simple "hello world" app running on Cloudflare Workers looks like this:因此,在 Cloudflare Workers 上运行的简单“hello world”应用程序如下所示:

// 1. Register a FetchEvent listener that sends a custom
//    response for the given request.
addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

// 2. Return a custom request object
async function handleRequest(request) {
  return new Response('hello world')
}

and rendering a simple Next.js function might look something like this:并呈现一个简单的 Next.js 函数可能如下所示:

const page = require('./.next/serverless/pages/some_page.js');

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  return page.render(request, new Response());
}

there is an inherent mismatch between the resuest and response types so I suspect errors will be thrown.结果和响应类型之间存在固有的不匹配,所以我怀疑会抛出错误。

I know that I need to modify these so that they're passed in as Next.js expects.我知道我需要修改这些,以便按照 Next.js 的预期传入。 I know that the Worker's Request object can be modified as seen here .我知道,工人的Request所看到物体可以修改这里 But I'm unsure of how exactly to make it match with the http.IncomingMessage object.但我不确定如何使其与http.IncomingMessage对象匹配。 And I'm not sure on how to format the Response object either.而且我也不确定如何格式化Response对象。 Any thoughts?有什么想法吗?

Thank you谢谢

What you're aiming to do is replicate the API for the Node.js http module which provides http.IncomingMessage andhttp.ServerResponse .您的目标是复制Node.js http模块的 API,该模块提供http.IncomingMessagehttp.ServerResponse That seems like a monumental undertaking that, in some ways, defeats the purpose of working with Cloudflare Workers which are modeled on the Service Worker API.这似乎是一项艰巨的任务,在某些方面,它违背了使用基于 Service Worker API 建模的 Cloudflare Workers 的目的。 Instead, consider dropping Next.js for a Cloudflare Workers-specific approach to SSR for React, as Cloudflare demonstrates here .相反,考虑将 Next.js 用于 Cloudflare Workers 特定的 React SSR 方法,正如 Cloudflare 在此处演示的那样。

You can try to mimic the response object like this.您可以尝试像这样模拟响应对象。

        const page = require('./.next/serverless/pages/some_page.js');

        addEventListener('fetch', event => {
            event.respondWith(handleRequest(event.request))
        })

        async function handleRequest(request) {
            return new Promise((resolve) => {
                const response = {
                    setHeader: () => {},
                    getHeader: () => {},
                    end: (htmlString) => {
                        resolve(htmlString);
                    },
                };
                page.render(request, response);
            });
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM