英文:
Why does my Next.js 13 app keep getting 'WebSocket connection failed' error on server deployment?
问题
WebSocket连接到'
WebSocket连接到'
我正在使用nextjs 13,在服务器上部署后,我不断遇到这个问题。
我尝试在next.config.js中禁用WebSocket,如下所示:
// 禁用WebSocket HMR
experimental: {
disableWebpackHMR: true,
},
我还创建了一个server.js文件,如下所示:
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl);
}).listen(3000, (
<details>
<summary>英文:</summary>
WebSocket connection to '<URL>' failed: WebSocket is closed before the connection is established
WebSocket connection to <'URL'> failed: WebSocket is closed before the connection is established.
I'm using nextjs 13, once i deploy on server i keep getting this issue continuously.
i tried to do this in next.config.js to disble websocket,
// Disable WebSocket HMR
experimental: {
disableWebpackHMR: true,
},
i also created one server.js file,
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl);
}).listen(3000, (err) => {
if (err) throw err;
console.log('> Ready on http://localhost:3000');
});
});
I tried these things to get rid of the issue. On local there is no error but once i deploy i keep getting these errors. Please guys help me out. Thanks in advance
</details>
# 答案1
**得分**: 0
我认为这是因为Next.js无法维持服务器和客户端之间的连接。您应该使用`express.js`库来维护连接。
***参考***
https://youtu.be/6xvLUWpCSFM
<details>
<summary>英文:</summary>
I think this is happening because nextjs can't maintain a connection between the server and client. You should use `express.js` library to maintain a connection.
***Refrence***
https://youtu.be/6xvLUWpCSFM
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论