From 30f8e82873ec45a6ea9a7e45dde937a56f6669a1 Mon Sep 17 00:00:00 2001 From: Robert Soriano Date: Tue, 3 Dec 2024 01:13:15 -0800 Subject: [PATCH] chore(astro): Add backwards compatibility for ignoring prerendered routes in Astro middleware (#4694) --- .changeset/tame-lamps-rule.md | 5 +++++ packages/astro/src/server/clerk-middleware.ts | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 .changeset/tame-lamps-rule.md diff --git a/.changeset/tame-lamps-rule.md b/.changeset/tame-lamps-rule.md new file mode 100644 index 0000000000..a7f68ebedb --- /dev/null +++ b/.changeset/tame-lamps-rule.md @@ -0,0 +1,5 @@ +--- +"@clerk/astro": patch +--- + +Add backwards compatibility for ignoring prerendered routes in Astro diff --git a/packages/astro/src/server/clerk-middleware.ts b/packages/astro/src/server/clerk-middleware.ts index ba1426c7f7..3bdf0a53cf 100644 --- a/packages/astro/src/server/clerk-middleware.ts +++ b/packages/astro/src/server/clerk-middleware.ts @@ -62,7 +62,7 @@ export const clerkMiddleware: ClerkMiddleware = (...args: unknown[]): any => { const astroMiddleware: AstroMiddleware = async (context, next) => { // if the current page is prerendered, do nothing - if ('isPrerendered' in context && context.isPrerendered) { + if (isPrerenderedPage(context)) { return next(); } @@ -129,6 +129,15 @@ export const clerkMiddleware: ClerkMiddleware = (...args: unknown[]): any => { return astroMiddleware; }; +const isPrerenderedPage = (context: APIContext) => { + return ( + // for Astro v5 + ('isPrerendered' in context && context.isPrerendered) || + // for Astro v4 + ('_isPrerendered' in context && context._isPrerendered) + ); +}; + // TODO-SHARED: Duplicate from '@clerk/nextjs' const parseHandlerAndOptions = (args: unknown[]) => { return [