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,clerk-js): View transitions support #4342

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/perfect-singers-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@clerk/astro": minor
"@clerk/clerk-js": patch
---

Add support for Astro View Transitions
11 changes: 10 additions & 1 deletion packages/astro/src/integration/create-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,16 @@ function createIntegration<Params extends HotloadAstroClerkIntegrationParams>()
`
${command === 'dev' ? `console.log("${packageName}","Initialize Clerk: page")` : ''}
import { runInjectionScript } from "${buildImportPath}";
await runInjectionScript(${JSON.stringify(internalParams)});`,

await runInjectionScript(${JSON.stringify(internalParams)});

document.addEventListener('astro:before-swap', () => {
window.Clerk.__internal_flushEmotionCache();
});

document.addEventListener('astro:page-load', async () => {
await runInjectionScript(${JSON.stringify(internalParams)});
})`,
Comment on lines +120 to +122
Copy link
Member Author

@wobsoriano wobsoriano Oct 17, 2024

Choose a reason for hiding this comment

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

This and astro:before-swap runs only on view transition enabled pages. This will run on load and every page navigation.

It mounts all Clerk components in the new document.

);
},
},
Expand Down
11 changes: 11 additions & 0 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,17 @@ export class Clerk implements ClerkInterface {
return this.navigate(to);
}

/**
* This clears the emotion cache, which is particularly useful
* when working with Astro View Transitions to prevent style loss during page navigation.
*
* For more info on the issue, see:
* https://github.com/withastro/astro/issues/11000
*/
__internal_flushEmotionCache() {
Copy link
Member Author

@wobsoriano wobsoriano Oct 17, 2024

Choose a reason for hiding this comment

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

@nikosdouvlis asking in advance here if there's a better way to access the internal emotion cache rather than what I did here. Any advice would do!

Issue: With Astro's View Transitions, navigating to a new page removes unnecessary head elements, including the emotion cache. Clearing the cache resolves this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, this adds 140KB to the bundle size so this is not a good solution

return this.#componentControls?.ensureMounted().then(controls => controls.flushEmotionCache());
}

#hasJustSynced = () => getClerkQueryParam(CLERK_SYNCED) === 'true';
#clearJustSynced = () => removeClerkQueryParam(CLERK_SYNCED);

Expand Down
8 changes: 8 additions & 0 deletions packages/clerk-js/src/ui/Components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
LazyProviders,
OrganizationSwitcherPrefetch,
} from './lazyModules/providers';
import { emotionCache } from './styledSystem/InternalThemeProvider';
import type { AvailableComponentProps } from './types';

const ROOT_ELEMENT_ID = 'clerk-components';
Expand Down Expand Up @@ -90,6 +91,7 @@ export type ComponentControls = {
},
) => void;
prefetch: (component: 'organizationSwitcher') => void;
flushEmotionCache: () => void;
// Special case, as the impersonation fab mounts automatically
mountImpersonationFab: () => void;
};
Expand Down Expand Up @@ -309,6 +311,12 @@ const Components = (props: ComponentsProps) => {
setState(s => ({ ...s, [`${component}Prefetch`]: true }));
};

componentsControls.flushEmotionCache = () => {
emotionCache.sheet.flush();
emotionCache.inserted = {};
emotionCache.registered = {};
};
Comment on lines +314 to +318
Copy link
Member Author

Choose a reason for hiding this comment

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

I got the idea here


props.onComponentsMounted();
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { InternalTheme } from './index';

const el = document.querySelector('style#cl-style-insertion-point');

const cache = createCache({
export const emotionCache = createCache({
key: 'cl-internal',
prepend: !el,
insertionPoint: el ? (el as HTMLElement) : undefined,
Expand All @@ -23,7 +23,7 @@ export const InternalThemeProvider = (props: InternalThemeProviderProps) => {
const { parsedInternalTheme } = useAppearance();

return (
<CacheProvider value={cache}>
<CacheProvider value={emotionCache}>
<ThemeProvider theme={parsedInternalTheme}>{props.children}</ThemeProvider>
</CacheProvider>
);
Expand Down
Loading