timhall / svelte-apollo

Svelte integration for Apollo GraphQL
MIT License
947 stars 68 forks source link

Routify SSR: Cannot read property 'dispatchEvent' of undefined #60

Closed Kneemund closed 3 years ago

Kneemund commented 3 years ago

Using svelte-apollo together with routify/SSR throws TypeError: Cannot read property 'dispatchEvent' of undefined on the server when it's rendering the page. There are no errors on the client and everything works as expected there.

Reproducible Example npx @roxi/routify init --branch 2.x npm i -D svelte-apollo apollo-boost graphql

src/pages/_layout.svelte:

<script>
    import { ready } from "@roxi/routify";
    import ApolloClient, { gql, InMemoryCache } from "apollo-boost";
    import { setClient } from "svelte-apollo";

    const client = new ApolloClient({uri: "https://countries.trevorblades.com/", cache: new InMemoryCache()})
    setClient(client);

    const QUERY = gql`
      {
        continents {
          name
        }
      }
    `;

    const countries = client.query({ query: QUERY }).finally($ready);
</script>

{#await countries then data}
  {JSON.stringify(data)}
{/await}

<slot />
timhall commented 3 years ago

Unfortunately, I'm not sure I can diagnose this without a stack trace. dispatchEvent is not used in svelte-apollo so a dependency is causing this issue. Also, for this simple example, svelte-apollo isn't technically needed, so I'd try removing it to reduce your test case further.