Skip to content

svelte-kit / ssr error because of client side only library (jquery) #8

@notYou263

Description

@notYou263

Hi,

I created a new svelte-kit app, added svantic and got some errors to deal with because of fomantics jquery depency as well as some
design problems.
I would like to discuss a possible solution for this problem.

Here is what I did to get it working.

First I had to add a typeof window check at src/modules/utils/loader.js

const loader = async (type) => {
    if(typeof window === 'undefined')
        return 

    await loadJQ()
    return import(`../../../semantic/dist/components/${type}.min`)
}

Then I made some changes on the components.

  • remove the {#await isReady } because it leads to ugly flickering and design resizings at the client side as there is nothing to render before the promise resolves
  • add variable instance and use it to reference the components outer element instead of directly bind to $executer
  • await isReady in onMount callback. after, set $executer to elements instace.

For example, src/modules/dropdown/dropdown.svelte

<script context="module">
  import { dropdownLoader, transitionLoader, load } from '../utils'
  const isReady = load(transitionLoader, dropdownLoader)
</script>

<script>
  import '../../../semantic/dist/components/site.min.css'
  import '../../../semantic/dist/components/reset.min.css'

/* .....  */

  const  executer = dropdown(settings)

  const dispatch = createEventDispatcher()

  let instance
  
  onMounted(async () => {
    await isReady
    $executer = instance
    onMount?.($executer)
    dispatch('mount', $executer)
  })

  $: executer.setSettings(settings)

/* .....  */

</script>

  {#if as === 'div'}
    <div bind:this="{instance}" use:css="{style}" class="{classnames}">
      <slot />
    </div>
  {:else}
    <select
      bind:this="{instance}"
      use:css="{style}"
      multiple="{multiple}"
      class="{classnames}" >
      <slot />
    </select>
  {/if}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions