【发布时间】:2021-09-21 17:21:09
【问题描述】:
我正在关注此链接 https://nextjs.org/docs/api-reference/next.config.js/headers
添加x-slug键。我是这样添加的
module.exports = {
async headers() {
return [
{
source: '/:slug',
headers: [
{
key: 'x-slug',
value: `${abc(':slug')}` // Matched parameters can be used in the value
}
]
}
];
}
};
当我使用这个网址时 https://nextjs-vetexh--3000.local.webcontainer.io/dsd “蛞蝓”---> dsd
但是当我使用toUppercase() 函数时它不起作用,为什么
function abc(a) {
// working
// return a;
// not working
return a.toUpperCase();
}
SLUG 大写。
这是我的代码 https://stackblitz.com/edit/nextjs-vetexh?file=next.config.js
function abc(a) {
// working
// return a;
// not working
return a.toUpperCase();
}
module.exports = {
async headers() {
return [
{
source: '/:slug',
headers: [
{
key: 'x-slug',
value: `${abc(':slug')}` // Matched parameters can be used in the value
}
]
}
];
}
};
【问题讨论】:
-
abc函数从何而来?大概是value: ":SLUG"断开了指向/:slug的链接,因为在实际发出请求时,该对象没有被评估。 -
有什么想法可以改成大写吗?预期的输出是“DSD”它给
:SLUG -
鉴于您的示例显示,您可能不能。除非您可以在下游插入一些中间件,否则一旦设置了实际值,如果存在,则将该标头大写。
value在该对象中是一个模板,您将其大写而不是实际值。 -
@PsyGik 是的,我确实阅读了链接的文档,我的意思是我不明白您为什么希望将实际值大写。
标签: javascript reactjs next.js