Duplicate $alert notifications appear after SSR hydration in Nuxt 3 with @nuxtjs/alert
0 reputation · 31 Aug 2022, 09:18 UTC
0 reputation · 31 Aug 2022, 09:18 UTC
Prevent duplicate alert messages from appearing when the same $alert call is executed both during server‑side rendering and again after client‑side hydration in a Nuxt 3 application using @nuxtjs/alert.
The module does not expose a built‑in option to suppress client‑side re‑emission of server‑rendered alerts, and there is no documented way to attach a unique identifier or deduplication key to alerts. Developers currently rely on manual workarounds such as guarding calls with process.client or using timers, which are undocumented and may introduce race conditions or memory leaks.
33775 reputation · 31 Aug 2022, 13:53 UTC
Nuxt 3 has no built‑in flag to suppress re‑emission of alerts that were already rendered on the server. The standard, supported way to avoid duplicates is to ensure the $alert call runs only on the client side:
if (process.client) { … } inside setup() or a composable.onMounted() so it never executes during SSR.Either approach guarantees that the alert is triggered only once – after hydration.
setup() or top‑level lifecycle hooks runs on both the server and the client during hydration.$alert is called in that shared code, the server renders the message into the HTML, and the client immediately re‑triggers it.@nuxtjs/alert module does not expose a message ID or deduplication API, so the client has no way to know the alert was already shown.There is no Nuxt 3 lifecycle hook or module option that can skip hydration of alert state. The module’s state is purely client‑side and is not synced from the server.
// Example – guard in setup
import { useAlert } from "@nuxtjs/alert";
export default {
setup() {
const alert = useAlert();
if (process.client) {
alert.error("Page loaded");
}
}
}
After adding the guard, refresh the page and confirm that a single alert appears.
If you are calling $alert inside a composable that is imported by both server‑side and client‑side code, the same guard pattern applies. Let me know if the alert is triggered by a router hook or a plugin, and I can suggest a more targeted guard.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.