Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting $session in Sveltekit breaks the map #58

Open
wfendler opened this issue Oct 29, 2021 · 4 comments
Open

Setting $session in Sveltekit breaks the map #58

wfendler opened this issue Oct 29, 2021 · 4 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@wfendler
Copy link

wfendler commented Oct 29, 2021

If I have a page displaying the map and add the following code it breaks.

import { session } from '$app/stores';
import { browser } from '$app/env';

if (browser) {
  // try to set the $session to anything
  $session = 'asdf'
}

The error I get in the console is:

Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating 'window.mapboxgl.accessToken = options.accessToken')
init — map-action.js:31
(anonymous function) — map-action.js:19
load — asset-loader.js:7
action — map-action.js:18
mount — Map.svelte:15
(anonymous function) — svelte-hooks.js:197
mount_component — index.mjs:1727
mount — index.svelte:28
(anonymous function) — svelte-hooks.js:197
mount_component — index.mjs:1727
update — root.svelte:38
update — root.svelte:37
update_slot_base — index.mjs:98
update — layout.svelte:40
update — index.mjs:1057
flush — index.mjs:1025
promiseReactionJob
B — util.js:516
@wfendler
Copy link
Author

Updated svelte and sveltekit and it seems to have resolved the issue.

@wfendler
Copy link
Author

Sorry, closed too soon. The error doesn't show up right away. It only happens after a few refreshes/hot-reloads.

package.json

{
  "name": "my-app",
  "version": "0.0.1",
  "scripts": {
    "dev": "svelte-kit dev",
    "build": "svelte-kit build",
    "preview": "svelte-kit preview"
  },
  "devDependencies": {
    "@sveltejs/kit": "^1.0.0-next.192",
    "svelte": "^3.44.0"
  },
  "type": "module",
  "dependencies": {
    "@beyonk/svelte-mapbox": "^8.1.4"
  }
}

index.svelte

<script>
  import { Map, Marker } from '@beyonk/svelte-mapbox';
  import { session } from '$app/stores';
  import { browser } from '$app/env';

  if (browser) {
    // setting $session to anything at all seems to cause the error
    $session = 'asdf'
  }

  let mapComponent;

  function onReady() {
    mapComponent.flyTo({ center: [-74.0060152, 40.7127281] });
  }
</script>

<div>
  <h1>Map</h1>
  <div class="wrapper">
    <Map
      accessToken={import.meta.env.VITE_MAPBOX_ACCESS_TOKEN}
      bind:this={mapComponent}
      on:ready={onReady}
    >
      <Marker lat="-74.0060152" lng="40.7127281" label="NYC" />
    </Map>
  </div>
</div>

<style>
  .wrapper {
    height: 500px;
    width: 500px;
    border: 1px solid black;
  }
</style>

@wfendler wfendler reopened this Oct 31, 2021
@wfendler
Copy link
Author

I've tried to put together a minimal recreation here:

https://stackblitz.com/edit/svelte-kit-qdaku8?file=src/routes/index.svelte

@antony
Copy link
Member

antony commented Nov 1, 2021

Thanks for the reproduction. I'll have a look and see if I can find out what the issue is as soon as possible.

@antony antony added bug Something isn't working help wanted Extra attention is needed labels Nov 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants