Skip to content

Conversation

@wobsoriano
Copy link
Member

@wobsoriano wobsoriano commented Jan 29, 2025

Description

This PR moves the core path matcher logic living inside the createRouteMatcher of our Astro and Next.js SDKs to shared package to avoid duplication and pave the way for other SDKs like Nuxt. We'd want to make it easy to adopt in other SDKs while keeping framework-specific handling separate.

In Nuxt, it will be implemented like this to be used within pages:

import type { RouteLocationNormalizedLoadedGeneric } from 'vue-router'

export const createRouteMatcher = (routes: RouteMatcherParam) => {
  const matcher = createPathMatcher(routes);
  return (to: RouteLocationNormalizedLoadedGeneric) => matcher(to.path)
}

and in a global nuxt route middleware, they can use it like so:

export default defineNuxtRouteMiddleware((to) => {
  const { userId } = useAuth()

  const isProtectedRoute = createRouteMatcher(['/dashboard(.*)', '/forum(.*)'])

  if (!userId.value && isProtectedRoute(to)) {
    return navigateTo('/sign-in')
  }
})

For API routes, a separate createRouteMatcher will be created that accepts an H3 Event

import { H3Event, getRequestURL } from 'h3'

export const createRouteMatcher = (routes: RouteMatcherParam) => {
  const matcher = createPathMatcher(routes);
  return (event: H3Event) => {
    const url = getRequestURL(event); // returns the full url
    return matcher(new URL(url).pathname)
  }
}

Resolves ECO-352

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@changeset-bot
Copy link

changeset-bot bot commented Jan 29, 2025

🦋 Changeset detected

Latest commit: aca469a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 19 packages
Name Type
@clerk/astro Patch
@clerk/nextjs Patch
@clerk/shared Patch
@clerk/backend Patch
@clerk/chrome-extension Patch
@clerk/clerk-js Patch
@clerk/elements Patch
@clerk/expo-passkeys Patch
@clerk/clerk-expo Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/clerk-react Patch
@clerk/remix Patch
@clerk/tanstack-start Patch
@clerk/testing Patch
@clerk/ui Patch
@clerk/vue Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Jan 29, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
clerk-js-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 30, 2025 4:09pm

Co-authored-by: Lennart <lekoarts@gmail.com>
@wobsoriano wobsoriano enabled auto-merge (squash) January 30, 2025 16:08
@wobsoriano wobsoriano merged commit 26225f2 into main Jan 30, 2025
29 checks passed
@wobsoriano wobsoriano deleted the rob/eco-352-move-createroutematcher-to-shared-package branch January 30, 2025 16:17
wobsoriano added a commit that referenced this pull request Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants