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

how to setup on sveltekit? #3

Open
tibeemts opened this issue May 23, 2021 · 9 comments
Open

how to setup on sveltekit? #3

tibeemts opened this issue May 23, 2021 · 9 comments

Comments

@tibeemts
Copy link

could you guide me how to setup on sveltekit?

@0gust1
Copy link

0gust1 commented Dec 6, 2021

TLDR:

  • use onMount() to dynamically import your component containing your svelte-leaflet related code
  • use {#if browser} to condition the rendering when in browser.
<script lang="ts">
	import { browser } from '$app/env';
	import { onMount } from 'svelte';

	let LeafletContainer;

	onMount(async () => {
		if (browser) {
			LeafletContainer = (await import('$lib/LeafletContainer.svelte')).default;
		}
	});
</script>

{#if browser}
<svelte:component this={LeafletContainer} />
{/if}

@timwis
Copy link

timwis commented Feb 9, 2022

Instead of importing the library in onMount, I was able to get it working using vite-plugin-iso-import:

import { LeafletMap } from 'svelte-leafletjs?client'

@dritter
Copy link

dritter commented Jun 29, 2022

@timwis was a bit sparingly with his informations.

After installing vite-plugin-iso-import, you need to do three things:

  1. Configure vite in svelte.config.js to use it as plugin:
    import { isoImport } from 'vite-plugin-iso-import'
    
    const config = {
        kit: {
            vite: {
                plugins: [isoImport()]
            }
        }
    }
  2. Configure it in tsconfig.json or jsconfig.json:
    {
        "compilerOptions": {
            "plugins": [{ "name": "vite-plugin-iso-import" }]
        }
    }
  3. Use it with {#if browser} in your template and import svelte-leafletjs with ?client
    <div id="map" style="height: 80vh;">
    {#if browser}
        <LeafletMap options={mapOptions}>
            <TileLayer url={tileUrl} options={tileLayerOptions}/>
        </LeafletMap>
    {/if}
    </div>
    
    <script>
    import { browser } from '$app/env'
    import {LeafletMap, TileLayer} from 'svelte-leafletjs?client';
    import 'leaflet/dist/leaflet.css'
    
    const mapOptions = {
        center: [1.364917, 103.822872],
        zoom: 11,
    };
    const tileUrl = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";
    const tileLayerOptions = {
        minZoom: 0,
        maxZoom: 20,
        maxNativeZoom: 19,
        attribution: "© OpenStreetMap contributors",
    };
    </script>

@beebase
Copy link

beebase commented Jul 6, 2022

Would someone happen to know how to install with plain Svelte + vitejs?

@Erildo
Copy link

Erildo commented Jul 17, 2022

I'm getting a 500 window is not defined.....does anyone know how to solve this,cant make it run

@baisong
Copy link

baisong commented Aug 18, 2022

@Erildo I'm also getting window not defined error, I think Leaflet 1.9.0 will resolve this in coming weeks:

...And maybe the enhanced ESM support added will obviate the need to implement one or more (or all) of the other methods listed on this thread? i.e. onMount((async () => { if (browser) {})) / {#if browser}...{/if} / vite-plugin-iso-import, etc.

@nickboucart
Copy link

I have a serverless (sst) project, using svelte for the frontend. It uses vite to deal with the frontend stuff. (see https://sst.dev/examples/how-to-create-a-svelte-app-with-serverless.html for the general setup, as a side note - I really liked the experience so far)

I tried the mimic the approach as proposed by @dritter in the comment above, but to no avail. When I try to import leaflet, it complains about having no default import, when I add the ?client, I get

Failed to resolve import "svelte-leafletjs?client"

Any ideas? (Sorry, I'm rather new to vite/svelte)

Thanks!

@fhennig
Copy link

fhennig commented Jan 2, 2023

I've got this to run, I noticed that this line:

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

should be

import { browser } from '$app/environment';

Other than that you need to install https://github.com/bluwy/vite-plugin-iso-import if the ?client import isn't working for you.

@deekshithmr95
Copy link

thanks @dritter , this saved my day,

sverhoeven added a commit to carbon-budget-explorer/cabe that referenced this issue Sep 8, 2023
Use workaround on ngyewch/svelte-leafletjs#3 (comment)
to get working in svelte kit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants