简体   繁体   English

Next.js 在 next.config.js 中动态重定向

[英]Next.js Redirects in next.config.js dynamicly

I want to update Redirects routes after build of project.我想在项目构建后更新重定向路由。 So, we have a redirect routes on BE Api.所以,我们在 BE Api 上有一个重定向路由。

I want to get it and implement to next.config.js dynamicly, here what i tried to do.我想动态地获取它并实施到 next.config.js,这就是我试图做的。

const getUrls = () =>
    new Promise(resolve => {
        setTimeout(() => resolve(fetch("https://api.mocki.io/v1/a0b7f0b0").then(function(response) {
            return response.json();
          }).then(function(data) {
            return data.data;
          }) ), 3000);
    });

    // ExampleResponse = {
    //     "data":[
    //         {
    //             "source":"/test",
    //             "permanent":true,
    //             "destination":"/internet-application"
    //         }
    //     ]
    // }
    
module.exports = {
    poweredByHeader: false,
    async redirects() {
        return getUrls();
      },
}

Is there any solution to do it like that.有没有这样的解决方案。 I just want to update redirects from database.If the database update can we trigger redirects without stop project我只想从数据库更新重定向。如果数据库更新可以触发重定向而不停止项目

That's perfectly doable, the problem in your code is that you need to await for the getUrls promise to resolve.这是完全可行的,您代码中的问题是您需要await getUrls promise 解决。

module.exports = {
    poweredByHeader: false,
    async redirects() {
        return await getUrls();
    }
}

Also, you could simplify getUrls to not use setTimeout at all - I don't think you need that.此外,您可以简化getUrls以完全不使用setTimeout - 我认为您不需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM