Skip to content

docs: Update Clerk integration example to Clerk Core 2 #1021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 29, 2024

Conversation

gcascio
Copy link
Contributor

@gcascio gcascio commented Apr 25, 2024

Clerk recently release Clerk Core 2 wich changes the procedure of integrating with next-intl. This PR updates the docs to reflect these changes.

Copy link

vercel bot commented Apr 25, 2024

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

Name Status Preview Comments Updated (UTC)
next-intl-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 29, 2024 8:11am
next-intl-example-app-router ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 29, 2024 8:11am

Copy link

vercel bot commented Apr 25, 2024

@gcascio is attempting to deploy a commit to the next-intl Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Owner

@amannn amannn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for proposing this update @gcascio! 🙌

import {
clerkMiddleware,
createRouteMatcher,
redirectToSignIn,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are correct, I removed it.

Comment on lines 622 to 625
/**
* Match all paths except of internal /_next/ routes, static files and api paths
*/
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api)(.*)'],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need to update the matcher from the previous value? Generally, we recommend a matcher based on available locales as this is safer than checking for requests not containing a dot for example.

Suggested change
/**
* Match all paths except of internal /_next/ routes, static files and api paths
*/
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api)(.*)'],
// Match only internationalized pathnames
matcher: ['/', '/(de|en)/:path*']

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is that the clerk middleware runs on all page routes ensuring that auth checks are performed. Alternatively one could manually ensure that the matcher covers all protected routes, something like this:

const isProtectedRoute = createRouteMatcher([
  '/(de|en)/dashboard(.*)',
]);

// ...

export const config = {
  // Match only internationalized pathnames
  matcher: ['/', '/(de|en)/:path*'],
};

Maybe the above would be good for the current example, while more sophisticated use cases should be handled individually. WDYT?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that's a good point.

Previously we had:

  publicRoutes: ['/:locale', '/:locale/sign-in']

The updated docs now include:

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

At least in the default config of next-intl, there should be a locale prefix for every route. That's also what the default matcher I've mentioned above assumes.

Does Clerk still support a pattern like /:locale? Or would this be /[\w-]+/dashboard(.*) with the new config to assume a locale prefix? If we don't need to hardcode locales that's a good thing, the Next.js matcher unfortunately requires a static config.

Do you have an app where you've upgraded to the latest Clerk version and is this working for you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes Clerk still does support the mentioned path pattern!

I upgraded a production app to the latest Clerk version without issues so far, unfortunately it is closed source. The biggest change IMHO ist that now by default, clerkMiddleware treats all pages as public.

I just created a test repo to play around, you can find it here. There I used the /:locale pattern in the clerk middleware which looks like this:

import createMiddleware from 'next-intl/middleware';
import {
  clerkMiddleware,
  createRouteMatcher,
} from '@clerk/nextjs/server';

const intlMiddleware = createMiddleware({
  locales: ['en', 'de'],
  defaultLocale: 'en'
});

const isProtectedRoute = createRouteMatcher([
  '/:locale/dashboard(.*)',
]);

export default clerkMiddleware((auth, req) => {
  if (isProtectedRoute(req)) auth().protect();

  return intlMiddleware(req);
});

export const config = {
  // Match only internationalized pathnames
  matcher: ['/', '/(de|en)/:path*'],
};

I think this is as close as it gets to the previous example, if I am not missing something.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, that looks perfect from my perspective! Would you mind updating the PR again with that snippet? Happy to merge then!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, I updated the PR!

@amannn amannn changed the title docs: Update Clerk integration example docs: Update Clerk integration example to Clerk Core 2 Apr 29, 2024
Copy link
Owner

@amannn amannn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some formatting nits to be consistent, I'll apply the suggestions from the GitHub UI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants