Skip to content

Commit

Permalink
fix: handle scripts missing src
Browse files Browse the repository at this point in the history
Fixes #240
  • Loading branch information
harlan-zw committed Sep 4, 2024
1 parent f4de446 commit 510d7b9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/plugins/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti
if (canBundle) {
const newSrc = options.resolveScript(src)
if (src === newSrc) {
if (src.startsWith('/'))
if (src && src.startsWith('/'))
console.warn(`[Nuxt Scripts: Bundle Transformer] Relative scripts are already bundled. Skipping bundling for \`${src}\`.`)
else
console.warn(`[Nuxt Scripts: Bundle Transformer] Failed to bundle ${src}.`)
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/composables/useScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
options = defu(options, useNuxtScriptRuntimeConfig()?.defaultScriptOptions) as NuxtUseScriptOptions<T, U>
// browser hint optimizations
const rel = options.trigger === 'onNuxtReady' ? 'preload' : 'preconnect'
const isCrossOrigin = input.src && !input.src.startsWith('/')
const id = resolveScriptKey(input) as keyof typeof nuxtApp._scripts
if (options.trigger !== 'server' && (rel === 'preload' || !input.src.startsWith('/'))) {
if (input.src && options.trigger !== 'server' && (rel === 'preload' || isCrossOrigin)) {
useHead({
link: [
{
rel,
as: rel === 'preload' ? 'script' : undefined,
href: input.src,
crossorigin: input.src.startsWith('/') ? undefined : (typeof input.crossorigin !== 'undefined' ? input.crossorigin : 'anonymous'),
crossorigin: isCrossOrigin ? undefined : (typeof input.crossorigin !== 'undefined' ? input.crossorigin : 'anonymous'),
key: `nuxt-script-${id}`,
tagPriority: rel === 'preload' ? 'high' : 0,
fetchpriority: 'low',
Expand Down

0 comments on commit 510d7b9

Please sign in to comment.