Skip to content
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

feat(astro): Add support for View Transitions #4354

Merged
merged 15 commits into from
Oct 18, 2024

Conversation

wobsoriano
Copy link
Member

@wobsoriano wobsoriano commented Oct 17, 2024

Description

This PR adds Astro View Transitions support to our Clerk components. It is basically the "SPA mode" for Astro.

Before this change, Clerk components didn't work properly with Astro View Transitions. When using the <ViewTransitions /> component in an Astro app, our Clerk components weren't remounting during page navigation.

The solution was to:

  1. Run the runInjectionScript function in the astro:page-load to ensure Clerk components are properly mounted when switching pages.
  2. Create a custom swap function and keep the elements added by @emotion/cache. This maintains the styling across page transitions.

Here's a basic demo using <SignIn />, <UserProfile /> and <UserButton /> components being rendered in a view transitions enabled pages:

Screen.Recording.2024-10-17.at.1.10.21.PM.mov

Resolves ECO-64

Checklist

  • npm test runs as expected.
  • npm run 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:

Copy link

changeset-bot bot commented Oct 17, 2024

🦋 Changeset detected

Latest commit: b58e76b

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

This PR includes changesets to release 1 package
Name Type
@clerk/astro Minor

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

@wobsoriano wobsoriano marked this pull request as ready for review October 17, 2024 20:29
Comment on lines +19 to +20
const emotionElements = document.querySelectorAll(`style[${EMOTION_ATTR}]`);
swapHeadElements(doc, Array.from(emotionElements));
Copy link
Member Author

Choose a reason for hiding this comment

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

We need to keep the <style> element added by @emotion/cache to keep the styling when switching between pages.

Screenshot 2024-10-17 at 1 34 41 PM

The default Astro swap function will remove this one

Copy link
Member

@panteliselef panteliselef left a comment

Choose a reason for hiding this comment

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

Seems great. In the video, i see that the "afterSignIn" navigation did not run with the page transitions (fade-in/out). Is this expected ?

@@ -0,0 +1,61 @@
// eslint-disable-next-line import/no-unresolved
import { swapFunctions } from 'astro:transitions/client';
Copy link
Member

@panteliselef panteliselef Oct 18, 2024

Choose a reason for hiding this comment

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

❓ Is this always bundled with Astro ? Is there a case where the module will not exists causing our integration to break ?

Copy link
Member Author

@wobsoriano wobsoriano Oct 18, 2024

Choose a reason for hiding this comment

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

It is indeed bundled with Astro (astro/virtual-modules/transitions-swap-functions.js), but is not available in Astro v3.

Also we let the Astro Vite plugin handle it by excluding from vite.optimizeDeps:

config.optimizeDeps?.exclude?.push('astro:transitions/client');

@wobsoriano
Copy link
Member Author

wobsoriano commented Oct 18, 2024

Seems great. In the video, i see that the "afterSignIn" navigation did not run with the page transitions (fade-in/out). Is this expected ?

Good catch! I've added another update that will use the navigate method from astro:transitions/client if transition is enabled on a page:

Screen.Recording.2024-10-18.at.7.59.28.AM.mov

It should now handle any routing within the components too

@wobsoriano wobsoriano marked this pull request as draft October 18, 2024 15:36
@clerk clerk deleted a comment from clerk-cookie Oct 18, 2024
@wobsoriano
Copy link
Member Author

!snapshot

@clerk-cookie
Copy link
Collaborator

Hey @wobsoriano - the snapshot version command generated the following package versions:

Package Version
@clerk/astro 1.4.0-snapshot.vb58e76b
@clerk/backend 1.14.2-snapshot.vb58e76b
@clerk/chrome-extension 1.3.21-snapshot.vb58e76b
@clerk/clerk-js 5.28.0-snapshot.vb58e76b
@clerk/elements 0.17.0-snapshot.vb58e76b
@clerk/clerk-expo 2.2.27-snapshot.vb58e76b
@clerk/express 1.3.2-snapshot.vb58e76b
@clerk/fastify 2.0.4-snapshot.vb58e76b
@clerk/localizations 3.3.1-snapshot.vb58e76b
@clerk/nextjs 5.8.0-snapshot.vb58e76b
@clerk/clerk-react 5.12.1-snapshot.vb58e76b
@clerk/remix 4.2.40-snapshot.vb58e76b
@clerk/clerk-sdk-node 5.0.53-snapshot.vb58e76b
@clerk/shared 2.10.0-snapshot.vb58e76b
@clerk/tanstack-start 0.4.16-snapshot.vb58e76b
@clerk/testing 1.3.14-snapshot.vb58e76b
@clerk/themes 2.1.38-snapshot.vb58e76b
@clerk/types 4.27.0-snapshot.vb58e76b

Tip: Use the snippet copy button below to quickly install the required packages.
@clerk/astro

npm i @clerk/[email protected] --save-exact

@clerk/backend

npm i @clerk/[email protected] --save-exact

@clerk/chrome-extension

npm i @clerk/[email protected] --save-exact

@clerk/clerk-js

npm i @clerk/[email protected] --save-exact

@clerk/elements

npm i @clerk/[email protected] --save-exact

@clerk/clerk-expo

npm i @clerk/[email protected] --save-exact

@clerk/express

npm i @clerk/[email protected] --save-exact

@clerk/fastify

npm i @clerk/[email protected] --save-exact

@clerk/localizations

npm i @clerk/[email protected] --save-exact

@clerk/nextjs

npm i @clerk/[email protected] --save-exact

@clerk/clerk-react

npm i @clerk/[email protected] --save-exact

@clerk/remix

npm i @clerk/[email protected] --save-exact

@clerk/clerk-sdk-node

npm i @clerk/[email protected] --save-exact

@clerk/shared

npm i @clerk/[email protected] --save-exact

@clerk/tanstack-start

npm i @clerk/[email protected] --save-exact

@clerk/testing

npm i @clerk/[email protected] --save-exact

@clerk/themes

npm i @clerk/[email protected] --save-exact

@clerk/types

npm i @clerk/[email protected] --save-exact

@wobsoriano wobsoriano marked this pull request as ready for review October 18, 2024 15:51
@wobsoriano wobsoriano merged commit 9d34d4f into main Oct 18, 2024
21 checks passed
@wobsoriano wobsoriano deleted the rob/eco-64-astro-view-transitions-support branch October 18, 2024 16:02
@mlafeldt
Copy link
Contributor

mlafeldt commented Nov 6, 2024

Hey @wobsoriano!

I'm using @clerk/astro v1.4.7, which should contain this change. However, I'm still seeing exactly this behavior on Cloudflare Pages with the UserButton disappearing randomly on navigation:

Before this change, Clerk components didn't work properly with Astro View Transitions. When using the <ViewTransitions /> component in an Astro app, our Clerk components weren't remounting during page navigation.

My workaround is to use UserButton from @clerk/astro/react instead of @clerk/astro/components, which works fine.

Maybe they changed something in the latest Astro release?

❯ bun astro info
Astro                    v4.16.9
Node                     v22.9.0
System                   macOS (arm64)
Package Manager          bun
Output                   hybrid
Adapter                  @astrojs/cloudflare
Integrations             @clerk/astro/integration
                         @astrojs/react
                         @astrojs/tailwind

@wobsoriano
Copy link
Member Author

@mlafeldt that is interesting. I will try and update my astro template here and check if the UI components from @clerk/astro/components fails 👀

@mlafeldt
Copy link
Contributor

mlafeldt commented Nov 6, 2024

@wobsoriano It looks like the UserButton disappears when navigating between pre-rendered and server-rendered pages. One page in my app uses React without SSR. Adding export const prerender = false to that page fixes the problem.

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.

4 participants