AlexxNB / tinro

Highly declarative, tiny, dependency free router for Svelte's web applications.
MIT License
669 stars 30 forks source link

Lazy loading always shows loading #67

Closed unbiased-dev closed 3 years ago

unbiased-dev commented 3 years ago

Hi

the current suggestion for lazy loading is to write your tiny "Lazy.svelte"

<!-- Lazy.svelte-->
<script>
    export let component;
</script>

{#await component.then ? component : component()}
    Loading component...
{:then Cmp}
   <svelte:component this={Cmp.default} />
{/await}

This works, however, after first fetch of a Lazy wrapped component, renavigating to it still triggers the import and shows the Loading component.... Even tho it resolves instantly as the file is pulled from cache by the browser, the flash of swapping components is still pretty glaring.

Is this fixable/avoidable?

AlexxNB commented 3 years ago

As you see the "Loading..." promise is not resolves immediately even in case of cached resources. You may try add something with timer (show "Loading..." after 500ms for example)

AlexxNB commented 3 years ago

The tick function from svelte package also might work (I didn't test in real):

<script>
    import {tick} from 'svelte';

    export let component;
</script>

{#await component.then ? component : component()}
    {#await tick()}
        Loading component...
    {/await}
{:then Cmp}
   <svelte:component this={Cmp.default} />
{/await}