From 2bc76919dbb34c97e0102786d7a9ba0345fd0d50 Mon Sep 17 00:00:00 2001 From: Josh GM Walker <56300765+Josh-Walker-GM@users.noreply.github.com> Date: Wed, 3 Jan 2024 11:34:32 +0000 Subject: [PATCH] fix(fastify): Prevent duplicate `@fastify/url-data` registration (#9794) --- packages/api-server/src/plugins/withFunctions.ts | 4 +++- packages/api-server/src/plugins/withWebServer.ts | 4 +++- packages/fastify/src/api.ts | 4 +++- packages/fastify/src/graphql.ts | 4 +++- packages/fastify/src/web.ts | 4 +++- packages/web-server/src/web.ts | 4 +++- 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/api-server/src/plugins/withFunctions.ts b/packages/api-server/src/plugins/withFunctions.ts index d985fcbb0697..dd0a2006bb79 100644 --- a/packages/api-server/src/plugins/withFunctions.ts +++ b/packages/api-server/src/plugins/withFunctions.ts @@ -13,7 +13,9 @@ const withFunctions = async ( ) => { const { apiRootPath } = options // Add extra fastify plugins - fastify.register(fastifyUrlData) + if (!fastify.hasPlugin('@fastify/url-data')) { + await fastify.register(fastifyUrlData) + } // Fastify v4 must await the fastifyRawBody plugin // registration to ensure the plugin is ready diff --git a/packages/api-server/src/plugins/withWebServer.ts b/packages/api-server/src/plugins/withWebServer.ts index 98c5b9d8ce6c..d265c428159f 100644 --- a/packages/api-server/src/plugins/withWebServer.ts +++ b/packages/api-server/src/plugins/withWebServer.ts @@ -28,7 +28,9 @@ const withWebServer = async ( fastify: FastifyInstance, options: WebServerArgs ) => { - fastify.register(fastifyUrlData) + if (!fastify.hasPlugin('@fastify/url-data')) { + await fastify.register(fastifyUrlData) + } const prerenderedFiles = findPrerenderedHtml() const indexPath = getFallbackIndexPath() diff --git a/packages/fastify/src/api.ts b/packages/fastify/src/api.ts index 795d6af1debc..85d6d37d2c6a 100644 --- a/packages/fastify/src/api.ts +++ b/packages/fastify/src/api.ts @@ -14,7 +14,9 @@ export async function redwoodFastifyAPI( opts: RedwoodFastifyAPIOptions, done: HookHandlerDoneFunction ) { - fastify.register(fastifyUrlData) + if (!fastify.hasPlugin('@fastify/url-data')) { + await fastify.register(fastifyUrlData) + } await fastify.register(fastifyRawBody) // TODO: This should be refactored to only be defined once and it might not live here diff --git a/packages/fastify/src/graphql.ts b/packages/fastify/src/graphql.ts index 7c83145077ef..b2830829c971 100644 --- a/packages/fastify/src/graphql.ts +++ b/packages/fastify/src/graphql.ts @@ -38,7 +38,9 @@ export async function redwoodFastifyGraphQLServer( // These two plugins are needed to transform a Fastify Request to a Lambda event // which is used by the RedwoodGraphQLContext and mimics the behavior of the // api-server withFunction plugin - fastify.register(fastifyUrlData) + if (!fastify.hasPlugin('@fastify/url-data')) { + await fastify.register(fastifyUrlData) + } await fastify.register(fastifyRawBody) try { diff --git a/packages/fastify/src/web.ts b/packages/fastify/src/web.ts index 93bd0cc89e7e..b8d1bbac26e7 100644 --- a/packages/fastify/src/web.ts +++ b/packages/fastify/src/web.ts @@ -21,7 +21,9 @@ export async function redwoodFastifyWeb( opts: RedwoodFastifyWebOptions, done: HookHandlerDoneFunction ) { - fastify.register(fastifyUrlData) + if (!fastify.hasPlugin('@fastify/url-data')) { + await fastify.register(fastifyUrlData) + } const prerenderedFiles = findPrerenderedHtml() // Serve prerendered HTML directly, instead of the index. diff --git a/packages/web-server/src/web.ts b/packages/web-server/src/web.ts index 8b115880335a..1a60ee88efb2 100644 --- a/packages/web-server/src/web.ts +++ b/packages/web-server/src/web.ts @@ -27,7 +27,9 @@ export async function redwoodFastifyWeb( opts: RedwoodFastifyWebOptions, done: HookHandlerDoneFunction ) { - fastify.register(fastifyUrlData) + if (!fastify.hasPlugin('@fastify/url-data')) { + await fastify.register(fastifyUrlData) + } const prerenderedFiles = findPrerenderedHtml() // Serve prerendered HTML directly, instead of the index.