Sam*_*man 5 typescript webpack vercel
我有个问题。我创建了一个 url 缩短网站,typescript它是一个单页网站,但可以更改路线。在我使用的这个项目中webpack,对于我使用的开发服务器webpack-dev-server以及返回index.html几乎所有路由,我在以下位置编写了这段代码webpack.config.js:
devServer: {
static: {
directory: path.join(__dirname, "dist"),
},
compress: true,
proxy: {
"/**": {
target: "/index.html",
secure: false,
bypass: function (req, res, opt) {
if (
req.path.indexOf("/img/") !== -1 ||
req.path.indexOf("/public/") !== -1
) {
return "/";
}
if (req.path.indexOf("/build.css") !== -1) {
return "/build.css";
}
if (req.headers.accept.indexOf("html") !== -1) {
return "/index.html";
} else return;
},
},
},
}
Run Code Online (Sandbox Code Playgroud)
现在我想index.html为应用程序的 vercel 服务器启用此功能(每次路由更改时返回)。这时,当我改变路线时,我会得到404页面。但我想得到index.html。我进行了很多搜索来实现我想要的,并且测试了一些方法,但我无法做到我想要的。vercel.json我在项目的根文件夹中创建了一个名为的文件,我尝试了redirects,rewrites并且routes. 我不知道,也许我错误地使用了这些属性。那么我该怎么做呢?感谢您的帮助。我尝试了这些配置等:
{
"redirects": [{ "source": "/[^.]+", "destination": "/" }],
"rewrites": [{ "source": "/[^.]+", "destination": "/" }]
}
{
"routes": [{ "src": "/[^.]+", "dest": "/", "status": 200 }]
}
Run Code Online (Sandbox Code Playgroud)
我发现我不应该使用redirects和routes财产来vercel.json实现我想要的。我应该用它rewrites来代替。还有一件事是我用rewrites错了。我应该这样使用它:
{
"rewrites": [{ "source": "/(.*)", "destination": "/index.html" }]
}
Run Code Online (Sandbox Code Playgroud)
根据文档,这是有效的“因为在应用重写之前优先考虑文件系统”。
| 归档时间: |
|
| 查看次数: |
1717 次 |
| 最近记录: |