Skip to content

Commit 4bfecf0

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

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
@@ -125,9 +125,10 @@ function createIntegration<Params extends HotloadAstroClerkIntegrationParams>()
125125
}
126126
127127
if (transitionEnabledOnThisPage()) {
128-
const { navigate, swapFunctions } = await import('astro:transitions/client');
128+
// We must do the dynamic imports within the event listeners because otherwise we may race and miss initial astro:page-load
129+
document.addEventListener('astro:before-swap', async (e) => {
130+
const { swapFunctions } = await import('astro:transitions/client');
129131
130-
document.addEventListener('astro:before-swap', (e) => {
131132
const clerkComponents = document.querySelector('#clerk-components');
132133
// Keep the div element added by Clerk
133134
if (clerkComponents) {
@@ -139,6 +140,8 @@ function createIntegration<Params extends HotloadAstroClerkIntegrationParams>()
139140
});
140141
141142
document.addEventListener('astro:page-load', async (e) => {
143+
const { navigate } = await import('astro:transitions/client');
144+
142145
await runInjectionScript({
143146
...${JSON.stringify(internalParams)},
144147
routerPush: navigate,

0 commit comments

Comments
 (0)