Skip Navigation

Scott Spence

Using SvelteKit Endpoints in Netlify

1 min read
Hey! Thanks for stopping by! Just a word of warning, this post is over 2 years old, . If there's technical information in here it's more than likely out of date.

I’ve been using Netlify recently to make a project that uses endpoints. Unlike with Vercel where you can use the endpoint to fetch the data dynamically Netlify needs to statically generate the data.

There’s some SvelteKit prerender settings that need configuration here’s what I set in the svelte.config.js file:

import adapter from '@sveltejs/adapter-static'
import preprocess from 'svelte-preprocess'

/** @type {import('@sveltejs/kit').Config} */
const config = {
  kit: {
    // hydrate the <div id="svelte"> element in src/app.html
    target: '#svelte',
    adapter: adapter(),
    prerender: {
      crawl: true,
      enabled: true,
      onError: 'continue',
      pages: ['*'],
    },
  },

  preprocess: [
    preprocess({
      postcss: true,
    }),
  ],
}

export default config

Also for the pages that use the endpoint I’ve added the following to any page that uses an endpoint:

<script context="module">
  export const prerender = true
  // rest of the code for the endpoint
</script>

There's a reactions leaderboard you can check out too.

Copyright © 2017 - 2024 - All rights reserved Scott Spence