getServerSideProps 访问当前浏览器 url [英] getServerSideProps access current browser url

查看:22
本文介绍了getServerSideProps 访问当前浏览器 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用 getServerSideProps 并像这样传入 req 和 res 参数:

I am calling getServerSideProps and passing in the req and res parameters like this:

export async function getServerSideProps({ req, res }) {}

我需要获取当前浏览器的url路径,在request对象中找不到.有没有办法在 getServerSideProps 中获取当前 url?

I need to get the current browser url path and I can't find it in the request object. Is there a way to get the current url inside getServerSideProps?

推荐答案

您可以使用上下文参数中的 resolvedUrl 字段.

You can use the resolvedUrl field from the context parameter.

export async function getServerSideProps({ req, res, resolvedUrl }) {
    console.log(resolvedUrl)

   // Remaining code
}

来自 getServerSideProps 文档:

From the getServerSideProps documentation:

resolvedUrl:请求 URL 的规范化版本,客户端转换的 _next/data 前缀,包括原始查询值.

resolvedUrl: A normalized version of the request URL that strips the _next/data prefix for client transitions and includes original query values.

这篇关于getServerSideProps 访问当前浏览器 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆