Skip to content

Commit

Permalink
feat: introduces default asset path to jsDelivr
Browse files Browse the repository at this point in the history
  • Loading branch information
toastedtoast authored and borisdiakur committed Feb 21, 2023
1 parent af32de8 commit 140b298
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/liquid/utils/assetPath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Build } from '@stencil/core'
import { version } from '../../../package.json'

let missingAssetPathWarningDisplayed = false

/**
* Reads components asset path config from meta tag or global variable.
Expand All @@ -7,12 +10,32 @@ import { Build } from '@stencil/core'
* https://github.com/ionic-team/stencil-ds-output-targets/issues/186
*/
export const getAssetPath = (path: string) => {
// Get asset path from meta tag if available
const metaLdAssetPath = document.head.querySelector<HTMLMetaElement>(
'meta[data-ld-asset-path]'
)?.dataset.ldAssetPath
// Get asset path from window if available
const windowLdAssetPath = window.__LD_ASSET_PATH__
// Uses CDN as fallback if no asset path is set
const cdnAssetPath = `https://cdn.jsdelivr.net/npm/@emdgroup-liquid/liquid${
version ? '@' + version : ''
}/dist/liquid/`

const assetBasePath = Build.isTesting
? '/dist/liquid'
: document.head.querySelector<HTMLMetaElement>('meta[data-ld-asset-path]')
?.dataset.ldAssetPath ||
window.__LD_ASSET_PATH__ ||
'/'
: metaLdAssetPath || windowLdAssetPath || cdnAssetPath || '/'

// Display warning if assets are fetched from CDN. This is only displayed once.
if (
assetBasePath.startsWith('https://cdn.jsdelivr.net/npm/') &&
!missingAssetPathWarningDisplayed
) {
missingAssetPathWarningDisplayed = true
console.warn(
`Fetching Liquid Oxygen assets from jsDelivr CDN.\n\nWe recommend bundling Liquid Oxygen assets with your application and setting the asset path accordingly.\n\nFor more information see the documentation:\nhttps://liquid.merck.design/liquid/guides/component-assets/`
)
}

let assetPath = path

if (path.startsWith('./')) {
Expand Down

1 comment on commit 140b298

@vercel
Copy link

@vercel vercel bot commented on 140b298 Feb 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

liquid – ./

liquid-oxygen.vercel.app
liquid-git-main-uxsd.vercel.app
liquid-uxsd.vercel.app

Please sign in to comment.