Skip to content

Commit d455c13

Browse files
fix(astro): Ensure runInjectionScript runs on initial page loads with view transitions enabled (#7801)
Co-authored-by: Robert Soriano <sorianorobertc@gmail.com>
1 parent 079a8c4 commit d455c13

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

.changeset/ready-cats-tease.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/astro': patch
3+
---
4+
5+
Fixed an issue when using `ClientRouter` where Clerk components don't load until navigation is performed.

packages/astro/src/integration/create-integration.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@ function createIntegration<Params extends HotloadAstroClerkIntegrationParams>()
135135
}
136136
137137
if (transitionEnabledOnThisPage()) {
138-
const { navigate, swapFunctions } = await import('astro:transitions/client');
138+
// We must do the dynamic imports within the event listeners because otherwise we may race and miss initial astro:page-load
139+
document.addEventListener('astro:before-swap', async (e) => {
140+
const { swapFunctions } = await import('astro:transitions/client');
139141
140-
document.addEventListener('astro:before-swap', (e) => {
141142
const clerkComponents = document.querySelector('#clerk-components');
142143
// Keep the div element added by Clerk
143144
if (clerkComponents) {
@@ -149,6 +150,8 @@ function createIntegration<Params extends HotloadAstroClerkIntegrationParams>()
149150
});
150151
151152
document.addEventListener('astro:page-load', async (e) => {
153+
const { navigate } = await import('astro:transitions/client');
154+
152155
await runInjectionScript({
153156
...${JSON.stringify(internalParams)},
154157
routerPush: navigate,

0 commit comments

Comments
 (0)