From 347cd33e139864f2140fc65dd04d68bd49874ac2 Mon Sep 17 00:00:00 2001 From: Bas van den Aakster Date: Sun, 14 Sep 2025 10:22:34 +0200 Subject: [PATCH] Fix model overwrite error when plugin is initialized multiple times - Filter out existing collections with same slugs before adding plugin collections - Prevents 'Cannot overwrite model once compiled' errors in Next.js apps - Fixes issue during hot reload and multiple getPayload() calls - Bump version to 0.1.19 --- package.json | 2 +- src/payload-types.ts | 5 +++++ src/plugin.ts | 7 ++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 1d3c761..8448d49 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@xtr-dev/payload-mailing", - "version": "0.1.18", + "version": "0.1.19", "description": "Template-based email system with scheduling and job processing for PayloadCMS", "type": "module", "main": "dist/index.js", diff --git a/src/payload-types.ts b/src/payload-types.ts index 08838c9..2f762f5 100644 --- a/src/payload-types.ts +++ b/src/payload-types.ts @@ -143,6 +143,10 @@ export interface Email { * Sender email address (optional, uses default if not provided) */ from?: string | null; + /** + * Sender display name (optional, e.g., "John Doe" for "John Doe ") + */ + fromName?: string | null; /** * Reply-to email address */ @@ -336,6 +340,7 @@ export interface EmailsSelect { cc?: T; bcc?: T; from?: T; + fromName?: T; replyTo?: T; subject?: T; html?: T; diff --git a/src/plugin.ts b/src/plugin.ts index a447526..89ce6b6 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -74,10 +74,15 @@ export const mailingPlugin = (pluginConfig: MailingPluginConfig) => (config: Con }), } satisfies CollectionConfig + // Filter out any existing collections with the same slugs to prevent duplicates + const existingCollections = (config.collections || []).filter( + (collection) => collection.slug !== templatesSlug && collection.slug !== emailsSlug + ) + return { ...config, collections: [ - ...(config.collections || []), + ...existingCollections, templatesCollection, emailsCollection, ],