Salta al contenuto

🌐 Read in English

API REST statiche in JSON con SvelteKit

Esporre i contenuti del blog come endpoint JSON prerenderizzati: un'API in sola lettura che non richiede alcun server.

Alessandro Manias Alessandro Manias Pubblicato il · 1 min di lettura
Parentesi graffe stilizzate su sfondo verde

Un sito statico può comunque offrire un'API: basta generare i file JSON durante la build.

Endpoint prerenderizzati

In SvelteKit un file +server.ts con prerender = true viene eseguito una volta sola in fase di build e il suo output salvato su disco.

export const prerender = true;

export const GET = () =>
	new Response(JSON.stringify(getPosts()), {
		headers: { 'Content-Type': 'application/json' }
	});

Route con parametri

Per le route dinamiche come /api/posts/[slug].json va esportata la funzione entries, che elenca tutti i valori da generare.

Endpoint disponibili

  • /api/posts.json
  • /api/posts/<slug>.json
  • /api/categories.json
  • /api/tags.json
  • /api/authors.json
  • /api/authors/<nick>.json

© 2026 Wordpress e PHP. Generato staticamente con SvelteKit.