Skip to content

<:Async> component #742

@Rich-Harris

Description

@Rich-Harris

A proposal for first-class async component support:

<:Async src:'./Widget.html' loading:LoadingComponent error:ErrorComponent foo='{{bar}}'>
  contents go here
</:Async>

<script>
  import LoadingComponent from './LoadingComponent.html';
  import ErrorComponent from './ErrorComponent.html';

  export default {
    components: {
      LoadingComponent,
      ErrorComponent
    }
  };
</script>

Behind the scenes that would become roughly equivalent to this...

{{#if ready}}
  <Widget foo='{{bar}}'>
    contents go here
  </Widget>
{{elseif error}}
  <ErrorComponent/>
{{else}}
  <LoadingComponent/>
{{/if}}

...except that you wouldn't need to declare Widget ahead of time.

It would get transpiled to import('./Widget.html').then(...) in dom mode, but could be an inline require('./Widget.html') in ssr mode.

Advantages of this approach over a 'just use a component' approach, for the benefit of @TehShrike 😉 :

  • One clear blessed way to do it, with documentation etc, that's used consistently across the ecosystem. No need to mess around with external libraries. It's the difference between <ReactCSSTransitionGroup> and in:fade.
  • Optimisations. We haven't talked much yet about app-level analysis/compilation, but there are certain potential compile-time wins that you get from knowing the structure of an application ahead of time. The more you know...
  • With dynamic components you have a tricky SSR problem — a hypothetical <Async> component (as opposed to <:Async>) would have to do its work in oncreate, which doesn't run on the server. With <:Async> we can load the component synchronously with require(...) and generate markup without 'loading...' messages.
  • It's not mutually exclusive with 'just use a component' approaches if you have some truly exotic requirements.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions