From c4ffc4f3acf4e829802b0deed13f6e7d0b399b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Prod=27homme?= Date: Wed, 31 Jul 2024 15:09:43 +0200 Subject: [PATCH] Correct the path to Strapi's socket --- .../modules/alerts/services/alerts-service.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/admin/src/modules/alerts/services/alerts-service.js b/admin/src/modules/alerts/services/alerts-service.js index ae54490..1fa1fc3 100644 --- a/admin/src/modules/alerts/services/alerts-service.js +++ b/admin/src/modules/alerts/services/alerts-service.js @@ -8,7 +8,23 @@ export default class AlertsService { _streamIdentifier; constructor() { - this._client = SocketIoClient.connect(process.env.STRAPI_ADMIN_BACKEND_URL); + const uri = process.env.STRAPI_ADMIN_BACKEND_URL; + let path = '/socket.io'; // SocketIoClient defaults to this path too if it is not defined + + // If uri is not undefined nor empty, use the correct path to Strapi's socket + if (uri !== undefined && uri.length > 0) { + try { + const { pathname } = new URL( + uri, + window.location.href // Mandatory in case uri is not a complete URL (e.g. /cms) + ); + path = `${pathname}${pathname.endsWith('/') ? '' : '/'}socket.io`; + } catch (e) { + console.error(e); + } + } + + this._client = SocketIoClient.connect(uri, { path }); } setStreamIdentifier(streamIdentifier) {