gpt4 book ai didi

javascript - 为什么正则表达式不能处理 header 来检查请求?

转载 作者:行者123 更新时间:2023-12-05 06:50:20 24 4
gpt4 key购买 nike

我正在使用正则表达式向响应 header 添加一些值。我正在关注此文档

https://nextjs.org/docs/api-reference/next.config.js/headers

这里提到了如何添加 Regex Path Matching 它不起作用。

例如,我创建了一个演示应用程序,如果请求是 /about.css,我会在其中添加 my custom header value in 边缘缓存标签它工作正常

module.exports = {
async headers() {
return [
{
source: '/about.css',
headers: [
{
key: 'Edge-Cache-Tag',
value: 'my custom header value',
}
],
},
{
source: '/(name-)',
headers: [
{
key: 'Edge-Cache-Tag',
value: 'peivarer value',
}
],
},
]
},
}

但我想如果请求从 name- 开始,那么它会在 Edge-Cache-Tag 中添加 peivarer value不工作。

在文档中是这样写的要匹配正则表达式路径,您可以将正则表达式括在参数后的括号中,例如/blog/:slug(\d{1,}) 将匹配/blog/123 但不匹配/blog/abc:

enter image description here这是我的代码 https://repl.it/@naveennsit/KindlySomberCleaninstall#next.config.js

最佳答案

按照文档的示例,此配置有效:

module.exports = {
async headers() {
return [
{
source: "/about.css",
headers: [
{
key: "Edge-Cache-Tag",
value: "my custom header value",
},
],
},
{
source: "/:slug(name-[a-zA-Z0-9]*\\.css)",
headers: [
{
key: "Edge-Cache-Tag",
value: "peivarer value",
},
],
},
];
},
};

正则表达式将允许 name- 后跟任何字符或数字,然后是 .css

关于javascript - 为什么正则表达式不能处理 header 来检查请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66433474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com