Skip to content

Commit

Permalink
perf: load client plugin in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Jan 1, 2024
1 parent 5bf4593 commit b7af861
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/runtime/plugin.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@ import type { ModuleOptions } from '../module'
import { initGtag } from './gtag'
import { defineNuxtPlugin, useHead, useRuntimeConfig } from '#imports'

export default defineNuxtPlugin(() => {
const { id, config, initialConsent, loadingStrategy } = useRuntimeConfig().public.gtag as Required<ModuleOptions>
export default defineNuxtPlugin({
parallel: true,
setup() {
const { id, config, initialConsent, loadingStrategy } = useRuntimeConfig().public.gtag as Required<ModuleOptions>

if (!id)
return
if (!id)
return

initGtag({ id, config })
initGtag({ id, config })

if (!initialConsent)
return
if (!initialConsent)
return

// Sanitize loading strategy to be either `async` or `defer`
const strategy = loadingStrategy === 'async' ? 'async' : 'defer'
// Sanitize loading strategy to be either `async` or `defer`
const strategy = loadingStrategy === 'async' ? 'async' : 'defer'

useHead({
script: [
{
src: `https://www.googletagmanager.com/gtag/js?id=${id}`,
[strategy]: true,
},
],
})
useHead({
script: [
{
src: `https://www.googletagmanager.com/gtag/js?id=${id}`,
[strategy]: true,
},
],
})
},
})

0 comments on commit b7af861

Please sign in to comment.