【问题标题】:Use Netlify Functions for Server Side rendering使用 Netlify 函数进行服务器端渲染
【发布时间】:2019-08-02 06:42:54
【问题描述】:

Netlify 函数通常位于某个子路径中,例如 /.netlify/functions。是否可以让一个函数负责渲染每个子路径,以便在函数中进行服务器端渲染?

【问题讨论】:

  • 我在我的回答中展示了如何将默认函数路径作为子路径之一。响应中返回的任何有效页面都是有效的。您可以使用该方法进行测试。

标签: netlify


【解决方案1】:

可以在重定向文件中创建 rewrite rule 以允许函数位于更漂亮的 url 端点。

为要用作子路径的函数创建一个重写路径

_redirects(see docs here)

/hello /.netlify/functions/sayhello 200

确保/hello 路径没有有效的端点。

使用下面的sayhello 函数,您也可以传递查询参数。

sayhello.js

exports.handler = function(event, context, callback) {
  const {name = 'World'} = event.queryStringParameters;

  const message = `Hello to the ${name}!`
  callback(null, {
  statusCode: 200,
  body: `${message}`
  });
}

致电https://example.com/hello?name=talvesHello to the talves! 作为正文响应。

【讨论】:

    猜你喜欢
    • 2017-04-04
    • 2018-03-06
    • 2019-02-12
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2016-08-06
    • 2016-04-06
    • 2019-10-02
    相关资源
    最近更新 更多