Skip to content

Commit

Permalink
fix: hydration issue
Browse files Browse the repository at this point in the history
  • Loading branch information
giraud florent committed Aug 6, 2023
1 parent 998ee2f commit e68ff2a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
8 changes: 3 additions & 5 deletions playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
<h1>Nuxt - Stripe module playground</h1>
<section>
<h2>Stripe client</h2>
<client-only>
<code>
{{ stripeClient ? stripeClient : 'Loading...' }}
</code>
</client-only>
<code>
{{ stripeClient ? stripeClient : 'Loading...' }}
</code>
</section>
<OtherComponent />
</main>
Expand Down
16 changes: 8 additions & 8 deletions playground/components/OtherComponent.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<template>
<div>Other component using stripe</div>
<section>
<div>
<div>Other component using stripe</div>
<section>
<h2>Stripe client</h2>
<client-only>
<code>
{{ stripeClient ? stripeClient : 'Loading...' }}
</code>
</client-only>
</section>
<code>
{{ stripeClient ? stripeClient : 'Loading...' }}
</code>
</section>
</div>
</template>

<script setup lang="ts">
Expand Down
2 changes: 1 addition & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface ServerStripeOptions {
}

export interface ClientStripeOptions {
key?: string | null;
key?: string | null;
options?: StripeConstructorOptions;
}

Expand Down
22 changes: 17 additions & 5 deletions src/runtime/composables/useClientStripe.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { useNuxtApp } from "#imports"

import { onBeforeMount, useNuxtApp, useState } from "#imports"
import { Stripe } from '@stripe/stripe-js'
/**
* useClientStripe function
*
* This function is a helper to easily access the Stripe instance provided by the Nuxt plugin.
* It can be used in components or pages to interact with the Stripe.js library.
*
*/
export default function useClientStripe() {
if (process.server) return null
return useNuxtApp().$stripe()
export default async function useClientStripe() {
const nuxtApp = useNuxtApp()
const stripe = useState<Stripe>('stripe-client', () => null)
const isLoading = useState('stripe-client-loading', () => false)

onBeforeMount(async () => {
if (!isLoading.value) {
isLoading.value = true
const _stripe = await nuxtApp.$stripe._loadStripe()
stripe.value = _stripe
isLoading.value = false
}
})

return stripe
}
4 changes: 3 additions & 1 deletion src/runtime/plugins/stripe.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export default defineNuxtPlugin(async(nuxtApp) => {

return {
provide: {
stripe: _loadStripe,
stripe: {
_loadStripe
},
}
}
})

0 comments on commit e68ff2a

Please sign in to comment.