【问题标题】:Why does using res.writeHead in NextJS cause a permanent redirect?为什么在 NextJS 中使用 res.writeHead 会导致永久重定向?
【发布时间】:2020-07-03 06:05:22
【问题描述】:

我的代码中有一部分,用于检查某事是否属实,然后重定向。

这是在这里完成的:

static async getInitialProps({ res }) {
        let accessToken = settings.get('access_token');
        if (accessToken) {
            if (res) {
                res.writeHead(301, {
                    Location: '/dashboard'
                });
                res.end();
                return {};
            }
        }
    }

它第一次重定向,但总是用上面的代码调用这个页面。

如果我重新启动我的应用程序,它会立即将我带到/dashboard 而不调用此第一页。它完全跳过它。什么可能导致这种行为?

我注意到我可以通过删除%appdata%/my-app 中应用的 AppData 来修复它。

我也在用:

const Store = require('electron-store');
const settings = new Store();

这是我的终端日志:

> electron-twitch-app@1.0.0 start C:\NodeApps\electron-twitch-app
> electron .

Defining routes from exportPathMap
(node:12868) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
event - compiled successfully
event - build page: /
wait  - compiling...
event - compiled successfully
event - build page: /dashboard
wait  - compiling...
event - compiled successfully
PS C:\NodeApps\electron-twitch-app> npm start

> electron-twitch-app@1.0.0 start C:\NodeApps\electron-twitch-app
> electron .


Defining routes from exportPathMap
(node:7500) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
event - compiled successfully
event - build page: /dashboard
wait  - compiling...
event - compiled successfully

请注意,正如我所说 - 第一次,它调用/,然后重定向到/dashboard。第二次直接发到/dashboard

【问题讨论】:

    标签: node.js electron next.js


    【解决方案1】:

    显然,使用:

    app.commandLine.appendSwitch("disable-http-cache");
    

    解决了我的问题。

    除此之外还有其他解决方案吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 2021-11-10
      • 2013-10-05
      相关资源
      最近更新 更多