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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(clerk-react): Add frontendAPI on window as a fallback #492

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export type ClerkCoreBroadcastChannelEvent = { type: 'signout' };
declare global {
interface Window {
Clerk?: Clerk;
__clerk_frontend_api?: string;
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/clerk-js/src/index.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { mountComponentRenderer } from './ui';
Clerk.mountComponentRenderer = mountComponentRenderer;

const frontendApi =
document.querySelector('script[data-clerk-frontend-api]')?.getAttribute('data-clerk-frontend-api') || '';
document.querySelector('script[data-clerk-frontend-api]')?.getAttribute('data-clerk-frontend-api') ||
window.__clerk_frontend_api ||
'';

window.Clerk = new Clerk(frontendApi);

Expand Down
4 changes: 3 additions & 1 deletion packages/clerk-js/src/index.headless.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'regenerator-runtime/runtime';
import Clerk from './core/clerk';

const frontendApi =
document.querySelector('script[data-clerk-frontend-api]')?.getAttribute('data-clerk-frontend-api') || '';
document.querySelector('script[data-clerk-frontend-api]')?.getAttribute('data-clerk-frontend-api') ||
window.__clerk_frontend_api ||
'';

window.Clerk = new Clerk(frontendApi);

Expand Down
13 changes: 13 additions & 0 deletions packages/react/src/isomorphicClerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ export default class IsomorphicClerk {
if (!this.frontendApi) {
this.throwError(noFrontendApiError);
}

// Store frontendAPI value on window as a fallback. This value can be used as a
// fallback during ClerkJS hot loading in case ClerkJS fails to find the
// "data-clerk-frontend-api" attribute on its script tag.

// This can happen when the DOM is altered completely during client rehydration.
// For example, in Remix with React 18 the document changes completely via `hydrateRoot(document)`.

// For more information refer to:
// - https://github.com/remix-run/remix/issues/2947
// - https://github.com/facebook/react/issues/24430
window.__clerk_frontend_api = this.frontendApi;

try {
if (this.Clerk) {
// Set a fixed Clerk version
Expand Down
6 changes: 6 additions & 0 deletions packages/react/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { Clerk, ClerkOptions, ClientResource, LoadedClerk, RedirectOptions, UserResource } from '@clerk/types';

declare global {
interface Window {
__clerk_frontend_api?: string;
}
}

export interface IsomorphicClerkOptions extends ClerkOptions {
Clerk?: ClerkProp;
clerkJSUrl?: string;
Expand Down