Content Negotiation with Accept header
import { createNegotiation } from "sveltekit-negotiate";
export const { handle, reroute, negotiate, Negotiate } = createNegotiation({
"text/markdown": { extension: ".md" },
"application/json": { extension: ".json" },
});
import { negotiate } from "#lib/negotiate.js";
export const load = async ({ locals }) => {
const message = "Hello, content negotiation!";
return {
message,
...negotiate(locals, {
"text/markdown": () => `# ${message}`,
}),
};
};
<script lang="ts">
let { data } = $props();
</script>
<section data-role="content">
<h1 class="text-2xl font-bold">{data.message}</h1>
</section>
import { definePageMetaTags } from "svelte-meta-tags";
export const load = async ({ parent, data }) => {
await parent();
const { pageMetaTags } = definePageMetaTags({
title: "Negotiate",
});
return { ...data, pageMetaTags };
};
import { sequence, type ServerInit } from "@sveltejs/kit/hooks";
import {
POCKETBASE_URL,
POCKETBASE_SUPERUSER_EMAIL,
POCKETBASE_SUPERUSER_PASSWORD,
} from "$app/env/private";
import { handlePocketbase } from "@velastack/pocketbase";
import { startWorker } from "#lib/server/workflows.js";
import { handle as handleNegotiate } from "#lib/negotiate.js";
export const handle = sequence(
handleNegotiate,
handlePocketbase({
pocketbaseUrl: POCKETBASE_URL,
superuserEmail: POCKETBASE_SUPERUSER_EMAIL,
superuserPassword: POCKETBASE_SUPERUSER_PASSWORD,
}),
);
// Runs once when the server starts: executes the workflows in src/lib/workflows.
export const init: ServerInit = () => startWorker();
import { reroute as negotiateReroute } from "#lib/negotiate.js";
export const reroute = ({ url }) => negotiateReroute(url.pathname);