Skip to content

Commit

Permalink
fix(fastify): Prevent duplicate @fastify/url-data registration (#9794)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Walker-GM authored Jan 3, 2024
1 parent f053729 commit 660f033
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/api-server/src/plugins/withFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion packages/api-server/src/plugins/withWebServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion packages/fastify/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion packages/fastify/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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 {
Expand Down
4 changes: 3 additions & 1 deletion packages/fastify/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion packages/web-server/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 660f033

Please sign in to comment.