#e
1 messages · Page 1 of 1 (latest)
I see you're encountering cross-origin request warnings in your Next.js development environment. The warning indicates that requests from IP address 192.168.2.145 to your Next.js resources are being detected as cross-origin requests.
Your current configuration is close, but there's an issue with how you've specified the allowedDevOrigins. According to the documentation, the allowedDevOrigins configuration should contain only the domain or IP without the protocol (http://).
Here's how you should modify your configuration:
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {
allowedDevOrigins: ['192.168.2.145'],
};
export default nextConfig;
The allowedDevOrigins option is not part of the experimental features - it's a top-level configuration option in Next.js. This option allows you to set additional origins that can be used in development mode beyond the default localhost.
You can also use wildcards for subdomains if needed, like '*.local-origin.dev' as shown in the Next.js documentation on allowedDevOrigins.
Cross-origin requests are blocked by default in development mode as a security measure to prevent unauthorized access to internal assets and endpoints.
If you're still experiencing issues after making this change, make sure to restart your Next.js development server for the changes to take effect.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@compact cedar Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Let me check the knowledge base for you again. Give me one moment! :books:
I can see you're trying to implement WebSockets in your Next.js application, but there are several issues with your current implementation. Based on the knowledge sources, here are the problems and solutions:
Issues with your implementation:
- Your client-side code is missing the WebSocket URL:
new WebSocket()should include a URL parameter. - The server-side implementation doesn't follow Next.js's WebSocket handling patterns.
- There might be conflicts between your WebSocket server and Next.js's built-in HMR (Hot Module Replacement) which also uses WebSockets.
Recommended solution:
Client-side (app/page.tsx):
"use client";
import { useState, useEffect } from "react";
export default function Home() {
const [isConnected, setIsConnected] = useState(false);
useEffect(() => {
const socket = new WebSocket("ws://localhost:3000/api/ws");
socket.onopen = () => {
console.log("WebSocket connection established");
setIsConnected(true);
};
socket.onerror = (error) => {
console.error("WebSocket error:", error);
};
socket.onclose = () => {
console.log("WebSocket connection closed");
setIsConnected(false);
};
return () => {
if (socket) {
socket.close();
}
};
}, []);
return (
<div className="flex w-full h-full p-1">
<p className="text-muted opacity-80 font-medium">
e
</p>
{isConnected ? (
<p className="text-green-500">WebSocket Connected!</p>
) : (
<p className="text-red-500">Connecting...</p>
)}
</div>
);
}
Important considerations:
-
WebSocket conflicts with HMR: Next.js uses WebSockets for Hot Module Replacement (HMR) since version 12. Your custom WebSocket implementation might conflict with it, as mentioned in this GitHub issue.
-
Custom server approach: For more complex WebSocket implementations, you might need a custom server. If you go this route, make sure to handle the upgrade request properly as shown in the Next.js documentation:
app.all('/_next/webpack-hmr', (req, res) => {
nextjsRequestHandler(req, res)
})
-
Using a WebSocket library: Consider using a higher-level WebSocket library like Ably, which has a starter kit for Next.js that handles many of the complexities.
-
WebSocket server with
noServeroption: If you're using a custom server with WebSocket, use thenoServer: trueoption and handle the upgrade manually to avoid conflicts:
const wsServer = new WebSocketServer({
noServer: true,
path: '/api/ws',
})
httpServer.on("upgrade", (request, socket, head) => {
if (request.url.startsWith('/api/ws')) {
wsServer.handleUpgrade(request, socket, head, (ws) => {
wsServer.emit("connection", ws, request)
})
}
})
This approach helps prevent conflicts with Next.js's internal WebSocket for HMR as mentioned in this GitHub issue.
If you continue to face issues, you might need to provide more details about your specific setup or consider using a specialized WebSocket service like Ably that's designed to work well with Next.js.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@compact cedar Kapa.ai is still learning and improving, please let me know how I did by reacting below.
e