DEV Community

Nicholas Reid
Nicholas Reid

Posted on • Updated on


Amplify Geo + Amplify CDN + Svelte!

Have you ever wanted to add maps to your Svelte app?

So, we're already seeing some great feedback on our Amplify Geo developer preview release - thanks to everyone who has taken the time to play with it. I thought I'd share some hacky code I threw together to try out using our new CDN-delivered Amplify libraries in a mostly-unbundled Svelte app.

CDN-powered unbundling can be nice. Until recently, Amplify-powered JS apps have been conventionally bundled at build time. CDN unbundling is particularly handy for Svelte+Rollup, because none of the Amplify libraries need to be parsed each build iteration, meaning lightning-fast refreshes. Also, the resulting bundle is pretty small. My bundle.js for this app is just 19Kb, unminified.

1. Svelte Starter

Grab your favorite Svelte template. I'm a typescript+pug+stylus weirdo, but the standard Svelte-provided one is a good place to start.

npx degit sveltejs/template my-geo-app
cd my-geo-app
Enter fullscreen mode Exit fullscreen mode

2. Add the Amplify CDN libraries to your index.html

Because we're using a CDN-based approached, we can just add these libraries to the public/index.html file to have them loaded by the browser.

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />

    <title>My Example Map</title>

    <link rel="icon" type="image/png" href="/favicon.png" />
    <link rel="stylesheet" href="/global.css" />
    <link rel="stylesheet" href="/build/bundle.css" />
    <link href="" rel="stylesheet" integrity="sha384-sZlnv03zeGbcXDiuZ98TrNVZFIfpsVhN0itUxRFONLo6lOZskJPIMlOwDy+nloRF" crossorigin="anonymous" referrerpolicy="no-referrer"></link>
    <link href="" rel="stylesheet" integrity="sha384-9INm4qwCgRPHsynDw8uatP2FHVIMItwPU+PH8RtOzYAGEjU4Hiirfmuc3Rsa/pBq" crossorigin="anonymous" referrerpolicy="no-referrer"></link>

    <script src="" integrity="sha384-ZJ0BipyxRjDHPcTLilxOMRf9grNEwTTUOmr8l8MUprgnpAnpK4Fz20ndOQElCtWb" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="" integrity="sha384-rqyJfFR2070OQyXIQqomdGCYa6TaR/1asvv2oaz9wB6R8YSiIBC08mWwgVtr1NNk" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="" integrity="sha384-jWZKsznBFj0Nl3kUaRKmmk89Hew9zDhTnmOz0pOLceWY7iag+l/8QNPeD0cQYaVG" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="" integrity="sha384-kB2/fEHOpfHm1Gzute77/GLQw40BlWkV4XCaqQ/+P8zPIwUm4X5gZxcYx7Nj3tpC" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="" integrity="sha384-3WpvDe5YSr8Xdmc31s/1cKXlG5DCmeQA2PZkuQUIgwPPwGNY/kbrTYYItxSO8JJJ" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="" integrity="sha384-UQ8x/n1AjWGF3Ti2loSD8SOXsISAZw8yqr+4xWwMOajLlmS36nbWeno3U1e003Pl" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script defer src="/build/bundle.js"></script>

Enter fullscreen mode Exit fullscreen mode

3. Create an AmplifyMap component.

Add a new file called AmplifyMap.svelte to your src directory, with something like this:

  import { onMount } from "svelte"

  export let width = "800px"
  export let height = "600px"

  async function initializeMap() {
    const map = await AmplifyMapLibre.createMap({
      container: "map", // The ID of a DOM element to inject a MapLibre map.
      center: [-123.1187, 49.2819],
      zoom: 11,
      region: "us-west-2",

  onMount(() => {

<div id="map" style="width: {width}; height: {height};" />
Enter fullscreen mode Exit fullscreen mode

4. Add AmplifyMap to App

In your App.svelte, import the AmplifyMap component you just created, and add it to the template. We're passing static height and width values to the map component, but a better way might be to make it responsive to window resizing by binding them dynamically.

  import AmplifyMap from "./AmplifyMap.svelte"

  <h1>My Example Map</h1>
  <div id="wrapper">
    <AmplifyMap width="900px" height="600px" />
Enter fullscreen mode Exit fullscreen mode

5. Add Amplify

Last, but most important, you'll need to add AWS Amplify to your project. There are a few ways to do this, but the easiest is via the Amplify CLI. You can just follow the first page of our Getting Started guide here. This takes you through the process of installing the Amplify CLI and provisioning the Amplify Geo backend services in your AWS account.

IMPORTANT NOTE Because the Geo functionality is under Developer Preview, you need to make sure you have the @geo branch of the CLI installed, as described in the Geo Getting Started guide. We'll remove this requirement when Geo is generally available.

Your main.js (or main.ts) file should look something like this after you've followed the instructions:

import App from "./App.svelte"

const Amplify = aws_amplify_core.Amplify

import awsconfig from "./aws-exports"

const app = new App({
  target: document.body
export default app
Enter fullscreen mode Exit fullscreen mode

6. Happy Mapping!

If everything is wired up correctly, when you npm run dev your app, you should see something like the following in your localhost:5000 browser session:


Oldest comments (0)