I am using Firebase v9 within NextJS 13 Edge API Routes.
FYI the Edge API Routes do not allow Node.JS APIs, only those in the JS V8 engine.
I call an auth
function in one of those routes to the REST API for a token and then get data from firestore and update firestore.
When building the application, I receive the following error:
./node_modules/@firebase/auth/dist/esm2017/index-30f3030e.js
A Node.js API is used (MessageChannel at line: 6512) which is not supported in the Edge Runtime.
Learn more: https://nextjs.org/docs/api-reference/edge-runtime
I have searched the originating code and this appears to be the most important part (the full class can be found at the bottom, but is a long piece of code)
async _send(eventType, data, timeout = 50 /* _TimeoutDuration.ACK */) {
const messageChannel = typeof MessageChannel !== 'undefined' ? new MessageChannel() : null;
if (!messageChannel) {
throw new Error("connection_unavailable" /* _MessageError.CONNECTION_UNAVAILABLE */);
}
// Node timers and browser timers return fundamentally different types.
// We don't actually care what the value is but TS won't accept unknown and
// we can't cast properly in both environments.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Would I be correct in saying that this piece of code appears to be unimportant with regards to it not working in a Edge runtime environment as it looks like its purpose is to do with Typescript only and the comment mentions that they do not care what the value is?
Any help appreciated, thank you in advance!
##########################
Full code for convienence:
class Sender {
constructor(target) {
this.target = target;
this.handlers = new Set();
}
/**
* Unsubscribe the handler and remove it from our tracking Set.
*
* @param handler - The handler to unsubscribe.
*/
removeMessageHandler(handler) {
if (handler.messageChannel) {
handler.messageChannel.port1.removeEventListener('message', handler.onMessage);
handler.messageChannel.port1.close();
}
this.handlers.delete(handler);
}
/**
* Send a message to the Receiver located at {@link target}.
*
* @remarks
* We'll first wait a bit for an ACK , if we get one we will wait significantly longer until the
* receiver has had a chance to fully process the event.
*
* @param eventType - Type of event to send.
* @param data - The payload of the event.
* @param timeout - Timeout for waiting on an ACK from the receiver.
*
* @returns An array of settled promises from all the handlers that were listening on the receiver.
*/
async _send(eventType, data, timeout = 50 /* _TimeoutDuration.ACK */) {
const messageChannel = typeof MessageChannel !== 'undefined' ? new MessageChannel() : null;
if (!messageChannel) {
throw new Error("connection_unavailable" /* _MessageError.CONNECTION_UNAVAILABLE */);
}
// Node timers and browser timers return fundamentally different types.
// We don't actually care what the value is but TS won't accept unknown and
// we can't cast properly in both environments.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let completionTimer;
let handler;
return new Promise((resolve, reject) => {
const eventId = _generateEventId('', 20);
messageChannel.port1.start();
const ackTimer = setTimeout(() => {
reject(new Error("unsupported_event" /* _MessageError.UNSUPPORTED_EVENT */));
}, timeout);
handler = {
messageChannel,
onMessage(event) {
const messageEvent = event;
if (messageEvent.data.eventId !== eventId) {
return;
}
switch (messageEvent.data.status) {
case "ack" /* _Status.ACK */:
// The receiver should ACK first.
clearTimeout(ackTimer);
completionTimer = setTimeout(() => {
reject(new Error("timeout" /* _MessageError.TIMEOUT */));
}, 3000 /* _TimeoutDuration.COMPLETION */);
break;
case "done" /* _Status.DONE */:
// Once the receiver's handlers are finished we will get the results.
clearTimeout(completionTimer);
resolve(messageEvent.data.response);
break;
default:
clearTimeout(ackTimer);
clearTimeout(completionTimer);
reject(new Error("invalid_response" /* _MessageError.INVALID_RESPONSE */));
break;
}
}
};
this.handlers.add(handler);
messageChannel.port1.addEventListener('message', handler.onMessage);
this.target.postMessage({
eventType,
eventId,
data
}, [messageChannel.port2]);
}).finally(() => {
if (handler) {
this.removeMessageHandler(handler);
}
});
}
}
2
Answers
After some testing it appears that this Firebase library does not work in the Edge runtime. Either it should be avoided or you should use the Firebase REST API.
The
next-firebase-auth-edge
library was designed for this specific use case.